LCOV - code coverage report
Current view: top level - lib_enc - ivas_init_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ b8e0d5b93229a45388410f93fd14fda7bdd8ab4a Lines: 523 584 89.6 %
Date: 2025-11-08 02:37:03 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_fx.h"
      38             : #include "ivas_prot_fx.h"
      39             : #include "ivas_stat_enc.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "wmc_auto.h"
      42             : #include "ivas_prot_fx.h"
      43             : #include "prot_fx_enc.h"
      44             : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
      45             : #include <string.h>
      46             : #endif
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * ivas_write_format()
      51             :  *
      52             :  * Write IVAS format signaling
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55      497679 : void ivas_write_format_fx(
      56             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure   */
      57             : )
      58             : {
      59             :     Word16 ind, nBits, extra_bits;
      60             : 
      61      497679 :     ind = 0;
      62      497679 :     move16();
      63             : 
      64      497679 :     nBits = IVAS_FORMAT_SIGNALING_NBITS;
      65      497679 :     move16();
      66             : 
      67      497679 :     extra_bits = sub( IVAS_FORMAT_SIGNALING_NBITS_EXTENDED, IVAS_FORMAT_SIGNALING_NBITS );
      68             : 
      69      497679 :     switch ( st_ivas->hEncoderConfig->ivas_format )
      70             :     {
      71       72486 :         case STEREO_FORMAT:
      72       72486 :             ind = 0;
      73       72486 :             move16();
      74       72486 :             BREAK;
      75      103826 :         case ISM_FORMAT:
      76      103826 :             ind = 2;
      77      103826 :             move16();
      78      103826 :             IF( GE_32( st_ivas->hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
      79             :             {
      80       95106 :                 ind = 4;
      81       95106 :                 move16();
      82             : 
      83       95106 :                 nBits = add( nBits, extra_bits ); /* Q0 */
      84             :             }
      85      103826 :             BREAK;
      86       48850 :         case MC_FORMAT:
      87       48850 :             ind = 1;
      88       48850 :             move16();
      89       48850 :             BREAK;
      90      124300 :         case SBA_FORMAT:
      91      124300 :             ind = 6;
      92      124300 :             move16();
      93             : 
      94      124300 :             nBits = add( nBits, extra_bits ); /* Q0 */
      95      124300 :             BREAK;
      96       72778 :         case MASA_FORMAT:
      97       72778 :             ind = 7;
      98       72778 :             move16();
      99             : 
     100       72778 :             nBits = add( nBits, extra_bits ); /* Q0 */
     101       72778 :             BREAK;
     102       38439 :         case MASA_ISM_FORMAT:
     103       38439 :             IF( st_ivas->ism_mode == ISM_MODE_NONE )
     104             :             {
     105        8375 :                 ind = 7; /* send MASA format */
     106        8375 :                 move16();
     107        8375 :                 nBits = add( nBits, extra_bits ); /* Q0 */
     108             :             }
     109             :             ELSE
     110             :             {
     111       30064 :                 ind = 10;
     112       30064 :                 move16();
     113       30064 :                 nBits = add( nBits, add( extra_bits, IVAS_COMBINED_FORMAT_SIGNALLING_BITS ) ); /* Q0 */
     114             :             }
     115       38439 :             BREAK;
     116       37000 :         case SBA_ISM_FORMAT:
     117       37000 :             IF( LT_32( st_ivas->hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
     118             :             {
     119        2881 :                 ind = 6; /* send SBA format */
     120        2881 :                 move16();
     121        2881 :                 nBits = add( nBits, extra_bits ); /* Q0 */
     122             :             }
     123             :             ELSE
     124             :             {
     125       34119 :                 ind = 11; /* 1011 */
     126       34119 :                 move16();
     127       34119 :                 nBits = add( nBits, add( extra_bits, IVAS_COMBINED_FORMAT_SIGNALLING_BITS ) ); /* Q0 */
     128             :             }
     129       37000 :             BREAK;
     130           0 :         default:
     131           0 :             assert( !"Invalid format. Aborting." );
     132             :             BREAK;
     133             :     }
     134             : 
     135      497679 :     IF( st_ivas->hSCE[0] != NULL )
     136             :     {
     137      219939 :         push_indice( st_ivas->hSCE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     138             :     }
     139      277740 :     ELSE IF( st_ivas->hCPE[0] != NULL )
     140             :     {
     141      277740 :         push_indice( st_ivas->hCPE[0]->hCoreCoder[0]->hBstr, IND_IVAS_FORMAT, ind, nBits );
     142             :     }
     143             : 
     144      497679 :     return;
     145             : }
     146             : 
     147             : 
     148             : /*-------------------------------------------------------------------*
     149             :  * ivas_write_format_sid()
     150             :  *
     151             :  * Write IVAS format signaling in SID frames
     152             :  *-------------------------------------------------------------------*/
     153             : 
     154        2997 : void ivas_write_format_sid_fx(
     155             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format               */
     156             :     const Word16 element_mode,     /* i  : element bitrate           Q0*/
     157             :     BSTR_ENC_HANDLE hBstr,         /* i/o: encoder bitstream handle  */
     158             :     const Word16 sba_order,        /* i  : Ambisonic (SBA) order     */
     159             :     const Word16 sba_planar        /* i  : SBA planar flag           */
     160             : )
     161             : {
     162        2997 :     Word16 ind = 0; /* to avoid compilation warning */
     163        2997 :     move16();
     164             : 
     165        2997 :     SWITCH( ivas_format )
     166             :     {
     167        1869 :         case STEREO_FORMAT:
     168        1869 :             IF( EQ_16( element_mode, IVAS_CPE_MDCT ) )
     169             :             {
     170         410 :                 ind = SID_MDCT_STEREO;
     171         410 :                 move16();
     172             :             }
     173        1459 :             ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
     174             :             {
     175        1459 :                 ind = SID_DFT_STEREO;
     176        1459 :                 move16();
     177             :             }
     178             :             ELSE
     179             :             {
     180           0 :                 assert( !"Wrong stereo mode for SID format signaling" );
     181             :             }
     182        1869 :             BREAK;
     183         542 :         case ISM_FORMAT:
     184         542 :             ind = SID_ISM;
     185         542 :             move16();
     186         542 :             BREAK;
     187         392 :         case SBA_FORMAT:
     188         392 :             SWITCH( element_mode )
     189             :             {
     190         261 :                 case IVAS_SCE:
     191         261 :                     ind = SID_SBA_1TC;
     192         261 :                     move16();
     193         261 :                     BREAK;
     194         131 :                 case IVAS_CPE_MDCT:
     195         131 :                     ind = SID_SBA_2TC;
     196         131 :                     move16();
     197         131 :                     BREAK;
     198           0 :                 default:
     199           0 :                     assert( !"Wrong element mode for SBA DTX!" );
     200             :                     BREAK;
     201             :             }
     202         392 :             BREAK;
     203         194 :         case MASA_FORMAT:
     204         194 :             IF( EQ_16( element_mode, IVAS_SCE ) )
     205             :             {
     206         120 :                 ind = SID_MASA_1TC;
     207         120 :                 move16();
     208             :             }
     209             :             ELSE
     210             :             {
     211          74 :                 ind = SID_MASA_2TC;
     212          74 :                 move16();
     213             :             }
     214         194 :             BREAK;
     215           0 :         default:
     216           0 :             assert( !"Reserved SID format symbol written." );
     217             :             BREAK;
     218             :     }
     219             : 
     220        2997 :     push_indice( hBstr, IND_IVAS_FORMAT, ind, SID_FORMAT_NBITS );
     221             : 
     222        2997 :     IF( EQ_32( ivas_format, SBA_FORMAT ) )
     223             :     {
     224             :         /* Write SBA planar flag */
     225         392 :         push_indice( hBstr, IND_SMODE, sba_planar, SBA_PLANAR_BITS );
     226             : 
     227             :         /* Write SBA order */
     228         392 :         push_indice( hBstr, IND_SMODE, sba_order, SBA_ORDER_BITS );
     229             :     }
     230             : 
     231        2997 :     return;
     232             : }
     233             : 
     234             : 
     235             : /*-------------------------------------------------------------------*
     236             :  * getNumChanAnalysis()
     237             :  *
     238             :  * get number of input channels used for analysis/coding
     239             :  *-------------------------------------------------------------------*/
     240             : 
     241             : /*! r: number of channels to be analysed */
     242      494136 : Word16 getNumChanAnalysis_fx(
     243             :     Encoder_Struct *st_ivas /* i  : IVAS encoder structure              */
     244             : )
     245             : {
     246             :     Word16 n;
     247             : 
     248      494136 :     n = add( st_ivas->nSCE, CPE_CHANNELS * st_ivas->nCPE ); /* Q0 */
     249      494136 :     test();
     250      494136 :     test();
     251      494136 :     test();
     252      494136 :     test();
     253      494136 :     IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_FORMAT ) )
     254             :     {
     255      124790 :         n = imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ); /* Q0 */
     256             :     }
     257      369346 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MC_FORMAT ) && ( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) || EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) ) )
     258             :     {
     259       22012 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     260       22012 :         move16();
     261             :     }
     262      347334 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
     263             :     {
     264         800 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     265         800 :         move16();
     266             :     }
     267      346534 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     268             :     {
     269       20754 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     270       20754 :         move16();
     271             :     }
     272      325780 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
     273             :     {
     274       33728 :         n = st_ivas->hEncoderConfig->nchan_inp; /* Q0 */
     275       33728 :         move16();
     276             :     }
     277      292052 :     ELSE IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
     278             :     {
     279       37074 :         n = add( st_ivas->hEncoderConfig->nchan_ism, imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ) ); /* Q0 */
     280             :     }
     281             : 
     282      494136 :     return n;
     283             : }
     284             : 
     285             : 
     286             : /*-------------------------------------------------------------------*
     287             :  * copy_encoder_config_fx()
     288             :  *
     289             :  * Copy configuration structrue to the state structrure
     290             :  *-------------------------------------------------------------------*/
     291             : 
     292       34847 : void copy_encoder_config_fx(
     293             :     Encoder_Struct *st_ivas, /* i  : IVAS encoder structure                                     */
     294             :     Encoder_State *st_fx,    /* o  : encoder state structure                            */
     295             :     const Word16 flag_all    /* i  : flag 1==update all, 0=partial update       Q0*/
     296             : )
     297             : {
     298       34847 :     IF( flag_all )
     299             :     {
     300       11343 :         st_fx->input_Fs = st_ivas->hEncoderConfig->input_Fs; /* Q0 */
     301       11343 :         move32();
     302             : 
     303       11343 :         st_fx->last_codec_mode = st_ivas->last_codec_mode; /* Q0 */
     304       11343 :         move16();
     305       11343 :         st_fx->last_total_brate = st_ivas->hEncoderConfig->last_ivas_total_brate; /* Q0 */
     306       11343 :         move32();
     307             : 
     308       11343 :         st_fx->Opt_DTX_ON = st_ivas->hEncoderConfig->Opt_DTX_ON; /* Q0 */
     309       11343 :         move16();
     310             : 
     311       11343 :         st_fx->last_Opt_SC_VBR = st_ivas->hEncoderConfig->last_Opt_SC_VBR; /* Q0 */
     312       11343 :         move16();
     313             :     }
     314             : 
     315       34847 :     st_fx->Opt_AMR_WB = st_ivas->hEncoderConfig->Opt_AMR_WB; /* Q0 */
     316       34847 :     move16();
     317       34847 :     st_fx->Opt_SC_VBR = st_ivas->hEncoderConfig->Opt_SC_VBR; /* Q0 */
     318       34847 :     move16();
     319             : 
     320       34847 :     st_fx->codec_mode = st_ivas->codec_mode; /* Q0 */
     321       34847 :     move16();
     322       34847 :     st_fx->max_bwidth = st_ivas->hEncoderConfig->max_bwidth; /* Q0 */
     323       34847 :     move16();
     324             : 
     325       34847 :     st_fx->Opt_RF_ON = st_ivas->hEncoderConfig->Opt_RF_ON; /* Q0 */
     326       34847 :     move16();
     327       34847 :     st_fx->rf_fec_offset = st_ivas->hEncoderConfig->rf_fec_offset; /* Q0 */
     328       34847 :     move16();
     329       34847 :     st_fx->rf_fec_indicator = st_ivas->hEncoderConfig->rf_fec_indicator; /* Q0 */
     330       34847 :     move16();
     331             : 
     332             : #ifdef DEBUGGING
     333             :     st_fx->force = st_ivas->hEncoderConfig->force;
     334             : #ifdef DEBUG_FORCE_DIR
     335             :     st_fx->force_dir = st_ivas->hEncoderConfig->force_dir;
     336             : #endif
     337             : #endif
     338       34847 :     st_fx->element_mode = st_ivas->hEncoderConfig->element_mode_init; /* Q0 */
     339       34847 :     move16();
     340             : 
     341       34847 :     return;
     342             : }
     343             : 
     344             : 
     345             : /*-------------------------------------------------------------------------
     346             :  * ivas_initialize_handles_enc_fx()
     347             :  *
     348             :  * NULL initialization of handles
     349             :  *-------------------------------------------------------------------------*/
     350             : 
     351         623 : void ivas_initialize_handles_enc_fx(
     352             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure                  */
     353             : )
     354             : {
     355             :     Word16 i;
     356             : 
     357        3115 :     FOR( i = 0; i < MAX_SCE; i++ )
     358             :     {
     359        2492 :         st_ivas->hSCE[i] = NULL;
     360             :     }
     361             : 
     362        4361 :     FOR( i = 0; i < MAX_CPE; i++ )
     363             :     {
     364        3738 :         st_ivas->hCPE[i] = NULL;
     365             :     }
     366             : 
     367         623 :     st_ivas->mem_hp20_in_fx = NULL;
     368             : 
     369             :     /* ISM metadata handles */
     370        3115 :     FOR( i = 0; i < MAX_NUM_OBJECTS; i++ )
     371             :     {
     372        2492 :         st_ivas->hIsmMetaData[i] = NULL;
     373             :     }
     374             : 
     375             :     /* ISM DTX handle */
     376         623 :     st_ivas->hISMDTX = NULL;
     377             : 
     378             :     /* Q Metadata handle */
     379         623 :     st_ivas->hQMetaData = NULL;
     380             : 
     381             :     /* DirAC handle */
     382         623 :     st_ivas->hDirAC = NULL;
     383             : 
     384             :     /* ParamISM handle */
     385         623 :     st_ivas->hParamIsm = NULL;
     386             :     /* SPAR handle */
     387         623 :     st_ivas->hSpar = NULL;
     388             : 
     389             :     /* MASA encoder handle */
     390         623 :     st_ivas->hMasa = NULL;
     391             : 
     392             :     /* MCT handle */
     393         623 :     st_ivas->hMCT = NULL;
     394             : 
     395             :     /* MC Param-Upmix handle */
     396         623 :     st_ivas->hMCParamUpmix = NULL;
     397             : 
     398             :     /* Parametric MC handle */
     399         623 :     st_ivas->hParamMC = NULL;
     400             : 
     401             :     /* Multi-channel MASA handle */
     402         623 :     st_ivas->hMcMasa = NULL;
     403             : 
     404             :     /* Stereo downmix for EVS encoder handle */
     405         623 :     st_ivas->hStereoDmxEVS = NULL;
     406             : 
     407             :     /* LFE  handle */
     408         623 :     st_ivas->hLFE = NULL;
     409             : 
     410             :     /* LFE low pass filter handle */
     411         623 :     st_ivas->hLfeLpf = NULL;
     412             : 
     413             :     /* Object MASA handle */
     414         623 :     st_ivas->hOMasa = NULL;
     415             : 
     416             :     /* OSBA handle */
     417         623 :     st_ivas->hOSba = NULL;
     418             : 
     419         623 :     return;
     420             : }
     421             : 
     422             : 
     423             : /*-------------------------------------------------------------------*
     424             :  * ivas_init_encoder()
     425             :  *
     426             :  * Initialize IVAS encoder state structure
     427             :  *-------------------------------------------------------------------*/
     428             : 
     429         623 : ivas_error ivas_init_encoder_fx(
     430             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
     431             : )
     432             : {
     433             :     Word16 i, n;
     434             :     Word16 nchan_inp_buff;
     435             :     Word16 sce_id, cpe_id;
     436             :     IVAS_FORMAT ivas_format;
     437             :     Word32 input_Fs, ivas_total_brate;
     438             :     Word32 element_brate_tmp[MAX_NUM_OBJECTS];
     439             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     440             :     ivas_error error;
     441             :     Word32 tmp_br, tmp32;
     442             : 
     443         623 :     error = IVAS_ERR_OK;
     444         623 :     move32();
     445             : 
     446         623 :     hEncoderConfig = st_ivas->hEncoderConfig;
     447         623 :     ivas_format = hEncoderConfig->ivas_format;
     448         623 :     move32();
     449         623 :     input_Fs = hEncoderConfig->input_Fs; /* Q0 */
     450         623 :     move32();
     451         623 :     ivas_total_brate = hEncoderConfig->ivas_total_brate; /* Q0 */
     452         623 :     move32();
     453             : 
     454         623 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate; /* Q0 */
     455         623 :     move32();
     456             : 
     457         623 :     if ( NE_16( ivas_format, MONO_FORMAT ) )
     458             :     {
     459             :         /* In IVAS, ensure that minimum coded bandwidth is WB */
     460         620 :         hEncoderConfig->max_bwidth = s_max( hEncoderConfig->max_bwidth, WB ); /* Q0 */
     461         620 :         move16();
     462             :     }
     463         623 :     st_ivas->ism_mode = ISM_MODE_NONE;
     464         623 :     move32();
     465         623 :     st_ivas->mc_mode = MC_MODE_NONE;
     466         623 :     move32();
     467             : 
     468         623 :     st_ivas->nchan_transport = -1;
     469         623 :     move16();
     470             : 
     471             :     /*-----------------------------------------------------------------*
     472             :      * Allocate input audio buffers
     473             :      *-----------------------------------------------------------------*/
     474             : 
     475         623 :     nchan_inp_buff = hEncoderConfig->nchan_inp; /* Q0 */
     476         623 :     move16();
     477         623 :     IF( EQ_16( ivas_format, MONO_FORMAT ) )
     478             :     {
     479           3 :         nchan_inp_buff = 0;
     480           3 :         move16();
     481             :     }
     482         620 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
     483             :     {
     484          39 :         IF( EQ_16( sub( hEncoderConfig->nchan_inp, hEncoderConfig->nchan_ism ), 1 ) ) /* mono is duplicated in monoMASA */
     485             :         {
     486          10 :             nchan_inp_buff = add( nchan_inp_buff, 1 ); /* Q0 */
     487             :         }
     488             : 
     489          39 :         nchan_inp_buff = add( nchan_inp_buff, 1 ); /* for *data_separated_object        Q0*/
     490             :     }
     491             : 
     492        4280 :     FOR( n = 0; n < nchan_inp_buff; n++ )
     493             :     {
     494             :         /* note: these are intra-frame heap memories */
     495        3657 :         IF( ( st_ivas->p_data_fx[n] = (Word32 *) malloc( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) * sizeof( Word32 ) ) ) == NULL )
     496             :         {
     497           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for input audio buffer!\n" ) );
     498             :         }
     499        3657 :         set32_fx( st_ivas->p_data_fx[n], 0, extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) ) );
     500             :     }
     501         623 :     st_ivas->q_data_fx = Q11;
     502         623 :     move16();
     503        9426 :     FOR( ; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     504             :     {
     505        8803 :         st_ivas->p_data_fx[n] = NULL;
     506             :     }
     507             : 
     508             : 
     509             :     /*-----------------------------------------------------------------*
     510             :      * Allocate and initialize buffer of indices
     511             :      *-----------------------------------------------------------------*/
     512             : 
     513             :     /* set the maximum allowed number of indices in the list */
     514         623 :     st_ivas->ivas_max_num_indices = get_ivas_max_num_indices_fx( ivas_format, ivas_total_brate ); /* Q0 */
     515         623 :     move16();
     516             : 
     517             :     /* allocate buffer of indices */
     518         623 :     IF( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
     519             :     {
     520           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
     521             :     }
     522             : 
     523             :     /* reset the list of indices */
     524      494333 :     FOR( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     525             :     {
     526      493710 :         st_ivas->ind_list[i].nb_bits = -1;
     527      493710 :         move16();
     528             :     }
     529             : 
     530             : #ifdef BITSTERAM_ANALYSIS
     531             :     for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
     532             :     {
     533             :         memset( st_ivas->ind_list[i].function_name, 'A', 100 * sizeof( char ) );
     534             :     }
     535             : #endif
     536             : 
     537             :     /* set the maximum allowed number of metadata indices in the list */
     538         623 :     st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata_fx( st_ivas->hEncoderConfig->ivas_format, st_ivas->hEncoderConfig->ivas_total_brate ); /* Q0 */
     539         623 :     move16();
     540             :     /* allocate buffer of metadata indices */
     541         623 :     IF( st_ivas->ivas_max_num_indices_metadata > 0 )
     542             :     {
     543         620 :         IF( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
     544             :         {
     545           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
     546             :         }
     547             : 
     548             :         /* reset the list of metadata indices */
     549      248240 :         FOR( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
     550             :         {
     551      247620 :             st_ivas->ind_list_metadata[i].nb_bits = -1;
     552      247620 :             move16();
     553             :         }
     554             :     }
     555             :     ELSE
     556             :     {
     557           3 :         st_ivas->ind_list_metadata = NULL;
     558             :     }
     559             : 
     560             :     /*-----------------------------------------------------------------*
     561             :      * Allocate and initialize SCE/CPE and other handles
     562             :      *-----------------------------------------------------------------*/
     563             : 
     564         623 :     IF( EQ_32( ivas_format, MONO_FORMAT ) )
     565             :     {
     566           3 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
     567           3 :         move16();
     568           3 :         st_ivas->nCPE = 0;
     569           3 :         move16();
     570           3 :         st_ivas->nchan_transport = 1;
     571           3 :         move16();
     572           3 :         sce_id = 0;
     573           3 :         move16();
     574             : 
     575           3 :         test();
     576           3 :         IF( NE_32( ( error = create_sce_enc_fx( st_ivas, sce_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
     577             :         {
     578           0 :             return error;
     579             :         }
     580             : 
     581             :         /* prepare stereo downmix for EVS */
     582           3 :         IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
     583             :         {
     584           2 :             IF( ( error = stereo_dmx_evs_init_encoder_fx( &( st_ivas->hStereoDmxEVS ), input_Fs ) ) != IVAS_ERR_OK )
     585             :             {
     586           0 :                 return error;
     587             :             }
     588             :         }
     589             :     }
     590         620 :     ELSE IF( EQ_32( ivas_format, STEREO_FORMAT ) )
     591             :     {
     592          68 :         st_ivas->nSCE = 0;
     593          68 :         move16();
     594          68 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
     595          68 :         move16();
     596          68 :         st_ivas->nchan_transport = CPE_CHANNELS;
     597          68 :         move16();
     598          68 :         cpe_id = 0;
     599          68 :         move16();
     600             : 
     601          68 :         IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
     602             :         {
     603           0 :             return error;
     604             :         }
     605             :     }
     606         552 :     ELSE IF( EQ_32( ivas_format, ISM_FORMAT ) )
     607             :     {
     608          74 :         st_ivas->ism_mode = ivas_ism_mode_select( hEncoderConfig->nchan_inp, ivas_total_brate ); /* Q0 */
     609          74 :         move32();
     610             : 
     611          74 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_inp, element_brate_tmp ) ) != IVAS_ERR_OK )
     612             :         {
     613           0 :             return error;
     614             :         }
     615             : 
     616         261 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     617             :         {
     618         187 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
     619             :             {
     620           0 :                 return error;
     621             :             }
     622             :         }
     623             : 
     624          74 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     625             :         {
     626          17 :             IF( ( error = ivas_param_ism_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     627             :             {
     628           0 :                 return error;
     629             :             }
     630             :         }
     631             : 
     632          74 :         IF( st_ivas->hEncoderConfig->Opt_DTX_ON )
     633             :         {
     634          14 :             IF( ( error = ivas_ism_dtx_open( st_ivas ) ) != IVAS_ERR_OK )
     635             :             {
     636           0 :                 return error;
     637             :             }
     638             :         }
     639             :     }
     640         478 :     ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, MASA_FORMAT ) )
     641             :     {
     642         320 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     643             :         {
     644           0 :             return error;
     645             :         }
     646             : 
     647         320 :         IF( EQ_32( ivas_format, SBA_FORMAT ) )
     648             :         {
     649         245 :             st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order ); /* Q0 */
     650         245 :             move16();
     651             : 
     652         245 :             IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
     653             :             {
     654           0 :                 return error;
     655             :             }
     656         245 :             IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     657             :             {
     658           0 :                 return error;
     659             :             }
     660             :         }
     661             :         ELSE
     662             :         {
     663          75 :             st_ivas->nchan_transport = hEncoderConfig->nchan_inp; /* Q0 */
     664          75 :             move16();
     665             : 
     666          75 :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     667             :             {
     668           0 :                 return error;
     669             :             }
     670             :         }
     671             : 
     672         320 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
     673         450 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     674             :         {
     675         130 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, tmp_br ) ) != IVAS_ERR_OK )
     676             :             {
     677           0 :                 return error;
     678             :             }
     679             : 
     680         130 :             test();
     681         130 :             IF( EQ_16( ivas_format, SBA_FORMAT ) && st_ivas->hEncoderConfig->Opt_DTX_ON )
     682             :             {
     683          45 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->dtx_sce_sba = 1;
     684          45 :                 move16();
     685             :             }
     686             :         }
     687             : 
     688         320 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
     689         320 :         tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
     690         594 :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     691             :         {
     692         274 :             IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     693             :             {
     694           0 :                 return error;
     695             :             }
     696             : 
     697         822 :             FOR( n = 0; n < CPE_CHANNELS; n++ )
     698             :             {
     699         548 :                 if ( hEncoderConfig->Opt_DTX_ON )
     700             :                 {
     701          56 :                     st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
     702          56 :                     move16();
     703             :                 }
     704             :             }
     705             :         }
     706             : 
     707         320 :         IF( GT_16( st_ivas->nCPE, 1 ) )
     708             :         {
     709          84 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     710             :             {
     711           0 :                 return error;
     712             :             }
     713             :         }
     714             :     }
     715         158 :     ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
     716             :     {
     717             :         Word32 ism_total_brate;
     718             :         Word16 k;
     719             : 
     720          39 :         st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( ivas_total_brate, hEncoderConfig->nchan_ism ); /* Q0 */
     721          39 :         move32();
     722          39 :         st_ivas->nchan_transport = 2;
     723          39 :         move16();
     724             : 
     725          39 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     726             :         {
     727           0 :             return error;
     728             :         }
     729             : 
     730          39 :         k = 0;
     731          39 :         move16();
     732          39 :         test();
     733         323 :         WHILE( LT_16( k, SIZE_IVAS_BRATE_TBL ) && NE_32( ivas_total_brate, ivas_brate_tbl[k] ) )
     734             :         {
     735         284 :             k++;
     736             :         }
     737             : 
     738          39 :         ism_total_brate = 0;
     739          39 :         move32();
     740         101 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     741             :         {
     742          62 :             ism_total_brate = L_add( ism_total_brate, sep_object_brate[k - 2][st_ivas->nSCE - 1] ); /* Q0 */
     743          62 :             IF( ( error = create_sce_enc_fx( st_ivas, sce_id, sep_object_brate[k - 2][st_ivas->nSCE - 1] ) ) != IVAS_ERR_OK )
     744             :             {
     745           0 :                 return error;
     746             :             }
     747             :         }
     748             : 
     749          39 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     750             :         {
     751           0 :             return error;
     752             :         }
     753             : 
     754          39 :         IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     755             :         {
     756           0 :             return error;
     757             :         }
     758             : 
     759          39 :         IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     760             :         {
     761          22 :             IF( ( error = ivas_omasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     762             :             {
     763           0 :                 return error;
     764             :             }
     765             :         }
     766             : 
     767          39 :         IF( GE_32( L_sub( ivas_total_brate, ism_total_brate ), MIN_BRATE_MDCT_STEREO ) )
     768             :         {
     769          14 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     770          14 :             move16();
     771             :         }
     772             :         ELSE
     773             :         {
     774          25 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     775          25 :             move16();
     776             :         }
     777             : 
     778          39 :         IF( ( error = create_cpe_enc_fx( st_ivas, 0, L_sub( ivas_total_brate, ism_total_brate ) ) ) != IVAS_ERR_OK )
     779             :         {
     780           0 :             return error;
     781             :         }
     782             :     }
     783         119 :     ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
     784             :     {
     785          37 :         st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->hEncoderConfig->nchan_ism );
     786             : 
     787          37 :         IF( ( error = ivas_ism_metadata_enc_create_fx( st_ivas, hEncoderConfig->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
     788             :         {
     789           0 :             return error;
     790             :         }
     791             : 
     792             :         /* allocate and initialize SBA handles */
     793          37 :         IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     794             :         {
     795           0 :             return error;
     796             :         }
     797             : 
     798          37 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->hEncoderConfig->sba_order ); /* Q0 */
     799             : 
     800          37 :         IF( ( error = ivas_spar_enc_open_fx( st_ivas, 0 ) ) != IVAS_ERR_OK )
     801             :         {
     802           0 :             return error;
     803             :         }
     804             : 
     805          37 :         IF( ( error = ivas_dirac_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     806             :         {
     807           0 :             return error;
     808             :         }
     809             : 
     810          37 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_NONE ) )
     811             :         {
     812             :             /* allocate and initialize SBA core-coders */
     813          18 :             IF( EQ_16( st_ivas->nchan_transport, 1 ) )
     814             :             {
     815          12 :                 IF( ( error = create_sce_enc_fx( st_ivas, 0, ivas_total_brate ) ) != IVAS_ERR_OK )
     816             :                 {
     817           0 :                     return error;
     818             :                 }
     819             :             }
     820             : 
     821          18 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
     822          18 :             tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
     823          25 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     824             :             {
     825           7 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     826             :                 {
     827           0 :                     return error;
     828             :                 }
     829             :             }
     830             : 
     831          18 :             IF( GT_16( st_ivas->nCPE, 1 ) )
     832             :             {
     833           1 :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     834             :                 {
     835           0 :                     return error;
     836             :                 }
     837             :             }
     838             :         }
     839             :         ELSE
     840             :         {
     841             :             /* allocate and initialize MCT core coder */
     842          19 :             Word16 n_all = add( st_ivas->nchan_transport, st_ivas->hEncoderConfig->nchan_ism );
     843          19 :             st_ivas->nCPE = shr_r( n_all, 1 );
     844             : 
     845          19 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
     846          19 :             tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
     847          86 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     848             :             {
     849          67 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     850             :                 {
     851           0 :                     return error;
     852             :                 }
     853             :             }
     854             : 
     855          19 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     856             :             {
     857           0 :                 return error;
     858             :             }
     859             :         }
     860             : 
     861          37 :         IF( ( error = ivas_osba_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     862             :         {
     863           0 :             return error;
     864             :         }
     865             :     }
     866          82 :     ELSE IF( EQ_32( ivas_format, MC_FORMAT ) )
     867             :     {
     868          82 :         st_ivas->mc_mode = ivas_mc_mode_select_fx( hEncoderConfig->mc_input_setup, ivas_total_brate ); /* Q0 */
     869          82 :         move32();
     870             : 
     871          82 :         IF( ( error = ivas_create_lfe_lpf_enc_fx( &st_ivas->hLfeLpf, input_Fs ) ) != IVAS_ERR_OK )
     872             :         {
     873           0 :             return error;
     874             :         }
     875             : 
     876          82 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
     877             :         {
     878          36 :             st_ivas->nSCE = 0;
     879          36 :             move16();
     880          36 :             st_ivas->nCPE = shr( hEncoderConfig->nchan_inp, CPE_CHANNELS_LOG2 ); /* Q0 */
     881          36 :             move16();
     882             : 
     883          36 :             iDiv_and_mod_32( ivas_total_brate, sub( hEncoderConfig->nchan_inp, 1 ), &tmp_br, &tmp32, 0 );
     884          36 :             tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
     885         181 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     886             :             {
     887         145 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     888             :                 {
     889           0 :                     return error;
     890             :                 }
     891             :             }
     892             : 
     893          36 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     894             :             {
     895           0 :                 return error;
     896             :             }
     897             : 
     898          36 :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     899             :             {
     900           0 :                 return error;
     901             :             }
     902             : 
     903          36 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup ); /* Q0 */
     904          36 :             move16();
     905             :         }
     906          46 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
     907             :         {
     908           5 :             st_ivas->nSCE = 0;
     909           5 :             move16();
     910           5 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS >> 1;
     911           5 :             move16();
     912           5 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     913           5 :             move16();
     914             : 
     915           5 :             IF( ( error = ivas_mc_paramupmix_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     916             :             {
     917           0 :                 return error;
     918             :             }
     919             : 
     920           5 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nCPE, &tmp_br, &tmp32, 0 );
     921          25 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     922             :             {
     923          20 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     924             :                 {
     925           0 :                     return error;
     926             :                 }
     927             :             }
     928             : 
     929           5 :             IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     930             :             {
     931           0 :                 return error;
     932             :             }
     933             : 
     934           5 :             IF( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, input_Fs ) ) != IVAS_ERR_OK )
     935             :             {
     936           0 :                 return error;
     937             :             }
     938             :         }
     939          41 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
     940             :         {
     941          10 :             IF( ( error = ivas_param_mc_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     942             :             {
     943           0 :                 return error;
     944             :             }
     945             : 
     946          10 :             iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nCPE, st_ivas->nSCE ), &tmp_br, &tmp32, 0 );
     947          20 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     948             :             {
     949          10 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, tmp_br ) ) != IVAS_ERR_OK )
     950             :                 {
     951           0 :                     return error;
     952             :                 }
     953             :             }
     954             : 
     955          10 :             IF( GT_16( st_ivas->nCPE, 1 ) )
     956             :             {
     957           0 :                 IF( ( error = create_mct_enc_fx( st_ivas ) ) != IVAS_ERR_OK )
     958             :                 {
     959           0 :                     return error;
     960             :                 }
     961             :             }
     962             :         }
     963          31 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
     964             :         {
     965             :             Word32 brate_sce, brate_cpe;
     966             : 
     967          31 :             ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( hEncoderConfig->element_mode_init ), ivas_total_brate );
     968             : 
     969          31 :             IF( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     970             :             {
     971           0 :                 return error;
     972             :             }
     973             : 
     974          31 :             IF( ( error = ivas_masa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     975             :             {
     976           0 :                 return error;
     977             :             }
     978             : 
     979          31 :             IF( ( error = ivas_mcmasa_enc_open_fx( st_ivas ) ) != IVAS_ERR_OK )
     980             :             {
     981           0 :                 return error;
     982             :             }
     983             : 
     984          31 :             ivas_mcmasa_split_brate_fx( st_ivas->hMcMasa->separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
     985             : 
     986          60 :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
     987             :             {
     988          29 :                 IF( ( error = create_sce_enc_fx( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
     989             :                 {
     990           0 :                     return error;
     991             :                 }
     992             :             }
     993             : 
     994          36 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     995             :             {
     996           5 :                 hEncoderConfig->element_mode_init = IVAS_CPE_MDCT; /* Q0 */
     997           5 :                 move16();
     998             : 
     999           5 :                 IF( ( error = create_cpe_enc_fx( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
    1000             :                 {
    1001           0 :                     return error;
    1002             :                 }
    1003             :             }
    1004             :         }
    1005             :     }
    1006             : 
    1007             :     /*-----------------------------------------------------------------*
    1008             :      * Allocate and initialize HP20 filter memories
    1009             :      *-----------------------------------------------------------------*/
    1010             : 
    1011             :     /* set number of input channels used for analysis/coding */
    1012         623 :     n = getNumChanAnalysis_fx( st_ivas ); /* Q0 */
    1013         623 :     move16();
    1014             : 
    1015         623 :     IF( n > 0 )
    1016             :     {
    1017         623 :         IF( ( st_ivas->mem_hp20_in_fx = (Word32 **) malloc( n * sizeof( Word32 * ) ) ) == NULL )
    1018             :         {
    1019           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1020             :         }
    1021             :     }
    1022             :     ELSE
    1023             :     {
    1024           0 :         st_ivas->mem_hp20_in_fx = NULL;
    1025             :     }
    1026             : 
    1027        3377 :     FOR( i = 0; i < n; i++ )
    1028             :     {
    1029        2754 :         IF( ( st_ivas->mem_hp20_in_fx[i] = (Word32 *) malloc( ( L_HP20_MEM + 2 ) * sizeof( Word32 ) ) ) == NULL )
    1030             :         {
    1031           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    1032             :         }
    1033             : 
    1034        2754 :         set32_fx( st_ivas->mem_hp20_in_fx[i], 0, L_HP20_MEM + 2 );
    1035             :     }
    1036             : 
    1037         623 :     return error;
    1038             : }
    1039             : 
    1040             : 
    1041             : /*-------------------------------------------------------------------------
    1042             :  * destroy_core_enc()
    1043             :  *
    1044             :  * Close core encoder handles
    1045             :  *-------------------------------------------------------------------------*/
    1046             : 
    1047       11343 : void destroy_core_enc_fx(
    1048             :     ENC_CORE_HANDLE hCoreCoder /* i/o: core encoder structure      */
    1049             : )
    1050             : {
    1051       11343 :     destroy_cldfb_encoder_fx( hCoreCoder );
    1052             : 
    1053       11343 :     IF( hCoreCoder->hSignalBuf != NULL )
    1054             :     {
    1055       11274 :         free( hCoreCoder->hSignalBuf );
    1056       11274 :         hCoreCoder->hSignalBuf = NULL;
    1057             :     }
    1058             : 
    1059       11343 :     IF( hCoreCoder->hBstr != NULL )
    1060             :     {
    1061       11274 :         free( hCoreCoder->hBstr );
    1062       11274 :         hCoreCoder->hBstr = NULL;
    1063             :     }
    1064             : 
    1065       11343 :     IF( hCoreCoder->hLPDmem != NULL )
    1066             :     {
    1067        5597 :         free( hCoreCoder->hLPDmem );
    1068        5597 :         hCoreCoder->hLPDmem = NULL;
    1069             :     }
    1070             : 
    1071       11343 :     IF( hCoreCoder->hTranDet != NULL )
    1072             :     {
    1073       11274 :         free( hCoreCoder->hTranDet );
    1074       11274 :         hCoreCoder->hTranDet = NULL;
    1075             :     }
    1076             : 
    1077       11343 :     IF( hCoreCoder->hNoiseEst != NULL )
    1078             :     {
    1079       11262 :         free( hCoreCoder->hNoiseEst );
    1080       11262 :         hCoreCoder->hNoiseEst = NULL;
    1081             :     }
    1082             : 
    1083       11343 :     IF( hCoreCoder->hVAD != NULL )
    1084             :     {
    1085       11193 :         free( hCoreCoder->hVAD );
    1086       11193 :         hCoreCoder->hVAD = NULL;
    1087             :     }
    1088             : 
    1089       11343 :     IF( hCoreCoder->hVAD_CLDFB != NULL )
    1090             :     {
    1091           3 :         free( hCoreCoder->hVAD_CLDFB );
    1092           3 :         hCoreCoder->hVAD_CLDFB = NULL;
    1093             :     }
    1094             : 
    1095       11343 :     IF( hCoreCoder->hTdCngEnc != NULL )
    1096             :     {
    1097          91 :         free( hCoreCoder->hTdCngEnc );
    1098          91 :         hCoreCoder->hTdCngEnc = NULL;
    1099             :     }
    1100             : 
    1101       11343 :     IF( hCoreCoder->hDtxEnc != NULL )
    1102             :     {
    1103         500 :         free( hCoreCoder->hDtxEnc );
    1104         500 :         hCoreCoder->hDtxEnc = NULL;
    1105             :     }
    1106             : 
    1107       11343 :     IF( hCoreCoder->hSpMusClas != NULL )
    1108             :     {
    1109       11262 :         free( hCoreCoder->hSpMusClas );
    1110       11262 :         hCoreCoder->hSpMusClas = NULL;
    1111             :     }
    1112             : 
    1113       11343 :     IF( hCoreCoder->hGSCEnc != NULL )
    1114             :     {
    1115        5597 :         free( hCoreCoder->hGSCEnc );
    1116        5597 :         hCoreCoder->hGSCEnc = NULL;
    1117             :     }
    1118             : 
    1119       11343 :     IF( hCoreCoder->hSC_VBR != NULL )
    1120             :     {
    1121           3 :         free( hCoreCoder->hSC_VBR );
    1122           3 :         hCoreCoder->hSC_VBR = NULL;
    1123             :     }
    1124             : 
    1125       11343 :     IF( hCoreCoder->hAmrwb_IO != NULL )
    1126             :     {
    1127           3 :         free( hCoreCoder->hAmrwb_IO );
    1128           3 :         hCoreCoder->hAmrwb_IO = NULL;
    1129             :     }
    1130             : 
    1131       11343 :     IF( hCoreCoder->hBWE_TD != NULL )
    1132             :     {
    1133        5596 :         free( hCoreCoder->hBWE_TD );
    1134        5596 :         hCoreCoder->hBWE_TD = NULL;
    1135             :     }
    1136             : 
    1137       11343 :     IF( hCoreCoder->hBWE_FD != NULL )
    1138             :     {
    1139        5596 :         free( hCoreCoder->hBWE_FD );
    1140        5596 :         hCoreCoder->hBWE_FD = NULL;
    1141             :     }
    1142             : 
    1143       11343 :     IF( hCoreCoder->hRF != NULL )
    1144             :     {
    1145           3 :         free( hCoreCoder->hRF );
    1146           3 :         hCoreCoder->hRF = NULL;
    1147             :     }
    1148             : 
    1149       11343 :     IF( hCoreCoder->hTECEnc != NULL )
    1150             :     {
    1151           3 :         free( hCoreCoder->hTECEnc );
    1152           3 :         hCoreCoder->hTECEnc = NULL;
    1153             :     }
    1154             : 
    1155       11343 :     IF( hCoreCoder->hTcxEnc != NULL )
    1156             :     {
    1157       11192 :         free( hCoreCoder->hTcxEnc );
    1158       11192 :         hCoreCoder->hTcxEnc = NULL;
    1159             :     }
    1160             : 
    1161       11343 :     IF( hCoreCoder->hTcxCfg != NULL )
    1162             :     {
    1163       11192 :         free( hCoreCoder->hTcxCfg );
    1164       11192 :         hCoreCoder->hTcxCfg = NULL;
    1165             :     }
    1166             : 
    1167       11343 :     IF( hCoreCoder->hIGFEnc != NULL )
    1168             :     {
    1169        9182 :         free( hCoreCoder->hIGFEnc );
    1170        9182 :         hCoreCoder->hIGFEnc = NULL;
    1171             :     }
    1172             : 
    1173       11343 :     IF( hCoreCoder->hPlcExt != NULL )
    1174             :     {
    1175           3 :         free( hCoreCoder->hPlcExt );
    1176           3 :         hCoreCoder->hPlcExt = NULL;
    1177             :     }
    1178             : 
    1179       11343 :     IF( hCoreCoder->hHQ_core != NULL )
    1180             :     {
    1181        5611 :         free( hCoreCoder->hHQ_core );
    1182        5611 :         hCoreCoder->hHQ_core = NULL;
    1183             :     }
    1184             : 
    1185       11343 :     free( hCoreCoder );
    1186             : 
    1187       11343 :     return;
    1188             : }
    1189             : 
    1190             : 
    1191             : /*-------------------------------------------------------------------------
    1192             :  * ivas_destroy_enc_fx()
    1193             :  *
    1194             :  * Close IVAS encoder handles
    1195             :  *-------------------------------------------------------------------------*/
    1196             : 
    1197         623 : void ivas_destroy_enc_fx(
    1198             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
    1199             : )
    1200             : {
    1201             :     Word16 i, n, nchan_inp;
    1202             : 
    1203         623 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
    1204         623 :     move16();
    1205             : 
    1206             :     /* SCE handles */
    1207        3115 :     FOR( i = 0; i < MAX_SCE; i++ )
    1208             :     {
    1209        2492 :         IF( st_ivas->hSCE[i] != NULL )
    1210             :         {
    1211         386 :             destroy_sce_enc_fx( st_ivas->hSCE[i] );
    1212         386 :             st_ivas->hSCE[i] = NULL;
    1213             :         }
    1214             :     }
    1215             : 
    1216             :     /* CPE handles */
    1217        4361 :     FOR( i = 0; i < MAX_CPE; i++ )
    1218             :     {
    1219        3738 :         IF( st_ivas->hCPE[i] != NULL )
    1220             :         {
    1221         739 :             destroy_cpe_enc( st_ivas->hCPE[i] );
    1222         739 :             st_ivas->hCPE[i] = NULL;
    1223             :         }
    1224             :     }
    1225             : 
    1226             :     /* HP20 filter handles */
    1227         623 :     IF( st_ivas->mem_hp20_in_fx != NULL )
    1228             :     {
    1229         623 :         n = getNumChanAnalysis_fx( st_ivas ); /* Q0 */
    1230             : 
    1231        3500 :         FOR( i = 0; i < n; i++ )
    1232             :         {
    1233        2877 :             free( st_ivas->mem_hp20_in_fx[i] );
    1234        2877 :             st_ivas->mem_hp20_in_fx[i] = NULL;
    1235             :         }
    1236         623 :         free( st_ivas->mem_hp20_in_fx );
    1237         623 :         st_ivas->mem_hp20_in_fx = NULL;
    1238             :     }
    1239             : 
    1240             :     /* ISM metadata handles */
    1241         623 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    1242             : 
    1243             :     /* ISM DTX Handle */
    1244         623 :     IF( st_ivas->hISMDTX != NULL )
    1245             :     {
    1246          14 :         free( st_ivas->hISMDTX );
    1247          14 :         st_ivas->hISMDTX = NULL;
    1248             :     }
    1249             : 
    1250             :     /* Q Metadata handle */
    1251         623 :     ivas_qmetadata_close_fx( &( st_ivas->hQMetaData ) );
    1252             : 
    1253             :     /* DirAC handle */
    1254         623 :     ivas_dirac_enc_close_fx( &( st_ivas->hDirAC ), st_ivas->hEncoderConfig->input_Fs );
    1255             : 
    1256             :     /* ParamISM handle */
    1257         623 :     ivas_param_ism_enc_close_fx( &( st_ivas->hParamIsm ), st_ivas->hEncoderConfig->input_Fs );
    1258             : 
    1259             :     /* SPAR handle */
    1260         623 :     ivas_spar_enc_close_fx( &( st_ivas->hSpar ), st_ivas->hEncoderConfig->input_Fs, nchan_inp, 0 );
    1261             : 
    1262             :     /* MASA handle */
    1263         623 :     ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
    1264             : 
    1265             :     /* MCT handle */
    1266         623 :     ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
    1267             : 
    1268             :     /* LFE handle */
    1269         623 :     ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
    1270             : 
    1271             :     /* LFE low pass filter state */
    1272         623 :     ivas_lfe_lpf_enc_close_fx( &( st_ivas->hLfeLpf ) );
    1273             : 
    1274             :     /* Param-Upmix MC handle */
    1275         623 :     ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
    1276             : 
    1277             :     /* Parametric MC handle */
    1278         623 :     ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
    1279             : 
    1280             :     /* Multi-channel MASA handle */
    1281         623 :     ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
    1282             : 
    1283             :     /* OMASA handle */
    1284         623 :     ivas_omasa_enc_close_fx( &( st_ivas->hOMasa ) );
    1285             : 
    1286             :     /* OSBA handle */
    1287         623 :     ivas_osba_enc_close_fx( &( st_ivas->hOSba ) );
    1288             : 
    1289             :     /* Stereo downmix for EVS encoder handle */
    1290         623 :     stereo_dmx_evs_close_encoder_fx( &( st_ivas->hStereoDmxEVS ) );
    1291             : 
    1292             :     /* Encoder configuration handle */
    1293         623 :     IF( st_ivas->hEncoderConfig != NULL )
    1294             :     {
    1295         623 :         free( st_ivas->hEncoderConfig );
    1296         623 :         st_ivas->hEncoderConfig = NULL;
    1297             :     }
    1298             : 
    1299             :     /* Buffer of indices */
    1300         623 :     IF( st_ivas->ind_list != NULL )
    1301             :     {
    1302         623 :         free( st_ivas->ind_list );
    1303             :     }
    1304             : 
    1305         623 :     IF( st_ivas->ind_list_metadata != NULL )
    1306             :     {
    1307         620 :         free( st_ivas->ind_list_metadata );
    1308             :     }
    1309             : 
    1310             :     /* input audio buffers */
    1311       13083 :     FOR( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1312             :     {
    1313       12460 :         IF( st_ivas->p_data_fx[n] != NULL )
    1314             :         {
    1315        3657 :             free( st_ivas->p_data_fx[n] );
    1316        3657 :             st_ivas->p_data_fx[n] = NULL;
    1317             :         }
    1318             :     }
    1319             : 
    1320             :     /* main IVAS handle */
    1321         623 :     free( st_ivas );
    1322             : 
    1323         623 :     return;
    1324             : }
    1325             : 
    1326             : 
    1327             : /*-------------------------------------------------------------------------
    1328             :  * ivas_initialize_MD_bstr_enc_fx()
    1329             :  *
    1330             :  * Allocate and initialize SCE/CPE MD bitstream handle
    1331             :  *-------------------------------------------------------------------------*/
    1332             : 
    1333        6231 : ivas_error ivas_initialize_MD_bstr_enc_fx(
    1334             :     BSTR_ENC_HANDLE *hMetaData_out, /* o  : encoder MD bitstream handle */
    1335             :     Encoder_Struct *st_ivas         /* i/o: IVAS encoder structure      */
    1336             : )
    1337             : {
    1338             :     BSTR_ENC_HANDLE hMetaData;
    1339             : 
    1340        6231 :     IF( ( hMetaData = (BSTR_ENC_HANDLE) malloc( sizeof( BSTR_ENC_DATA ) ) ) == NULL )
    1341             :     {
    1342           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MetaData structure\n" ) );
    1343             :     }
    1344             : 
    1345             :     /* set pointer to the buffer of metadata indices */
    1346        6231 :     hMetaData->ind_list = st_ivas->ind_list_metadata;
    1347        6231 :     hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1348        6231 :     hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata; /* Q0 */
    1349        6231 :     hMetaData->st_ivas = st_ivas;
    1350             : 
    1351        6231 :     reset_indices_enc_fx( hMetaData, st_ivas->ivas_max_num_indices_metadata );
    1352             : 
    1353        6231 :     *hMetaData_out = hMetaData;
    1354             : 
    1355        6231 :     return IVAS_ERR_OK;
    1356             : }
    1357             : 
    1358             : 
    1359             : /*-------------------------------------------------------------------------
    1360             :  * ivas_destroy_MD_bstr_enc_fx()
    1361             :  *
    1362             :  * Destroy SCE/CPE MD bitstream handle
    1363             :  *-------------------------------------------------------------------------*/
    1364             : 
    1365       12813 : void ivas_destroy_MD_bstr_enc_fx(
    1366             :     BSTR_ENC_HANDLE *hMetaData /* i/o: encoder MD bitstream handle  */
    1367             : )
    1368             : {
    1369       12813 :     test();
    1370       12813 :     IF( hMetaData == NULL || *hMetaData == NULL )
    1371             :     {
    1372        6582 :         return;
    1373             :     }
    1374             : 
    1375        6231 :     free( *hMetaData );
    1376        6231 :     *hMetaData = NULL;
    1377             : 
    1378        6231 :     return;
    1379             : }

Generated by: LCOV version 1.14