LCOV - code coverage report
Current view: top level - lib_enc - ivas_init_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 575 658 87.4 %
Date: 2025-05-03 01:55:50 Functions: 11 11 100.0 %

          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 <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include "ivas_cnst.h"
      37             : #include "prot_fx.h"
      38             : #include "ivas_prot_fx.h"
      39             : #include "ivas_stat_enc.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "wmc_auto.h"
      42             : #include "ivas_prot_fx.h"
      43             : #include "prot_fx_enc.h"
      44             : 
      45             : 
      46             : /*-------------------------------------------------------------------*
      47             :  * ivas_write_format()
      48             :  *
      49             :  * Write IVAS format signaling
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52      422922 : void ivas_write_format_fx(
      53             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure   */
      54             : )
      55             : {
      56             :     Word16 ind, nBits, extra_bits;
      57             : 
      58      422922 :     ind = 0;
      59      422922 :     move16();
      60             : 
      61      422922 :     nBits = IVAS_FORMAT_SIGNALING_NBITS;
      62      422922 :     move16();
      63             : 
      64      422922 :     extra_bits = sub( IVAS_FORMAT_SIGNALING_NBITS_EXTENDED, IVAS_FORMAT_SIGNALING_NBITS );
      65             : 
      66      422922 :     switch ( st_ivas->hEncoderConfig->ivas_format )
      67             :     {
      68       72486 :         case STEREO_FORMAT:
      69       72486 :             ind = 0;
      70       72486 :             move16();
      71       72486 :             BREAK;
      72      103826 :         case ISM_FORMAT:
      73      103826 :             ind = 2;
      74      103826 :             move16();
      75      103826 :             IF( GE_32( st_ivas->hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
      76             :             {
      77       95106 :                 ind = 4;
      78       95106 :                 move16();
      79             : 
      80       95106 :                 nBits = add( nBits, extra_bits ); /* Q0 */
      81             :             }
      82      103826 :             BREAK;
      83       47850 :         case MC_FORMAT:
      84       47850 :             ind = 1;
      85       47850 :             move16();
      86       47850 :             BREAK;
      87      122500 :         case SBA_FORMAT:
      88      122500 :             ind = 6;
      89      122500 :             move16();
      90             : 
      91      122500 :             nBits = add( nBits, extra_bits ); /* Q0 */
      92      122500 :             BREAK;
      93       30626 :         case MASA_FORMAT:
      94       30626 :             ind = 7;
      95       30626 :             move16();
      96             : 
      97       30626 :             nBits = add( nBits, extra_bits ); /* Q0 */
      98       30626 :             BREAK;
      99       10634 :         case MASA_ISM_FORMAT:
     100       10634 :             IF( st_ivas->ism_mode == ISM_MODE_NONE )
     101             :             {
     102        2388 :                 ind = 7; /* send MASA format */
     103        2388 :                 move16();
     104        2388 :                 nBits = add( nBits, extra_bits ); /* Q0 */
     105             :             }
     106             :             ELSE
     107             :             {
     108        8246 :                 ind = 10;
     109        8246 :                 move16();
     110        8246 :                 nBits = add( nBits, add( extra_bits, IVAS_COMBINED_FORMAT_SIGNALLING_BITS ) ); /* Q0 */
     111             :             }
     112       10634 :             BREAK;
     113       35000 :         case SBA_ISM_FORMAT:
     114       35000 :             IF( LT_32( st_ivas->hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
     115             :             {
     116        2651 :                 ind = 6; /* send SBA format */
     117        2651 :                 move16();
     118        2651 :                 nBits = add( nBits, extra_bits ); /* Q0 */
     119             :             }
     120             :             ELSE
     121             :             {
     122       32349 :                 ind = 11; /* 1011 */
     123       32349 :                 move16();
     124       32349 :                 nBits = add( nBits, add( extra_bits, IVAS_COMBINED_FORMAT_SIGNALLING_BITS ) ); /* Q0 */
     125             :             }
     126       35000 :             BREAK;
     127           0 :         default:
     128           0 :             assert( !"Invalid format. Aborting." );
     129             :             BREAK;
     130             :     }
     131             : 
     132      422922 :     IF( st_ivas->hSCE[0] != NULL )
     133             :     {
     134      181118 :         push_indice( st_ivas->hSCE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     135             :     }
     136      241804 :     ELSE IF( st_ivas->hCPE[0] != NULL )
     137             :     {
     138      241804 :         push_indice( st_ivas->hCPE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     139             :     }
     140             : 
     141      422922 :     return;
     142             : }
     143             : 
     144             : /*-------------------------------------------------------------------*
     145             :  * ivas_write_format_sid()
     146             :  *
     147             :  * Write IVAS format signaling in SID frames
     148             :  *-------------------------------------------------------------------*/
     149             : 
     150        2941 : void ivas_write_format_sid_fx(
     151             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format               */
     152             :     const Word16 element_mode,     /* i  : element bitrate           Q0*/
     153             :     BSTR_ENC_HANDLE hBstr          /* i/o: encoder bitstream handle  */
     154             : )
     155             : {
     156        2941 :     Word16 ind = 0; /* to avoid compilation warning */
     157        2941 :     move16();
     158             : 
     159        2941 :     SWITCH( ivas_format )
     160             :     {
     161        1865 :         case STEREO_FORMAT:
     162        1865 :             IF( EQ_16( element_mode, IVAS_CPE_MDCT ) )
     163             :             {
     164         416 :                 ind = SID_MDCT_STEREO;
     165         416 :                 move16();
     166             :             }
     167        1449 :             ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
     168             :             {
     169        1449 :                 ind = SID_DFT_STEREO;
     170        1449 :                 move16();
     171             :             }
     172             :             ELSE
     173             :             {
     174           0 :                 assert( !"Wrong stereo mode for SID format signaling" );
     175             :             }
     176        1865 :             BREAK;
     177         535 :         case ISM_FORMAT:
     178         535 :             ind = SID_ISM;
     179         535 :             move16();
     180         535 :             BREAK;
     181           0 :         case MC_FORMAT:
     182           0 :             ind = SID_MULTICHANNEL;
     183           0 :             move16();
     184           0 :             BREAK;
     185         350 :         case SBA_FORMAT:
     186         350 :             SWITCH( element_mode )
     187             :             {
     188         227 :                 case IVAS_SCE:
     189         227 :                     ind = SID_SBA_1TC;
     190         227 :                     move16();
     191         227 :                     BREAK;
     192         123 :                 case IVAS_CPE_MDCT:
     193         123 :                     ind = SID_SBA_2TC;
     194         123 :                     move16();
     195         123 :                     BREAK;
     196           0 :                 default:
     197           0 :                     assert( !"Wrong element mode for SBA DTX!" );
     198             :                     BREAK;
     199             :             }
     200         350 :             BREAK;
     201         191 :         case MASA_FORMAT:
     202         191 :             IF( EQ_16( element_mode, IVAS_SCE ) )
     203             :             {
     204         117 :                 ind = SID_MASA_1TC;
     205         117 :                 move16();
     206             :             }
     207             :             ELSE
     208             :             {
     209          74 :                 ind = SID_MASA_2TC;
     210          74 :                 move16();
     211             :             }
     212         191 :             BREAK;
     213           0 :         default:
     214           0 :             assert( !"Reserved SID format symbol written." );
     215             :             BREAK;
     216             :     }
     217             : 
     218        2941 :     push_indice( hBstr, IND_IVAS_FORMAT, ind, SID_FORMAT_NBITS );
     219             : 
     220        2941 :     return;
     221             : }
     222             : 
     223             : /*-------------------------------------------------------------------*
     224             :  * getNumChanAnalysis()
     225             :  *
     226             :  * get number of input channels used for analysis/coding
     227             :  *-------------------------------------------------------------------*/
     228             : 
     229             : /*! r: number of channels to be analysed */
     230      422536 : Word16 getNumChanAnalysis_fx(
     231             :     Encoder_Struct *st_ivas /* i  : IVAS encoder structure              */
     232             : )
     233             : {
     234             :     Word16 n;
     235             : 
     236      422536 :     n = add( st_ivas->nSCE, CPE_CHANNELS * st_ivas->nCPE ); /* Q0 */
     237      422536 :     test();
     238      422536 :     test();
     239      422536 :     test();
     240      422536 :     test();
     241      422536 :     IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_FORMAT ) )
     242             :     {
     243      122988 :         n = imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ); /* Q0 */
     244             :     }
     245      299548 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MC_FORMAT ) && ( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) || EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) ) )
     246             :     {
     247       21510 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     248       21510 :         move16();
     249             :     }
     250      278038 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
     251             :     {
     252         800 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     253         800 :         move16();
     254             :     }
     255      277238 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     256             :     {
     257       20754 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     258       20754 :         move16();
     259             :     }
     260      256484 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
     261             :     {
     262        9088 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     263        9088 :         move16();
     264             :     }
     265      247396 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
     266             :     {
     267       35070 :         n = add( st_ivas->hEncoderConfig->nchan_ism, imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ) ); /* Q0 */
     268             :     }
     269             : 
     270      422536 :     return n;
     271             : }
     272             : #ifndef HARM_ENC_INIT
     273             : /*-------------------------------------------------------------------*
     274             :  * copy_encoder_config_ivas_fx()
     275             :  *
     276             :  * Copy configuration structrue to the state structrure
     277             :  *-------------------------------------------------------------------*/
     278             : 
     279             : void copy_encoder_config_ivas_fx(
     280             :     Encoder_Struct *st_ivas, /* i  : IVAS encoder structure                                     */
     281             :     Encoder_State *st,       /* o  : encoder state structure                            */
     282             :     const Word16 flag_all    /* i  : flag 1==update all, 0=partial update       Q0*/
     283             : )
     284             : {
     285             :     IF( flag_all )
     286             :     {
     287             :         st->input_Fs = st_ivas->hEncoderConfig->input_Fs; /* Q0 */
     288             :         move32();
     289             : 
     290             :         st->last_codec_mode = st_ivas->last_codec_mode; /* Q0 */
     291             :         move16();
     292             :         st->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate; /* Q0 */
     293             :         move32();
     294             : 
     295             :         st->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON; /* Q0 */
     296             :         move16();
     297             : 
     298             :         st->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR; /* Q0 */
     299             :         move16();
     300             :     }
     301             : 
     302             :     st->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB; /* Q0 */
     303             :     st->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR; /* Q0 */
     304             :     move16();
     305             :     move16();
     306             : 
     307             :     st->codec_mode = st_ivas->codec_mode;                 /* Q0 */
     308             :     st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth; /* Q0 */
     309             :     move16();
     310             :     move16();
     311             : 
     312             :     st->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON;               /* Q0 */
     313             :     st->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset;       /* Q0 */
     314             :     st->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator; /* Q0 */
     315             :     move16();
     316             :     move16();
     317             :     move16();
     318             : 
     319             :     st->element_mode = st_ivas->hEncoderConfig->element_mode_init; /* Q0 */
     320             :     move16();
     321             : 
     322             :     return;
     323             : }
     324             : #endif
     325             : /*-------------------------------------------------------------------*
     326             :  * copy_encoder_config_fx()
     327             :  *
     328             :  * Copy configuration structrue to the state structrure
     329             :  *-------------------------------------------------------------------*/
     330             : 
     331       23135 : void copy_encoder_config_fx(
     332             :     Encoder_Struct *st_ivas, /* i  : IVAS encoder structure                                     */
     333             :     Encoder_State *st_fx,    /* o  : encoder state structure                            */
     334             :     const Word16 flag_all    /* i  : flag 1==update all, 0=partial update       Q0*/
     335             : )
     336             : {
     337       23135 :     IF( flag_all )
     338             :     {
     339        8252 :         st_fx->input_Fs = st_ivas->hEncoderConfig->input_Fs; /* Q0 */
     340        8252 :         move32();
     341             : 
     342        8252 :         st_fx->last_codec_mode = st_ivas->last_codec_mode; /* Q0 */
     343        8252 :         move16();
     344        8252 :         st_fx->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate; /* Q0 */
     345        8252 :         move32();
     346             : 
     347        8252 :         st_fx->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON; /* Q0 */
     348        8252 :         move16();
     349             : 
     350        8252 :         st_fx->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR; /* Q0 */
     351        8252 :         move16();
     352             :     }
     353             : 
     354       23135 :     st_fx->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB; /* Q0 */
     355       23135 :     move16();
     356       23135 :     st_fx->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR; /* Q0 */
     357       23135 :     move16();
     358             : 
     359       23135 :     st_fx->codec_mode = st_ivas->codec_mode; /* Q0 */
     360       23135 :     move16();
     361       23135 :     st_fx->max_bwidth = st_ivas->hEncoderConfig->max_bwidth; /* Q0 */
     362       23135 :     move16();
     363             : 
     364       23135 :     st_fx->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON; /* Q0 */
     365       23135 :     move16();
     366       23135 :     st_fx->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset; /* Q0 */
     367       23135 :     move16();
     368       23135 :     st_fx->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator; /* Q0 */
     369       23135 :     move16();
     370             : 
     371             : #ifdef DEBUGGING
     372             :     st_fx->force = st_ivas->hEncoderConfig->force;
     373             : #ifdef DEBUG_FORCE_DIR
     374             :     st_fx->force_dir = st_ivas->hEncoderConfig->force_dir;
     375             : #endif
     376             : #endif
     377             : 
     378       23135 :     st_fx->element_mode = st_ivas->hEncoderConfig->element_mode_init; /* Q0 */
     379       23135 :     move16();
     380             : 
     381       23135 :     return;
     382             : }
     383             : 
     384             : /*-------------------------------------------------------------------------
     385             :  * ivas_initialize_handles_enc_fx()
     386             :  *
     387             :  * NULL initialization of handles
     388             :  *-------------------------------------------------------------------------*/
     389             : 
     390         624 : void ivas_initialize_handles_enc_fx(
     391             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure                  */
     392             : )
     393             : {
     394             :     Word16 i;
     395             : 
     396        3120 :     FOR( i = 0; i < MAX_SCE; i++ )
     397             :     {
     398        2496 :         st_ivas->hSCE[i] = NULL;
     399             :     }
     400             : 
     401        4368 :     FOR( i = 0; i < MAX_CPE; i++ )
     402             :     {
     403        3744 :         st_ivas->hCPE[i] = NULL;
     404             :     }
     405             : 
     406         624 :     st_ivas->mem_hp20_in_fx = NULL;
     407             : 
     408             :     /* ISM metadata handles */
     409        3120 :     FOR( i = 0; i < MAX_NUM_OBJECTS; i++ )
     410             :     {
     411        2496 :         st_ivas->hIsmMetaData[i] = NULL;
     412             :     }
     413             : 
     414             :     /* ISM DTX handle */
     415         624 :     st_ivas->hISMDTX = NULL;
     416             : 
     417             :     /* Q Metadata handle */
     418         624 :     st_ivas->hQMetaData = NULL;
     419             : 
     420             :     /* DirAC handle */
     421         624 :     st_ivas->hDirAC = NULL;
     422             : 
     423             :     /* ParamISM handle */
     424         624 :     st_ivas->hParamIsm = NULL;
     425             :     /* SPAR handle */
     426         624 :     st_ivas->hSpar = NULL;
     427             : 
     428             :     /* MASA encoder handle */
     429         624 :     st_ivas->hMasa = NULL;
     430             : 
     431             :     /* MCT handle */
     432         624 :     st_ivas->hMCT = NULL;
     433             : 
     434             :     /* MC Param-Upmix handle */
     435         624 :     st_ivas->hMCParamUpmix = NULL;
     436             : 
     437             :     /* Parametric MC handle */
     438         624 :     st_ivas->hParamMC = NULL;
     439             : 
     440             :     /* Multi-channel MASA handle */
     441         624 :     st_ivas->hMcMasa = NULL;
     442             : 
     443             :     /* Stereo downmix for EVS encoder handle */
     444         624 :     st_ivas->hStereoDmxEVS = NULL;
     445             : 
     446             :     /* LFE  handle */
     447         624 :     st_ivas->hLFE = NULL;
     448             : 
     449             :     /* LFE low pass filter handle */
     450         624 :     st_ivas->hLfeLpf = NULL;
     451             : 
     452             :     /* Object MASA handle */
     453         624 :     st_ivas->hOMasa = NULL;
     454             : 
     455             :     /* OSBA handle */
     456         624 :     st_ivas->hOSba = NULL;
     457             : 
     458         624 :     return;
     459             : }
     460             : 
     461             : 
     462             : /*-------------------------------------------------------------------*
     463             :  * ivas_init_encoder()
     464             :  *
     465             :  * Initialize IVAS encoder state structure
     466             :  *-------------------------------------------------------------------*/
     467             : #ifndef HARM_ENC_INIT
     468             : ivas_error ivas_init_encoder(
     469             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
     470             : )
     471             : {
     472             :     Word16 i, n;
     473             :     Word16 nchan_inp_buff;
     474             :     Word16 sce_id, cpe_id;
     475             :     IVAS_FORMAT ivas_format;
     476             :     Word32 input_Fs, ivas_total_brate;
     477             :     Word32 element_brate_tmp[MAX_NUM_OBJECTS];
     478             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     479             :     ivas_error error;
     480             : 
     481             :     error = IVAS_ERR_OK;
     482             : 
     483             :     hEncoderConfig = st_ivas->hEncoderConfig;
     484             :     ivas_format = hEncoderConfig->ivas_format;
     485             :     input_Fs = hEncoderConfig->input_Fs;                 /* Q0 */
     486             :     ivas_total_brate = hEncoderConfig->ivas_total_brate; /* Q0 */
     487             :     move32();
     488             :     move32();
     489             : 
     490             :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate; /* Q0 */
     491             :     move32();
     492             : 
     493             :     if ( NE_32( ivas_format, MONO_FORMAT ) )
     494             :     {
     495             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
     496             :         hEncoderConfig->max_bwidth = s_max( hEncoderConfig->max_bwidth, WB ); /* Q0 */
     497             :         move16();
     498             :     }
     499             :     st_ivas->ism_mode = ISM_MODE_NONE;
     500             :     st_ivas->mc_mode = MC_MODE_NONE;
     501             :     move32();
     502             :     move32();
     503             : 
     504             :     st_ivas->nchan_transport = -1;
     505             :     move16();
     506             : 
     507             :     /*-----------------------------------------------------------------*
     508             :      * Allocate floating-point input audio buffers
     509             :      *-----------------------------------------------------------------*/
     510             : 
     511             :     nchan_inp_buff = hEncoderConfig->nchan_inp; /* Q0 */
     512             :     move16();
     513             : 
     514             :     IF( EQ_32( ivas_format, MONO_FORMAT ) )
     515             :     {
     516             :         nchan_inp_buff = 0;
     517             :         move16();
     518             :     }
     519             :     ELSE IF( EQ_32( ivas_format, MASA_ISM_FORMAT ) )
     520             :     {
     521             :         if ( EQ_32( sub( hEncoderConfig->nchan_inp, hEncoderConfig->nchan_ism ), 1 ) ) /* mono is duplicated in monoMASA */
     522             :         {
     523             :             nchan_inp_buff = add( nchan_inp_buff, 1 );
     524             :         }
     525             : 
     526             :         nchan_inp_buff = add( nchan_inp_buff, 1 ); /* for *data_separated_object */
     527             :     }
     528             : 
     529             :     FOR( n = 0; n < nchan_inp_buff; n++ )
     530             :     {
     531             :         /* note: these are intra-frame heap memories */
     532             :         IF( ( st_ivas->p_data_fx[n] = (Word32 *) malloc( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) * sizeof( Word32 ) ) ) == NULL )
     533             :         {
     534             :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for floating-point input audio buffer!\n" ) );
     535             :         }
     536             :         set32_fx( st_ivas->p_data_fx[n], 0, extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) ) );
     537             :     }
     538             :     st_ivas->q_data_fx = Q11;
     539             :     move16();
     540             :     FOR( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     541             :     {
     542             :         st_ivas->p_data_fx[n] = NULL;
     543             :     }
     544             :     /*-----------------------------------------------------------------*
     545             :      * Allocate and initialize buffer of indices
     546             :      *-----------------------------------------------------------------*/
     547             : 
     548             :     /* set the maximum allowed number of indices in the list */
     549             :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices_fx( ivas_format, ivas_total_brate ); /* Q0 */
     550             :     move16();
     551             : 
     552             :     /* allocate buffer of indices */
     553             :     IF( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
     554             :     {
     555             :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
     556             :     }
     557             : 
     558             :     /* reset the list of indices */
     559             :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     560             :     {
     561             :         st_ivas->ind_list[i].nb_bits = -1;
     562             :         move16();
     563             :     }
     564             : 
     565             :     /* set the maximum allowed number of metadata indices in the list */
     566             :     st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata_fx( st_ivas->hEncoderConfig->ivas_format, st_ivas->hEncoderConfig->ivas_total_brate ); /* Q0 */
     567             :     move16();
     568             : 
     569             :     /* allocate buffer of metadata indices */
     570             :     IF( st_ivas->ivas_max_num_indices_metadata > 0 )
     571             :     {
     572             :         IF( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
     573             :         {
     574             :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
     575             :         }
     576             : 
     577             :         /* reset the list of metadata indices */
     578             :         for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
     579             :         {
     580             :             st_ivas->ind_list_metadata[i].nb_bits = -1;
     581             :             move16();
     582             :         }
     583             :     }
     584             :     ELSE
     585             :     {
     586             :         st_ivas->ind_list_metadata = NULL;
     587             :     }
     588             : 
     589             :     /*-----------------------------------------------------------------*
     590             :      * Allocate and initialize SCE/CPE and other handles
     591             :      *-----------------------------------------------------------------*/
     592             : 
     593             :     IF( EQ_32( ivas_format, MONO_FORMAT ) )
     594             :     {
     595             :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
     596             :         move16();
     597             :         st_ivas->nCPE = 0;
     598             :         move16();
     599             :         st_ivas->nchan_transport = 1;
     600             :         move16();
     601             :         sce_id = 0;
     602             :         move16();
     603             : 
     604             :         test();
     605             :         IF( st_ivas->hEncoderConfig->element_mode_init != EVS_MONO )
     606             :         {
     607             :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     608             :             {
     609             :                 return error;
     610             :             }
     611             :         }
     612             :         ELSE
     613             :         {
     614             :             IF( NE_32( ( error = create_evs_sce_enc_fx( st_ivas, sce_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
     615             :             {
     616             :                 return error;
     617             :             }
     618             :         }
     619             : 
     620             :         /* prepare stereo downmix for EVS */
     621             :         IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
     622             :         {
     623             :             IF( ( error = stereo_dmx_evs_init_encoder_fx( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
     624             :             {
     625             :                 return error;
     626             :             }
     627             :         }
     628             :     }
     629             :     ELSE IF( EQ_32( ivas_format, STEREO_FORMAT ) )
     630             :     {
     631             :         st_ivas->nSCE = 0;
     632             :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
     633             :         st_ivas->nchan_transport = CPE_CHANNELS;
     634             :         cpe_id = 0;
     635             :         move16();
     636             :         move16();
     637             :         move16();
     638             :         move16();
     639             : 
     640             :         IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     641             :         {
     642             :             return error;
     643             :         }
     644             :     }
     645             :     ELSE IF( EQ_32( ivas_format, ISM_FORMAT ) )
     646             :     {
     647             :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate );
     648             : 
     649             :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_inp, element_brate_tmp ) ) != IVAS_ERR_OK )
     650             :         {
     651             :             return error;
     652             :         }
     653             : 
     654             :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     655             :         {
     656             :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
     657             :             {
     658             :                 return error;
     659             :             }
     660             :         }
     661             : 
     662             :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     663             :         {
     664             :             IF( ( error = ivas_param_ism_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     665             :             {
     666             :                 return error;
     667             :             }
     668             :         }
     669             : 
     670             :         IF( st_ivas->hEncoderConfig->Opt_DTX_ON )
     671             :         {
     672             :             IF( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
     673             :             {
     674             :                 return error;
     675             :             }
     676             :         }
     677             :     }
     678             :     ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, MASA_FORMAT ) )
     679             :     {
     680             :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     681             :         {
     682             :             return error;
     683             :         }
     684             : 
     685             :         IF( EQ_32( ivas_format, SBA_FORMAT ) )
     686             :         {
     687             :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order ); /* Q0 */
     688             :             move16();
     689             : 
     690             :             IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
     691             :             {
     692             :                 return error;
     693             :             }
     694             : 
     695             :             IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     696             :             {
     697             :                 return error;
     698             :             }
     699             :         }
     700             :         ELSE
     701             :         {
     702             :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp; /* Q0 */
     703             :             move16();
     704             : 
     705             :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     706             :             {
     707             :                 return error;
     708             :             }
     709             :         }
     710             : 
     711             :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     712             :         {
     713             :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
     714             :             {
     715             :                 return error;
     716             :             }
     717             : 
     718             :             if ( EQ_32( ivas_format, SBA_FORMAT ) && st_ivas->hEncoderConfig->Opt_DTX_ON )
     719             :             {
     720             :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
     721             :                 move16();
     722             :             }
     723             :         }
     724             : 
     725             :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     726             :         {
     727             :             Word32 res_dec, res_frac;
     728             :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
     729             :             IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
     730             :             {
     731             :                 return error;
     732             :             }
     733             : 
     734             :             FOR( n = 0; n < CPE_CHANNELS; n++ )
     735             :             {
     736             :                 if ( hEncoderConfig->Opt_DTX_ON )
     737             :                 {
     738             :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
     739             :                     move16();
     740             :                 }
     741             :             }
     742             :         }
     743             : 
     744             :         IF( GT_16( st_ivas->nCPE, 1 ) )
     745             :         {
     746             :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     747             :             {
     748             :                 return error;
     749             :             }
     750             :         }
     751             :     }
     752             :     ELSE IF( EQ_32( ivas_format, MASA_ISM_FORMAT ) )
     753             :     {
     754             :         Word32 ism_total_brate;
     755             :         Word16 k;
     756             : 
     757             :         st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( ivas_total_brate, hEncoderConfig->nchan_ism ); /* Q0 */
     758             :         move32();
     759             :         st_ivas->nchan_transport = 2;
     760             :         move16();
     761             : 
     762             :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     763             :         {
     764             :             return error;
     765             :         }
     766             : 
     767             :         k = 0;
     768             :         move16();
     769             :         while ( k < SIZE_IVAS_BRATE_TBL && NE_32( ivas_total_brate, ivas_brate_tbl[k] ) )
     770             :         {
     771             :             k++;
     772             :         }
     773             : 
     774             :         ism_total_brate = 0;
     775             :         move32();
     776             :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     777             :         {
     778             :             ism_total_brate = L_add( ism_total_brate, sep_object_brate[k - 2][st_ivas->nSCE - 1] );
     779             :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, sep_object_brate[k - 2][st_ivas->nSCE - 1] ) ) != IVAS_ERR_OK )
     780             :             {
     781             :                 return error;
     782             :             }
     783             :         }
     784             : 
     785             :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     786             :         {
     787             :             return error;
     788             :         }
     789             :         IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     790             :         {
     791             :             return error;
     792             :         }
     793             : 
     794             :         IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     795             :         {
     796             :             IF( ( error = ivas_omasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     797             :             {
     798             :                 return error;
     799             :             }
     800             :         }
     801             : 
     802             :         if ( GE_32( L_sub( ivas_total_brate, ism_total_brate ), MIN_BRATE_MDCT_STEREO ) )
     803             :         {
     804             :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     805             :             move16();
     806             :         }
     807             :         else
     808             :         {
     809             :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     810             :             move16();
     811             :         }
     812             : 
     813             :         IF( ( error = create_cpe_enc_fx( st_ivas, 0, L_sub( ivas_total_brate, ism_total_brate ) ) ) != IVAS_ERR_OK )
     814             :         {
     815             :             return error;
     816             :         }
     817             :     }
     818             :     ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
     819             :     {
     820             :         st_ivas->ism_mode = ISM_MODE_NONE;
     821             : 
     822             :         if ( GE_32( ivas_total_brate, IVAS_256k ) )
     823             :         {
     824             :             st_ivas->ism_mode = ISM_SBA_MODE_DISC;
     825             :         }
     826             : 
     827             :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     828             :         {
     829             :             return error;
     830             :         }
     831             : 
     832             :         /* allocate and initialize SBA handles */
     833             :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     834             :         {
     835             :             return error;
     836             :         }
     837             : 
     838             :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     839             :         move16();
     840             : 
     841             :         IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
     842             :         {
     843             :             return error;
     844             :         }
     845             : 
     846             :         IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     847             :         {
     848             :             return error;
     849             :         }
     850             : 
     851             :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_NONE ) )
     852             :         {
     853             :             /* allocate and initialize SBA core-coders */
     854             :             IF( EQ_16( st_ivas->nchan_transport, 1 ) )
     855             :             {
     856             :                 IF( ( error = create_sce_enc_fx( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
     857             :                 {
     858             :                     return error;
     859             :                 }
     860             :             }
     861             : 
     862             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     863             :             {
     864             :                 Word32 res_dec, res_frac;
     865             :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
     866             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
     867             :                 {
     868             :                     return error;
     869             :                 }
     870             :             }
     871             : 
     872             :             IF( GT_16( st_ivas->nCPE, 1 ) )
     873             :             {
     874             :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     875             :                 {
     876             :                     return error;
     877             :                 }
     878             :             }
     879             :         }
     880             :         ELSE
     881             :         {
     882             :             /* allocate and initialize MCT core coder */
     883             :             st_ivas->nCPE = add( st_ivas->nCPE, shr( add( st_ivas->hEncoderConfig->nchan_ism, 1 ), 1 ) );
     884             : 
     885             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     886             :             {
     887             :                 Word32 res_dec, res_frac;
     888             :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
     889             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
     890             :                 {
     891             :                     return error;
     892             :                 }
     893             :             }
     894             : 
     895             :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     896             :             {
     897             :                 return error;
     898             :             }
     899             :         }
     900             :         IF( ( error = ivas_osba_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     901             :         {
     902             :             return error;
     903             :         }
     904             :     }
     905             :     ELSE IF( EQ_32( ivas_format, MC_FORMAT ) )
     906             :     {
     907             :         st_ivas->mc_mode = ivas_mc_mode_select_fx( hEncoderConfig->mc_input_setup, ivas_total_brate ); /* Q0 */
     908             : 
     909             :         IF( ( error = ivas_create_lfe_lpf_enc_fx( &st_ivas->hLfeLpf, hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
     910             :         {
     911             :             return error;
     912             :         }
     913             : 
     914             :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
     915             :         {
     916             :             st_ivas->nSCE = 0;
     917             :             st_ivas->nCPE = idiv1616( hEncoderConfig->nchan_inp, CPE_CHANNELS ); /* Q0 */
     918             :             move16();
     919             :             move16();
     920             : 
     921             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     922             :             {
     923             :                 Word32 res_dec, res_frac;
     924             :                 iDiv_and_mod_32( ivas_total_brate, sub( hEncoderConfig->nchan_inp, 1 ), &res_dec, &res_frac, 0 );
     925             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
     926             :                 {
     927             :                     return error;
     928             :                 }
     929             :             }
     930             : 
     931             :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     932             :             {
     933             :                 return error;
     934             :             }
     935             : 
     936             :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     937             :             {
     938             :                 return error;
     939             :             }
     940             : 
     941             :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup ); /* Q0 */
     942             :             move16();
     943             :         }
     944             :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
     945             :         {
     946             :             st_ivas->nSCE = 0;
     947             :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
     948             :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     949             :             move16();
     950             :             move16();
     951             :             move16();
     952             : 
     953             :             IF( ( error = ivas_mc_paramupmix_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     954             :             {
     955             :                 return error;
     956             :             }
     957             : 
     958             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     959             :             {
     960             :                 Word32 res_dec, res_frac;
     961             :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nCPE, &res_dec, &res_frac, 0 );
     962             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, res_dec ) ) != IVAS_ERR_OK )
     963             :                 {
     964             :                     return error;
     965             :                 }
     966             :             }
     967             : 
     968             :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     969             :             {
     970             :                 return error;
     971             :             }
     972             : 
     973             :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     974             :             {
     975             :                 return error;
     976             :             }
     977             :         }
     978             :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
     979             :         {
     980             :             IF( ( error = ivas_param_mc_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     981             :             {
     982             :                 return error;
     983             :             }
     984             : 
     985             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     986             :             {
     987             :                 Word32 res_dec, res_frac;
     988             :                 iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nCPE, st_ivas->nSCE ), &res_dec, &res_frac, 0 );
     989             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, res_dec ) ) != IVAS_ERR_OK )
     990             :                 {
     991             :                     return error;
     992             :                 }
     993             :             }
     994             : 
     995             :             IF( GT_16( st_ivas->nCPE, 1 ) )
     996             :             {
     997             :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     998             :                 {
     999             :                     return error;
    1000             :                 }
    1001             :             }
    1002             :         }
    1003             :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1004             :         {
    1005             :             Word32 brate_sce, brate_cpe;
    1006             : 
    1007             :             ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
    1008             : 
    1009             :             IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1010             :             {
    1011             :                 return error;
    1012             :             }
    1013             : 
    1014             :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1015             :             {
    1016             :                 return error;
    1017             :             }
    1018             :             IF( ( error = ivas_mcmasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1019             :             {
    1020             :                 return error;
    1021             :             }
    1022             : 
    1023             :             ivas_mcmasa_split_brate_fx( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
    1024             : 
    1025             :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1026             :             {
    1027             :                 IF( ( error = create_sce_enc_fx( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
    1028             :                 {
    1029             :                     return error;
    1030             :                 }
    1031             :             }
    1032             : 
    1033             :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1034             :             {
    1035             :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
    1036             :                 move16();
    1037             : 
    1038             :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
    1039             :                 {
    1040             :                     return error;
    1041             :                 }
    1042             :             }
    1043             :         }
    1044             :     }
    1045             : 
    1046             :     /*-----------------------------------------------------------------*
    1047             :      * Allocate and initialize HP20 filter memories
    1048             :      *-----------------------------------------------------------------*/
    1049             : 
    1050             :     /* set number of input channels used for analysis/coding */
    1051             :     n = getNumChanAnalysis_fx( st_ivas ); /* Q0 */
    1052             :     move16();
    1053             : 
    1054             :     IF( n > 0 )
    1055             :     {
    1056             :         IF( ( st_ivas->mem_hp20_in_fx = (Word32 **) malloc( n * sizeof( Word32 * ) ) ) == NULL )
    1057             :         {
    1058             :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1059             :         }
    1060             :     }
    1061             :     ELSE
    1062             :     {
    1063             :         st_ivas->mem_hp20_in_fx = NULL;
    1064             :     }
    1065             : 
    1066             :     FOR( i = 0; i < n; i++ )
    1067             :     {
    1068             :         IF( ( st_ivas->mem_hp20_in_fx[i] = (Word32 *) malloc( ( L_HP20_MEM + 2 ) * sizeof( Word32 ) ) ) == NULL )
    1069             :         {
    1070             :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1071             :         }
    1072             : 
    1073             :         set32_fx( st_ivas->mem_hp20_in_fx[i], 0, L_HP20_MEM + 2 );
    1074             :     }
    1075             :     return error;
    1076             : }
    1077             : #endif
    1078         624 : ivas_error ivas_init_encoder_fx(
    1079             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    1080             : )
    1081             : {
    1082             :     Word16 i, n;
    1083             :     Word16 nchan_inp_buff;
    1084             :     Word16 sce_id, cpe_id;
    1085             :     IVAS_FORMAT ivas_format;
    1086             :     Word32 input_Fs, ivas_total_brate;
    1087             :     Word32 element_brate_tmp[MAX_NUM_OBJECTS];
    1088             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1089             :     ivas_error error;
    1090             : 
    1091         624 :     error = IVAS_ERR_OK;
    1092         624 :     move32();
    1093             : 
    1094         624 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1095         624 :     ivas_format = hEncoderConfig->ivas_format;
    1096         624 :     move32();
    1097         624 :     input_Fs = hEncoderConfig->input_Fs; /* Q0 */
    1098         624 :     move32();
    1099         624 :     ivas_total_brate = hEncoderConfig->ivas_total_brate; /* Q0 */
    1100         624 :     move32();
    1101             : 
    1102         624 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate; /* Q0 */
    1103         624 :     move32();
    1104             : 
    1105         624 :     if ( NE_16( ivas_format, MONO_FORMAT ) )
    1106             :     {
    1107             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
    1108         621 :         hEncoderConfig->max_bwidth = s_max( hEncoderConfig->max_bwidth, WB ); /* Q0 */
    1109         621 :         move16();
    1110             :     }
    1111         624 :     st_ivas->ism_mode = ISM_MODE_NONE;
    1112         624 :     move32();
    1113         624 :     st_ivas->mc_mode = MC_MODE_NONE;
    1114         624 :     move32();
    1115             : 
    1116         624 :     st_ivas->nchan_transport = -1;
    1117         624 :     move16();
    1118             : 
    1119             :     /*-----------------------------------------------------------------*
    1120             :      * Allocate floating-point input audio buffers
    1121             :      *-----------------------------------------------------------------*/
    1122             : 
    1123         624 :     nchan_inp_buff = hEncoderConfig->nchan_inp; /* Q0 */
    1124         624 :     move16();
    1125         624 :     IF( EQ_16( ivas_format, MONO_FORMAT ) )
    1126             :     {
    1127           3 :         nchan_inp_buff = 0;
    1128           3 :         move16();
    1129             :     }
    1130         621 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
    1131             :     {
    1132          44 :         IF( EQ_16( sub( hEncoderConfig->nchan_inp, hEncoderConfig->nchan_ism ), 1 ) ) /* mono is duplicated in monoMASA */
    1133             :         {
    1134          12 :             nchan_inp_buff = add( nchan_inp_buff, 1 ); /* Q0 */
    1135             :         }
    1136             : 
    1137          44 :         nchan_inp_buff = add( nchan_inp_buff, 1 ); /* for *data_separated_object        Q0*/
    1138             :     }
    1139             : 
    1140        4278 :     FOR( n = 0; n < nchan_inp_buff; n++ )
    1141             :     {
    1142             :         /* note: these are intra-frame heap memories */
    1143        3654 :         IF( ( st_ivas->p_data_fx[n] = (Word32 *) malloc( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) * sizeof( Word32 ) ) ) == NULL )
    1144             :         {
    1145           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for floating-point input audio buffer!\n" ) );
    1146             :         }
    1147        3654 :         set32_fx( st_ivas->p_data_fx[n], 0, extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) ) );
    1148             :     }
    1149         624 :     st_ivas->q_data_fx = Q11;
    1150         624 :     move16();
    1151        9450 :     FOR( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1152             :     {
    1153        8826 :         st_ivas->p_data_fx[n] = NULL;
    1154             :     }
    1155             : 
    1156             : 
    1157             :     /*-----------------------------------------------------------------*
    1158             :      * Allocate and initialize buffer of indices
    1159             :      *-----------------------------------------------------------------*/
    1160             : 
    1161             :     /* set the maximum allowed number of indices in the list */
    1162         624 :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices_fx( ivas_format, ivas_total_brate ); /* Q0 */
    1163         624 :     move16();
    1164             : 
    1165             :     /* allocate buffer of indices */
    1166         624 :     IF( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
    1167             :     {
    1168           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
    1169             :     }
    1170             : 
    1171             :     /* reset the list of indices */
    1172      495014 :     FOR( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
    1173             :     {
    1174      494390 :         st_ivas->ind_list[i].nb_bits = -1;
    1175      494390 :         move16();
    1176             :     }
    1177             : 
    1178             :     /* set the maximum allowed number of metadata indices in the list */
    1179         624 :     st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata_fx( st_ivas->hEncoderConfig->ivas_format, st_ivas->hEncoderConfig->ivas_total_brate ); /* Q0 */
    1180         624 :     move16();
    1181             :     /* allocate buffer of metadata indices */
    1182         624 :     IF( st_ivas->ivas_max_num_indices_metadata > 0 )
    1183             :     {
    1184         621 :         IF( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
    1185             :         {
    1186           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
    1187             :         }
    1188             : 
    1189             :         /* reset the list of metadata indices */
    1190      248661 :         FOR( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
    1191             :         {
    1192      248040 :             st_ivas->ind_list_metadata[i].nb_bits = -1;
    1193      248040 :             move16();
    1194             :         }
    1195             :     }
    1196             :     ELSE
    1197             :     {
    1198           3 :         st_ivas->ind_list_metadata = NULL;
    1199             :     }
    1200             : 
    1201             :     /*-----------------------------------------------------------------*
    1202             :      * Allocate and initialize SCE/CPE and other handles
    1203             :      *-----------------------------------------------------------------*/
    1204             : 
    1205         624 :     IF( EQ_32( ivas_format, MONO_FORMAT ) )
    1206             :     {
    1207           3 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
    1208           3 :         move16();
    1209           3 :         st_ivas->nCPE = 0;
    1210           3 :         move16();
    1211           3 :         st_ivas->nchan_transport = 1;
    1212           3 :         move16();
    1213           3 :         sce_id = 0;
    1214           3 :         move16();
    1215             : #ifndef HARM_SCE_INIT
    1216           3 :         test();
    1217           3 :         IF( NE_32( ( error = create_evs_sce_enc_fx( st_ivas, sce_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
    1218             :         {
    1219           0 :             return error;
    1220             :         }
    1221             : #else
    1222             :         test();
    1223             :         IF( NE_32( ( error = create_sce_enc_fx( st_ivas, sce_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
    1224             :         {
    1225             :             return error;
    1226             :         }
    1227             : #endif
    1228             :         /* prepare stereo downmix for EVS */
    1229           3 :         IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
    1230             :         {
    1231           2 :             IF( ( error = stereo_dmx_evs_init_encoder_fx( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
    1232             :             {
    1233           0 :                 return error;
    1234             :             }
    1235             :         }
    1236             :     }
    1237         621 :     ELSE IF( EQ_32( ivas_format, STEREO_FORMAT ) )
    1238             :     {
    1239          68 :         st_ivas->nSCE = 0;
    1240          68 :         move16();
    1241          68 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
    1242          68 :         move16();
    1243          68 :         st_ivas->nchan_transport = CPE_CHANNELS;
    1244          68 :         move16();
    1245          68 :         cpe_id = 0;
    1246          68 :         move16();
    1247             : 
    1248          68 :         IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
    1249             :         {
    1250           0 :             return error;
    1251             :         }
    1252             :     }
    1253         553 :     ELSE IF( EQ_32( ivas_format, ISM_FORMAT ) )
    1254             :     {
    1255          74 :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate ); /* Q0 */
    1256          74 :         move32();
    1257             : 
    1258          74 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_inp, element_brate_tmp ) ) != IVAS_ERR_OK )
    1259             :         {
    1260           0 :             return error;
    1261             :         }
    1262             : 
    1263         261 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1264             :         {
    1265         187 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
    1266             :             {
    1267           0 :                 return error;
    1268             :             }
    1269             :         }
    1270             : 
    1271          74 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1272             :         {
    1273          17 :             IF( ( error = ivas_param_ism_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1274             :             {
    1275           0 :                 return error;
    1276             :             }
    1277             :         }
    1278             : 
    1279          74 :         IF( st_ivas->hEncoderConfig->Opt_DTX_ON )
    1280             :         {
    1281          14 :             IF( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
    1282             :             {
    1283           0 :                 return error;
    1284             :             }
    1285             :         }
    1286             :     }
    1287         479 :     ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, MASA_FORMAT ) )
    1288             :     {
    1289         319 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1290             :         {
    1291           0 :             return error;
    1292             :         }
    1293             : 
    1294         319 :         IF( EQ_32( ivas_format, SBA_FORMAT ) )
    1295             :         {
    1296         244 :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order ); /* Q0 */
    1297         244 :             move16();
    1298             : 
    1299         244 :             IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1300             :             {
    1301           0 :                 return error;
    1302             :             }
    1303         244 :             IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1304             :             {
    1305           0 :                 return error;
    1306             :             }
    1307             :         }
    1308             :         ELSE
    1309             :         {
    1310          75 :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp; /* Q0 */
    1311          75 :             move16();
    1312             : 
    1313          75 :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1314             :             {
    1315           0 :                 return error;
    1316             :             }
    1317             :         }
    1318             : 
    1319         449 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1320             :         {
    1321             :             Word32 res_dec, res_frac;
    1322         130 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
    1323         130 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, res_dec ) ) != IVAS_ERR_OK )
    1324             :             {
    1325           0 :                 return error;
    1326             :             }
    1327         130 :             test();
    1328         130 :             IF( EQ_16( ivas_format, SBA_FORMAT ) && st_ivas->hEncoderConfig->Opt_DTX_ON )
    1329             :             {
    1330          45 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
    1331          45 :                 move16();
    1332             :             }
    1333             :         }
    1334             : 
    1335         591 :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1336             :         {
    1337             :             Word32 res_dec, res_frac;
    1338         272 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
    1339         272 :             IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1340             :             {
    1341           0 :                 return error;
    1342             :             }
    1343             : 
    1344         816 :             FOR( n = 0; n < CPE_CHANNELS; n++ )
    1345             :             {
    1346         544 :                 if ( hEncoderConfig->Opt_DTX_ON )
    1347             :                 {
    1348          56 :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
    1349          56 :                     move16();
    1350             :                 }
    1351             :             }
    1352             :         }
    1353             : 
    1354         319 :         IF( GT_16( st_ivas->nCPE, 1 ) )
    1355             :         {
    1356          83 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1357             :             {
    1358           0 :                 return error;
    1359             :             }
    1360             :         }
    1361             :     }
    1362         160 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
    1363             :     {
    1364             :         Word32 ism_total_brate;
    1365             :         Word16 k;
    1366             : 
    1367          44 :         st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( ivas_total_brate, hEncoderConfig->nchan_ism ); /* Q0 */
    1368          44 :         move32();
    1369          44 :         st_ivas->nchan_transport = 2;
    1370          44 :         move16();
    1371             : 
    1372          44 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
    1373             :         {
    1374           0 :             return error;
    1375             :         }
    1376             : 
    1377          44 :         k = 0;
    1378          44 :         move16();
    1379          44 :         test();
    1380         359 :         WHILE( LT_16( k, SIZE_IVAS_BRATE_TBL ) && NE_32( ivas_total_brate, ivas_brate_tbl[k] ) )
    1381             :         {
    1382         315 :             k++;
    1383             :         }
    1384             : 
    1385          44 :         ism_total_brate = 0;
    1386          44 :         move32();
    1387         111 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1388             :         {
    1389          67 :             ism_total_brate = L_add( ism_total_brate, sep_object_brate[k - 2][st_ivas->nSCE - 1] ); /* Q0 */
    1390          67 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, sep_object_brate[k - 2][st_ivas->nSCE - 1] ) ) != IVAS_ERR_OK )
    1391             :             {
    1392           0 :                 return error;
    1393             :             }
    1394             :         }
    1395             : 
    1396          44 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1397             :         {
    1398           0 :             return error;
    1399             :         }
    1400          44 :         IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1401             :         {
    1402           0 :             return error;
    1403             :         }
    1404             : 
    1405          44 :         IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    1406             :         {
    1407          27 :             IF( ( error = ivas_omasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1408             :             {
    1409           0 :                 return error;
    1410             :             }
    1411             :         }
    1412             : 
    1413          44 :         IF( GE_32( L_sub( ivas_total_brate, ism_total_brate ), MIN_BRATE_MDCT_STEREO ) )
    1414             :         {
    1415          17 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
    1416          17 :             move16();
    1417             :         }
    1418             :         ELSE
    1419             :         {
    1420          27 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
    1421          27 :             move16();
    1422             :         }
    1423             : 
    1424          44 :         IF( ( error = create_cpe_enc_fx( st_ivas, 0, L_sub( ivas_total_brate, ism_total_brate ) ) ) != IVAS_ERR_OK )
    1425             :         {
    1426           0 :             return error;
    1427             :         }
    1428             :     }
    1429         116 :     ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
    1430             :     {
    1431          35 :         st_ivas->ism_mode = ISM_MODE_NONE;
    1432          35 :         move16();
    1433             : 
    1434          35 :         IF( GE_32( ivas_total_brate, IVAS_256k ) )
    1435             :         {
    1436          15 :             st_ivas->ism_mode = ISM_SBA_MODE_DISC;
    1437          15 :             move16();
    1438             :         }
    1439             : 
    1440          35 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
    1441             :         {
    1442           0 :             return error;
    1443             :         }
    1444             : 
    1445             :         /* allocate and initialize SBA handles */
    1446          35 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1447             :         {
    1448           0 :             return error;
    1449             :         }
    1450             : 
    1451          35 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order ); /* Q0 */
    1452             : 
    1453          35 :         IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1454             :         {
    1455           0 :             return error;
    1456             :         }
    1457             : 
    1458          35 :         IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1459             :         {
    1460           0 :             return error;
    1461             :         }
    1462             : 
    1463          35 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_NONE ) )
    1464             :         {
    1465             :             /* allocate and initialize SBA core-coders */
    1466          20 :             IF( EQ_16( st_ivas->nchan_transport, 1 ) )
    1467             :             {
    1468          11 :                 IF( ( error = create_sce_enc_fx( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
    1469             :                 {
    1470           0 :                     return error;
    1471             :                 }
    1472             :             }
    1473             : 
    1474          34 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1475             :             {
    1476             :                 Word32 res_dec, res_frac;
    1477          14 :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
    1478          14 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1479             :                 {
    1480           0 :                     return error;
    1481             :                 }
    1482             :             }
    1483             : 
    1484          20 :             IF( GT_16( st_ivas->nCPE, 1 ) )
    1485             :             {
    1486           5 :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1487             :                 {
    1488           0 :                     return error;
    1489             :                 }
    1490             :             }
    1491             :         }
    1492             :         ELSE
    1493             :         {
    1494             :             /* allocate and initialize MCT core coder */
    1495          15 :             st_ivas->nCPE = add( st_ivas->nCPE, shr( add( st_ivas->hEncoderConfig->nchan_ism, 1 ), 1 ) ); /* Q0 */
    1496             : 
    1497          70 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1498             :             {
    1499             :                 Word32 res_dec, res_frac;
    1500          55 :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
    1501          55 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1502             :                 {
    1503           0 :                     return error;
    1504             :                 }
    1505             :             }
    1506             : 
    1507          15 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1508             :             {
    1509           0 :                 return error;
    1510             :             }
    1511             :         }
    1512          35 :         IF( ( error = ivas_osba_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1513             :         {
    1514           0 :             return error;
    1515             :         }
    1516             :     }
    1517          81 :     ELSE IF( EQ_32( ivas_format, MC_FORMAT ) )
    1518             :     {
    1519          81 :         st_ivas->mc_mode = ivas_mc_mode_select_fx( hEncoderConfig->mc_input_setup, ivas_total_brate ); /* Q0 */
    1520          81 :         move32();
    1521             : 
    1522          81 :         IF( ( error = ivas_create_lfe_lpf_enc_fx( &st_ivas->hLfeLpf, hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
    1523             :         {
    1524           0 :             return error;
    1525             :         }
    1526             : 
    1527          81 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
    1528             :         {
    1529          36 :             st_ivas->nSCE = 0;
    1530          36 :             move16();
    1531          36 :             st_ivas->nCPE = idiv1616( hEncoderConfig->nchan_inp, CPE_CHANNELS ); /* Q0 */
    1532          36 :             move16();
    1533             : 
    1534         181 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1535             :             {
    1536             :                 Word32 res_dec, res_frac;
    1537         145 :                 iDiv_and_mod_32( ivas_total_brate, sub( hEncoderConfig->nchan_inp, 1 ), &res_dec, &res_frac, 0 );
    1538         145 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, imult3216( res_dec, CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1539             :                 {
    1540           0 :                     return error;
    1541             :                 }
    1542             :             }
    1543             : 
    1544          36 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1545             :             {
    1546           0 :                 return error;
    1547             :             }
    1548             : 
    1549          36 :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
    1550             :             {
    1551           0 :                 return error;
    1552             :             }
    1553             : 
    1554          36 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup ); /* Q0 */
    1555          36 :             move16();
    1556             :         }
    1557          45 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    1558             :         {
    1559           5 :             st_ivas->nSCE = 0;
    1560           5 :             move16();
    1561           5 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS >> 1;
    1562           5 :             move16();
    1563           5 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    1564           5 :             move16();
    1565             : 
    1566           5 :             IF( ( error = ivas_mc_paramupmix_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1567             :             {
    1568           0 :                 return error;
    1569             :             }
    1570             : 
    1571          25 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1572             :             {
    1573             :                 Word32 res_dec, res_frac;
    1574          20 :                 iDiv_and_mod_32( ivas_total_brate, st_ivas->nCPE, &res_dec, &res_frac, 0 );
    1575          20 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, res_dec ) ) != IVAS_ERR_OK )
    1576             :                 {
    1577           0 :                     return error;
    1578             :                 }
    1579             :             }
    1580             : 
    1581           5 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1582             :             {
    1583           0 :                 return error;
    1584             :             }
    1585             : 
    1586           5 :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
    1587             :             {
    1588           0 :                 return error;
    1589             :             }
    1590             :         }
    1591          40 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    1592             :         {
    1593          10 :             IF( ( error = ivas_param_mc_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1594             :             {
    1595           0 :                 return error;
    1596             :             }
    1597             : 
    1598          20 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1599             :             {
    1600             :                 Word32 res_dec, res_frac;
    1601          10 :                 iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nCPE, st_ivas->nSCE ), &res_dec, &res_frac, 0 );
    1602          10 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, res_dec ) ) != IVAS_ERR_OK )
    1603             :                 {
    1604           0 :                     return error;
    1605             :                 }
    1606             :             }
    1607             : 
    1608          10 :             IF( GT_16( st_ivas->nCPE, 1 ) )
    1609             :             {
    1610           0 :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
    1611             :                 {
    1612           0 :                     return error;
    1613             :                 }
    1614             :             }
    1615             :         }
    1616          30 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1617             :         {
    1618             :             Word32 brate_sce, brate_cpe;
    1619             : 
    1620          30 :             ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
    1621             : 
    1622          30 :             IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1623             :             {
    1624           0 :                 return error;
    1625             :             }
    1626             : 
    1627          30 :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1628             :             {
    1629           0 :                 return error;
    1630             :             }
    1631             : 
    1632          30 :             IF( ( error = ivas_mcmasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
    1633             :             {
    1634           0 :                 return error;
    1635             :             }
    1636          30 :             ivas_mcmasa_split_brate_fx( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
    1637             : 
    1638          58 :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1639             :             {
    1640          28 :                 IF( ( error = create_sce_enc_fx( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
    1641             :                 {
    1642           0 :                     return error;
    1643             :                 }
    1644             :             }
    1645             : 
    1646          35 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1647             :             {
    1648           5 :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT; /* Q0 */
    1649           5 :                 move16();
    1650             : 
    1651           5 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
    1652             :                 {
    1653           0 :                     return error;
    1654             :                 }
    1655             :             }
    1656             :         }
    1657             :     }
    1658             : 
    1659             :     /*-----------------------------------------------------------------*
    1660             :      * Allocate and initialize HP20 filter memories
    1661             :      *-----------------------------------------------------------------*/
    1662             : 
    1663             :     /* set number of input channels used for analysis/coding */
    1664         624 :     n = getNumChanAnalysis_fx( st_ivas ); /* Q0 */
    1665         624 :     move16();
    1666             : 
    1667         624 :     IF( n > 0 )
    1668             :     {
    1669         624 :         IF( ( st_ivas->mem_hp20_in_fx = (Word32 **) malloc( n * sizeof( Word32 * ) ) ) == NULL )
    1670             :         {
    1671           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1672             :         }
    1673             :     }
    1674             :     ELSE
    1675             :     {
    1676           0 :         st_ivas->mem_hp20_in_fx = NULL;
    1677             :     }
    1678             : 
    1679        3380 :     FOR( i = 0; i < n; i++ )
    1680             :     {
    1681        2756 :         IF( ( st_ivas->mem_hp20_in_fx[i] = (Word32 *) malloc( ( L_HP20_MEM + 2 ) * sizeof( Word32 ) ) ) == NULL )
    1682             :         {
    1683           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1684             :         }
    1685             : 
    1686        2756 :         set32_fx( st_ivas->mem_hp20_in_fx[i], 0, L_HP20_MEM + 2 );
    1687             :     }
    1688             : 
    1689         624 :     return error;
    1690             : }
    1691             : /*-------------------------------------------------------------------------
    1692             :  * destroy_core_enc()
    1693             :  *
    1694             :  * Close core encoder handles
    1695             :  *-------------------------------------------------------------------------*/
    1696             : 
    1697        8252 : void destroy_core_enc_fx(
    1698             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
    1699             : )
    1700             : {
    1701             :     Word16 i;
    1702             : 
    1703        8252 :     destroy_cldfb_encoder_fx( hCoreCoder );
    1704             : 
    1705        8252 :     IF( hCoreCoder->hSignalBuf != NULL )
    1706             :     {
    1707        8183 :         free( hCoreCoder->hSignalBuf );
    1708        8183 :         hCoreCoder->hSignalBuf = NULL;
    1709             :     }
    1710             : 
    1711        8252 :     IF( hCoreCoder->hBstr != NULL )
    1712             :     {
    1713             :         /* reset buffer of indices */
    1714        8183 :         FOR( i = 0; i < hCoreCoder->hBstr->nb_ind_tot; i++ )
    1715             :         {
    1716           0 :             hCoreCoder->hBstr->ind_list[i].nb_bits = -1;
    1717           0 :             move16();
    1718             :         }
    1719        8183 :         free( hCoreCoder->hBstr );
    1720        8183 :         hCoreCoder->hBstr = NULL;
    1721             :     }
    1722             : 
    1723        8252 :     IF( hCoreCoder->hLPDmem != NULL )
    1724             :     {
    1725        3042 :         free( hCoreCoder->hLPDmem );
    1726        3042 :         hCoreCoder->hLPDmem = NULL;
    1727             :     }
    1728             : 
    1729        8252 :     IF( hCoreCoder->hTranDet != NULL )
    1730             :     {
    1731        8183 :         free( hCoreCoder->hTranDet );
    1732        8183 :         hCoreCoder->hTranDet = NULL;
    1733             :     }
    1734             : 
    1735        8252 :     IF( hCoreCoder->hNoiseEst != NULL )
    1736             :     {
    1737        8171 :         free( hCoreCoder->hNoiseEst );
    1738        8171 :         hCoreCoder->hNoiseEst = NULL;
    1739             :     }
    1740             : 
    1741        8252 :     IF( hCoreCoder->hVAD != NULL )
    1742             :     {
    1743        8102 :         free( hCoreCoder->hVAD );
    1744        8102 :         hCoreCoder->hVAD = NULL;
    1745             :     }
    1746             : 
    1747        8252 :     IF( hCoreCoder->hVAD_CLDFB != NULL )
    1748             :     {
    1749             :         /* This is not required as we are not allocating memory dynamically */
    1750             :         // free( hCoreCoder->hVAD_CLDFB );
    1751           0 :         hCoreCoder->hVAD_CLDFB = NULL;
    1752             :     }
    1753             : 
    1754        8252 :     IF( hCoreCoder->hTdCngEnc != NULL )
    1755             :     {
    1756          85 :         free( hCoreCoder->hTdCngEnc );
    1757          85 :         hCoreCoder->hTdCngEnc = NULL;
    1758             :     }
    1759             : 
    1760        8252 :     IF( hCoreCoder->hDtxEnc != NULL )
    1761             :     {
    1762         488 :         free( hCoreCoder->hDtxEnc );
    1763         488 :         hCoreCoder->hDtxEnc = NULL;
    1764             :     }
    1765             : 
    1766        8252 :     IF( hCoreCoder->hSpMusClas != NULL )
    1767             :     {
    1768        8171 :         free( hCoreCoder->hSpMusClas );
    1769        8171 :         hCoreCoder->hSpMusClas = NULL;
    1770             :     }
    1771             : 
    1772        8252 :     IF( hCoreCoder->hGSCEnc != NULL )
    1773             :     {
    1774        3042 :         free( hCoreCoder->hGSCEnc );
    1775        3042 :         hCoreCoder->hGSCEnc = NULL;
    1776             :     }
    1777             : 
    1778        8252 :     IF( hCoreCoder->hSC_VBR != NULL )
    1779             :     {
    1780           0 :         free( hCoreCoder->hSC_VBR );
    1781           0 :         hCoreCoder->hSC_VBR = NULL;
    1782             :     }
    1783             : 
    1784        8252 :     IF( hCoreCoder->hAmrwb_IO != NULL )
    1785             :     {
    1786           0 :         free( hCoreCoder->hAmrwb_IO );
    1787           0 :         hCoreCoder->hAmrwb_IO = NULL;
    1788             :     }
    1789             : 
    1790        8252 :     IF( hCoreCoder->hBWE_TD != NULL )
    1791             :     {
    1792        3041 :         free( hCoreCoder->hBWE_TD );
    1793        3041 :         hCoreCoder->hBWE_TD = NULL;
    1794             :     }
    1795             : 
    1796        8252 :     IF( hCoreCoder->hBWE_FD != NULL )
    1797             :     {
    1798        3041 :         free( hCoreCoder->hBWE_FD );
    1799        3041 :         hCoreCoder->hBWE_FD = NULL;
    1800             :     }
    1801             : 
    1802        8252 :     IF( hCoreCoder->hRF != NULL )
    1803             :     {
    1804           0 :         free( hCoreCoder->hRF );
    1805           0 :         hCoreCoder->hRF = NULL;
    1806             :     }
    1807             : 
    1808        8252 :     IF( hCoreCoder->hTECEnc != NULL )
    1809             :     {
    1810           0 :         free( hCoreCoder->hTECEnc );
    1811           0 :         hCoreCoder->hTECEnc = NULL;
    1812             :     }
    1813             : 
    1814        8252 :     IF( hCoreCoder->hTcxEnc != NULL )
    1815             :     {
    1816        8101 :         free( hCoreCoder->hTcxEnc );
    1817        8101 :         hCoreCoder->hTcxEnc = NULL;
    1818             :     }
    1819             : 
    1820        8252 :     IF( hCoreCoder->hTcxCfg != NULL )
    1821             :     {
    1822        8101 :         free( hCoreCoder->hTcxCfg );
    1823        8101 :         hCoreCoder->hTcxCfg = NULL;
    1824             :     }
    1825             : 
    1826        8252 :     IF( hCoreCoder->hIGFEnc != NULL )
    1827             :     {
    1828        6382 :         free( hCoreCoder->hIGFEnc );
    1829        6382 :         hCoreCoder->hIGFEnc = NULL;
    1830             :     }
    1831             : 
    1832        8252 :     IF( hCoreCoder->hPlcExt != NULL )
    1833             :     {
    1834           0 :         free( hCoreCoder->hPlcExt );
    1835           0 :         hCoreCoder->hPlcExt = NULL;
    1836             :     }
    1837             : 
    1838        8252 :     IF( hCoreCoder->hHQ_core != NULL )
    1839             :     {
    1840        3061 :         free( hCoreCoder->hHQ_core );
    1841        3061 :         hCoreCoder->hHQ_core = NULL;
    1842             :     }
    1843             : 
    1844        8252 :     free( hCoreCoder );
    1845             : 
    1846        8252 :     return;
    1847             : }
    1848             : 
    1849           3 : void destroy_evs_core_enc_fx(
    1850             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
    1851             : )
    1852             : {
    1853             :     Word16 i;
    1854             : 
    1855           3 :     destroy_encoder_fx( hCoreCoder );
    1856             : 
    1857           3 :     IF( hCoreCoder->hSignalBuf != NULL )
    1858             :     {
    1859           3 :         free( hCoreCoder->hSignalBuf );
    1860           3 :         hCoreCoder->hSignalBuf = NULL;
    1861             :     }
    1862             : 
    1863           3 :     IF( hCoreCoder->hBstr != NULL )
    1864             :     {
    1865             :         /* reset buffer of indices */
    1866           3 :         FOR( i = 0; i < hCoreCoder->hBstr->nb_ind_tot; i++ )
    1867             :         {
    1868           0 :             hCoreCoder->hBstr->ind_list[i].nb_bits = -1;
    1869           0 :             move16();
    1870             :         }
    1871           3 :         free( hCoreCoder->hBstr );
    1872           3 :         hCoreCoder->hBstr = NULL;
    1873             :     }
    1874             : 
    1875           3 :     IF( hCoreCoder->hLPDmem != NULL )
    1876             :     {
    1877           3 :         free( hCoreCoder->hLPDmem );
    1878           3 :         hCoreCoder->hLPDmem = NULL;
    1879             :     }
    1880             : 
    1881           3 :     IF( hCoreCoder->hTranDet != NULL )
    1882             :     {
    1883           0 :         free( hCoreCoder->hTranDet );
    1884           0 :         hCoreCoder->hTranDet = NULL;
    1885             :     }
    1886             : 
    1887           3 :     IF( hCoreCoder->hNoiseEst != NULL )
    1888             :     {
    1889           3 :         free( hCoreCoder->hNoiseEst );
    1890           3 :         hCoreCoder->hNoiseEst = NULL;
    1891             :     }
    1892             : 
    1893           3 :     IF( hCoreCoder->hVAD != NULL )
    1894             :     {
    1895           3 :         free( hCoreCoder->hVAD );
    1896           3 :         hCoreCoder->hVAD = NULL;
    1897             :     }
    1898             : 
    1899           3 :     IF( hCoreCoder->hVAD_CLDFB != NULL )
    1900             :     {
    1901             :         /* This is not required as we are not allocating memory dynamically */
    1902             :         // free( hCoreCoder->hVAD_CLDFB );
    1903           0 :         hCoreCoder->hVAD_CLDFB = NULL;
    1904             :     }
    1905             : 
    1906           3 :     IF( hCoreCoder->hTdCngEnc != NULL )
    1907             :     {
    1908           3 :         free( hCoreCoder->hTdCngEnc );
    1909           3 :         hCoreCoder->hTdCngEnc = NULL;
    1910             :     }
    1911             : 
    1912           3 :     IF( hCoreCoder->hDtxEnc != NULL )
    1913             :     {
    1914           3 :         free( hCoreCoder->hDtxEnc );
    1915           3 :         hCoreCoder->hDtxEnc = NULL;
    1916             :     }
    1917             : 
    1918           3 :     IF( hCoreCoder->hSpMusClas != NULL )
    1919             :     {
    1920           3 :         free( hCoreCoder->hSpMusClas );
    1921           3 :         hCoreCoder->hSpMusClas = NULL;
    1922             :     }
    1923             : 
    1924           3 :     IF( hCoreCoder->hGSCEnc != NULL )
    1925             :     {
    1926           3 :         free( hCoreCoder->hGSCEnc );
    1927           3 :         hCoreCoder->hGSCEnc = NULL;
    1928             :     }
    1929             : 
    1930           3 :     IF( hCoreCoder->hSC_VBR != NULL )
    1931             :     {
    1932           3 :         free( hCoreCoder->hSC_VBR );
    1933           3 :         hCoreCoder->hSC_VBR = NULL;
    1934             :     }
    1935             : 
    1936           3 :     IF( hCoreCoder->hAmrwb_IO != NULL )
    1937             :     {
    1938           3 :         free( hCoreCoder->hAmrwb_IO );
    1939           3 :         hCoreCoder->hAmrwb_IO = NULL;
    1940             :     }
    1941             : 
    1942           3 :     IF( hCoreCoder->hBWE_TD != NULL )
    1943             :     {
    1944           3 :         free( hCoreCoder->hBWE_TD );
    1945           3 :         hCoreCoder->hBWE_TD = NULL;
    1946             :     }
    1947             : 
    1948           3 :     IF( hCoreCoder->hBWE_FD != NULL )
    1949             :     {
    1950           3 :         free( hCoreCoder->hBWE_FD );
    1951           3 :         hCoreCoder->hBWE_FD = NULL;
    1952             :     }
    1953             : 
    1954           3 :     IF( hCoreCoder->hRF != NULL )
    1955             :     {
    1956           3 :         free( hCoreCoder->hRF );
    1957           3 :         hCoreCoder->hRF = NULL;
    1958             :     }
    1959             : 
    1960           3 :     IF( hCoreCoder->hTECEnc != NULL )
    1961             :     {
    1962           3 :         free( hCoreCoder->hTECEnc );
    1963           3 :         hCoreCoder->hTECEnc = NULL;
    1964             :     }
    1965             : 
    1966           3 :     IF( hCoreCoder->hTcxEnc != NULL )
    1967             :     {
    1968           3 :         free( hCoreCoder->hTcxEnc );
    1969           3 :         hCoreCoder->hTcxEnc = NULL;
    1970             :     }
    1971             : 
    1972           3 :     IF( hCoreCoder->hTcxCfg != NULL )
    1973             :     {
    1974           3 :         free( hCoreCoder->hTcxCfg );
    1975           3 :         hCoreCoder->hTcxCfg = NULL;
    1976             :     }
    1977             : 
    1978           3 :     IF( hCoreCoder->hIGFEnc != NULL )
    1979             :     {
    1980           3 :         free( hCoreCoder->hIGFEnc );
    1981           3 :         hCoreCoder->hIGFEnc = NULL;
    1982             :     }
    1983             : 
    1984           3 :     IF( hCoreCoder->hPlcExt != NULL )
    1985             :     {
    1986           3 :         free( hCoreCoder->hPlcExt );
    1987           3 :         hCoreCoder->hPlcExt = NULL;
    1988             :     }
    1989             : 
    1990           3 :     IF( hCoreCoder->hHQ_core != NULL )
    1991             :     {
    1992           3 :         free( hCoreCoder->hHQ_core );
    1993           3 :         hCoreCoder->hHQ_core = NULL;
    1994             :     }
    1995             : 
    1996           3 :     free( hCoreCoder );
    1997             : 
    1998           3 :     return;
    1999             : }
    2000             : 
    2001             : 
    2002             : /*-------------------------------------------------------------------------
    2003             :  * ivas_destroy_enc_fx()
    2004             :  *
    2005             :  * Close IVAS encoder handles
    2006             :  *-------------------------------------------------------------------------*/
    2007         624 : void ivas_destroy_enc_fx(
    2008             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    2009             : )
    2010             : {
    2011             :     Word16 i, n, nchan_inp;
    2012         624 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
    2013         624 :     move16();
    2014             : 
    2015             :     /* SCE handles */
    2016        3120 :     for ( i = 0; i < MAX_SCE; i++ )
    2017             :     {
    2018        2496 :         if ( st_ivas->hSCE[i] != NULL )
    2019             :         {
    2020         409 :             destroy_sce_enc_fx( st_ivas->hSCE[i], EQ_16( st_ivas->hEncoderConfig->element_mode_init, EVS_MONO ) );
    2021         409 :             st_ivas->hSCE[i] = NULL;
    2022             :         }
    2023             :     }
    2024             : 
    2025             :     /* CPE handles */
    2026        4368 :     for ( i = 0; i < MAX_CPE; i++ )
    2027             :     {
    2028        3744 :         if ( st_ivas->hCPE[i] != NULL )
    2029             :         {
    2030         738 :             destroy_cpe_enc( st_ivas->hCPE[i] );
    2031         738 :             st_ivas->hCPE[i] = NULL;
    2032             :         }
    2033             :     }
    2034             : 
    2035             :     /* HP20 filter handles */
    2036         624 :     if ( st_ivas->mem_hp20_in_fx != NULL )
    2037             :     {
    2038         624 :         n = getNumChanAnalysis_fx( st_ivas ); /* Q0 */
    2039             : 
    2040        3503 :         for ( i = 0; i < n; i++ )
    2041             :         {
    2042        2879 :             free( st_ivas->mem_hp20_in_fx[i] );
    2043        2879 :             st_ivas->mem_hp20_in_fx[i] = NULL;
    2044             :         }
    2045         624 :         free( st_ivas->mem_hp20_in_fx );
    2046         624 :         st_ivas->mem_hp20_in_fx = NULL;
    2047             :     }
    2048             : 
    2049             :     /* ISM metadata handles */
    2050         624 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    2051             : 
    2052             :     /* ISM DTX Handle */
    2053         624 :     if ( st_ivas->hISMDTX != NULL )
    2054             :     {
    2055          14 :         free( st_ivas->hISMDTX );
    2056          14 :         st_ivas->hISMDTX = NULL;
    2057             :     }
    2058             : 
    2059             :     /* Q Metadata handle */
    2060         624 :     ivas_qmetadata_close_fx( &( st_ivas->hQMetaData ) );
    2061             : 
    2062             :     /* DirAC handle */
    2063         624 :     ivas_dirac_enc_close_fx( &( st_ivas->hDirAC ), st_ivas->hEncoderConfig->input_Fs );
    2064             : 
    2065             :     /* ParamISM handle */
    2066         624 :     ivas_param_ism_enc_close_fx( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
    2067             : 
    2068             :     /* SPAR handle */
    2069         624 :     ivas_spar_enc_close_fx( &( st_ivas->hSpar ), st_ivas->hEncoderConfig->input_Fs, nchan_inp, 0 );
    2070             : 
    2071             :     /* MASA handle */
    2072         624 :     ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
    2073             : 
    2074             :     /* MCT handle */
    2075         624 :     ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
    2076             : 
    2077             :     /* LFE handle */
    2078         624 :     ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
    2079             : 
    2080             :     /* LFE low pass filter state */
    2081         624 :     ivas_lfe_lpf_enc_close_fx( &( st_ivas->hLfeLpf ) );
    2082             : 
    2083             :     /* Param-Upmix MC handle */
    2084         624 :     ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
    2085             : 
    2086             :     /* Parametric MC handle */
    2087         624 :     ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
    2088             : 
    2089             :     /* Multi-channel MASA handle */
    2090         624 :     ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
    2091             : 
    2092             :     /* OMASA handle */
    2093         624 :     ivas_omasa_enc_close_fx( &( st_ivas->hOMasa ) );
    2094             : 
    2095             :     /* OSBA handle */
    2096         624 :     ivas_osba_enc_close_fx( &( st_ivas->hOSba ) );
    2097             : 
    2098             :     /* Stereo downmix for EVS encoder handle */
    2099         624 :     stereo_dmx_evs_close_encoder_fx( &( st_ivas->hStereoDmxEVS ) );
    2100             : 
    2101             :     /* Encoder configuration handle */
    2102         624 :     IF( st_ivas->hEncoderConfig != NULL )
    2103             :     {
    2104         624 :         free( st_ivas->hEncoderConfig );
    2105         624 :         st_ivas->hEncoderConfig = NULL;
    2106             :     }
    2107             : 
    2108             :     /* Buffer of indices */
    2109         624 :     IF( st_ivas->ind_list != NULL )
    2110             :     {
    2111         624 :         free( st_ivas->ind_list );
    2112             :     }
    2113             : 
    2114         624 :     IF( st_ivas->ind_list_metadata != NULL )
    2115             :     {
    2116         621 :         free( st_ivas->ind_list_metadata );
    2117             :     }
    2118             : 
    2119             :     /* floating-point input audio buffers */
    2120       13104 :     FOR( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    2121             :     {
    2122       12480 :         IF( st_ivas->p_data_fx[n] != NULL )
    2123             :         {
    2124        3654 :             free( st_ivas->p_data_fx[n] );
    2125        3654 :             st_ivas->p_data_fx[n] = NULL;
    2126             :         }
    2127             :     }
    2128             : 
    2129             :     /* main IVAS handle */
    2130         624 :     free( st_ivas );
    2131             : 
    2132         624 :     return;
    2133             : }
    2134             : 
    2135             : /*-------------------------------------------------------------------------
    2136             :  * ivas_initialize_MD_bstr_enc_fx()
    2137             :  *
    2138             :  * Allocate and initialize SCE/CPE MD bitstream handle
    2139             :  *-------------------------------------------------------------------------*/
    2140             : 
    2141        4208 : ivas_error ivas_initialize_MD_bstr_enc_fx(
    2142             :     BSTR_ENC_HANDLE *hMetaData_out, /* o  : encoder MD bitstream handle */
    2143             :     Encoder_Struct *st_ivas         /* i/o: IVAS encoder structure      */
    2144             : )
    2145             : {
    2146             :     BSTR_ENC_HANDLE hMetaData;
    2147             : 
    2148        4208 :     IF( ( hMetaData = (BSTR_ENC_HANDLE) malloc( sizeof( BSTR_ENC_DATA ) ) ) == NULL )
    2149             :     {
    2150           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MetaData structure\n" ) );
    2151             :     }
    2152             : 
    2153             :     /* set pointer to the buffer of metadata indices */
    2154        4208 :     hMetaData->ind_list = st_ivas->ind_list_metadata;
    2155        4208 :     hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    2156        4208 :     hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata; /* Q0 */
    2157        4208 :     hMetaData->st_ivas = st_ivas;
    2158             : 
    2159        4208 :     reset_indices_enc_fx( hMetaData, st_ivas->ivas_max_num_indices_metadata );
    2160             : 
    2161        4208 :     *hMetaData_out = hMetaData;
    2162             : 
    2163        4208 :     return IVAS_ERR_OK;
    2164             : }
    2165             : 
    2166             : 
    2167             : /*-------------------------------------------------------------------------
    2168             :  * ivas_destroy_MD_bstr_enc_fx()
    2169             :  *
    2170             :  * Destroy SCE/CPE MD bitstream handle
    2171             :  *-------------------------------------------------------------------------*/
    2172             : 
    2173        8770 : void ivas_destroy_MD_bstr_enc_fx(
    2174             :     BSTR_ENC_HANDLE *hMetaData /* i/o: encoder MD bitstream handle  */
    2175             : )
    2176             : {
    2177        8770 :     test();
    2178        8770 :     IF( hMetaData == NULL || *hMetaData == NULL )
    2179             :     {
    2180        4562 :         return;
    2181             :     }
    2182             : 
    2183        4208 :     free( *hMetaData );
    2184        4208 :     *hMetaData = NULL;
    2185             : 
    2186        4208 :     return;
    2187             : }

Generated by: LCOV version 1.14