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

Generated by: LCOV version 1.14