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 @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 422 484 87.2 %
Date: 2025-05-23 08:37:30 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include "ivas_cnst.h"
      37             : #include "prot.h"
      38             : #include "ivas_prot.h"
      39             : #include "ivas_stat_enc.h"
      40             : #include "ivas_rom_com.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * ivas_write_format()
      49             :  *
      50             :  * Write IVAS format signaling
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53      425922 : 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      425922 :     ind = 0;
      60      425922 :     nBits = IVAS_FORMAT_SIGNALING_NBITS;
      61      425922 :     extra_bits = ( IVAS_FORMAT_SIGNALING_NBITS_EXTENDED - IVAS_FORMAT_SIGNALING_NBITS );
      62             : 
      63      425922 :     switch ( st_ivas->hEncoderConfig->ivas_format )
      64             :     {
      65       72486 :         case STEREO_FORMAT:
      66       72486 :             ind = 0;
      67       72486 :             break;
      68      103826 :         case ISM_FORMAT:
      69      103826 :             ind = 2;
      70      103826 :             if ( st_ivas->hEncoderConfig->ivas_total_brate >= IVAS_24k4 )
      71             :             {
      72       95106 :                 ind = 4;
      73       95106 :                 nBits += extra_bits;
      74             :             }
      75      103826 :             break;
      76       48850 :         case MC_FORMAT:
      77       48850 :             ind = 1;
      78       48850 :             break;
      79      122500 :         case SBA_FORMAT:
      80      122500 :             ind = 6;
      81      122500 :             nBits += extra_bits;
      82      122500 :             break;
      83       30626 :         case MASA_FORMAT:
      84       30626 :             ind = 7;
      85       30626 :             nBits += extra_bits;
      86       30626 :             break;
      87       10634 :         case MASA_ISM_FORMAT:
      88       10634 :             if ( st_ivas->ism_mode == ISM_MODE_NONE )
      89             :             {
      90        2388 :                 ind = 7; /* send MASA format */
      91        2388 :                 nBits += extra_bits;
      92             :             }
      93             :             else
      94             :             {
      95        8246 :                 ind = 10;
      96        8246 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
      97             :             }
      98       10634 :             break;
      99       37000 :         case SBA_ISM_FORMAT:
     100       37000 :             if ( st_ivas->hEncoderConfig->ivas_total_brate < IVAS_24k4 )
     101             :             {
     102        2881 :                 ind = 6; /* send SBA format */
     103        2881 :                 nBits += extra_bits;
     104             :             }
     105             :             else
     106             :             {
     107       34119 :                 ind = 11; /* 1011 */
     108       34119 :                 nBits += extra_bits + IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     109             :             }
     110       37000 :             break;
     111           0 :         default:
     112           0 :             assert( !"Invalid format. Aborting." );
     113             :             break;
     114             :     }
     115             : 
     116      425922 :     if ( st_ivas->hSCE[0] != NULL )
     117             :     {
     118      181689 :         push_indice( st_ivas->hSCE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     119             :     }
     120      244233 :     else if ( st_ivas->hCPE[0] != NULL )
     121             :     {
     122      244233 :         push_indice( st_ivas->hCPE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     123             :     }
     124             : 
     125      425922 :     return;
     126             : }
     127             : 
     128             : 
     129             : /*-------------------------------------------------------------------*
     130             :  * ivas_write_format_sid()
     131             :  *
     132             :  * Write IVAS format signaling in SID frames
     133             :  *-------------------------------------------------------------------*/
     134             : 
     135        3105 : 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        3105 :     int16_t ind = 0; /* to avoid compilation warning */
     144             : 
     145        3105 :     switch ( ivas_format )
     146             :     {
     147        1861 :         case STEREO_FORMAT:
     148        1861 :             if ( element_mode == IVAS_CPE_MDCT )
     149             :             {
     150         416 :                 ind = SID_MDCT_STEREO;
     151             :             }
     152        1445 :             else if ( element_mode == IVAS_CPE_DFT )
     153             :             {
     154        1445 :                 ind = SID_DFT_STEREO;
     155             :             }
     156             :             else
     157             :             {
     158           0 :                 assert( !"Wrong stereo mode for SID format signaling" );
     159             :             }
     160        1861 :             break;
     161         555 :         case ISM_FORMAT:
     162         555 :             ind = SID_ISM;
     163         555 :             break;
     164         484 :         case SBA_FORMAT:
     165         484 :             switch ( element_mode )
     166             :             {
     167         277 :                 case IVAS_SCE:
     168         277 :                     ind = SID_SBA_1TC;
     169         277 :                     break;
     170         207 :                 case IVAS_CPE_MDCT:
     171         207 :                     ind = SID_SBA_2TC;
     172         207 :                     break;
     173           0 :                 default:
     174           0 :                     assert( !"Wrong element mode for SBA DTX!" );
     175             :                     break;
     176             :             }
     177         484 :             break;
     178         205 :         case MASA_FORMAT:
     179         205 :             if ( element_mode == IVAS_SCE )
     180             :             {
     181         131 :                 ind = SID_MASA_1TC;
     182             :             }
     183             :             else
     184             :             {
     185          74 :                 ind = SID_MASA_2TC;
     186             :             }
     187         205 :             break;
     188           0 :         default:
     189           0 :             assert( !"Reserved SID format symbol written." );
     190             :             break;
     191             :     }
     192             : 
     193        3105 :     push_indice( hBstr, IND_IVAS_FORMAT, ind, SID_FORMAT_NBITS );
     194             : 
     195        3105 :     if ( ivas_format == SBA_FORMAT )
     196             :     {
     197             :         /* Write SBA planar flag */
     198         484 :         push_indice( hBstr, IND_SMODE, sba_planar, SBA_PLANAR_BITS );
     199             : 
     200             :         /* Write SBA order */
     201         484 :         push_indice( hBstr, IND_SMODE, sba_order, SBA_ORDER_BITS );
     202             :     }
     203             : 
     204        3105 :     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      425542 : int16_t getNumChanAnalysis(
     216             :     Encoder_Struct *st_ivas /* i  : IVAS encoder structure              */
     217             : )
     218             : {
     219             :     int16_t n;
     220             : 
     221      425542 :     n = st_ivas->nSCE + CPE_CHANNELS * st_ivas->nCPE;
     222      425542 :     if ( st_ivas->hEncoderConfig->ivas_format == SBA_FORMAT )
     223             :     {
     224      122988 :         n = ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     225             :     }
     226      302554 :     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       22012 :         n = st_ivas->hEncoderConfig->nchan_inp;
     229             :     }
     230      280542 :     else if ( st_ivas->hEncoderConfig->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     231             :     {
     232         800 :         n = st_ivas->hEncoderConfig->nchan_inp;
     233             :     }
     234      279742 :     else if ( st_ivas->hEncoderConfig->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM )
     235             :     {
     236       20754 :         n = st_ivas->hEncoderConfig->nchan_inp;
     237             :     }
     238      258988 :     else if ( st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
     239             :     {
     240        9088 :         n = st_ivas->hEncoderConfig->nchan_inp;
     241             :     }
     242      249900 :     else if ( st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT )
     243             :     {
     244       37074 :         n = st_ivas->hEncoderConfig->nchan_ism + ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     245             :     }
     246             : 
     247      425542 :     return n;
     248             : }
     249             : 
     250             : 
     251             : /*-------------------------------------------------------------------*
     252             :  * copy_encoder_config()
     253             :  *
     254             :  * Copy configuration structrue to the state structrure
     255             :  *-------------------------------------------------------------------*/
     256             : 
     257       24168 : 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       24168 :     if ( flag_all )
     264             :     {
     265        8836 :         st->input_Fs = st_ivas->hEncoderConfig->input_Fs;
     266             : 
     267        8836 :         st->last_codec_mode = st_ivas->last_codec_mode;
     268        8836 :         st->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate;
     269             : 
     270        8836 :         st->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON;
     271             : 
     272        8836 :         st->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR;
     273             :     }
     274             : 
     275       24168 :     st->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB;
     276       24168 :     st->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR;
     277             : 
     278       24168 :     st->codec_mode = st_ivas->codec_mode;
     279       24168 :     st->max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     280             : 
     281       24168 :     st->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON;
     282       24168 :     st->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset;
     283       24168 :     st->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator;
     284             : 
     285             : #ifdef DEBUGGING
     286             :     st->force = st_ivas->hEncoderConfig->force;
     287             : #endif
     288       24168 :     st->element_mode = st_ivas->hEncoderConfig->element_mode_init;
     289             : 
     290       24168 :     return;
     291             : }
     292             : 
     293             : 
     294             : /*-------------------------------------------------------------------------
     295             :  * ivas_initialize_handles_enc()
     296             :  *
     297             :  * NULL initialization of handles
     298             :  *-------------------------------------------------------------------------*/
     299             : 
     300         627 : void ivas_initialize_handles_enc(
     301             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure                  */
     302             : )
     303             : {
     304             :     int16_t i;
     305             : 
     306        3135 :     for ( i = 0; i < MAX_SCE; i++ )
     307             :     {
     308        2508 :         st_ivas->hSCE[i] = NULL;
     309             :     }
     310             : 
     311        4389 :     for ( i = 0; i < MAX_CPE; i++ )
     312             :     {
     313        3762 :         st_ivas->hCPE[i] = NULL;
     314             :     }
     315             : 
     316         627 :     st_ivas->mem_hp20_in = NULL;
     317             : 
     318             :     /* ISM metadata handles */
     319        3135 :     for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
     320             :     {
     321        2508 :         st_ivas->hIsmMetaData[i] = NULL;
     322             :     }
     323             : 
     324             :     /* ISM DTX handle */
     325         627 :     st_ivas->hISMDTX = NULL;
     326             : 
     327             :     /* Q Metadata handle */
     328         627 :     st_ivas->hQMetaData = NULL;
     329             : 
     330             :     /* DirAC handle */
     331         627 :     st_ivas->hDirAC = NULL;
     332             : 
     333             :     /* ParamISM handle */
     334         627 :     st_ivas->hParamIsm = NULL;
     335             :     /* SPAR handle */
     336         627 :     st_ivas->hSpar = NULL;
     337             : 
     338             :     /* MASA encoder handle */
     339         627 :     st_ivas->hMasa = NULL;
     340             : 
     341             :     /* MCT handle */
     342         627 :     st_ivas->hMCT = NULL;
     343             : 
     344             :     /* MC Param-Upmix handle */
     345         627 :     st_ivas->hMCParamUpmix = NULL;
     346             : 
     347             :     /* Parametric MC handle */
     348         627 :     st_ivas->hParamMC = NULL;
     349             : 
     350             :     /* Multi-channel MASA handle */
     351         627 :     st_ivas->hMcMasa = NULL;
     352             : 
     353             :     /* Stereo downmix for EVS encoder handle */
     354         627 :     st_ivas->hStereoDmxEVS = NULL;
     355             : 
     356             :     /* LFE  handle */
     357         627 :     st_ivas->hLFE = NULL;
     358             : 
     359             :     /* LFE low pass filter handle */
     360         627 :     st_ivas->hLfeLpf = NULL;
     361             : 
     362             :     /* Object MASA handle */
     363         627 :     st_ivas->hOMasa = NULL;
     364             : 
     365             :     /* OSBA handle */
     366         627 :     st_ivas->hOSba = NULL;
     367             : 
     368         627 :     return;
     369             : }
     370             : 
     371             : 
     372             : /*-------------------------------------------------------------------*
     373             :  * ivas_init_encoder()
     374             :  *
     375             :  * Initialize IVAS encoder state structure
     376             :  *-------------------------------------------------------------------*/
     377             : 
     378         627 : 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         627 :     error = IVAS_ERR_OK;
     392             : 
     393         627 :     hEncoderConfig = st_ivas->hEncoderConfig;
     394         627 :     ivas_format = hEncoderConfig->ivas_format;
     395         627 :     input_Fs = hEncoderConfig->input_Fs;
     396         627 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     397             : 
     398         627 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate;
     399             : 
     400         627 :     if ( ivas_format != MONO_FORMAT )
     401             :     {
     402             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
     403         624 :         hEncoderConfig->max_bwidth = max( hEncoderConfig->max_bwidth, WB );
     404             :     }
     405         627 :     st_ivas->ism_mode = ISM_MODE_NONE;
     406         627 :     st_ivas->mc_mode = MC_MODE_NONE;
     407             : 
     408         627 :     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         627 :     nchan_inp_buff = hEncoderConfig->nchan_inp;
     420         627 :     if ( ivas_format == MONO_FORMAT )
     421             :     {
     422           3 :         nchan_inp_buff = 0;
     423             :     }
     424         624 :     else if ( ivas_format == MASA_ISM_FORMAT )
     425             :     {
     426          44 :         if ( hEncoderConfig->nchan_inp - hEncoderConfig->nchan_ism == 1 ) /* mono is duplicated in monoMASA */
     427             :         {
     428          12 :             nchan_inp_buff++;
     429             :         }
     430             : 
     431          44 :         nchan_inp_buff++; /* for *data_separated_object */
     432             :     }
     433             : 
     434        4301 :     for ( n = 0; n < nchan_inp_buff; n++ )
     435             :     {
     436             :         /* note: these are intra-frame heap memories */
     437        3674 :         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        9493 :     for ( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     443             :     {
     444        8866 :         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         627 :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices( ivas_format, ivas_total_brate );
     453             : 
     454             :     /* allocate buffer of indices */
     455         627 :     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      496367 :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     462             :     {
     463      495740 :         st_ivas->ind_list[i].nb_bits = -1;
     464             :     }
     465             : 
     466             :     /* set the maximum allowed number of metadata indices in the list */
     467         627 :     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         627 :     if ( st_ivas->ivas_max_num_indices_metadata > 0 )
     471             :     {
     472         624 :         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      249364 :         for ( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
     479             :         {
     480      248740 :             st_ivas->ind_list_metadata[i].nb_bits = -1;
     481             :         }
     482             :     }
     483             :     else
     484             :     {
     485           3 :         st_ivas->ind_list_metadata = NULL;
     486             :     }
     487             : 
     488             :     /*-----------------------------------------------------------------*
     489             :      * Allocate and initialize SCE/CPE and other handles
     490             :      *-----------------------------------------------------------------*/
     491             : 
     492         627 :     if ( ivas_format == MONO_FORMAT )
     493             :     {
     494           3 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
     495           3 :         st_ivas->nCPE = 0;
     496           3 :         st_ivas->nchan_transport = 1;
     497           3 :         sce_id = 0;
     498             : 
     499           3 :         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           3 :         if ( hEncoderConfig->stereo_dmx_evs == 1 )
     506             :         {
     507           2 :             if ( ( error = stereo_dmx_evs_init_encoder( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
     508             :             {
     509           0 :                 return error;
     510             :             }
     511             :         }
     512             :     }
     513         624 :     else if ( ivas_format == STEREO_FORMAT )
     514             :     {
     515          68 :         st_ivas->nSCE = 0;
     516          68 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
     517          68 :         st_ivas->nchan_transport = CPE_CHANNELS;
     518          68 :         cpe_id = 0;
     519             : 
     520          68 :         if ( ( error = create_cpe_enc( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     521             :         {
     522           0 :             return error;
     523             :         }
     524             :     }
     525         556 :     else if ( ivas_format == ISM_FORMAT )
     526             :     {
     527          74 :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate );
     528             : 
     529          74 :         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         261 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     535             :         {
     536         187 :             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          74 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     543             :         {
     544          17 :             if ( ( error = ivas_param_ism_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     545             :             {
     546           0 :                 return error;
     547             :             }
     548             :         }
     549             : 
     550          74 :         if ( st_ivas->hEncoderConfig->Opt_DTX_ON )
     551             :         {
     552          14 :             if ( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
     553             :             {
     554           0 :                 return error;
     555             :             }
     556             :         }
     557             :     }
     558         482 :     else if ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT )
     559             :     {
     560         319 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     561             :         {
     562           0 :             return error;
     563             :         }
     564             : 
     565         319 :         if ( ivas_format == SBA_FORMAT )
     566             :         {
     567         244 :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     568             : 
     569         244 :             if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     570             :             {
     571           0 :                 return error;
     572             :             }
     573             : 
     574         244 :             if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     575             :             {
     576           0 :                 return error;
     577             :             }
     578             :         }
     579             :         else
     580             :         {
     581          75 :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp;
     582             : 
     583          75 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     584             :             {
     585           0 :                 return error;
     586             :             }
     587             :         }
     588             : 
     589         449 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     590             :         {
     591         130 :             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         130 :             if ( ivas_format == SBA_FORMAT && st_ivas->hEncoderConfig->Opt_DTX_ON )
     597             :             {
     598          45 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
     599             :             }
     600             :         }
     601             : 
     602         591 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     603             :         {
     604         272 :             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         816 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     610             :             {
     611         544 :                 if ( hEncoderConfig->Opt_DTX_ON )
     612             :                 {
     613          56 :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
     614             :                 }
     615             :             }
     616             :         }
     617             : 
     618         319 :         if ( st_ivas->nCPE > 1 )
     619             :         {
     620          83 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     621             :             {
     622           0 :                 return error;
     623             :             }
     624             :         }
     625             :     }
     626         163 :     else if ( ivas_format == MASA_ISM_FORMAT )
     627             :     {
     628             :         int32_t ism_total_brate;
     629             :         int16_t k;
     630             : 
     631          44 :         st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, hEncoderConfig->nchan_ism );
     632          44 :         st_ivas->nchan_transport = 2;
     633             : 
     634          44 :         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          44 :         k = 0;
     640         359 :         while ( k < SIZE_IVAS_BRATE_TBL && ivas_total_brate != ivas_brate_tbl[k] )
     641             :         {
     642         315 :             k++;
     643             :         }
     644             : 
     645          44 :         ism_total_brate = 0;
     646         111 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     647             :         {
     648          67 :             ism_total_brate += sep_object_brate[k - 2][st_ivas->nSCE - 1];
     649          67 :             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          44 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     656             :         {
     657           0 :             return error;
     658             :         }
     659          44 :         if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     660             :         {
     661           0 :             return error;
     662             :         }
     663             : 
     664          44 :         if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC )
     665             :         {
     666          27 :             if ( ( error = ivas_omasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     667             :             {
     668           0 :                 return error;
     669             :             }
     670             :         }
     671             : 
     672          44 :         if ( ivas_total_brate - ism_total_brate >= MIN_BRATE_MDCT_STEREO )
     673             :         {
     674          17 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     675             :         }
     676             :         else
     677             :         {
     678          27 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     679             :         }
     680             : 
     681          44 :         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         119 :     else if ( ivas_format == SBA_ISM_FORMAT )
     687             :     {
     688          37 :         st_ivas->ism_mode = ISM_MODE_NONE;
     689             : 
     690          37 :         st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->hEncoderConfig->nchan_ism );
     691             : 
     692          37 :         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          37 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     699             :         {
     700           0 :             return error;
     701             :         }
     702             : 
     703          37 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->hEncoderConfig->sba_order );
     704             : 
     705          37 :         if ( ( error = ivas_spar_enc_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
     706             :         {
     707           0 :             return error;
     708             :         }
     709             : 
     710          37 :         if ( ( error = ivas_dirac_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     711             :         {
     712           0 :             return error;
     713             :         }
     714             : 
     715          37 :         if ( st_ivas->ism_mode == ISM_MODE_NONE )
     716             :         {
     717             :             /* allocate and initialize SBA core-coders */
     718          18 :             if ( st_ivas->nchan_transport == 1 )
     719             :             {
     720          12 :                 if ( ( error = create_sce_enc( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
     721             :                 {
     722           0 :                     return error;
     723             :                 }
     724             :             }
     725             : 
     726          25 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     727             :             {
     728           7 :                 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          18 :             if ( st_ivas->nCPE > 1 )
     735             :             {
     736           1 :                 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          19 :                 n_all = st_ivas->nchan_transport + st_ivas->hEncoderConfig->nchan_ism;
     750          19 :                 st_ivas->nCPE = ( n_all + 1 ) >> 1;
     751             :             }
     752          86 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     753             :             {
     754          67 :                 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          19 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     761             :             {
     762           0 :                 return error;
     763             :             }
     764             :         }
     765             : 
     766          37 :         if ( ( error = ivas_osba_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     767             :         {
     768           0 :             return error;
     769             :         }
     770             :     }
     771          82 :     else if ( ivas_format == MC_FORMAT )
     772             :     {
     773          82 :         st_ivas->mc_mode = ivas_mc_mode_select( hEncoderConfig->mc_input_setup, ivas_total_brate );
     774             : 
     775          82 :         if ( ( error = ivas_create_lfe_lpf_enc( &st_ivas->hLfeLpf, hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
     776             :         {
     777           0 :             return error;
     778             :         }
     779             : 
     780          82 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
     781             :         {
     782          36 :             st_ivas->nSCE = 0;
     783          36 :             st_ivas->nCPE = hEncoderConfig->nchan_inp / CPE_CHANNELS;
     784             : 
     785         181 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     786             :             {
     787         145 :                 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          36 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     794             :             {
     795           0 :                 return error;
     796             :             }
     797             : 
     798          36 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     799             :             {
     800           0 :                 return error;
     801             :             }
     802             : 
     803          36 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( st_ivas->hEncoderConfig->mc_input_setup );
     804             :         }
     805          46 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     806             :         {
     807           5 :             st_ivas->nSCE = 0;
     808           5 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
     809           5 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     810             : 
     811           5 :             if ( ( error = ivas_mc_paramupmix_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     812             :             {
     813           0 :                 return error;
     814             :             }
     815             : 
     816          25 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     817             :             {
     818          20 :                 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           5 :             if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     825             :             {
     826           0 :                 return error;
     827             :             }
     828             : 
     829           5 :             if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     830             :             {
     831           0 :                 return error;
     832             :             }
     833             :         }
     834          41 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     835             :         {
     836          10 :             if ( ( error = ivas_param_mc_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     837             :             {
     838           0 :                 return error;
     839             :             }
     840             : 
     841          20 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     842             :             {
     843          10 :                 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          10 :             if ( st_ivas->nCPE > 1 )
     850             :             {
     851           0 :                 if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
     852             :                 {
     853           0 :                     return error;
     854             :                 }
     855             :             }
     856             :         }
     857          31 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     858             :         {
     859             :             int32_t brate_sce, brate_cpe;
     860             : 
     861          31 :             ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
     862             : 
     863          31 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     864             :             {
     865           0 :                 return error;
     866             :             }
     867             : 
     868          31 :             if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     869             :             {
     870           0 :                 return error;
     871             :             }
     872             : 
     873          31 :             if ( ( error = ivas_mcmasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
     874             :             {
     875           0 :                 return error;
     876             :             }
     877             : 
     878          31 :             ivas_mcmasa_split_brate( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
     879             : 
     880          60 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     881             :             {
     882          29 :                 if ( ( error = create_sce_enc( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
     883             :                 {
     884           0 :                     return error;
     885             :                 }
     886             :             }
     887             : 
     888          36 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     889             :             {
     890           5 :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     891             : 
     892           5 :                 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         627 :     n = getNumChanAnalysis( st_ivas );
     912             : 
     913         627 :     if ( n > 0 )
     914             :     {
     915         627 :         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        3403 :     for ( i = 0; i < n; i++ )
     926             :     {
     927        2776 :         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        2776 :         set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
     933             :     }
     934             : 
     935         627 :     return error;
     936             : }
     937             : 
     938             : 
     939             : /*-------------------------------------------------------------------------
     940             :  * destroy_core_enc()
     941             :  *
     942             :  * Close core encoder handles
     943             :  *-------------------------------------------------------------------------*/
     944             : 
     945        8836 : void destroy_core_enc(
     946             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
     947             : )
     948             : {
     949             :     int16_t i;
     950             : 
     951        8836 :     destroy_cldfb_encoder( hCoreCoder );
     952             : 
     953        8836 :     if ( hCoreCoder->hSignalBuf != NULL )
     954             :     {
     955        8767 :         free( hCoreCoder->hSignalBuf );
     956        8767 :         hCoreCoder->hSignalBuf = NULL;
     957             :     }
     958             : 
     959        8836 :     if ( hCoreCoder->hBstr != NULL )
     960             :     {
     961             :         /* reset buffer of indices */
     962        8767 :         for ( i = 0; i < hCoreCoder->hBstr->nb_ind_tot; i++ )
     963             :         {
     964           0 :             hCoreCoder->hBstr->ind_list[i].nb_bits = -1;
     965             :         }
     966        8767 :         free( hCoreCoder->hBstr );
     967        8767 :         hCoreCoder->hBstr = NULL;
     968             :     }
     969             : 
     970        8836 :     if ( hCoreCoder->hLPDmem != NULL )
     971             :     {
     972        3090 :         free( hCoreCoder->hLPDmem );
     973        3090 :         hCoreCoder->hLPDmem = NULL;
     974             :     }
     975             : 
     976        8836 :     if ( hCoreCoder->hTranDet != NULL )
     977             :     {
     978        8767 :         free( hCoreCoder->hTranDet );
     979        8767 :         hCoreCoder->hTranDet = NULL;
     980             :     }
     981             : 
     982        8836 :     if ( hCoreCoder->hNoiseEst != NULL )
     983             :     {
     984        8755 :         free( hCoreCoder->hNoiseEst );
     985        8755 :         hCoreCoder->hNoiseEst = NULL;
     986             :     }
     987             : 
     988        8836 :     if ( hCoreCoder->hVAD != NULL )
     989             :     {
     990        8686 :         free( hCoreCoder->hVAD );
     991        8686 :         hCoreCoder->hVAD = NULL;
     992             :     }
     993             : 
     994        8836 :     if ( hCoreCoder->hVAD_CLDFB != NULL )
     995             :     {
     996           3 :         free( hCoreCoder->hVAD_CLDFB );
     997           3 :         hCoreCoder->hVAD_CLDFB = NULL;
     998             :     }
     999             : 
    1000        8836 :     if ( hCoreCoder->hTdCngEnc != NULL )
    1001             :     {
    1002          88 :         free( hCoreCoder->hTdCngEnc );
    1003          88 :         hCoreCoder->hTdCngEnc = NULL;
    1004             :     }
    1005             : 
    1006        8836 :     if ( hCoreCoder->hDtxEnc != NULL )
    1007             :     {
    1008         491 :         free( hCoreCoder->hDtxEnc );
    1009         491 :         hCoreCoder->hDtxEnc = NULL;
    1010             :     }
    1011             : 
    1012        8836 :     if ( hCoreCoder->hSpMusClas != NULL )
    1013             :     {
    1014        8755 :         free( hCoreCoder->hSpMusClas );
    1015        8755 :         hCoreCoder->hSpMusClas = NULL;
    1016             :     }
    1017             : 
    1018        8836 :     if ( hCoreCoder->hGSCEnc != NULL )
    1019             :     {
    1020        3090 :         free( hCoreCoder->hGSCEnc );
    1021        3090 :         hCoreCoder->hGSCEnc = NULL;
    1022             :     }
    1023             : 
    1024        8836 :     if ( hCoreCoder->hSC_VBR != NULL )
    1025             :     {
    1026           3 :         free( hCoreCoder->hSC_VBR );
    1027           3 :         hCoreCoder->hSC_VBR = NULL;
    1028             :     }
    1029             : 
    1030        8836 :     if ( hCoreCoder->hAmrwb_IO != NULL )
    1031             :     {
    1032           3 :         free( hCoreCoder->hAmrwb_IO );
    1033           3 :         hCoreCoder->hAmrwb_IO = NULL;
    1034             :     }
    1035             : 
    1036        8836 :     if ( hCoreCoder->hBWE_TD != NULL )
    1037             :     {
    1038        3089 :         free( hCoreCoder->hBWE_TD );
    1039        3089 :         hCoreCoder->hBWE_TD = NULL;
    1040             :     }
    1041             : 
    1042        8836 :     if ( hCoreCoder->hBWE_FD != NULL )
    1043             :     {
    1044        3089 :         free( hCoreCoder->hBWE_FD );
    1045        3089 :         hCoreCoder->hBWE_FD = NULL;
    1046             :     }
    1047             : 
    1048        8836 :     if ( hCoreCoder->hRF != NULL )
    1049             :     {
    1050           3 :         free( hCoreCoder->hRF );
    1051           3 :         hCoreCoder->hRF = NULL;
    1052             :     }
    1053             : 
    1054        8836 :     if ( hCoreCoder->hTECEnc != NULL )
    1055             :     {
    1056           3 :         free( hCoreCoder->hTECEnc );
    1057           3 :         hCoreCoder->hTECEnc = NULL;
    1058             :     }
    1059             : 
    1060        8836 :     if ( hCoreCoder->hTcxEnc != NULL )
    1061             :     {
    1062        8685 :         free( hCoreCoder->hTcxEnc );
    1063        8685 :         hCoreCoder->hTcxEnc = NULL;
    1064             :     }
    1065             : 
    1066        8836 :     if ( hCoreCoder->hTcxCfg != NULL )
    1067             :     {
    1068        8685 :         free( hCoreCoder->hTcxCfg );
    1069        8685 :         hCoreCoder->hTcxCfg = NULL;
    1070             :     }
    1071             : 
    1072        8836 :     if ( hCoreCoder->hIGFEnc != NULL )
    1073             :     {
    1074        6684 :         free( hCoreCoder->hIGFEnc );
    1075        6684 :         hCoreCoder->hIGFEnc = NULL;
    1076             :     }
    1077             : 
    1078        8836 :     if ( hCoreCoder->hPlcExt != NULL )
    1079             :     {
    1080           3 :         free( hCoreCoder->hPlcExt );
    1081           3 :         hCoreCoder->hPlcExt = NULL;
    1082             :     }
    1083             : 
    1084        8836 :     if ( hCoreCoder->hHQ_core != NULL )
    1085             :     {
    1086        3109 :         free( hCoreCoder->hHQ_core );
    1087        3109 :         hCoreCoder->hHQ_core = NULL;
    1088             :     }
    1089             : 
    1090        8836 :     free( hCoreCoder );
    1091             : 
    1092        8836 :     return;
    1093             : }
    1094             : 
    1095             : 
    1096             : /*-------------------------------------------------------------------------
    1097             :  * ivas_destroy_enc()
    1098             :  *
    1099             :  * Close IVAS encoder handles
    1100             :  *-------------------------------------------------------------------------*/
    1101             : 
    1102         627 : void ivas_destroy_enc(
    1103             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    1104             : )
    1105             : {
    1106             :     int16_t i, n, nchan_inp;
    1107         627 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
    1108             : 
    1109             :     /* SCE handles */
    1110        3135 :     for ( i = 0; i < MAX_SCE; i++ )
    1111             :     {
    1112        2508 :         if ( st_ivas->hSCE[i] != NULL )
    1113             :         {
    1114         409 :             destroy_sce_enc( st_ivas->hSCE[i] );
    1115         409 :             st_ivas->hSCE[i] = NULL;
    1116             :         }
    1117             :     }
    1118             : 
    1119             :     /* CPE handles */
    1120        4389 :     for ( i = 0; i < MAX_CPE; i++ )
    1121             :     {
    1122        3762 :         if ( st_ivas->hCPE[i] != NULL )
    1123             :         {
    1124         748 :             destroy_cpe_enc( st_ivas->hCPE[i] );
    1125         748 :             st_ivas->hCPE[i] = NULL;
    1126             :         }
    1127             :     }
    1128             : 
    1129             :     /* HP20 filter handles */
    1130         627 :     if ( st_ivas->mem_hp20_in != NULL )
    1131             :     {
    1132         627 :         n = getNumChanAnalysis( st_ivas );
    1133             : 
    1134        3526 :         for ( i = 0; i < n; i++ )
    1135             :         {
    1136        2899 :             free( st_ivas->mem_hp20_in[i] );
    1137        2899 :             st_ivas->mem_hp20_in[i] = NULL;
    1138             :         }
    1139         627 :         free( st_ivas->mem_hp20_in );
    1140         627 :         st_ivas->mem_hp20_in = NULL;
    1141             :     }
    1142             : 
    1143             :     /* ISM metadata handles */
    1144         627 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    1145             : 
    1146             :     /* ISM DTX Handle */
    1147         627 :     if ( st_ivas->hISMDTX != NULL )
    1148             :     {
    1149          14 :         free( st_ivas->hISMDTX );
    1150          14 :         st_ivas->hISMDTX = NULL;
    1151             :     }
    1152             : 
    1153             :     /* Q Metadata handle */
    1154         627 :     ivas_qmetadata_close( &( st_ivas->hQMetaData ) );
    1155             : 
    1156             :     /* DirAC handle */
    1157         627 :     ivas_dirac_enc_close( &( st_ivas->hDirAC ), st_ivas->hEncoderConfig->input_Fs );
    1158             : 
    1159             :     /* ParamISM handle */
    1160         627 :     ivas_param_ism_enc_close( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
    1161             : 
    1162             :     /* SPAR handle */
    1163         627 :     ivas_spar_enc_close( &( st_ivas->hSpar ), st_ivas->hEncoderConfig->input_Fs, nchan_inp, 0 );
    1164             : 
    1165             :     /* MASA handle */
    1166         627 :     ivas_masa_enc_close( &( st_ivas->hMasa ) );
    1167             : 
    1168             :     /* MCT handle */
    1169         627 :     ivas_mct_enc_close( &( st_ivas->hMCT ) );
    1170             : 
    1171             :     /* LFE handle */
    1172         627 :     ivas_lfe_enc_close( &( st_ivas->hLFE ) );
    1173             : 
    1174             :     /* LFE low pass filter state */
    1175         627 :     ivas_lfe_lpf_enc_close( &( st_ivas->hLfeLpf ) );
    1176             : 
    1177             :     /* Param-Upmix MC handle */
    1178         627 :     ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
    1179             : 
    1180             :     /* Parametric MC handle */
    1181         627 :     ivas_param_mc_enc_close( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
    1182             : 
    1183             :     /* Multi-channel MASA handle */
    1184         627 :     ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
    1185             : 
    1186             :     /* OMASA handle */
    1187         627 :     ivas_omasa_enc_close( &( st_ivas->hOMasa ) );
    1188             : 
    1189             :     /* OSBA handle */
    1190         627 :     ivas_osba_enc_close( &( st_ivas->hOSba ) );
    1191             : 
    1192             :     /* Stereo downmix for EVS encoder handle */
    1193         627 :     stereo_dmx_evs_close_encoder( &( st_ivas->hStereoDmxEVS ) );
    1194             : 
    1195             :     /* Encoder configuration handle */
    1196         627 :     if ( st_ivas->hEncoderConfig != NULL )
    1197             :     {
    1198         627 :         free( st_ivas->hEncoderConfig );
    1199         627 :         st_ivas->hEncoderConfig = NULL;
    1200             :     }
    1201             : 
    1202             :     /* Buffer of indices */
    1203         627 :     if ( st_ivas->ind_list != NULL )
    1204             :     {
    1205         627 :         free( st_ivas->ind_list );
    1206             :     }
    1207             : 
    1208         627 :     if ( st_ivas->ind_list_metadata != NULL )
    1209             :     {
    1210         624 :         free( st_ivas->ind_list_metadata );
    1211             :     }
    1212             : 
    1213             :     /* floating-point input audio buffers */
    1214       13167 :     for ( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1215             :     {
    1216       12540 :         if ( st_ivas->p_data_f[n] != NULL )
    1217             :         {
    1218        3674 :             free( st_ivas->p_data_f[n] );
    1219        3674 :             st_ivas->p_data_f[n] = NULL;
    1220             :         }
    1221             :     }
    1222             : 
    1223             :     /* main IVAS handle */
    1224         627 :     free( st_ivas );
    1225             : 
    1226         627 :     return;
    1227             : }
    1228             : 
    1229             : /*-------------------------------------------------------------------------
    1230             :  * ivas_initialize_MD_bstr_enc()
    1231             :  *
    1232             :  * Allocate and initialize SCE/CPE MD bitstream handle
    1233             :  *-------------------------------------------------------------------------*/
    1234             : 
    1235        4354 : 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        4354 :     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        4354 :     hMetaData->ind_list = st_ivas->ind_list_metadata;
    1249        4354 :     hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1250        4354 :     hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata;
    1251        4354 :     hMetaData->st_ivas = st_ivas;
    1252             : 
    1253        4354 :     reset_indices_enc( hMetaData, st_ivas->ivas_max_num_indices_metadata );
    1254             : 
    1255        4354 :     *hMetaData_out = hMetaData;
    1256             : 
    1257        4354 :     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        9385 : void ivas_destroy_MD_bstr_enc(
    1268             :     BSTR_ENC_HANDLE *hMetaData /* i/o: encoder MD bitstream handle  */
    1269             : )
    1270             : {
    1271        9385 :     if ( hMetaData == NULL || *hMetaData == NULL )
    1272             :     {
    1273        5031 :         return;
    1274             :     }
    1275             : 
    1276        4354 :     free( *hMetaData );
    1277        4354 :     *hMetaData = NULL;
    1278             : 
    1279        4354 :     return;
    1280             : }

Generated by: LCOV version 1.14