LCOV - code coverage report
Current view: top level - lib_enc - ivas_init_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 423 484 87.4 %
Date: 2025-11-01 05:53:46 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      901563 : 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      901563 :     ind = 0;
      63      901563 :     nBits = IVAS_FORMAT_SIGNALING_NBITS;
      64      901563 :     extra_bits = ( IVAS_FORMAT_SIGNALING_NBITS_EXTENDED - IVAS_FORMAT_SIGNALING_NBITS );
      65             : 
      66      901563 :     switch ( st_ivas->hEncoderConfig->ivas_format )
      67             :     {
      68       87056 :         case STEREO_FORMAT:
      69       87056 :             ind = 0;
      70       87056 :             break;
      71      167126 :         case ISM_FORMAT:
      72      167126 :             ind = 2;
      73      167126 :             if ( st_ivas->hEncoderConfig->ivas_total_brate >= IVAS_24k4 )
      74             :             {
      75      157086 :                 ind = 4;
      76      157086 :                 nBits += extra_bits;
      77             :             }
      78      167126 :             break;
      79      139780 :         case MC_FORMAT:
      80      139780 :             ind = 1;
      81      139780 :             break;
      82      214915 :         case SBA_FORMAT:
      83      214915 :             ind = 6;
      84      214915 :             nBits += extra_bits;
      85      214915 :             break;
      86       69956 :         case MASA_FORMAT:
      87       69956 :             ind = 7;
      88       69956 :             nBits += extra_bits;
      89       69956 :             break;
      90       86170 :         case MASA_ISM_FORMAT:
      91       86170 :             if ( st_ivas->ism_mode == ISM_MODE_NONE )
      92             :             {
      93       20679 :                 ind = 7; /* send MASA format */
      94       20679 :                 nBits += extra_bits;
      95             :             }
      96             :             else
      97             :             {
      98       65491 :                 ind = 10;
      99       65491 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     100             :             }
     101       86170 :             break;
     102      136560 :         case SBA_ISM_FORMAT:
     103      136560 :             if ( st_ivas->hEncoderConfig->ivas_total_brate < IVAS_24k4 )
     104             :             {
     105       11751 :                 ind = 6; /* send SBA format */
     106       11751 :                 nBits += extra_bits;
     107             :             }
     108             :             else
     109             :             {
     110      124809 :                 ind = 11; /* 1011 */
     111      124809 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     112             :             }
     113      136560 :             break;
     114           0 :         default:
     115           0 :             assert( !"Invalid format. Aborting." );
     116             :             break;
     117             :     }
     118             : 
     119      901563 :     if ( st_ivas->hSCE[0] != NULL )
     120             :     {
     121      354940 :         push_indice( st_ivas->hSCE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     122             :     }
     123      546623 :     else if ( st_ivas->hCPE[0] != NULL )
     124             :     {
     125      546623 :         push_indice( st_ivas->hCPE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     126             :     }
     127             : 
     128      901563 :     return;
     129             : }
     130             : 
     131             : 
     132             : /*-------------------------------------------------------------------*
     133             :  * ivas_write_format_sid()
     134             :  *
     135             :  * Write IVAS format signaling in SID frames
     136             :  *-------------------------------------------------------------------*/
     137             : 
     138        3493 : 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        3493 :     int16_t ind = 0; /* to avoid compilation warning */
     147             : 
     148        3493 :     switch ( ivas_format )
     149             :     {
     150        2033 :         case STEREO_FORMAT:
     151        2033 :             if ( element_mode == IVAS_CPE_MDCT )
     152             :             {
     153         471 :                 ind = SID_MDCT_STEREO;
     154             :             }
     155        1562 :             else if ( element_mode == IVAS_CPE_DFT )
     156             :             {
     157        1562 :                 ind = SID_DFT_STEREO;
     158             :             }
     159             :             else
     160             :             {
     161           0 :                 assert( !"Wrong stereo mode for SID format signaling" );
     162             :             }
     163        2033 :             break;
     164         734 :         case ISM_FORMAT:
     165         734 :             ind = SID_ISM;
     166         734 :             break;
     167         521 :         case SBA_FORMAT:
     168         521 :             switch ( element_mode )
     169             :             {
     170         290 :                 case IVAS_SCE:
     171         290 :                     ind = SID_SBA_1TC;
     172         290 :                     break;
     173         231 :                 case IVAS_CPE_MDCT:
     174         231 :                     ind = SID_SBA_2TC;
     175         231 :                     break;
     176           0 :                 default:
     177           0 :                     assert( !"Wrong element mode for SBA DTX!" );
     178             :                     break;
     179             :             }
     180         521 :             break;
     181         205 :         case MASA_FORMAT:
     182         205 :             if ( element_mode == IVAS_SCE )
     183             :             {
     184         131 :                 ind = SID_MASA_1TC;
     185             :             }
     186             :             else
     187             :             {
     188          74 :                 ind = SID_MASA_2TC;
     189             :             }
     190         205 :             break;
     191           0 :         default:
     192           0 :             assert( !"Reserved SID format symbol written." );
     193             :             break;
     194             :     }
     195             : 
     196        3493 :     push_indice( hBstr, IND_IVAS_FORMAT, ind, SID_FORMAT_NBITS );
     197             : 
     198        3493 :     if ( ivas_format == SBA_FORMAT )
     199             :     {
     200             :         /* Write SBA planar flag */
     201         521 :         push_indice( hBstr, IND_SMODE, sba_planar, SBA_PLANAR_BITS );
     202             : 
     203             :         /* Write SBA order */
     204         521 :         push_indice( hBstr, IND_SMODE, sba_order, SBA_ORDER_BITS );
     205             :     }
     206             : 
     207        3493 :     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      911385 : int16_t getNumChanAnalysis(
     219             :     Encoder_Struct *st_ivas /* i  : IVAS encoder structure              */
     220             : )
     221             : {
     222             :     int16_t n;
     223             : 
     224      911385 :     n = st_ivas->nSCE + CPE_CHANNELS * st_ivas->nCPE;
     225      911385 :     if ( st_ivas->hEncoderConfig->ivas_format == SBA_FORMAT )
     226             :     {
     227      216995 :         n = ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     228             :     }
     229      694390 :     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       39670 :         n = st_ivas->hEncoderConfig->nchan_inp;
     232             :     }
     233      654720 :     else if ( st_ivas->hEncoderConfig->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     234             :     {
     235        8436 :         n = st_ivas->hEncoderConfig->nchan_inp;
     236             :     }
     237      646284 :     else if ( st_ivas->hEncoderConfig->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM )
     238             :     {
     239       33388 :         n = st_ivas->hEncoderConfig->nchan_inp;
     240             :     }
     241      612896 :     else if ( st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
     242             :     {
     243       86260 :         n = st_ivas->hEncoderConfig->nchan_inp;
     244             :     }
     245      526636 :     else if ( st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
     246             :     {
     247      139208 :         n = st_ivas->hEncoderConfig->nchan_ism + ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     248             :     }
     249             : 
     250      911385 :     return n;
     251             : }
     252             : 
     253             : 
     254             : /*-------------------------------------------------------------------*
     255             :  * copy_encoder_config()
     256             :  *
     257             :  * Copy configuration structrue to the state structrure
     258             :  *-------------------------------------------------------------------*/
     259             : 
     260       60525 : 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       60525 :     if ( flag_all )
     267             :     {
     268       28754 :         st->input_Fs = st_ivas->hEncoderConfig->input_Fs;
     269             : 
     270       28754 :         st->last_codec_mode = st_ivas->last_codec_mode;
     271       28754 :         st->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate;
     272             : 
     273       28754 :         st->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON;
     274             : 
     275       28754 :         st->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR;
     276             :     }
     277             : 
     278       60525 :     st->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB;
     279       60525 :     st->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR;
     280             : 
     281       60525 :     st->codec_mode = st_ivas->codec_mode;
     282       60525 :     st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     283             : 
     284       60525 :     st->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON;
     285       60525 :     st->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset;
     286       60525 :     st->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator;
     287             : 
     288             : #ifdef DEBUGGING
     289             :     st->force = st_ivas->hEncoderConfig->force;
     290             : #endif
     291       60525 :     st->element_mode = st_ivas->hEncoderConfig->element_mode_init;
     292             : 
     293       60525 :     return;
     294             : }
     295             : 
     296             : 
     297             : /*-------------------------------------------------------------------------
     298             :  * ivas_initialize_handles_enc()
     299             :  *
     300             :  * NULL initialization of handles
     301             :  *-------------------------------------------------------------------------*/
     302             : 
     303        5801 : void ivas_initialize_handles_enc(
     304             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure                  */
     305             : )
     306             : {
     307             :     int16_t i;
     308             : 
     309       29005 :     for ( i = 0; i < MAX_SCE; i++ )
     310             :     {
     311       23204 :         st_ivas->hSCE[i] = NULL;
     312             :     }
     313             : 
     314       40607 :     for ( i = 0; i < MAX_CPE; i++ )
     315             :     {
     316       34806 :         st_ivas->hCPE[i] = NULL;
     317             :     }
     318             : 
     319        5801 :     st_ivas->mem_hp20_in = NULL;
     320             : 
     321             :     /* ISM metadata handles */
     322       29005 :     for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
     323             :     {
     324       23204 :         st_ivas->hIsmMetaData[i] = NULL;
     325             :     }
     326             : 
     327             :     /* ISM DTX handle */
     328        5801 :     st_ivas->hISMDTX = NULL;
     329             : 
     330             :     /* Q Metadata handle */
     331        5801 :     st_ivas->hQMetaData = NULL;
     332             : 
     333             :     /* DirAC handle */
     334        5801 :     st_ivas->hDirAC = NULL;
     335             : 
     336             :     /* ParamISM handle */
     337        5801 :     st_ivas->hParamIsm = NULL;
     338             :     /* SPAR handle */
     339        5801 :     st_ivas->hSpar = NULL;
     340             : 
     341             :     /* MASA encoder handle */
     342        5801 :     st_ivas->hMasa = NULL;
     343             : 
     344             :     /* MCT handle */
     345        5801 :     st_ivas->hMCT = NULL;
     346             : 
     347             :     /* MC Param-Upmix handle */
     348        5801 :     st_ivas->hMCParamUpmix = NULL;
     349             : 
     350             :     /* Parametric MC handle */
     351        5801 :     st_ivas->hParamMC = NULL;
     352             : 
     353             :     /* Multi-channel MASA handle */
     354        5801 :     st_ivas->hMcMasa = NULL;
     355             : 
     356             :     /* Stereo downmix for EVS encoder handle */
     357        5801 :     st_ivas->hStereoDmxEVS = NULL;
     358             : 
     359             :     /* LFE  handle */
     360        5801 :     st_ivas->hLFE = NULL;
     361             : 
     362             :     /* LFE low pass filter handle */
     363        5801 :     st_ivas->hLfeLpf = NULL;
     364             : 
     365             :     /* Object MASA handle */
     366        5801 :     st_ivas->hOMasa = NULL;
     367             : 
     368             :     /* OSBA handle */
     369        5801 :     st_ivas->hOSba = NULL;
     370             : 
     371        5801 :     return;
     372             : }
     373             : 
     374             : 
     375             : /*-------------------------------------------------------------------*
     376             :  * ivas_init_encoder()
     377             :  *
     378             :  * Initialize IVAS encoder state structure
     379             :  *-------------------------------------------------------------------*/
     380             : 
     381        5801 : 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        5801 :     error = IVAS_ERR_OK;
     395             : 
     396        5801 :     hEncoderConfig = st_ivas->hEncoderConfig;
     397        5801 :     ivas_format = hEncoderConfig->ivas_format;
     398        5801 :     input_Fs = hEncoderConfig->input_Fs;
     399        5801 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     400             : 
     401        5801 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate;
     402             : 
     403        5801 :     if ( ivas_format != MONO_FORMAT )
     404             :     {
     405             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
     406        5556 :         hEncoderConfig->max_bwidth = max( hEncoderConfig->max_bwidth, WB );
     407             :     }
     408        5801 :     st_ivas->ism_mode = ISM_MODE_NONE;
     409        5801 :     st_ivas->mc_mode = MC_MODE_NONE;
     410             : 
     411        5801 :     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        5801 :     nchan_inp_buff = hEncoderConfig->nchan_inp;
     423        5801 :     if ( ivas_format == MONO_FORMAT )
     424             :     {
     425         245 :         nchan_inp_buff = 0;
     426             :     }
     427        5556 :     else if ( ivas_format == MASA_ISM_FORMAT )
     428             :     {
     429         935 :         if ( hEncoderConfig->nchan_inp - hEncoderConfig->nchan_ism == 1 ) /* mono is duplicated in monoMASA */
     430             :         {
     431         452 :             nchan_inp_buff++;
     432             :         }
     433             : 
     434         935 :         nchan_inp_buff++; /* for *data_separated_object */
     435             :     }
     436             : 
     437       46845 :     for ( n = 0; n < nchan_inp_buff; n++ )
     438             :     {
     439             :         /* note: these are intra-frame heap memories */
     440       41044 :         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       80777 :     for ( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     446             :     {
     447       74976 :         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        5801 :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices( ivas_format, ivas_total_brate );
     456             : 
     457             :     /* allocate buffer of indices */
     458        5801 :     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     5601631 :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     465             :     {
     466     5595830 :         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        5801 :     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        5801 :     if ( st_ivas->ivas_max_num_indices_metadata > 0 )
     481             :     {
     482        5556 :         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     3000546 :         for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
     489             :         {
     490     2994990 :             st_ivas->ind_list_metadata[i].nb_bits = -1;
     491             :         }
     492             :     }
     493             :     else
     494             :     {
     495         245 :         st_ivas->ind_list_metadata = NULL;
     496             :     }
     497             : 
     498             :     /*-----------------------------------------------------------------*
     499             :      * Allocate and initialize SCE/CPE and other handles
     500             :      *-----------------------------------------------------------------*/
     501             : 
     502        5801 :     if ( ivas_format == MONO_FORMAT )
     503             :     {
     504         245 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
     505         245 :         st_ivas->nCPE = 0;
     506         245 :         st_ivas->nchan_transport = 1;
     507         245 :         sce_id = 0;
     508             : 
     509         245 :         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         245 :         if ( hEncoderConfig->stereo_dmx_evs == 1 )
     516             :         {
     517         117 :             if ( ( error = stereo_dmx_evs_init_encoder( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
     518             :             {
     519           0 :                 return error;
     520             :             }
     521             :         }
     522             :     }
     523        5556 :     else if ( ivas_format == STEREO_FORMAT )
     524             :     {
     525         171 :         st_ivas->nSCE = 0;
     526         171 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
     527         171 :         st_ivas->nchan_transport = CPE_CHANNELS;
     528         171 :         cpe_id = 0;
     529             : 
     530         171 :         if ( ( error = create_cpe_enc( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     531             :         {
     532           0 :             return error;
     533             :         }
     534             :     }
     535        5385 :     else if ( ivas_format == ISM_FORMAT )
     536             :     {
     537         679 :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate );
     538             : 
     539         679 :         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        2484 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     545             :         {
     546        1805 :             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         679 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     553             :         {
     554          74 :             if ( ( error = ivas_param_ism_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     555             :             {
     556           0 :                 return error;
     557             :             }
     558             :         }
     559             : 
     560         679 :         if ( st_ivas->hEncoderConfig->Opt_DTX_ON )
     561             :         {
     562         267 :             if ( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
     563             :             {
     564           0 :                 return error;
     565             :             }
     566             :         }
     567             :     }
     568        4706 :     else if ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT )
     569             :     {
     570        1554 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     571             :         {
     572           0 :             return error;
     573             :         }
     574             : 
     575        1554 :         if ( ivas_format == SBA_FORMAT )
     576             :         {
     577        1040 :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     578             : 
     579        1040 :             if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     580             :             {
     581           0 :                 return error;
     582             :             }
     583             : 
     584        1040 :             if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     585             :             {
     586           0 :                 return error;
     587             :             }
     588             :         }
     589             :         else
     590             :         {
     591         514 :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp;
     592             : 
     593         514 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     594             :             {
     595           0 :                 return error;
     596             :             }
     597             :         }
     598             : 
     599        2042 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     600             :         {
     601         488 :             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         488 :             if ( ivas_format == SBA_FORMAT && st_ivas->hEncoderConfig->Opt_DTX_ON )
     607             :             {
     608         101 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
     609             :             }
     610             :         }
     611             : 
     612        3101 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     613             :         {
     614        1547 :             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        4641 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     620             :             {
     621        3094 :                 if ( hEncoderConfig->Opt_DTX_ON )
     622             :                 {
     623         246 :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
     624             :                 }
     625             :             }
     626             :         }
     627             : 
     628        1554 :         if ( st_ivas->nCPE > 1 )
     629             :         {
     630         481 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     631             :             {
     632           0 :                 return error;
     633             :             }
     634             :         }
     635             :     }
     636        3152 :     else if ( ivas_format == MASA_ISM_FORMAT )
     637             :     {
     638             :         int32_t ism_total_brate;
     639             :         int16_t k;
     640             : 
     641         935 :         st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, hEncoderConfig->nchan_ism );
     642         935 :         st_ivas->nchan_transport = 2;
     643             : 
     644         935 :         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         935 :         k = 0;
     650        8240 :         while ( k < SIZE_IVAS_BRATE_TBL && ivas_total_brate != ivas_brate_tbl[k] )
     651             :         {
     652        7305 :             k++;
     653             :         }
     654             : 
     655         935 :         ism_total_brate = 0;
     656        2101 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     657             :         {
     658        1166 :             ism_total_brate += sep_object_brate[k - 2][st_ivas->nSCE - 1];
     659        1166 :             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         935 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     666             :         {
     667           0 :             return error;
     668             :         }
     669         935 :         if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     670             :         {
     671           0 :             return error;
     672             :         }
     673             : 
     674         935 :         if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC )
     675             :         {
     676         453 :             if ( ( error = ivas_omasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     677             :             {
     678           0 :                 return error;
     679             :             }
     680             :         }
     681             : 
     682         935 :         if ( ivas_total_brate - ism_total_brate >= MIN_BRATE_MDCT_STEREO )
     683             :         {
     684         471 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     685             :         }
     686             :         else
     687             :         {
     688         464 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     689             :         }
     690             : 
     691         935 :         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        2217 :     else if ( ivas_format == SBA_ISM_FORMAT )
     697             :     {
     698        1324 :         st_ivas->ism_mode = ISM_MODE_NONE;
     699             : 
     700        1324 :         st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->hEncoderConfig->nchan_ism );
     701             : 
     702        1324 :         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        1324 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     709             :         {
     710           0 :             return error;
     711             :         }
     712             : 
     713        1324 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     714             : 
     715        1324 :         if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     716             :         {
     717           0 :             return error;
     718             :         }
     719             : 
     720        1324 :         if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     721             :         {
     722           0 :             return error;
     723             :         }
     724             : 
     725        1324 :         if ( st_ivas->ism_mode == ISM_MODE_NONE )
     726             :         {
     727             :             /* allocate and initialize SBA core-coders */
     728         652 :             if ( st_ivas->nchan_transport == 1 )
     729             :             {
     730         374 :                 if ( ( error = create_sce_enc( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
     731             :                 {
     732           0 :                     return error;
     733             :                 }
     734             :             }
     735             : 
     736         986 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     737             :             {
     738         334 :                 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         652 :             if ( st_ivas->nCPE > 1 )
     745             :             {
     746          56 :                 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         672 :                 n_all = st_ivas->nchan_transport + st_ivas->hEncoderConfig->nchan_ism;
     760         672 :                 st_ivas->nCPE = ( n_all + 1 ) >> 1;
     761             :             }
     762        2926 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     763             :             {
     764        2254 :                 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         672 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     771             :             {
     772           0 :                 return error;
     773             :             }
     774             :         }
     775             : 
     776        1324 :         if ( ( error = ivas_osba_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     777             :         {
     778           0 :             return error;
     779             :         }
     780             :     }
     781         893 :     else if ( ivas_format == MC_FORMAT )
     782             :     {
     783         893 :         st_ivas->mc_mode = ivas_mc_mode_select( hEncoderConfig->mc_input_setup, ivas_total_brate );
     784             : 
     785         893 :         if ( ( error = ivas_create_lfe_lpf_enc( &st_ivas->hLfeLpf, input_Fs ) ) != IVAS_ERR_OK )
     786             :         {
     787           0 :             return error;
     788             :         }
     789             : 
     790         893 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
     791             :         {
     792         535 :             st_ivas->nSCE = 0;
     793         535 :             st_ivas->nCPE = hEncoderConfig->nchan_inp / CPE_CHANNELS;
     794             : 
     795        2534 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     796             :             {
     797        1999 :                 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         535 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     804             :             {
     805           0 :                 return error;
     806             :             }
     807             : 
     808         535 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     809             :             {
     810           0 :                 return error;
     811             :             }
     812             : 
     813         535 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( st_ivas->hEncoderConfig->mc_input_setup );
     814             :         }
     815         358 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     816             :         {
     817          78 :             st_ivas->nSCE = 0;
     818          78 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
     819          78 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     820             : 
     821          78 :             if ( ( error = ivas_mc_paramupmix_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     822             :             {
     823           0 :                 return error;
     824             :             }
     825             : 
     826         390 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     827             :             {
     828         312 :                 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          78 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     835             :             {
     836           0 :                 return error;
     837             :             }
     838             : 
     839          78 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     840             :             {
     841           0 :                 return error;
     842             :             }
     843             :         }
     844         280 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     845             :         {
     846         137 :             if ( ( error = ivas_param_mc_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     847             :             {
     848           0 :                 return error;
     849             :             }
     850             : 
     851         363 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     852             :             {
     853         226 :                 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         137 :             if ( st_ivas->nCPE > 1 )
     860             :             {
     861          89 :                 if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     862             :                 {
     863           0 :                     return error;
     864             :                 }
     865             :             }
     866             :         }
     867         143 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     868             :         {
     869             :             int32_t brate_sce, brate_cpe;
     870             : 
     871         143 :             ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
     872             : 
     873         143 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     874             :             {
     875           0 :                 return error;
     876             :             }
     877             : 
     878         143 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     879             :             {
     880           0 :                 return error;
     881             :             }
     882             : 
     883         143 :             if ( ( error = ivas_mcmasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     884             :             {
     885           0 :                 return error;
     886             :             }
     887             : 
     888         143 :             ivas_mcmasa_split_brate( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
     889             : 
     890         276 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     891             :             {
     892         133 :                 if ( ( error = create_sce_enc( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
     893             :                 {
     894           0 :                     return error;
     895             :                 }
     896             :             }
     897             : 
     898         177 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     899             :             {
     900          34 :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     901             : 
     902          34 :                 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        5801 :     n = getNumChanAnalysis( st_ivas );
     922             : 
     923        5801 :     if ( n > 0 )
     924             :     {
     925        5801 :         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       37488 :     for ( i = 0; i < n; i++ )
     936             :     {
     937       31687 :         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       31687 :         set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
     943             :     }
     944             : 
     945        5801 :     return error;
     946             : }
     947             : 
     948             : 
     949             : /*-------------------------------------------------------------------------
     950             :  * destroy_core_enc()
     951             :  *
     952             :  * Close core encoder handles
     953             :  *-------------------------------------------------------------------------*/
     954             : 
     955       28754 : void destroy_core_enc(
     956             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
     957             : )
     958             : {
     959             :     int16_t i;
     960             : 
     961       28754 :     destroy_cldfb_encoder( hCoreCoder );
     962             : 
     963       28754 :     if ( hCoreCoder->hSignalBuf != NULL )
     964             :     {
     965       28573 :         free( hCoreCoder->hSignalBuf );
     966       28573 :         hCoreCoder->hSignalBuf = NULL;
     967             :     }
     968             : 
     969       28754 :     if ( hCoreCoder->hBstr != NULL )
     970             :     {
     971             :         /* reset buffer of indices */
     972       28573 :         for ( i = 0; i < hCoreCoder->hBstr->nb_ind_tot; i++ )
     973             :         {
     974           0 :             hCoreCoder->hBstr->ind_list[i].nb_bits = -1;
     975             :         }
     976       28573 :         free( hCoreCoder->hBstr );
     977       28573 :         hCoreCoder->hBstr = NULL;
     978             :     }
     979             : 
     980       28754 :     if ( hCoreCoder->hLPDmem != NULL )
     981             :     {
     982        7817 :         free( hCoreCoder->hLPDmem );
     983        7817 :         hCoreCoder->hLPDmem = NULL;
     984             :     }
     985             : 
     986       28754 :     if ( hCoreCoder->hTranDet != NULL )
     987             :     {
     988       28573 :         free( hCoreCoder->hTranDet );
     989       28573 :         hCoreCoder->hTranDet = NULL;
     990             :     }
     991             : 
     992       28754 :     if ( hCoreCoder->hNoiseEst != NULL )
     993             :     {
     994       28112 :         free( hCoreCoder->hNoiseEst );
     995       28112 :         hCoreCoder->hNoiseEst = NULL;
     996             :     }
     997             : 
     998       28754 :     if ( hCoreCoder->hVAD != NULL )
     999             :     {
    1000       27931 :         free( hCoreCoder->hVAD );
    1001       27931 :         hCoreCoder->hVAD = NULL;
    1002             :     }
    1003             : 
    1004       28754 :     if ( hCoreCoder->hVAD_CLDFB != NULL )
    1005             :     {
    1006         245 :         free( hCoreCoder->hVAD_CLDFB );
    1007         245 :         hCoreCoder->hVAD_CLDFB = NULL;
    1008             :     }
    1009             : 
    1010       28754 :     if ( hCoreCoder->hTdCngEnc != NULL )
    1011             :     {
    1012         477 :         free( hCoreCoder->hTdCngEnc );
    1013         477 :         hCoreCoder->hTdCngEnc = NULL;
    1014             :     }
    1015             : 
    1016       28754 :     if ( hCoreCoder->hDtxEnc != NULL )
    1017             :     {
    1018        2018 :         free( hCoreCoder->hDtxEnc );
    1019        2018 :         hCoreCoder->hDtxEnc = NULL;
    1020             :     }
    1021             : 
    1022       28754 :     if ( hCoreCoder->hSpMusClas != NULL )
    1023             :     {
    1024       28112 :         free( hCoreCoder->hSpMusClas );
    1025       28112 :         hCoreCoder->hSpMusClas = NULL;
    1026             :     }
    1027             : 
    1028       28754 :     if ( hCoreCoder->hGSCEnc != NULL )
    1029             :     {
    1030        7817 :         free( hCoreCoder->hGSCEnc );
    1031        7817 :         hCoreCoder->hGSCEnc = NULL;
    1032             :     }
    1033             : 
    1034       28754 :     if ( hCoreCoder->hSC_VBR != NULL )
    1035             :     {
    1036         245 :         free( hCoreCoder->hSC_VBR );
    1037         245 :         hCoreCoder->hSC_VBR = NULL;
    1038             :     }
    1039             : 
    1040       28754 :     if ( hCoreCoder->hAmrwb_IO != NULL )
    1041             :     {
    1042         245 :         free( hCoreCoder->hAmrwb_IO );
    1043         245 :         hCoreCoder->hAmrwb_IO = NULL;
    1044             :     }
    1045             : 
    1046       28754 :     if ( hCoreCoder->hBWE_TD != NULL )
    1047             :     {
    1048        7816 :         free( hCoreCoder->hBWE_TD );
    1049        7816 :         hCoreCoder->hBWE_TD = NULL;
    1050             :     }
    1051             : 
    1052       28754 :     if ( hCoreCoder->hBWE_FD != NULL )
    1053             :     {
    1054        7816 :         free( hCoreCoder->hBWE_FD );
    1055        7816 :         hCoreCoder->hBWE_FD = NULL;
    1056             :     }
    1057             : 
    1058       28754 :     if ( hCoreCoder->hRF != NULL )
    1059             :     {
    1060         245 :         free( hCoreCoder->hRF );
    1061         245 :         hCoreCoder->hRF = NULL;
    1062             :     }
    1063             : 
    1064       28754 :     if ( hCoreCoder->hTECEnc != NULL )
    1065             :     {
    1066         245 :         free( hCoreCoder->hTECEnc );
    1067         245 :         hCoreCoder->hTECEnc = NULL;
    1068             :     }
    1069             : 
    1070       28754 :     if ( hCoreCoder->hTcxEnc != NULL )
    1071             :     {
    1072       27928 :         free( hCoreCoder->hTcxEnc );
    1073       27928 :         hCoreCoder->hTcxEnc = NULL;
    1074             :     }
    1075             : 
    1076       28754 :     if ( hCoreCoder->hTcxCfg != NULL )
    1077             :     {
    1078       27928 :         free( hCoreCoder->hTcxCfg );
    1079       27928 :         hCoreCoder->hTcxCfg = NULL;
    1080             :     }
    1081             : 
    1082       28754 :     if ( hCoreCoder->hIGFEnc != NULL )
    1083             :     {
    1084       18794 :         free( hCoreCoder->hIGFEnc );
    1085       18794 :         hCoreCoder->hIGFEnc = NULL;
    1086             :     }
    1087             : 
    1088       28754 :     if ( hCoreCoder->hPlcExt != NULL )
    1089             :     {
    1090         245 :         free( hCoreCoder->hPlcExt );
    1091         245 :         hCoreCoder->hPlcExt = NULL;
    1092             :     }
    1093             : 
    1094       28754 :     if ( hCoreCoder->hHQ_core != NULL )
    1095             :     {
    1096        7837 :         free( hCoreCoder->hHQ_core );
    1097        7837 :         hCoreCoder->hHQ_core = NULL;
    1098             :     }
    1099             : 
    1100       28754 :     free( hCoreCoder );
    1101             : 
    1102       28754 :     return;
    1103             : }
    1104             : 
    1105             : 
    1106             : /*-------------------------------------------------------------------------
    1107             :  * ivas_destroy_enc()
    1108             :  *
    1109             :  * Close IVAS encoder handles
    1110             :  *-------------------------------------------------------------------------*/
    1111             : 
    1112        5801 : void ivas_destroy_enc(
    1113             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    1114             : )
    1115             : {
    1116             :     int16_t i, n, nchan_inp;
    1117        5801 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
    1118             : 
    1119             :     /* SCE handles */
    1120       29005 :     for ( i = 0; i < MAX_SCE; i++ )
    1121             :     {
    1122       23204 :         if ( st_ivas->hSCE[i] != NULL )
    1123             :         {
    1124        4183 :             destroy_sce_enc( st_ivas->hSCE[i] );
    1125        4183 :             st_ivas->hSCE[i] = NULL;
    1126             :         }
    1127             :     }
    1128             : 
    1129             :     /* CPE handles */
    1130       40607 :     for ( i = 0; i < MAX_CPE; i++ )
    1131             :     {
    1132       34806 :         if ( st_ivas->hCPE[i] != NULL )
    1133             :         {
    1134        7930 :             destroy_cpe_enc( st_ivas->hCPE[i] );
    1135        7930 :             st_ivas->hCPE[i] = NULL;
    1136             :         }
    1137             :     }
    1138             : 
    1139             :     /* HP20 filter handles */
    1140        5801 :     if ( st_ivas->mem_hp20_in != NULL )
    1141             :     {
    1142        5801 :         n = getNumChanAnalysis( st_ivas );
    1143             : 
    1144       37616 :         for ( i = 0; i < n; i++ )
    1145             :         {
    1146       31815 :             free( st_ivas->mem_hp20_in[i] );
    1147       31815 :             st_ivas->mem_hp20_in[i] = NULL;
    1148             :         }
    1149        5801 :         free( st_ivas->mem_hp20_in );
    1150        5801 :         st_ivas->mem_hp20_in = NULL;
    1151             :     }
    1152             : 
    1153             :     /* ISM metadata handles */
    1154        5801 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    1155             : 
    1156             :     /* ISM DTX Handle */
    1157        5801 :     if ( st_ivas->hISMDTX != NULL )
    1158             :     {
    1159         267 :         free( st_ivas->hISMDTX );
    1160         267 :         st_ivas->hISMDTX = NULL;
    1161             :     }
    1162             : 
    1163             :     /* Q Metadata handle */
    1164        5801 :     ivas_qmetadata_close( &( st_ivas->hQMetaData ) );
    1165             : 
    1166             :     /* DirAC handle */
    1167        5801 :     ivas_dirac_enc_close( &( st_ivas->hDirAC ), st_ivas->hEncoderConfig->input_Fs );
    1168             : 
    1169             :     /* ParamISM handle */
    1170        5801 :     ivas_param_ism_enc_close( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
    1171             : 
    1172             :     /* SPAR handle */
    1173        5801 :     ivas_spar_enc_close( &( st_ivas->hSpar ), st_ivas->hEncoderConfig->input_Fs, nchan_inp, 0 );
    1174             : 
    1175             :     /* MASA handle */
    1176        5801 :     ivas_masa_enc_close( &( st_ivas->hMasa ) );
    1177             : 
    1178             :     /* MCT handle */
    1179        5801 :     ivas_mct_enc_close( &( st_ivas->hMCT ) );
    1180             : 
    1181             :     /* LFE handle */
    1182        5801 :     ivas_lfe_enc_close( &( st_ivas->hLFE ) );
    1183             : 
    1184             :     /* LFE low pass filter state */
    1185        5801 :     ivas_lfe_lpf_enc_close( &( st_ivas->hLfeLpf ) );
    1186             : 
    1187             :     /* Param-Upmix MC handle */
    1188        5801 :     ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
    1189             : 
    1190             :     /* Parametric MC handle */
    1191        5801 :     ivas_param_mc_enc_close( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
    1192             : 
    1193             :     /* Multi-channel MASA handle */
    1194        5801 :     ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
    1195             : 
    1196             :     /* OMASA handle */
    1197        5801 :     ivas_omasa_enc_close( &( st_ivas->hOMasa ) );
    1198             : 
    1199             :     /* OSBA handle */
    1200        5801 :     ivas_osba_enc_close( &( st_ivas->hOSba ) );
    1201             : 
    1202             :     /* Stereo downmix for EVS encoder handle */
    1203        5801 :     stereo_dmx_evs_close_encoder( &( st_ivas->hStereoDmxEVS ) );
    1204             : 
    1205             :     /* Encoder configuration handle */
    1206        5801 :     if ( st_ivas->hEncoderConfig != NULL )
    1207             :     {
    1208        5801 :         free( st_ivas->hEncoderConfig );
    1209        5801 :         st_ivas->hEncoderConfig = NULL;
    1210             :     }
    1211             : 
    1212             :     /* Buffer of indices */
    1213        5801 :     if ( st_ivas->ind_list != NULL )
    1214             :     {
    1215        5801 :         free( st_ivas->ind_list );
    1216             :     }
    1217             : 
    1218        5801 :     if ( st_ivas->ind_list_metadata != NULL )
    1219             :     {
    1220        5556 :         free( st_ivas->ind_list_metadata );
    1221             :     }
    1222             : 
    1223             :     /* floating-point input audio buffers */
    1224      121821 :     for ( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1225             :     {
    1226      116020 :         if ( st_ivas->p_data_f[n] != NULL )
    1227             :         {
    1228       41044 :             free( st_ivas->p_data_f[n] );
    1229       41044 :             st_ivas->p_data_f[n] = NULL;
    1230             :         }
    1231             :     }
    1232             : 
    1233             :     /* main IVAS handle */
    1234        5801 :     free( st_ivas );
    1235             : 
    1236        5801 :     return;
    1237             : }
    1238             : 
    1239             : /*-------------------------------------------------------------------------
    1240             :  * ivas_initialize_MD_bstr_enc()
    1241             :  *
    1242             :  * Allocate and initialize SCE/CPE MD bitstream handle
    1243             :  *-------------------------------------------------------------------------*/
    1244             : 
    1245       10614 : 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       10614 :     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       10614 :     hMetaData->ind_list = st_ivas->ind_list_metadata;
    1259       10614 :     hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1260       10614 :     hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata;
    1261       10614 :     hMetaData->st_ivas = st_ivas;
    1262             : 
    1263       10614 :     reset_indices_enc( hMetaData, st_ivas->ivas_max_num_indices_metadata );
    1264             : 
    1265       10614 :     *hMetaData_out = hMetaData;
    1266             : 
    1267       10614 :     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       22012 : void ivas_destroy_MD_bstr_enc(
    1278             :     BSTR_ENC_HANDLE *hMetaData /* i/o: encoder MD bitstream handle  */
    1279             : )
    1280             : {
    1281       22012 :     if ( hMetaData == NULL || *hMetaData == NULL )
    1282             :     {
    1283       11398 :         return;
    1284             :     }
    1285             : 
    1286       10614 :     free( *hMetaData );
    1287       10614 :     *hMetaData = NULL;
    1288             : 
    1289       10614 :     return;
    1290             : }

Generated by: LCOV version 1.14