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

Generated by: LCOV version 1.14