LCOV - code coverage report
Current view: top level - lib_enc - ivas_init_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 422 484 87.2 %
Date: 2025-11-01 05:07:43 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.14