LCOV - code coverage report
Current view: top level - lib_enc - ivas_init_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 423 484 87.4 %
Date: 2025-09-13 07:56:30 Functions: 10 10 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.h"
      38             : #include "ivas_prot.h"
      39             : #include "ivas_stat_enc.h"
      40             : #include "ivas_rom_com.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
      46             : #include <string.h>
      47             : #endif
      48             : 
      49             : 
      50             : /*-------------------------------------------------------------------*
      51             :  * ivas_write_format()
      52             :  *
      53             :  * Write IVAS format signaling
      54             :  *-------------------------------------------------------------------*/
      55             : 
      56     5717435 : void ivas_write_format(
      57             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure   */
      58             : )
      59             : {
      60             :     int16_t ind, nBits, extra_bits;
      61             : 
      62     5717435 :     ind = 0;
      63     5717435 :     nBits = IVAS_FORMAT_SIGNALING_NBITS;
      64     5717435 :     extra_bits = ( IVAS_FORMAT_SIGNALING_NBITS_EXTENDED - IVAS_FORMAT_SIGNALING_NBITS );
      65             : 
      66     5717435 :     switch ( st_ivas->hEncoderConfig->ivas_format )
      67             :     {
      68      777567 :         case STEREO_FORMAT:
      69      777567 :             ind = 0;
      70      777567 :             break;
      71      764301 :         case ISM_FORMAT:
      72      764301 :             ind = 2;
      73      764301 :             if ( st_ivas->hEncoderConfig->ivas_total_brate >= IVAS_24k4 )
      74             :             {
      75      706341 :                 ind = 4;
      76      706341 :                 nBits += extra_bits;
      77             :             }
      78      764301 :             break;
      79      728490 :         case MC_FORMAT:
      80      728490 :             ind = 1;
      81      728490 :             break;
      82     2126508 :         case SBA_FORMAT:
      83     2126508 :             ind = 6;
      84     2126508 :             nBits += extra_bits;
      85     2126508 :             break;
      86      594911 :         case MASA_FORMAT:
      87      594911 :             ind = 7;
      88      594911 :             nBits += extra_bits;
      89      594911 :             break;
      90      340240 :         case MASA_ISM_FORMAT:
      91      340240 :             if ( st_ivas->ism_mode == ISM_MODE_NONE )
      92             :             {
      93       76087 :                 ind = 7; /* send MASA format */
      94       76087 :                 nBits += extra_bits;
      95             :             }
      96             :             else
      97             :             {
      98      264153 :                 ind = 10;
      99      264153 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     100             :             }
     101      340240 :             break;
     102      385418 :         case SBA_ISM_FORMAT:
     103      385418 :             if ( st_ivas->hEncoderConfig->ivas_total_brate < IVAS_24k4 )
     104             :             {
     105       30697 :                 ind = 6; /* send SBA format */
     106       30697 :                 nBits += extra_bits;
     107             :             }
     108             :             else
     109             :             {
     110      354721 :                 ind = 11; /* 1011 */
     111      354721 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     112             :             }
     113      385418 :             break;
     114           0 :         default:
     115           0 :             assert( !"Invalid format. Aborting." );
     116             :             break;
     117             :     }
     118             : 
     119     5717435 :     if ( st_ivas->hSCE[0] != NULL )
     120             :     {
     121     2237858 :         push_indice( st_ivas->hSCE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     122             :     }
     123     3479577 :     else if ( st_ivas->hCPE[0] != NULL )
     124             :     {
     125     3479577 :         push_indice( st_ivas->hCPE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     126             :     }
     127             : 
     128     5717435 :     return;
     129             : }
     130             : 
     131             : 
     132             : /*-------------------------------------------------------------------*
     133             :  * ivas_write_format_sid()
     134             :  *
     135             :  * Write IVAS format signaling in SID frames
     136             :  *-------------------------------------------------------------------*/
     137             : 
     138       12185 : void ivas_write_format_sid(
     139             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format               */
     140             :     const int16_t element_mode,    /* i  : element bitrate           */
     141             :     BSTR_ENC_HANDLE hBstr,         /* i/o: encoder bitstream handle  */
     142             :     const int16_t sba_order,       /* i  : Ambisonic (SBA) order     */
     143             :     const int16_t sba_planar       /* i  : SBA planar flag           */
     144             : )
     145             : {
     146       12185 :     int16_t ind = 0; /* to avoid compilation warning */
     147             : 
     148       12185 :     switch ( ivas_format )
     149             :     {
     150        6997 :         case STEREO_FORMAT:
     151        6997 :             if ( element_mode == IVAS_CPE_MDCT )
     152             :             {
     153        1582 :                 ind = SID_MDCT_STEREO;
     154             :             }
     155        5415 :             else if ( element_mode == IVAS_CPE_DFT )
     156             :             {
     157        5415 :                 ind = SID_DFT_STEREO;
     158             :             }
     159             :             else
     160             :             {
     161           0 :                 assert( !"Wrong stereo mode for SID format signaling" );
     162             :             }
     163        6997 :             break;
     164        2819 :         case ISM_FORMAT:
     165        2819 :             ind = SID_ISM;
     166        2819 :             break;
     167        1432 :         case SBA_FORMAT:
     168        1432 :             switch ( element_mode )
     169             :             {
     170         933 :                 case IVAS_SCE:
     171         933 :                     ind = SID_SBA_1TC;
     172         933 :                     break;
     173         499 :                 case IVAS_CPE_MDCT:
     174         499 :                     ind = SID_SBA_2TC;
     175         499 :                     break;
     176           0 :                 default:
     177           0 :                     assert( !"Wrong element mode for SBA DTX!" );
     178             :                     break;
     179             :             }
     180        1432 :             break;
     181         937 :         case MASA_FORMAT:
     182         937 :             if ( element_mode == IVAS_SCE )
     183             :             {
     184         594 :                 ind = SID_MASA_1TC;
     185             :             }
     186             :             else
     187             :             {
     188         343 :                 ind = SID_MASA_2TC;
     189             :             }
     190         937 :             break;
     191           0 :         default:
     192           0 :             assert( !"Reserved SID format symbol written." );
     193             :             break;
     194             :     }
     195             : 
     196       12185 :     push_indice( hBstr, IND_IVAS_FORMAT, ind, SID_FORMAT_NBITS );
     197             : 
     198       12185 :     if ( ivas_format == SBA_FORMAT )
     199             :     {
     200             :         /* Write SBA planar flag */
     201        1432 :         push_indice( hBstr, IND_SMODE, sba_planar, SBA_PLANAR_BITS );
     202             : 
     203             :         /* Write SBA order */
     204        1432 :         push_indice( hBstr, IND_SMODE, sba_order, SBA_ORDER_BITS );
     205             :     }
     206             : 
     207       12185 :     return;
     208             : }
     209             : 
     210             : 
     211             : /*-------------------------------------------------------------------*
     212             :  * getNumChanAnalysis()
     213             :  *
     214             :  * get number of input channels used for analysis/coding
     215             :  *-------------------------------------------------------------------*/
     216             : 
     217             : /*! r: number of channels to be analysed */
     218     5676285 : int16_t getNumChanAnalysis(
     219             :     Encoder_Struct *st_ivas /* i  : IVAS encoder structure              */
     220             : )
     221             : {
     222             :     int16_t n;
     223             : 
     224     5676285 :     n = st_ivas->nSCE + CPE_CHANNELS * st_ivas->nCPE;
     225     5676285 :     if ( st_ivas->hEncoderConfig->ivas_format == SBA_FORMAT )
     226             :     {
     227     2128222 :         n = ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     228             :     }
     229     3548063 :     else if ( st_ivas->hEncoderConfig->ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_PARAMMC || st_ivas->mc_mode == MC_MODE_MCMASA ) )
     230             :     {
     231      309110 :         n = st_ivas->hEncoderConfig->nchan_inp;
     232             :     }
     233     3238953 :     else if ( st_ivas->hEncoderConfig->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     234             :     {
     235       37913 :         n = st_ivas->hEncoderConfig->nchan_inp;
     236             :     }
     237     3201040 :     else if ( st_ivas->hEncoderConfig->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM )
     238             :     {
     239      155925 :         n = st_ivas->hEncoderConfig->nchan_inp;
     240             :     }
     241     3045115 :     else if ( st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
     242             :     {
     243      291408 :         n = st_ivas->hEncoderConfig->nchan_inp;
     244             :     }
     245     2753707 :     else if ( st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
     246             :     {
     247      387592 :         n = st_ivas->hEncoderConfig->nchan_ism + ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     248             :     }
     249             : 
     250     5676285 :     return n;
     251             : }
     252             : 
     253             : 
     254             : /*-------------------------------------------------------------------*
     255             :  * copy_encoder_config()
     256             :  *
     257             :  * Copy configuration structrue to the state structrure
     258             :  *-------------------------------------------------------------------*/
     259             : 
     260      459881 : void copy_encoder_config(
     261             :     Encoder_Struct *st_ivas, /* i  : IVAS encoder structure              */
     262             :     Encoder_State *st,       /* o  : encoder state structure             */
     263             :     const int16_t flag_all   /* i  : flag 1==update all, 0=partial update*/
     264             : )
     265             : {
     266      459881 :     if ( flag_all )
     267             :     {
     268      130293 :         st->input_Fs = st_ivas->hEncoderConfig->input_Fs;
     269             : 
     270      130293 :         st->last_codec_mode = st_ivas->last_codec_mode;
     271      130293 :         st->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate;
     272             : 
     273      130293 :         st->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON;
     274             : 
     275      130293 :         st->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR;
     276             :     }
     277             : 
     278      459881 :     st->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB;
     279      459881 :     st->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR;
     280             : 
     281      459881 :     st->codec_mode = st_ivas->codec_mode;
     282      459881 :     st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     283             : 
     284      459881 :     st->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON;
     285      459881 :     st->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset;
     286      459881 :     st->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator;
     287             : 
     288             : #ifdef DEBUGGING
     289             :     st->force = st_ivas->hEncoderConfig->force;
     290             : #endif
     291      459881 :     st->element_mode = st_ivas->hEncoderConfig->element_mode_init;
     292             : 
     293      459881 :     return;
     294             : }
     295             : 
     296             : 
     297             : /*-------------------------------------------------------------------------
     298             :  * ivas_initialize_handles_enc()
     299             :  *
     300             :  * NULL initialization of handles
     301             :  *-------------------------------------------------------------------------*/
     302             : 
     303        4270 : void ivas_initialize_handles_enc(
     304             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure                  */
     305             : )
     306             : {
     307             :     int16_t i;
     308             : 
     309       21350 :     for ( i = 0; i < MAX_SCE; i++ )
     310             :     {
     311       17080 :         st_ivas->hSCE[i] = NULL;
     312             :     }
     313             : 
     314       29890 :     for ( i = 0; i < MAX_CPE; i++ )
     315             :     {
     316       25620 :         st_ivas->hCPE[i] = NULL;
     317             :     }
     318             : 
     319        4270 :     st_ivas->mem_hp20_in = NULL;
     320             : 
     321             :     /* ISM metadata handles */
     322       21350 :     for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
     323             :     {
     324       17080 :         st_ivas->hIsmMetaData[i] = NULL;
     325             :     }
     326             : 
     327             :     /* ISM DTX handle */
     328        4270 :     st_ivas->hISMDTX = NULL;
     329             : 
     330             :     /* Q Metadata handle */
     331        4270 :     st_ivas->hQMetaData = NULL;
     332             : 
     333             :     /* DirAC handle */
     334        4270 :     st_ivas->hDirAC = NULL;
     335             : 
     336             :     /* ParamISM handle */
     337        4270 :     st_ivas->hParamIsm = NULL;
     338             :     /* SPAR handle */
     339        4270 :     st_ivas->hSpar = NULL;
     340             : 
     341             :     /* MASA encoder handle */
     342        4270 :     st_ivas->hMasa = NULL;
     343             : 
     344             :     /* MCT handle */
     345        4270 :     st_ivas->hMCT = NULL;
     346             : 
     347             :     /* MC Param-Upmix handle */
     348        4270 :     st_ivas->hMCParamUpmix = NULL;
     349             : 
     350             :     /* Parametric MC handle */
     351        4270 :     st_ivas->hParamMC = NULL;
     352             : 
     353             :     /* Multi-channel MASA handle */
     354        4270 :     st_ivas->hMcMasa = NULL;
     355             : 
     356             :     /* Stereo downmix for EVS encoder handle */
     357        4270 :     st_ivas->hStereoDmxEVS = NULL;
     358             : 
     359             :     /* LFE  handle */
     360        4270 :     st_ivas->hLFE = NULL;
     361             : 
     362             :     /* LFE low pass filter handle */
     363        4270 :     st_ivas->hLfeLpf = NULL;
     364             : 
     365             :     /* Object MASA handle */
     366        4270 :     st_ivas->hOMasa = NULL;
     367             : 
     368             :     /* OSBA handle */
     369        4270 :     st_ivas->hOSba = NULL;
     370             : 
     371        4270 :     return;
     372             : }
     373             : 
     374             : 
     375             : /*-------------------------------------------------------------------*
     376             :  * ivas_init_encoder()
     377             :  *
     378             :  * Initialize IVAS encoder state structure
     379             :  *-------------------------------------------------------------------*/
     380             : 
     381        4270 : ivas_error ivas_init_encoder(
     382             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
     383             : )
     384             : {
     385             :     int16_t i, n;
     386             :     int16_t nchan_inp_buff;
     387             :     int16_t sce_id, cpe_id;
     388             :     IVAS_FORMAT ivas_format;
     389             :     int32_t input_Fs, ivas_total_brate;
     390             :     int32_t element_brate_tmp[MAX_NUM_OBJECTS];
     391             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     392             :     ivas_error error;
     393             : 
     394        4270 :     error = IVAS_ERR_OK;
     395             : 
     396        4270 :     hEncoderConfig = st_ivas->hEncoderConfig;
     397        4270 :     ivas_format = hEncoderConfig->ivas_format;
     398        4270 :     input_Fs = hEncoderConfig->input_Fs;
     399        4270 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     400             : 
     401        4270 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate;
     402             : 
     403        4270 :     if ( ivas_format != MONO_FORMAT )
     404             :     {
     405             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
     406        3969 :         hEncoderConfig->max_bwidth = max( hEncoderConfig->max_bwidth, WB );
     407             :     }
     408        4270 :     st_ivas->ism_mode = ISM_MODE_NONE;
     409        4270 :     st_ivas->mc_mode = MC_MODE_NONE;
     410             : 
     411        4270 :     st_ivas->nchan_transport = -1;
     412             : 
     413             : #ifdef DEBUGGING
     414             :     st_ivas->noClipping = 0;
     415             :     st_ivas->maxOverload = 0;
     416             :     st_ivas->minOverload = 0;
     417             : #endif
     418             :     /*-----------------------------------------------------------------*
     419             :      * Allocate floating-point input audio buffers
     420             :      *-----------------------------------------------------------------*/
     421             : 
     422        4270 :     nchan_inp_buff = hEncoderConfig->nchan_inp;
     423        4270 :     if ( ivas_format == MONO_FORMAT )
     424             :     {
     425         301 :         nchan_inp_buff = 0;
     426             :     }
     427        3969 :     else if ( ivas_format == MASA_ISM_FORMAT )
     428             :     {
     429         429 :         if ( hEncoderConfig->nchan_inp - hEncoderConfig->nchan_ism == 1 ) /* mono is duplicated in monoMASA */
     430             :         {
     431         192 :             nchan_inp_buff++;
     432             :         }
     433             : 
     434         429 :         nchan_inp_buff++; /* for *data_separated_object */
     435             :     }
     436             : 
     437       33146 :     for ( n = 0; n < nchan_inp_buff; n++ )
     438             :     {
     439             :         /* note: these are intra-frame heap memories */
     440       28876 :         if ( ( st_ivas->p_data_f[n] = (float *) malloc( ( input_Fs / FRAMES_PER_SEC ) * sizeof( float ) ) ) == NULL )
     441             :         {
     442           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for floating-point input audio buffer!\n" ) );
     443             :         }
     444             :     }
     445       60794 :     for ( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     446             :     {
     447       56524 :         st_ivas->p_data_f[n] = NULL;
     448             :     }
     449             : 
     450             :     /*-----------------------------------------------------------------*
     451             :      * Allocate and initialize buffer of indices
     452             :      *-----------------------------------------------------------------*/
     453             : 
     454             :     /* set the maximum allowed number of indices in the list */
     455        4270 :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices( ivas_format, ivas_total_brate );
     456             : 
     457             :     /* allocate buffer of indices */
     458        4270 :     if ( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
     459             :     {
     460           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
     461             :     }
     462             : 
     463             :     /* reset the list of indices */
     464     3399910 :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     465             :     {
     466     3395640 :         st_ivas->ind_list[i].nb_bits = -1;
     467             :     }
     468             : 
     469             : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
     470             :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     471             :     {
     472             :         memset( st_ivas->ind_list[i].function_name, 'A', 100 * sizeof( char ) );
     473             :     }
     474             : #endif
     475             : 
     476             :     /* set the maximum allowed number of metadata indices in the list */
     477        4270 :     st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata( st_ivas->hEncoderConfig->ivas_format, st_ivas->hEncoderConfig->ivas_total_brate );
     478             : 
     479             :     /* allocate buffer of metadata indices */
     480        4270 :     if ( st_ivas->ivas_max_num_indices_metadata > 0 )
     481             :     {
     482        3969 :         if ( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
     483             :         {
     484           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
     485             :         }
     486             : 
     487             :         /* reset the list of metadata indices */
     488     1733379 :         for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
     489             :         {
     490     1729410 :             st_ivas->ind_list_metadata[i].nb_bits = -1;
     491             :         }
     492             :     }
     493             :     else
     494             :     {
     495         301 :         st_ivas->ind_list_metadata = NULL;
     496             :     }
     497             : 
     498             :     /*-----------------------------------------------------------------*
     499             :      * Allocate and initialize SCE/CPE and other handles
     500             :      *-----------------------------------------------------------------*/
     501             : 
     502        4270 :     if ( ivas_format == MONO_FORMAT )
     503             :     {
     504         301 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
     505         301 :         st_ivas->nCPE = 0;
     506         301 :         st_ivas->nchan_transport = 1;
     507         301 :         sce_id = 0;
     508             : 
     509         301 :         if ( ( error = create_sce_enc( st_ivas, sce_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     510             :         {
     511           0 :             return error;
     512             :         }
     513             : 
     514             :         /* prepare stereo downmix for EVS */
     515         301 :         if ( hEncoderConfig->stereo_dmx_evs == 1 )
     516             :         {
     517         118 :             if ( ( error = stereo_dmx_evs_init_encoder( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
     518             :             {
     519           0 :                 return error;
     520             :             }
     521             :         }
     522             :     }
     523        3969 :     else if ( ivas_format == STEREO_FORMAT )
     524             :     {
     525         226 :         st_ivas->nSCE = 0;
     526         226 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
     527         226 :         st_ivas->nchan_transport = CPE_CHANNELS;
     528         226 :         cpe_id = 0;
     529             : 
     530         226 :         if ( ( error = create_cpe_enc( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     531             :         {
     532           0 :             return error;
     533             :         }
     534             :     }
     535        3743 :     else if ( ivas_format == ISM_FORMAT )
     536             :     {
     537         676 :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate );
     538             : 
     539         676 :         if ( ( error = ivas_ism_metadata_enc_create( st_ivas, hEncoderConfig->nchan_inp, element_brate_tmp ) ) != IVAS_ERR_OK )
     540             :         {
     541           0 :             return error;
     542             :         }
     543             : 
     544        2362 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     545             :         {
     546        1686 :             if ( ( error = create_sce_enc( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
     547             :             {
     548           0 :                 return error;
     549             :             }
     550             :         }
     551             : 
     552         676 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     553             :         {
     554          90 :             if ( ( error = ivas_param_ism_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     555             :             {
     556           0 :                 return error;
     557             :             }
     558             :         }
     559             : 
     560         676 :         if ( st_ivas->hEncoderConfig->Opt_DTX_ON )
     561             :         {
     562         280 :             if ( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
     563             :             {
     564           0 :                 return error;
     565             :             }
     566             :         }
     567             :     }
     568        3067 :     else if ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT )
     569             :     {
     570        1167 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     571             :         {
     572           0 :             return error;
     573             :         }
     574             : 
     575        1167 :         if ( ivas_format == SBA_FORMAT )
     576             :         {
     577         857 :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     578             : 
     579         857 :             if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     580             :             {
     581           0 :                 return error;
     582             :             }
     583             : 
     584         857 :             if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     585             :             {
     586           0 :                 return error;
     587             :             }
     588             :         }
     589             :         else
     590             :         {
     591         310 :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp;
     592             : 
     593         310 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     594             :             {
     595           0 :                 return error;
     596             :             }
     597             :         }
     598             : 
     599        1638 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     600             :         {
     601         471 :             if ( ( error = create_sce_enc( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
     602             :             {
     603           0 :                 return error;
     604             :             }
     605             : 
     606         471 :             if ( ivas_format == SBA_FORMAT && st_ivas->hEncoderConfig->Opt_DTX_ON )
     607             :             {
     608         145 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
     609             :             }
     610             :         }
     611             : 
     612        2158 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     613             :         {
     614         991 :             if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
     615             :             {
     616           0 :                 return error;
     617             :             }
     618             : 
     619        2973 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     620             :             {
     621        1982 :                 if ( hEncoderConfig->Opt_DTX_ON )
     622             :                 {
     623         300 :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
     624             :                 }
     625             :             }
     626             :         }
     627             : 
     628        1167 :         if ( st_ivas->nCPE > 1 )
     629             :         {
     630         295 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     631             :             {
     632           0 :                 return error;
     633             :             }
     634             :         }
     635             :     }
     636        1900 :     else if ( ivas_format == MASA_ISM_FORMAT )
     637             :     {
     638             :         int32_t ism_total_brate;
     639             :         int16_t k;
     640             : 
     641         429 :         st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, hEncoderConfig->nchan_ism );
     642         429 :         st_ivas->nchan_transport = 2;
     643             : 
     644         429 :         if ( ( error = ivas_ism_metadata_enc_create( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     645             :         {
     646           0 :             return error;
     647             :         }
     648             : 
     649         429 :         k = 0;
     650        3762 :         while ( k < SIZE_IVAS_BRATE_TBL && ivas_total_brate != ivas_brate_tbl[k] )
     651             :         {
     652        3333 :             k++;
     653             :         }
     654             : 
     655         429 :         ism_total_brate = 0;
     656        1029 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     657             :         {
     658         600 :             ism_total_brate += sep_object_brate[k - 2][st_ivas->nSCE - 1];
     659         600 :             if ( ( error = create_sce_enc( st_ivas, sce_id, sep_object_brate[k - 2][st_ivas->nSCE - 1] ) ) != IVAS_ERR_OK )
     660             :             {
     661           0 :                 return error;
     662             :             }
     663             :         }
     664             : 
     665         429 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     666             :         {
     667           0 :             return error;
     668             :         }
     669         429 :         if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     670             :         {
     671           0 :             return error;
     672             :         }
     673             : 
     674         429 :         if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC )
     675             :         {
     676         208 :             if ( ( error = ivas_omasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     677             :             {
     678           0 :                 return error;
     679             :             }
     680             :         }
     681             : 
     682         429 :         if ( ivas_total_brate - ism_total_brate >= MIN_BRATE_MDCT_STEREO )
     683             :         {
     684         211 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     685             :         }
     686             :         else
     687             :         {
     688         218 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     689             :         }
     690             : 
     691         429 :         if ( ( error = create_cpe_enc( st_ivas, 0, ivas_total_brate - ism_total_brate ) ) != IVAS_ERR_OK )
     692             :         {
     693           0 :             return error;
     694             :         }
     695             :     }
     696        1471 :     else if ( ivas_format == SBA_ISM_FORMAT )
     697             :     {
     698        1087 :         st_ivas->ism_mode = ISM_MODE_NONE;
     699             : 
     700        1087 :         st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->hEncoderConfig->nchan_ism );
     701             : 
     702        1087 :         if ( ( error = ivas_ism_metadata_enc_create( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     703             :         {
     704           0 :             return error;
     705             :         }
     706             : 
     707             :         /* allocate and initialize SBA handles */
     708        1087 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     709             :         {
     710           0 :             return error;
     711             :         }
     712             : 
     713        1087 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     714             : 
     715        1087 :         if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     716             :         {
     717           0 :             return error;
     718             :         }
     719             : 
     720        1087 :         if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     721             :         {
     722           0 :             return error;
     723             :         }
     724             : 
     725        1087 :         if ( st_ivas->ism_mode == ISM_MODE_NONE )
     726             :         {
     727             :             /* allocate and initialize SBA core-coders */
     728         669 :             if ( st_ivas->nchan_transport == 1 )
     729             :             {
     730         385 :                 if ( ( error = create_sce_enc( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
     731             :                 {
     732           0 :                     return error;
     733             :                 }
     734             :             }
     735             : 
     736        1010 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     737             :             {
     738         341 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
     739             :                 {
     740           0 :                     return error;
     741             :                 }
     742             :             }
     743             : 
     744         669 :             if ( st_ivas->nCPE > 1 )
     745             :             {
     746          57 :                 if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     747             :                 {
     748           0 :                     return error;
     749             :                 }
     750             :             }
     751             :         }
     752             :         else
     753             :         {
     754             :             /* allocate and initialize MCT core coder */
     755             : 
     756             :             {
     757             :                 int16_t n_all;
     758             : 
     759         418 :                 n_all = st_ivas->nchan_transport + st_ivas->hEncoderConfig->nchan_ism;
     760         418 :                 st_ivas->nCPE = ( n_all + 1 ) >> 1;
     761             :             }
     762        1783 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     763             :             {
     764        1365 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
     765             :                 {
     766           0 :                     return error;
     767             :                 }
     768             :             }
     769             : 
     770         418 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     771             :             {
     772           0 :                 return error;
     773             :             }
     774             :         }
     775             : 
     776        1087 :         if ( ( error = ivas_osba_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     777             :         {
     778           0 :             return error;
     779             :         }
     780             :     }
     781         384 :     else if ( ivas_format == MC_FORMAT )
     782             :     {
     783         384 :         st_ivas->mc_mode = ivas_mc_mode_select( hEncoderConfig->mc_input_setup, ivas_total_brate );
     784             : 
     785         384 :         if ( ( error = ivas_create_lfe_lpf_enc( &st_ivas->hLfeLpf, hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
     786             :         {
     787           0 :             return error;
     788             :         }
     789             : 
     790         384 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
     791             :         {
     792         160 :             st_ivas->nSCE = 0;
     793         160 :             st_ivas->nCPE = hEncoderConfig->nchan_inp / CPE_CHANNELS;
     794             : 
     795         821 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     796             :             {
     797         661 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / ( hEncoderConfig->nchan_inp - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
     798             :                 {
     799           0 :                     return error;
     800             :                 }
     801             :             }
     802             : 
     803         160 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     804             :             {
     805           0 :                 return error;
     806             :             }
     807             : 
     808         160 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     809             :             {
     810           0 :                 return error;
     811             :             }
     812             : 
     813         160 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( st_ivas->hEncoderConfig->mc_input_setup );
     814             :         }
     815         224 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     816             :         {
     817          13 :             st_ivas->nSCE = 0;
     818          13 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
     819          13 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     820             : 
     821          13 :             if ( ( error = ivas_mc_paramupmix_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     822             :             {
     823           0 :                 return error;
     824             :             }
     825             : 
     826          65 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     827             :             {
     828          52 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
     829             :                 {
     830           0 :                     return error;
     831             :                 }
     832             :             }
     833             : 
     834          13 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     835             :             {
     836           0 :                 return error;
     837             :             }
     838             : 
     839          13 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     840             :             {
     841           0 :                 return error;
     842             :             }
     843             :         }
     844         211 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     845             :         {
     846          65 :             if ( ( error = ivas_param_mc_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     847             :             {
     848           0 :                 return error;
     849             :             }
     850             : 
     851         146 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     852             :             {
     853          81 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nCPE + st_ivas->nSCE ) ) ) != IVAS_ERR_OK )
     854             :                 {
     855           0 :                     return error;
     856             :                 }
     857             :             }
     858             : 
     859          65 :             if ( st_ivas->nCPE > 1 )
     860             :             {
     861          16 :                 if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     862             :                 {
     863           0 :                     return error;
     864             :                 }
     865             :             }
     866             :         }
     867         146 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     868             :         {
     869             :             int32_t brate_sce, brate_cpe;
     870             : 
     871         146 :             ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
     872             : 
     873         146 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     874             :             {
     875           0 :                 return error;
     876             :             }
     877             : 
     878         146 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     879             :             {
     880           0 :                 return error;
     881             :             }
     882             : 
     883         146 :             if ( ( error = ivas_mcmasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     884             :             {
     885           0 :                 return error;
     886             :             }
     887             : 
     888         146 :             ivas_mcmasa_split_brate( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
     889             : 
     890         282 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     891             :             {
     892         136 :                 if ( ( error = create_sce_enc( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
     893             :                 {
     894           0 :                     return error;
     895             :                 }
     896             :             }
     897             : 
     898         178 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     899             :             {
     900          32 :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     901             : 
     902          32 :                 if ( ( error = create_cpe_enc( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
     903             :                 {
     904           0 :                     return error;
     905             :                 }
     906             :             }
     907             :         }
     908             :     }
     909             : #ifdef DEBUGGING
     910             :     else
     911             :     {
     912             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Invalid IVAS format. Exiting.\n" );
     913             :     }
     914             : #endif
     915             : 
     916             :     /*-----------------------------------------------------------------*
     917             :      * Allocate and initialize HP20 filter memories
     918             :      *-----------------------------------------------------------------*/
     919             : 
     920             :     /* set number of input channels used for analysis/coding */
     921        4270 :     n = getNumChanAnalysis( st_ivas );
     922             : 
     923        4270 :     if ( n > 0 )
     924             :     {
     925        4270 :         if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
     926             :         {
     927           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
     928             :         }
     929             :     }
     930             :     else
     931             :     {
     932           0 :         st_ivas->mem_hp20_in = NULL;
     933             :     }
     934             : 
     935       24834 :     for ( i = 0; i < n; i++ )
     936             :     {
     937       20564 :         if ( ( st_ivas->mem_hp20_in[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
     938             :         {
     939           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
     940             :         }
     941             : 
     942       20564 :         set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
     943             :     }
     944             : 
     945        4270 :     return error;
     946             : }
     947             : 
     948             : 
     949             : /*-------------------------------------------------------------------------
     950             :  * destroy_core_enc()
     951             :  *
     952             :  * Close core encoder handles
     953             :  *-------------------------------------------------------------------------*/
     954             : 
     955      130293 : void destroy_core_enc(
     956             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
     957             : )
     958             : {
     959             :     int16_t i;
     960             : 
     961      130293 :     destroy_cldfb_encoder( hCoreCoder );
     962             : 
     963      130293 :     if ( hCoreCoder->hSignalBuf != NULL )
     964             :     {
     965      130044 :         free( hCoreCoder->hSignalBuf );
     966      130044 :         hCoreCoder->hSignalBuf = NULL;
     967             :     }
     968             : 
     969      130293 :     if ( hCoreCoder->hBstr != NULL )
     970             :     {
     971             :         /* reset buffer of indices */
     972      130044 :         for ( i = 0; i < hCoreCoder->hBstr->nb_ind_tot; i++ )
     973             :         {
     974           0 :             hCoreCoder->hBstr->ind_list[i].nb_bits = -1;
     975             :         }
     976      130044 :         free( hCoreCoder->hBstr );
     977      130044 :         hCoreCoder->hBstr = NULL;
     978             :     }
     979             : 
     980      130293 :     if ( hCoreCoder->hLPDmem != NULL )
     981             :     {
     982       57625 :         free( hCoreCoder->hLPDmem );
     983       57625 :         hCoreCoder->hLPDmem = NULL;
     984             :     }
     985             : 
     986      130293 :     if ( hCoreCoder->hTranDet != NULL )
     987             :     {
     988      130044 :         free( hCoreCoder->hTranDet );
     989      130044 :         hCoreCoder->hTranDet = NULL;
     990             :     }
     991             : 
     992      130293 :     if ( hCoreCoder->hNoiseEst != NULL )
     993             :     {
     994      129908 :         free( hCoreCoder->hNoiseEst );
     995      129908 :         hCoreCoder->hNoiseEst = NULL;
     996             :     }
     997             : 
     998      130293 :     if ( hCoreCoder->hVAD != NULL )
     999             :     {
    1000      129659 :         free( hCoreCoder->hVAD );
    1001      129659 :         hCoreCoder->hVAD = NULL;
    1002             :     }
    1003             : 
    1004      130293 :     if ( hCoreCoder->hVAD_CLDFB != NULL )
    1005             :     {
    1006         301 :         free( hCoreCoder->hVAD_CLDFB );
    1007         301 :         hCoreCoder->hVAD_CLDFB = NULL;
    1008             :     }
    1009             : 
    1010      130293 :     if ( hCoreCoder->hTdCngEnc != NULL )
    1011             :     {
    1012        1100 :         free( hCoreCoder->hTdCngEnc );
    1013        1100 :         hCoreCoder->hTdCngEnc = NULL;
    1014             :     }
    1015             : 
    1016      130293 :     if ( hCoreCoder->hDtxEnc != NULL )
    1017             :     {
    1018        5829 :         free( hCoreCoder->hDtxEnc );
    1019        5829 :         hCoreCoder->hDtxEnc = NULL;
    1020             :     }
    1021             : 
    1022      130293 :     if ( hCoreCoder->hSpMusClas != NULL )
    1023             :     {
    1024      129908 :         free( hCoreCoder->hSpMusClas );
    1025      129908 :         hCoreCoder->hSpMusClas = NULL;
    1026             :     }
    1027             : 
    1028      130293 :     if ( hCoreCoder->hGSCEnc != NULL )
    1029             :     {
    1030       57625 :         free( hCoreCoder->hGSCEnc );
    1031       57625 :         hCoreCoder->hGSCEnc = NULL;
    1032             :     }
    1033             : 
    1034      130293 :     if ( hCoreCoder->hSC_VBR != NULL )
    1035             :     {
    1036         301 :         free( hCoreCoder->hSC_VBR );
    1037         301 :         hCoreCoder->hSC_VBR = NULL;
    1038             :     }
    1039             : 
    1040      130293 :     if ( hCoreCoder->hAmrwb_IO != NULL )
    1041             :     {
    1042         301 :         free( hCoreCoder->hAmrwb_IO );
    1043         301 :         hCoreCoder->hAmrwb_IO = NULL;
    1044             :     }
    1045             : 
    1046      130293 :     if ( hCoreCoder->hBWE_TD != NULL )
    1047             :     {
    1048       57623 :         free( hCoreCoder->hBWE_TD );
    1049       57623 :         hCoreCoder->hBWE_TD = NULL;
    1050             :     }
    1051             : 
    1052      130293 :     if ( hCoreCoder->hBWE_FD != NULL )
    1053             :     {
    1054       57623 :         free( hCoreCoder->hBWE_FD );
    1055       57623 :         hCoreCoder->hBWE_FD = NULL;
    1056             :     }
    1057             : 
    1058      130293 :     if ( hCoreCoder->hRF != NULL )
    1059             :     {
    1060         301 :         free( hCoreCoder->hRF );
    1061         301 :         hCoreCoder->hRF = NULL;
    1062             :     }
    1063             : 
    1064      130293 :     if ( hCoreCoder->hTECEnc != NULL )
    1065             :     {
    1066         301 :         free( hCoreCoder->hTECEnc );
    1067         301 :         hCoreCoder->hTECEnc = NULL;
    1068             :     }
    1069             : 
    1070      130293 :     if ( hCoreCoder->hTcxEnc != NULL )
    1071             :     {
    1072      129655 :         free( hCoreCoder->hTcxEnc );
    1073      129655 :         hCoreCoder->hTcxEnc = NULL;
    1074             :     }
    1075             : 
    1076      130293 :     if ( hCoreCoder->hTcxCfg != NULL )
    1077             :     {
    1078      129655 :         free( hCoreCoder->hTcxCfg );
    1079      129655 :         hCoreCoder->hTcxCfg = NULL;
    1080             :     }
    1081             : 
    1082      130293 :     if ( hCoreCoder->hIGFEnc != NULL )
    1083             :     {
    1084      110665 :         free( hCoreCoder->hIGFEnc );
    1085      110665 :         hCoreCoder->hIGFEnc = NULL;
    1086             :     }
    1087             : 
    1088      130293 :     if ( hCoreCoder->hPlcExt != NULL )
    1089             :     {
    1090         301 :         free( hCoreCoder->hPlcExt );
    1091         301 :         hCoreCoder->hPlcExt = NULL;
    1092             :     }
    1093             : 
    1094      130293 :     if ( hCoreCoder->hHQ_core != NULL )
    1095             :     {
    1096       57653 :         free( hCoreCoder->hHQ_core );
    1097       57653 :         hCoreCoder->hHQ_core = NULL;
    1098             :     }
    1099             : 
    1100      130293 :     free( hCoreCoder );
    1101             : 
    1102      130293 :     return;
    1103             : }
    1104             : 
    1105             : 
    1106             : /*-------------------------------------------------------------------------
    1107             :  * ivas_destroy_enc()
    1108             :  *
    1109             :  * Close IVAS encoder handles
    1110             :  *-------------------------------------------------------------------------*/
    1111             : 
    1112        4270 : void ivas_destroy_enc(
    1113             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    1114             : )
    1115             : {
    1116             :     int16_t i, n, nchan_inp;
    1117        4270 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
    1118             : 
    1119             :     /* SCE handles */
    1120       21350 :     for ( i = 0; i < MAX_SCE; i++ )
    1121             :     {
    1122       17080 :         if ( st_ivas->hSCE[i] != NULL )
    1123             :         {
    1124        3537 :             destroy_sce_enc( st_ivas->hSCE[i] );
    1125        3537 :             st_ivas->hSCE[i] = NULL;
    1126             :         }
    1127             :     }
    1128             : 
    1129             :     /* CPE handles */
    1130       29890 :     for ( i = 0; i < MAX_CPE; i++ )
    1131             :     {
    1132       25620 :         if ( st_ivas->hCPE[i] != NULL )
    1133             :         {
    1134        4366 :             destroy_cpe_enc( st_ivas->hCPE[i] );
    1135        4366 :             st_ivas->hCPE[i] = NULL;
    1136             :         }
    1137             :     }
    1138             : 
    1139             :     /* HP20 filter handles */
    1140        4270 :     if ( st_ivas->mem_hp20_in != NULL )
    1141             :     {
    1142        4270 :         n = getNumChanAnalysis( st_ivas );
    1143             : 
    1144       24957 :         for ( i = 0; i < n; i++ )
    1145             :         {
    1146       20687 :             free( st_ivas->mem_hp20_in[i] );
    1147       20687 :             st_ivas->mem_hp20_in[i] = NULL;
    1148             :         }
    1149        4270 :         free( st_ivas->mem_hp20_in );
    1150        4270 :         st_ivas->mem_hp20_in = NULL;
    1151             :     }
    1152             : 
    1153             :     /* ISM metadata handles */
    1154        4270 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    1155             : 
    1156             :     /* ISM DTX Handle */
    1157        4270 :     if ( st_ivas->hISMDTX != NULL )
    1158             :     {
    1159         280 :         free( st_ivas->hISMDTX );
    1160         280 :         st_ivas->hISMDTX = NULL;
    1161             :     }
    1162             : 
    1163             :     /* Q Metadata handle */
    1164        4270 :     ivas_qmetadata_close( &( st_ivas->hQMetaData ) );
    1165             : 
    1166             :     /* DirAC handle */
    1167        4270 :     ivas_dirac_enc_close( &( st_ivas->hDirAC ), st_ivas->hEncoderConfig->input_Fs );
    1168             : 
    1169             :     /* ParamISM handle */
    1170        4270 :     ivas_param_ism_enc_close( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
    1171             : 
    1172             :     /* SPAR handle */
    1173        4270 :     ivas_spar_enc_close( &( st_ivas->hSpar ), st_ivas->hEncoderConfig->input_Fs, nchan_inp, 0 );
    1174             : 
    1175             :     /* MASA handle */
    1176        4270 :     ivas_masa_enc_close( &( st_ivas->hMasa ) );
    1177             : 
    1178             :     /* MCT handle */
    1179        4270 :     ivas_mct_enc_close( &( st_ivas->hMCT ) );
    1180             : 
    1181             :     /* LFE handle */
    1182        4270 :     ivas_lfe_enc_close( &( st_ivas->hLFE ) );
    1183             : 
    1184             :     /* LFE low pass filter state */
    1185        4270 :     ivas_lfe_lpf_enc_close( &( st_ivas->hLfeLpf ) );
    1186             : 
    1187             :     /* Param-Upmix MC handle */
    1188        4270 :     ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
    1189             : 
    1190             :     /* Parametric MC handle */
    1191        4270 :     ivas_param_mc_enc_close( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
    1192             : 
    1193             :     /* Multi-channel MASA handle */
    1194        4270 :     ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
    1195             : 
    1196             :     /* OMASA handle */
    1197        4270 :     ivas_omasa_enc_close( &( st_ivas->hOMasa ) );
    1198             : 
    1199             :     /* OSBA handle */
    1200        4270 :     ivas_osba_enc_close( &( st_ivas->hOSba ) );
    1201             : 
    1202             :     /* Stereo downmix for EVS encoder handle */
    1203        4270 :     stereo_dmx_evs_close_encoder( &( st_ivas->hStereoDmxEVS ) );
    1204             : 
    1205             :     /* Encoder configuration handle */
    1206        4270 :     if ( st_ivas->hEncoderConfig != NULL )
    1207             :     {
    1208        4270 :         free( st_ivas->hEncoderConfig );
    1209        4270 :         st_ivas->hEncoderConfig = NULL;
    1210             :     }
    1211             : 
    1212             :     /* Buffer of indices */
    1213        4270 :     if ( st_ivas->ind_list != NULL )
    1214             :     {
    1215        4270 :         free( st_ivas->ind_list );
    1216             :     }
    1217             : 
    1218        4270 :     if ( st_ivas->ind_list_metadata != NULL )
    1219             :     {
    1220        3969 :         free( st_ivas->ind_list_metadata );
    1221             :     }
    1222             : 
    1223             :     /* floating-point input audio buffers */
    1224       89670 :     for ( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1225             :     {
    1226       85400 :         if ( st_ivas->p_data_f[n] != NULL )
    1227             :         {
    1228       28876 :             free( st_ivas->p_data_f[n] );
    1229       28876 :             st_ivas->p_data_f[n] = NULL;
    1230             :         }
    1231             :     }
    1232             : 
    1233             :     /* main IVAS handle */
    1234        4270 :     free( st_ivas );
    1235             : 
    1236        4270 :     return;
    1237             : }
    1238             : 
    1239             : /*-------------------------------------------------------------------------
    1240             :  * ivas_initialize_MD_bstr_enc()
    1241             :  *
    1242             :  * Allocate and initialize SCE/CPE MD bitstream handle
    1243             :  *-------------------------------------------------------------------------*/
    1244             : 
    1245       66934 : ivas_error ivas_initialize_MD_bstr_enc(
    1246             :     BSTR_ENC_HANDLE *hMetaData_out, /* o  : encoder MD bitstream handle */
    1247             :     Encoder_Struct *st_ivas         /* i/o: IVAS encoder structure      */
    1248             : )
    1249             : {
    1250             :     BSTR_ENC_HANDLE hMetaData;
    1251             : 
    1252       66934 :     if ( ( hMetaData = (BSTR_ENC_HANDLE) malloc( sizeof( BSTR_ENC_DATA ) ) ) == NULL )
    1253             :     {
    1254           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MetaData structure\n" ) );
    1255             :     }
    1256             : 
    1257             :     /* set pointer to the buffer of metadata indices */
    1258       66934 :     hMetaData->ind_list = st_ivas->ind_list_metadata;
    1259       66934 :     hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1260       66934 :     hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata;
    1261       66934 :     hMetaData->st_ivas = st_ivas;
    1262             : 
    1263       66934 :     reset_indices_enc( hMetaData, st_ivas->ivas_max_num_indices_metadata );
    1264             : 
    1265       66934 :     *hMetaData_out = hMetaData;
    1266             : 
    1267       66934 :     return IVAS_ERR_OK;
    1268             : }
    1269             : 
    1270             : 
    1271             : /*-------------------------------------------------------------------------
    1272             :  * ivas_destroy_MD_bstr_enc()
    1273             :  *
    1274             :  * Destroy SCE/CPE MD bitstream handle
    1275             :  *-------------------------------------------------------------------------*/
    1276             : 
    1277      147900 : void ivas_destroy_MD_bstr_enc(
    1278             :     BSTR_ENC_HANDLE *hMetaData /* i/o: encoder MD bitstream handle  */
    1279             : )
    1280             : {
    1281      147900 :     if ( hMetaData == NULL || *hMetaData == NULL )
    1282             :     {
    1283       80966 :         return;
    1284             :     }
    1285             : 
    1286       66934 :     free( *hMetaData );
    1287       66934 :     *hMetaData = NULL;
    1288             : 
    1289       66934 :     return;
    1290             : }

Generated by: LCOV version 1.14