LCOV - code coverage report
Current view: top level - lib_dec - ivas_init_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 1080 1261 85.6 %
Date: 2025-09-11 08:49:07 Functions: 13 13 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 "options.h"
      34             : #include "ivas_cnst.h"
      35             : #include "ivas_prot.h"
      36             : #include "ivas_prot_rend.h"
      37             : #include "ivas_rom_com.h"
      38             : #include "ivas_stat_enc.h"
      39             : #include "prot.h"
      40             : #include <assert.h>
      41             : #include <math.h>
      42             : #include <stdint.h>
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * Local function prototypes
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : static ivas_error ivas_read_format( Decoder_Struct *st_ivas, int16_t *num_bits_read );
      54             : 
      55             : static ivas_error doSanityChecks_IVAS( Decoder_Struct *st_ivas );
      56             : 
      57             : 
      58             : /*-------------------------------------------------------------------*
      59             :  * ivas_set_audio_config_from_sba_order()
      60             :  *
      61             :  *
      62             :  *-------------------------------------------------------------------*/
      63             : 
      64             : /*! r: audio configuration */
      65          54 : static AUDIO_CONFIG ivas_set_audio_config_from_sba_order(
      66             :     const int16_t sba_order /* i  : Ambisonic (SBA) order           */
      67             : )
      68             : {
      69             :     AUDIO_CONFIG output_config;
      70             : 
      71          54 :     output_config = IVAS_AUDIO_CONFIG_HOA3;
      72             : 
      73          54 :     switch ( sba_order )
      74             :     {
      75          18 :         case SBA_FOA_ORDER:
      76          18 :             output_config = IVAS_AUDIO_CONFIG_FOA;
      77          18 :             break;
      78          12 :         case SBA_HOA2_ORDER:
      79          12 :             output_config = IVAS_AUDIO_CONFIG_HOA2;
      80          12 :             break;
      81          24 :         case SBA_HOA3_ORDER:
      82          24 :             output_config = IVAS_AUDIO_CONFIG_HOA3;
      83          24 :             break;
      84           0 :         default:
      85           0 :             output_config = IVAS_AUDIO_CONFIG_INVALID;
      86           0 :             break;
      87             :     }
      88             : 
      89          54 :     return output_config;
      90             : }
      91             : 
      92             : 
      93             : /*---------------------------------------------------------------------*
      94             :  * ivas_dec_get_format( )
      95             :  *
      96             :  * Read main parameters from the bitstream to set-up the decoder:
      97             :  * - IVAS format
      98             :  * - IVAS format specific signaling
      99             :  *---------------------------------------------------------------------*/
     100             : 
     101     1232904 : ivas_error ivas_dec_get_format(
     102             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure     */
     103             : )
     104             : {
     105             :     int16_t k, idx, num_bits_read;
     106             :     int16_t nchan_ism, element_mode_flag;
     107             :     int16_t sba_order, sba_planar, sba_analysis_order;
     108             :     int32_t ivas_total_brate;
     109             :     uint16_t *bit_stream_orig;
     110             :     AUDIO_CONFIG signaled_config;
     111             :     ivas_error error;
     112             : 
     113     1232904 :     num_bits_read = 0;
     114     1232904 :     element_mode_flag = 0;
     115             : 
     116     1232904 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     117     1232904 :     bit_stream_orig = st_ivas->bit_stream;
     118             : 
     119             :     /*-------------------------------------------------------------------*
     120             :      * Read IVAS format
     121             :      *-------------------------------------------------------------------*/
     122             : 
     123     1232904 :     if ( ( error = ivas_read_format( st_ivas, &num_bits_read ) ) != IVAS_ERR_OK )
     124             :     {
     125           0 :         return error;
     126             :     }
     127             : 
     128     1232904 :     if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format != st_ivas->last_ivas_format &&
     129        2634 :          !( st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format == MASA_ISM_FORMAT ) &&
     130        1320 :          !( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->last_ivas_format == MASA_FORMAT ) )
     131             :     {
     132             : #ifdef DEBUGGING
     133             :         fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     134             : #endif
     135           0 :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     136             :     }
     137             : 
     138             :     /*-------------------------------------------------------------------*
     139             :      * Read other signaling (ISM/MC mode, number of channels, etc.)
     140             :      *-------------------------------------------------------------------*/
     141             : 
     142     1232904 :     if ( is_DTXrate( ivas_total_brate ) == 0 )
     143             :     {
     144             :         /*-------------------------------------------------------------------*
     145             :          * Read IVAS format related signaling:
     146             :          * - in ISM  : read number of objects
     147             :          * - in SBA  : read SBA planar flag and SBA order
     148             :          * - in MASA : read number of TC
     149             :          * - in MC   : read LS setup
     150             :          *-------------------------------------------------------------------*/
     151             : 
     152     1180035 :         if ( st_ivas->ivas_format == STEREO_FORMAT )
     153             :         {
     154      173763 :             element_mode_flag = 1;
     155             :         }
     156     1006272 :         else if ( st_ivas->ivas_format == ISM_FORMAT )
     157             :         {
     158             :             /* read the number of objects */
     159      300603 :             nchan_ism = 1;
     160      300603 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 );
     161      880869 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     162             :             {
     163      580266 :                 nchan_ism++;
     164      580266 :                 k--;
     165             :             }
     166             : 
     167      300603 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     168             :             {
     169             : #ifdef DEBUGGING
     170             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     171             : #endif
     172           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     173             :             }
     174             : 
     175      300603 :             st_ivas->nchan_ism = nchan_ism;
     176      300603 :             st_ivas->ism_mode = ivas_ism_mode_select( nchan_ism, ivas_total_brate );
     177             : 
     178      300603 :             st_ivas->nchan_transport = nchan_ism;
     179      300603 :             if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     180             :             {
     181       59829 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     182             :             }
     183             :         }
     184      705669 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     185             :         {
     186             :             /* read Ambisonic (SBA) planar flag */
     187      337356 :             sba_planar = st_ivas->bit_stream[num_bits_read];
     188      337356 :             num_bits_read += SBA_PLANAR_BITS;
     189             : 
     190      337356 :             if ( st_ivas->ini_frame > 0 && sba_planar != st_ivas->sba_planar )
     191             :             {
     192             : #ifdef DEBUGGING
     193             :                 fprintf( stderr, "\nError: Changing the SBA planar/3D layout is not supported!\n" );
     194             : #endif
     195           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA planar flag signalled!" );
     196             :             }
     197             : 
     198             :             /* read Ambisonic (SBA) order */
     199      337356 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     200      337356 :             sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     201             : 
     202      337356 :             if ( st_ivas->ini_frame > 0 && sba_order != st_ivas->sba_order )
     203             :             {
     204             : #ifdef DEBUGGING
     205             :                 fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
     206             : #endif
     207           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
     208             :             }
     209             : 
     210      337356 :             sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
     211      337356 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_analysis_order );
     212             :         }
     213      368313 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
     214             :         {
     215             :             /* read number of MASA transport channels */
     216       91893 :             if ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] )
     217             :             {
     218       48075 :                 st_ivas->nchan_transport = 2;
     219       48075 :                 element_mode_flag = 1;
     220             :             }
     221             :             else
     222             :             {
     223       43818 :                 st_ivas->nchan_transport = 1;
     224             :             }
     225             : 
     226             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     227       91893 :             st_ivas->ism_mode = ISM_MODE_NONE;
     228       91893 :             nchan_ism = st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 3] + 2 * st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 2];
     229             : 
     230       91893 :             if ( nchan_ism > 0 )
     231             :             {
     232             :                 /* the input_ivas_format should be MASA_ISM_FORMAT, but we cannot initialize it now */
     233             :                 /* info about the number of objects:
     234             :                           '00' - MASA format at the encoder
     235             :                           '01' - MASA_ISM_FORMAT at the encoder, with 4 objects
     236             :                           '10' - MASA_ISM_FORMAT at the encoder, with 3 objects
     237             :                           '11' - MASA_ISM_FORMAT at the encoder, with 1 or 2 objects
     238             :                           reading if 1 or 2 objects is performed later
     239             :                 */
     240        5664 :                 nchan_ism = 5 - nchan_ism;
     241        5664 :                 if ( st_ivas->nchan_transport == 1 && nchan_ism == 2 )
     242             :                 {
     243         663 :                     nchan_ism = 1;
     244             :                 }
     245             : 
     246             :                 /* for MASA_ISM_FORMAT at input the number of MASA transport channels is always 2 and the corresponding bit is not used here*/
     247        5664 :                 st_ivas->nchan_transport = 2;
     248        5664 :                 element_mode_flag = 1;
     249             :             }
     250             : 
     251       91893 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     252             :             {
     253             : #ifdef DEBUGGING
     254             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     255             : #endif
     256           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     257             :             }
     258             : 
     259       91893 :             st_ivas->nchan_ism = nchan_ism;
     260             :         }
     261      276420 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     262             :         {
     263       20832 :             st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */
     264             : 
     265             :             /* the number of objects are written at the end of the bitstream */
     266       20832 :             nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
     267       20832 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, nchan_ism );
     268             : 
     269       20832 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     270             :             {
     271             : #ifdef DEBUGGING
     272             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     273             : #endif
     274           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     275             :             }
     276             : 
     277       20832 :             st_ivas->nchan_ism = nchan_ism;
     278             :         }
     279      255588 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
     280             :         {
     281             :             /* the number of objects is written at the end of the bitstream, in the SBA metadata */
     282      110208 :             nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
     283             : 
     284      110208 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     285             :             {
     286             : #ifdef DEBUGGING
     287             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     288             : #endif
     289           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     290             :             }
     291             : 
     292      110208 :             st_ivas->nchan_ism = nchan_ism;
     293             : 
     294             :             /* read Ambisonic (SBA) planar flag */
     295             :             /*sba_planar = st_ivas->bit_stream[num_bits_read];*/
     296      110208 :             num_bits_read += SBA_PLANAR_BITS;
     297             : 
     298             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     299      110208 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     300      110208 :             sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     301      110208 :             num_bits_read += SBA_ORDER_BITS;
     302             : 
     303             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     304      110208 :             if ( ivas_total_brate < IVAS_24k4 )
     305             :             {
     306        8547 :                 sba_order = st_ivas->bit_stream[num_bits_read + 1];
     307        8547 :                 sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     308        8547 :                 num_bits_read += SBA_ORDER_BITS;
     309             :             }
     310             : 
     311      110208 :             if ( st_ivas->ini_frame > 0 && sba_order != st_ivas->sba_order )
     312             :             {
     313             : #ifdef DEBUGGING
     314             :                 fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
     315             : #endif
     316           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
     317             :             }
     318             : 
     319      110208 :             st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );
     320             : 
     321      110208 :             sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
     322      110208 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_analysis_order );
     323             :         }
     324      145380 :         else if ( st_ivas->ivas_format == MC_FORMAT )
     325             :         {
     326             :             /* read MC configuration */
     327      145380 :             idx = 0;
     328      581520 :             for ( k = 0; k < MC_LS_SETUP_BITS; k++ )
     329             :             {
     330      436140 :                 if ( st_ivas->bit_stream[num_bits_read + k] )
     331             :                 {
     332       20151 :                     idx += 1 << ( MC_LS_SETUP_BITS - 1 - k );
     333             :                 }
     334             :             }
     335      145380 :             num_bits_read += MC_LS_SETUP_BITS;
     336             : 
     337      145380 :             signaled_config = ivas_mc_map_ls_setup_to_output_config( idx );
     338             : 
     339      145380 :             if ( st_ivas->ini_frame > 0 && st_ivas->transport_config != signaled_config )
     340             :             {
     341             : #ifdef DEBUGGING
     342             :                 fprintf( stderr, "\nError: Switching of MC configurations is not supported!\n" );
     343             : #endif
     344           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong MC configuration signalled!" );
     345             :             }
     346             : 
     347      145380 :             st_ivas->mc_mode = ivas_mc_mode_select( ivas_mc_map_output_config_to_mc_ls_setup( signaled_config ), st_ivas->hDecoderConfig->ivas_total_brate );
     348      145380 :             st_ivas->transport_config = signaled_config;
     349             :         }
     350             : 
     351             :         /*-------------------------------------------------------------------*
     352             :          * Read element mode
     353             :          *-------------------------------------------------------------------*/
     354             : 
     355     1180035 :         if ( st_ivas->ini_frame == 0 && element_mode_flag )
     356             :         {
     357             :             /* read stereo technology info */
     358         357 :             if ( ivas_total_brate < MIN_BRATE_MDCT_STEREO )
     359             :             {
     360             :                 /* 1 bit */
     361         237 :                 if ( st_ivas->bit_stream[num_bits_read] )
     362             :                 {
     363           0 :                     st_ivas->element_mode_init = 1 + IVAS_CPE_DFT;
     364             :                 }
     365             :                 else
     366             :                 {
     367         237 :                     st_ivas->element_mode_init = 0 + IVAS_CPE_DFT;
     368             :                 }
     369             :             }
     370             :             else
     371             :             {
     372         120 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     373             :             }
     374             :         }
     375             :     }
     376       52869 :     else if ( ivas_total_brate == IVAS_SID_5k2 )
     377             :     {
     378        8481 :         switch ( st_ivas->sid_format )
     379             :         {
     380        4065 :             case SID_DFT_STEREO:
     381        4065 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     382        4065 :                 break;
     383        1203 :             case SID_MDCT_STEREO:
     384        1203 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     385        1203 :                 break;
     386        1626 :             case SID_ISM:
     387        1626 :                 st_ivas->element_mode_init = IVAS_SCE;
     388        1626 :                 break;
     389         393 :             case SID_MASA_1TC:
     390         393 :                 st_ivas->element_mode_init = IVAS_SCE;
     391         393 :                 st_ivas->nchan_transport = 1;
     392         393 :                 break;
     393         222 :             case SID_MASA_2TC:
     394         222 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1 - SID_FORMAT_NBITS] == 1 )
     395             :                 {
     396           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     397             :                 }
     398             :                 else
     399             :                 {
     400         222 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     401             :                 }
     402         222 :                 st_ivas->nchan_transport = 2;
     403         222 :                 break;
     404         471 :             case SID_SBA_1TC:
     405         471 :                 st_ivas->element_mode_init = IVAS_SCE;
     406         471 :                 break;
     407         501 :             case SID_SBA_2TC:
     408         501 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     409         501 :                 break;
     410             :         }
     411             : 
     412        8481 :         if ( st_ivas->ivas_format == ISM_FORMAT )
     413             :         {
     414             :             /* read the number of objects */
     415        1626 :             nchan_ism = 1;
     416        1626 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 - SID_FORMAT_NBITS );
     417        5397 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     418             :             {
     419        3771 :                 nchan_ism++;
     420        3771 :                 k--;
     421             :             }
     422        1626 :             k--;
     423             : 
     424        1626 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     425             :             {
     426             : #ifdef DEBUGGING
     427             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     428             : #endif
     429           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     430             :             }
     431             : 
     432        1626 :             st_ivas->nchan_ism = nchan_ism;
     433             : 
     434             :             /* read ism_mode */
     435        1626 :             st_ivas->ism_mode = ISM_MODE_DISC;
     436        1626 :             if ( nchan_ism > 2 )
     437             :             {
     438        1146 :                 k -= nchan_ism; /* SID metadata flags */
     439        1146 :                 idx = st_ivas->bit_stream[k];
     440        1146 :                 st_ivas->ism_mode = (ISM_MODE) ( idx + 1 );
     441             :             }
     442             : 
     443        1626 :             st_ivas->nchan_transport = nchan_ism;
     444        1626 :             if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     445             :             {
     446         321 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     447             :             }
     448             :         }
     449             :     }
     450             : 
     451     1232904 :     st_ivas->bit_stream = bit_stream_orig;
     452             : 
     453     1232904 :     return IVAS_ERR_OK;
     454             : }
     455             : 
     456             : 
     457             : /*-------------------------------------------------------------------*
     458             :  * ivas_dec_setup()
     459             :  *
     460             :  * IVAS decoder setup
     461             :  *-------------------------------------------------------------------*/
     462             : 
     463     1235967 : ivas_error ivas_dec_setup(
     464             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
     465             : )
     466             : {
     467             :     int16_t k, idx, num_bits_read;
     468             :     int16_t nchan_ism, element_mode_flag;
     469             :     Decoder_State *st;
     470             :     int32_t ivas_total_brate;
     471             :     ivas_error error;
     472             : 
     473     1235967 :     error = IVAS_ERR_OK;
     474             : 
     475     1235967 :     num_bits_read = 0;
     476     1235967 :     element_mode_flag = 0;
     477             : 
     478     1235967 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     479             : 
     480             :     /*-------------------------------------------------------------------*
     481             :      * Read IVAS format
     482             :      *-------------------------------------------------------------------*/
     483             : 
     484     1235967 :     if ( ( error = ivas_read_format( st_ivas, &num_bits_read ) ) != IVAS_ERR_OK )
     485             :     {
     486           0 :         return error;
     487             :     }
     488             : 
     489             :     /*-------------------------------------------------------------------*
     490             :      * Read other signaling (ISM/MC mode, number of channels, etc.)
     491             :      *-------------------------------------------------------------------*/
     492             : 
     493     1235967 :     if ( is_DTXrate( ivas_total_brate ) == 0 )
     494             :     {
     495             :         /*-------------------------------------------------------------------*
     496             :          * Read IVAS format related signaling:
     497             :          * - in ISM  : read number of objects
     498             :          * - in SBA  : read SBA planar flag and SBA order
     499             :          * - in MASA : read number of TC
     500             :          * - in MC   : read LS setup
     501             :          *-------------------------------------------------------------------*/
     502             : 
     503     1180035 :         if ( st_ivas->ivas_format == STEREO_FORMAT )
     504             :         {
     505      173763 :             element_mode_flag = 1;
     506             :         }
     507     1006272 :         else if ( st_ivas->ivas_format == ISM_FORMAT )
     508             :         {
     509             :             /* read the number of objects */
     510      300603 :             st_ivas->nchan_transport = 1;
     511      300603 :             nchan_ism = 1;
     512      300603 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 );
     513      880869 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     514             :             {
     515      580266 :                 nchan_ism++;
     516      580266 :                 k--;
     517             :             }
     518             : 
     519      300603 :             st_ivas->nchan_ism = nchan_ism;
     520             : 
     521      300603 :             if ( ( error = ivas_ism_dec_config( st_ivas, st_ivas->ism_mode ) ) != IVAS_ERR_OK )
     522             :             {
     523           0 :                 return error;
     524             :             }
     525             :         }
     526      705669 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     527             :         {
     528             :             /* read Ambisonic (SBA) planar flag */
     529      337356 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     530      337356 :             num_bits_read += SBA_PLANAR_BITS;
     531             : 
     532             :             /* read Ambisonic (SBA) order */
     533      337356 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     534      337356 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     535             : 
     536      337356 :             num_bits_read += SBA_ORDER_BITS;
     537      337356 :             if ( st_ivas->ini_frame > 0 && ivas_total_brate != st_ivas->last_active_ivas_total_brate && ivas_total_brate > IVAS_SID_5k2 )
     538             :             {
     539        3060 :                 if ( ( error = ivas_sba_dec_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     540             :                 {
     541           0 :                     return error;
     542             :                 }
     543             :             }
     544             :             else
     545             :             {
     546             :                 /* set Ambisonic (SBA) order used for analysis and coding */
     547      334296 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
     548             : 
     549      334296 :                 ivas_sba_config( ivas_total_brate, st_ivas->sba_analysis_order, -1, &( st_ivas->nchan_transport ), st_ivas->sba_planar, &st_ivas->nSCE, &st_ivas->nCPE, &st_ivas->element_mode_init );
     550             :             }
     551             :         }
     552      368313 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
     553             :         {
     554             :             /* read number of MASA transport channels */
     555       91893 :             if ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] )
     556             :             {
     557       48075 :                 st_ivas->nchan_transport = 2;
     558       48075 :                 element_mode_flag = 1;
     559             :             }
     560             :             else
     561             :             {
     562       43818 :                 st_ivas->nchan_transport = 1;
     563             :             }
     564             : 
     565             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     566       91893 :             st_ivas->ism_mode = ISM_MODE_NONE;
     567       91893 :             st_ivas->nchan_ism = st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 3] + 2 * st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 2];
     568             : 
     569       91893 :             if ( st_ivas->nchan_ism > 0 )
     570             :             {
     571             :                 /* the input_ivas_format should be MASA_ISM_FORMAT, but we cannot initialize it now */
     572             :                 /* info about the number of objects:
     573             :                           '00' - MASA format at the encoder
     574             :                           '01' - MASA_ISM_FORMAT at the encoder, with 4 objects
     575             :                           '10' - MASA_ISM_FORMAT at the encoder, with 3 objects
     576             :                           '11' - MASA_ISM_FORMAT at the encoder, with 1 or 2 objects
     577             :                           reading if 1 or 2 objects is performed later
     578             :                 */
     579        5664 :                 st_ivas->nchan_ism = 5 - st_ivas->nchan_ism;
     580        5664 :                 if ( st_ivas->nchan_transport == 1 && st_ivas->nchan_ism == 2 )
     581             :                 {
     582         663 :                     st_ivas->nchan_ism = 1;
     583             :                 }
     584             : 
     585             :                 /* for MASA_ISM_FORMAT at input the number of MASA transport channels is always 2 and the corresponding bit is not used here*/
     586        5664 :                 st_ivas->nchan_transport = 2;
     587        5664 :                 element_mode_flag = 1;
     588             :             }
     589             : 
     590       91893 :             if ( st_ivas->ini_frame > 0 )
     591             :             {
     592             :                 /* reconfigure in case a change of operation mode is detected */
     593       91647 :                 if ( ( ivas_total_brate > IVAS_SID_5k2 && ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) || ( st_ivas->ini_active_frame == 0 ) )
     594             :                 {
     595        5268 :                     if ( st_ivas->last_ivas_format == MASA_FORMAT )
     596             :                     {
     597        3954 :                         if ( st_ivas->ini_active_frame == 0 && ivas_total_brate != FRAME_NO_DATA && ivas_total_brate < MASA_STEREO_MIN_BITRATE && st_ivas->nCPE == 1 )
     598             :                         {
     599           0 :                             st_ivas->hCPE[0]->nchan_out = 1;
     600             :                         }
     601             :                         else
     602             :                         {
     603        3954 :                             if ( ( error = ivas_masa_dec_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     604             :                             {
     605           0 :                                 return error;
     606             :                             }
     607             :                         }
     608             :                     }
     609             :                     else
     610             :                     {
     611        1314 :                         if ( ( error = ivas_omasa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     612             :                         {
     613           0 :                             return error;
     614             :                         }
     615             :                     }
     616             :                 }
     617             :             }
     618             :         }
     619      276420 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     620             :         {
     621       20832 :             st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */
     622             : 
     623             :             /* for the DISC mode the number of objects are written at the end of the bitstream, in the MASA metadata */
     624       20832 :             st_ivas->nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
     625       20832 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );
     626             : 
     627       20832 :             if ( st_ivas->ini_frame > 0 )
     628             :             {
     629             :                 /* reconfigure in case a change of operation mode is detected */
     630       20721 :                 if ( ( ivas_total_brate > IVAS_SID_5k2 && ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) || ( st_ivas->ini_active_frame == 0 ) )
     631             :                 {
     632        3504 :                     if ( ( error = ivas_omasa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     633             :                     {
     634           0 :                         return error;
     635             :                     }
     636             :                 }
     637             :             }
     638             :         }
     639      255588 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
     640             :         {
     641             :             /* the number of objects is written at the end of the bitstream, in the SBA metadata */
     642      110208 :             st_ivas->nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
     643             : 
     644             :             /* read Ambisonic (SBA) planar flag */
     645      110208 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     646      110208 :             num_bits_read += SBA_PLANAR_BITS;
     647             : 
     648             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     649      110208 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     650      110208 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     651      110208 :             num_bits_read += SBA_ORDER_BITS;
     652             : 
     653             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     654      110208 :             if ( ivas_total_brate < IVAS_24k4 )
     655             :             {
     656        8547 :                 st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     657        8547 :                 st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     658        8547 :                 num_bits_read += SBA_ORDER_BITS;
     659             :             }
     660             : 
     661      110208 :             if ( st_ivas->ini_frame > 0 && ivas_total_brate != st_ivas->last_active_ivas_total_brate )
     662             :             {
     663        1959 :                 if ( ( error = ivas_sba_dec_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     664             :                 {
     665           0 :                     return error;
     666             :                 }
     667             :             }
     668             :             else
     669             :             {
     670             :                 /* set Ambisonic (SBA) order used for analysis and coding */
     671      108249 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
     672             : 
     673      108249 :                 ivas_sba_config( ivas_total_brate, st_ivas->sba_analysis_order, -1, &( st_ivas->nchan_transport ), st_ivas->sba_planar, &st_ivas->nSCE, &st_ivas->nCPE, &st_ivas->element_mode_init );
     674             : 
     675             :                 /*correct number of CPEs for discrete ISM coding*/
     676      108249 :                 if ( st_ivas->ini_frame > 0 && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     677             :                 {
     678             :                     {
     679             :                         int16_t n;
     680             : 
     681       64998 :                         n = st_ivas->nchan_transport + st_ivas->nchan_ism;
     682       64998 :                         st_ivas->nCPE = ( n + 1 ) >> 1;
     683             :                     }
     684             :                 }
     685             :             }
     686      110208 :             if ( ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism ) == ISM_SBA_MODE_DISC )
     687             :             {
     688       65820 :                 st_ivas->ism_mode = ISM_SBA_MODE_DISC;
     689             :             }
     690             :             else
     691             :             {
     692       44388 :                 st_ivas->ism_mode = ISM_MODE_NONE;
     693             :             }
     694             :         }
     695      145380 :         else if ( st_ivas->ivas_format == MC_FORMAT )
     696             :         {
     697             :             /* read MC configuration */
     698      145380 :             idx = 0;
     699      581520 :             for ( k = 0; k < MC_LS_SETUP_BITS; k++ )
     700             :             {
     701      436140 :                 if ( st_ivas->bit_stream[num_bits_read + k] )
     702             :                 {
     703       20151 :                     idx += 1 << ( MC_LS_SETUP_BITS - 1 - k );
     704             :                 }
     705             :             }
     706      145380 :             num_bits_read += MC_LS_SETUP_BITS;
     707             : 
     708             :             /* select MC format mode; reconfigure the MC format decoder */
     709      145380 :             if ( ( error = ivas_mc_dec_config( st_ivas, idx ) ) != IVAS_ERR_OK )
     710             :             {
     711           0 :                 return error;
     712             :             }
     713             :         }
     714             : 
     715             :         /*-------------------------------------------------------------------*
     716             :          * Read element mode
     717             :          *-------------------------------------------------------------------*/
     718             : 
     719     1180035 :         if ( st_ivas->ini_frame == 0 && element_mode_flag )
     720             :         {
     721             :             /* read stereo technology info */
     722         357 :             if ( ivas_total_brate < MIN_BRATE_MDCT_STEREO )
     723             :             {
     724             :                 /* 1 bit */
     725         237 :                 if ( st_ivas->bit_stream[num_bits_read] )
     726             :                 {
     727           0 :                     st_ivas->element_mode_init = 1 + IVAS_CPE_DFT;
     728             :                 }
     729             :                 else
     730             :                 {
     731         237 :                     st_ivas->element_mode_init = 0 + IVAS_CPE_DFT;
     732             :                 }
     733             :             }
     734             :             else
     735             :             {
     736         120 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     737             :             }
     738             :         }
     739             :     }
     740       55932 :     else if ( ivas_total_brate == IVAS_SID_5k2 )
     741             :     {
     742        8481 :         switch ( st_ivas->sid_format )
     743             :         {
     744        4065 :             case SID_DFT_STEREO:
     745        4065 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     746        4065 :                 break;
     747        1203 :             case SID_MDCT_STEREO:
     748        1203 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     749        1203 :                 break;
     750        1626 :             case SID_ISM:
     751        1626 :                 st_ivas->element_mode_init = IVAS_SCE;
     752        1626 :                 break;
     753         393 :             case SID_MASA_1TC:
     754         393 :                 st_ivas->element_mode_init = IVAS_SCE;
     755         393 :                 st_ivas->nchan_transport = 1;
     756         393 :                 break;
     757         222 :             case SID_MASA_2TC:
     758         222 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1 - SID_FORMAT_NBITS] == 1 )
     759             :                 {
     760           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     761             :                 }
     762             :                 else
     763             :                 {
     764         222 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     765             :                 }
     766         222 :                 st_ivas->nchan_transport = 2;
     767         222 :                 break;
     768         471 :             case SID_SBA_1TC:
     769         471 :                 st_ivas->element_mode_init = IVAS_SCE;
     770         471 :                 break;
     771         501 :             case SID_SBA_2TC:
     772         501 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     773         501 :                 break;
     774             :         }
     775             : 
     776        8481 :         if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format == SBA_FORMAT )
     777             :         {
     778             :             int16_t nchan_transport_old, nchan_transport;
     779         972 :             nchan_transport_old = st_ivas->nchan_transport;
     780         972 :             nchan_transport = ( st_ivas->sid_format == SID_SBA_2TC ) ? 2 : 1;
     781             : 
     782         972 :             if ( ( nchan_transport_old != nchan_transport ) )
     783             :             {
     784             :                 /*Setting the default bitrate for the reconfig function*/
     785           0 :                 if ( st_ivas->sid_format == SID_SBA_2TC )
     786             :                 {
     787           0 :                     st_ivas->hDecoderConfig->ivas_total_brate = IVAS_48k;
     788             :                 }
     789             :                 else
     790             :                 {
     791           0 :                     st_ivas->hDecoderConfig->ivas_total_brate = IVAS_24k4;
     792             :                 }
     793             : 
     794           0 :                 if ( ( error = ivas_sba_dec_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     795             :                 {
     796           0 :                     return error;
     797             :                 }
     798             : 
     799           0 :                 st_ivas->last_active_ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     800           0 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
     801             :             }
     802             :         }
     803             : 
     804        8481 :         if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format == MASA_FORMAT )
     805             :         {
     806         615 :             st_ivas->nchan_ism = 0;
     807         615 :             st_ivas->ism_mode = ISM_MODE_NONE;
     808             :         }
     809             : 
     810        8481 :         if ( st_ivas->ivas_format == ISM_FORMAT )
     811             :         {
     812        1626 :             ISM_MODE last_ism_mode = st_ivas->ism_mode;
     813             : 
     814             :             /* read the number of objects */
     815        1626 :             st_ivas->nchan_transport = 1;
     816        1626 :             nchan_ism = 1;
     817        1626 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 ) - SID_FORMAT_NBITS;
     818        5397 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     819             :             {
     820        3771 :                 nchan_ism++;
     821        3771 :                 k--;
     822             :             }
     823        1626 :             k--;
     824             : 
     825        1626 :             if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
     826             :             {
     827           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong number of objects signalled!" );
     828             :             }
     829             : 
     830        1626 :             st_ivas->nchan_ism = nchan_ism;
     831             : 
     832             :             /* read ism_mode */
     833        1626 :             st_ivas->ism_mode = ISM_MODE_DISC;
     834        1626 :             if ( nchan_ism > 2 )
     835             :             {
     836        1146 :                 k -= nchan_ism; /* SID metadata flags */
     837        1146 :                 idx = st_ivas->bit_stream[k];
     838        1146 :                 st_ivas->ism_mode = (ISM_MODE) ( idx + 1 );
     839             :             }
     840             : 
     841        1626 :             if ( st_ivas->ini_frame == 0 )
     842             :             {
     843           0 :                 last_ism_mode = st_ivas->ism_mode;
     844             :             }
     845             : 
     846        1626 :             if ( ( error = ivas_ism_dec_config( st_ivas, last_ism_mode ) ) != IVAS_ERR_OK )
     847             :             {
     848           0 :                 return error;
     849             :             }
     850             :         }
     851             :     }
     852             : 
     853             :     /*-------------------------------------------------------------------*
     854             :      * Initialize decoder in the first good frame based on IVAS format
     855             :      * and number of transport channels
     856             :      *-------------------------------------------------------------------*/
     857             : 
     858     1235967 :     if ( st_ivas->ini_frame == 0 && st_ivas->ivas_format != UNDEFINED_FORMAT )
     859             :     {
     860        1875 :         if ( ( error = doSanityChecks_IVAS( st_ivas ) ) != IVAS_ERR_OK )
     861             :         {
     862           0 :             return IVAS_ERROR( error, "Sanity checks failed" );
     863             :         }
     864             : 
     865        1875 :         if ( ( error = ivas_init_decoder( st_ivas ) ) != IVAS_ERR_OK )
     866             :         {
     867           0 :             return error;
     868             :         }
     869             :     }
     870             : 
     871             :     /*----------------------------------------------------------------*
     872             :      * Reset bitstream pointers
     873             :      *----------------------------------------------------------------*/
     874             : 
     875     1235967 :     ivas_set_bitstream_pointers( st_ivas );
     876             : 
     877     1235967 :     reset_elements( st_ivas );
     878             : 
     879             :     /* update bitstream buffer pointer -> take into account already read bits */
     880     1235967 :     if ( ( st_ivas->nSCE > 0 ) || ( st_ivas->nCPE > 0 ) )
     881             :     {
     882     1235967 :         st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     883     1235967 :         st->next_bit_pos = num_bits_read;
     884     1235967 :         st->total_brate = ACELP_8k00; /* only temporary initialization - this is needed for get_next_indice() in the frame following NO_DATA frame */
     885             :     }
     886             : 
     887     1235967 :     return error;
     888             : }
     889             : 
     890             : 
     891             : /*-------------------------------------------------------------------*
     892             :  * ivas_read_format()
     893             :  *
     894             :  * Read IVAS format signaling
     895             :  *-------------------------------------------------------------------*/
     896             : 
     897     2468871 : static ivas_error ivas_read_format(
     898             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure                                 */
     899             :     int16_t *num_bits_read   /* o  : number of IVAS signaling bits read from the bitstream */
     900             : )
     901             : {
     902             :     int16_t k, idx;
     903             :     int32_t ivas_total_brate;
     904             :     ivas_error error;
     905             : 
     906     2468871 :     error = IVAS_ERR_OK;
     907             : 
     908     2468871 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     909             : 
     910     2468871 :     *num_bits_read = 0;
     911             : 
     912     2468871 :     if ( !st_ivas->bfi && is_DTXrate( ivas_total_brate ) == 0 )
     913             :     {
     914             :         /* read IVAS format */
     915     2360070 :         k = 0;
     916     2360070 :         if ( st_ivas->bit_stream[*num_bits_read] )
     917             :         {
     918     1721784 :             k = 1;
     919             :         }
     920     2360070 :         k <<= 1;
     921     2360070 :         ( *num_bits_read )++;
     922             : 
     923     2360070 :         if ( st_ivas->bit_stream[*num_bits_read] )
     924             :         {
     925     1166352 :             k += 1;
     926             :         }
     927     2360070 :         ( *num_bits_read )++;
     928             : 
     929     2360070 :         switch ( k )
     930             :         {
     931      347526 :             case 0:
     932      347526 :                 st_ivas->ivas_format = STEREO_FORMAT;
     933      347526 :                 break;
     934      290760 :             case 1:
     935      290760 :                 st_ivas->ivas_format = MC_FORMAT;
     936      290760 :                 break;
     937      846192 :             case 2:
     938      846192 :                 st_ivas->ivas_format = ISM_FORMAT;
     939             : 
     940      846192 :                 if ( ivas_total_brate >= IVAS_24k4 )
     941             :                 {
     942      796254 :                     if ( st_ivas->bit_stream[*num_bits_read] )
     943             :                     {
     944      244986 :                         ( *num_bits_read )++;
     945      244986 :                         if ( st_ivas->bit_stream[*num_bits_read] )
     946             :                         {
     947      203322 :                             st_ivas->ivas_format = SBA_ISM_FORMAT;
     948             :                         }
     949             :                         else
     950             :                         {
     951       41664 :                             st_ivas->ivas_format = MASA_ISM_FORMAT;
     952             :                         }
     953             :                     }
     954      796254 :                     ( *num_bits_read )++;
     955             :                 }
     956      846192 :                 break;
     957      875592 :             case 3:
     958      875592 :                 if ( st_ivas->bit_stream[*num_bits_read] )
     959             :                 {
     960      183786 :                     st_ivas->ivas_format = MASA_FORMAT;
     961             :                 }
     962             :                 else
     963             :                 {
     964      691806 :                     st_ivas->ivas_format = SBA_FORMAT;
     965             :                     /* read Ambisonic (SBA) planar flag */
     966      691806 :                     st_ivas->sba_planar = st_ivas->bit_stream[( *num_bits_read ) + 1];
     967             : 
     968             :                     /* read Ambisonic (SBA) order */
     969      691806 :                     st_ivas->sba_order = st_ivas->bit_stream[( *num_bits_read ) + 2 + SBA_PLANAR_BITS];
     970      691806 :                     st_ivas->sba_order += 2 * st_ivas->bit_stream[( *num_bits_read ) + 1 + SBA_PLANAR_BITS];
     971      691806 :                     if ( st_ivas->sba_order == 0 )
     972             :                     {
     973       17094 :                         st_ivas->ivas_format = SBA_ISM_FORMAT;
     974             : 
     975             :                         /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     976       17094 :                         if ( ivas_total_brate < IVAS_24k4 )
     977             :                         {
     978       17094 :                             st_ivas->sba_order = st_ivas->bit_stream[*num_bits_read + 2 + SBA_PLANAR_BITS + SBA_ORDER_BITS];
     979       17094 :                             st_ivas->sba_order += 2 * st_ivas->bit_stream[*num_bits_read + 1 + SBA_PLANAR_BITS + SBA_ORDER_BITS];
     980             :                         }
     981             :                     }
     982             :                 }
     983      875592 :                 ( *num_bits_read )++;
     984             : 
     985      875592 :                 break;
     986             :         }
     987     2360070 :     }
     988      108801 :     else if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
     989             :     {
     990             :         /* read IVAS format in SID frame */
     991       16962 :         idx = 0;
     992       67848 :         for ( k = 0; k < SID_FORMAT_NBITS; k++ )
     993             :         {
     994       50886 :             idx += st_ivas->bit_stream[k] << ( SID_FORMAT_NBITS - 1 - k );
     995             :         }
     996             : 
     997       16962 :         ( *num_bits_read ) += SID_FORMAT_NBITS;
     998       16962 :         st_ivas->sid_format = idx;
     999             : 
    1000       16962 :         switch ( idx )
    1001             :         {
    1002       10536 :             case SID_DFT_STEREO:
    1003             :             case SID_MDCT_STEREO:
    1004       10536 :                 st_ivas->ivas_format = STEREO_FORMAT;
    1005       10536 :                 break;
    1006        3252 :             case SID_ISM:
    1007        3252 :                 st_ivas->ivas_format = ISM_FORMAT;
    1008        3252 :                 break;
    1009         942 :             case SID_SBA_1TC:
    1010         942 :                 st_ivas->ivas_format = SBA_FORMAT;
    1011         942 :                 st_ivas->element_mode_init = IVAS_SCE;
    1012         942 :                 break;
    1013        1002 :             case SID_SBA_2TC:
    1014        1002 :                 st_ivas->ivas_format = SBA_FORMAT;
    1015        1002 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1016        1002 :                 break;
    1017         786 :             case SID_MASA_1TC:
    1018         786 :                 st_ivas->ivas_format = MASA_FORMAT;
    1019         786 :                 st_ivas->element_mode_init = IVAS_SCE;
    1020         786 :                 break;
    1021         444 :             case SID_MASA_2TC:
    1022         444 :                 st_ivas->ivas_format = MASA_FORMAT;
    1023         444 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] == 1 )
    1024             :                 {
    1025           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1026             :                 }
    1027             :                 else
    1028             :                 {
    1029         444 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
    1030             :                 }
    1031         444 :                 break;
    1032           0 :             default:
    1033           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Invalid value %c found in SID format field.", st_ivas->sid_format );
    1034             :         }
    1035             : 
    1036       16962 :         if ( st_ivas->ivas_format == SBA_FORMAT )
    1037             :         {
    1038             :             /* read Ambisonic (SBA) planar flag */
    1039        1944 :             st_ivas->sba_planar = st_ivas->bit_stream[*num_bits_read];
    1040        1944 :             *num_bits_read += SBA_PLANAR_BITS;
    1041             : 
    1042             :             /* read Ambisonic (SBA) order */
    1043        1944 :             st_ivas->sba_order = st_ivas->bit_stream[*num_bits_read + 1];
    1044        1944 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[*num_bits_read];
    1045        1944 :             *num_bits_read += SBA_ORDER_BITS;
    1046        1944 :             if ( st_ivas->sba_analysis_order == 0 )
    1047             :             {
    1048           0 :                 st_ivas->sba_analysis_order = SBA_FOA_ORDER;
    1049             :             }
    1050             :         }
    1051             : 
    1052             :         /* reset bitstream handle to avoid BER detection after reading the 2400 kbps for ch0 */
    1053       16962 :         st_ivas->bit_stream += ( *num_bits_read );
    1054       16962 :         ( *num_bits_read ) = 0;
    1055             :     }
    1056             :     else
    1057             :     {
    1058             :         /* In SID/NO_DATA frames, use the previous frame IVAS format */
    1059             :     }
    1060             : 
    1061     2468871 :     return error;
    1062             : }
    1063             : 
    1064             : 
    1065             : /*-------------------------------------------------------------------*
    1066             :  * getNumChanSynthesis()
    1067             :  *
    1068             :  * get number of output channels used for synthesis/decoding
    1069             :  * (often different from number of output channels!)
    1070             :  *-------------------------------------------------------------------*/
    1071             : 
    1072             : /*! r: number of channels to be synthesised */
    1073      831795 : int16_t getNumChanSynthesis(
    1074             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure  */
    1075             : )
    1076             : {
    1077             :     int16_t n;
    1078             : 
    1079      831795 :     n = st_ivas->nSCE + CPE_CHANNELS * st_ivas->nCPE;
    1080             : 
    1081      831795 :     if ( st_ivas->sba_dirac_stereo_flag )
    1082             :     {
    1083       39060 :         n = CPE_CHANNELS;
    1084             :     }
    1085      792735 :     else if ( ( st_ivas->hMCT != NULL || st_ivas->ivas_format == SBA_FORMAT ) && st_ivas->ivas_format != SBA_ISM_FORMAT )
    1086             :     {
    1087       11532 :         n = st_ivas->nchan_transport;
    1088             :     }
    1089      781203 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1090             :     {
    1091      592356 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1092             :         {
    1093      488511 :             n = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1094             :         }
    1095             :         else
    1096             :         {
    1097      103845 :             n = st_ivas->nchan_transport;
    1098             :         }
    1099             :     }
    1100             : 
    1101      831795 :     return n;
    1102             : }
    1103             : 
    1104             : 
    1105             : /*-------------------------------------------------------------------*
    1106             :  * copy_decoder_config()
    1107             :  *
    1108             :  * Copy IVAS configuration structure to the CoreCoder state structure
    1109             :  *-------------------------------------------------------------------*/
    1110             : 
    1111       26508 : void copy_decoder_config(
    1112             :     Decoder_Struct *st_ivas, /* i  : IVAS decoder structure      */
    1113             :     Decoder_State *st        /* o  : decoder state structure     */
    1114             : )
    1115             : {
    1116       26508 :     st->output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1117       26508 :     st->Opt_AMR_WB = st_ivas->hDecoderConfig->Opt_AMR_WB;
    1118       26508 :     st->codec_mode = st_ivas->codec_mode;
    1119       26508 :     st->ini_frame = st_ivas->ini_frame;
    1120             : 
    1121       26508 :     st->bfi = st_ivas->bfi;
    1122             : 
    1123       26508 :     st->writeFECoffset = st_ivas->writeFECoffset;
    1124             : 
    1125       26508 :     st->element_mode = st_ivas->element_mode_init;
    1126             : 
    1127       26508 :     return;
    1128             : }
    1129             : 
    1130             : 
    1131             : /*-------------------------------------------------------------------*
    1132             :  * ivas_init_decoder_front()
    1133             :  *
    1134             :  * Set decoder parameters to initial values
    1135             :  *-------------------------------------------------------------------*/
    1136             : 
    1137        1884 : ivas_error ivas_init_decoder_front(
    1138             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
    1139             : )
    1140             : {
    1141             :     ivas_error error;
    1142             : 
    1143        1884 :     error = IVAS_ERR_OK;
    1144             : 
    1145             :     /*-----------------------------------------------------------------*
    1146             :      * Resets
    1147             :      *-----------------------------------------------------------------*/
    1148             : 
    1149        1884 :     st_ivas->nSCE = 0;
    1150        1884 :     st_ivas->nCPE = 0;
    1151        1884 :     st_ivas->nchan_ism = 0;
    1152        1884 :     st_ivas->nchan_transport = -1;
    1153             : 
    1154        1884 :     st_ivas->ism_mode = ISM_MODE_NONE;
    1155        1884 :     st_ivas->mc_mode = MC_MODE_NONE;
    1156             : 
    1157        1884 :     st_ivas->sba_dirac_stereo_flag = 0;
    1158             : 
    1159             :     /* HRTF binauralization latency in ns */
    1160        1884 :     st_ivas->binaural_latency_ns = 0;
    1161             : 
    1162             : #ifdef DEBUGGING
    1163             :     st_ivas->noClipping = 0;
    1164             : 
    1165             :     st_ivas->hDecoderConfig->force_rend = -1;
    1166             : #endif
    1167             :     /*-------------------------------------------------------------------*
    1168             :      * Allocate and initialize Custom loudspeaker layout handle
    1169             :      *--------------------------------------------------------------------*/
    1170             : 
    1171        1884 :     if ( st_ivas->hDecoderConfig->Opt_LsCustom )
    1172             :     {
    1173           9 :         if ( ( error = ivas_ls_custom_open( &( st_ivas->hLsSetupCustom ) ) ) != IVAS_ERR_OK )
    1174             :         {
    1175           0 :             return error;
    1176             :         }
    1177             :     }
    1178             : 
    1179             :     /*-------------------------------------------------------------------*
    1180             :      * Allocate and initialize Head-Tracking handle
    1181             :      *--------------------------------------------------------------------*/
    1182             : 
    1183        1884 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    1184             :     {
    1185         249 :         if ( ( error = ivas_headTrack_open( &( st_ivas->hHeadTrackData ) ) ) != IVAS_ERR_OK )
    1186             :         {
    1187           0 :             return error;
    1188             :         }
    1189         249 :         if ( ( error = ivas_orient_trk_SetTrackingType( st_ivas->hHeadTrackData->OrientationTracker, st_ivas->hDecoderConfig->orientation_tracking ) ) != IVAS_ERR_OK )
    1190             :         {
    1191           0 :             return error;
    1192             :         }
    1193             :     }
    1194             : 
    1195             :     /*-------------------------------------------------------------------*
    1196             :      * Allocate and initialize external orientation handle
    1197             :      *--------------------------------------------------------------------*/
    1198             : 
    1199        1884 :     if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1200             :     {
    1201          93 :         if ( ( error = ivas_external_orientation_open( &( st_ivas->hExtOrientationData ), st_ivas->hDecoderConfig->render_framesize ) ) != IVAS_ERR_OK )
    1202             :         {
    1203           0 :             return error;
    1204             :         }
    1205             :     }
    1206             : 
    1207             :     /*-------------------------------------------------------------------*
    1208             :      * Allocate and initialize combined orientation handle
    1209             :      *--------------------------------------------------------------------*/
    1210             : 
    1211        1884 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1212             :     {
    1213         249 :         if ( ( error = ivas_combined_orientation_open( &( st_ivas->hCombinedOrientationData ), st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->render_framesize ) ) != IVAS_ERR_OK )
    1214             :         {
    1215           0 :             return error;
    1216             :         }
    1217             :     }
    1218             : 
    1219             :     /*-------------------------------------------------------------------*
    1220             :      * Allocate and initialize Binaural Renderer configuration handle
    1221             :      *--------------------------------------------------------------------*/
    1222             : 
    1223        1884 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
    1224        1251 :          ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->Opt_non_diegetic_pan ) )
    1225             :     {
    1226         639 :         if ( ( error = ivas_render_config_open( &( st_ivas->hRenderConfig ) ) ) != IVAS_ERR_OK )
    1227             :         {
    1228           0 :             return error;
    1229             :         }
    1230             : 
    1231         639 :         if ( ( error = ivas_render_config_init_from_rom( &st_ivas->hRenderConfig ) ) != IVAS_ERR_OK )
    1232             :         {
    1233           0 :             return error;
    1234             :         }
    1235             :     }
    1236             : 
    1237        1884 :     return error;
    1238             : }
    1239             : 
    1240             : 
    1241             : /*-------------------------------------------------------------------*
    1242             :  * ivas_init_decoder()
    1243             :  *
    1244             :  * Initialize IVAS decoder state structure
    1245             :  *-------------------------------------------------------------------*/
    1246             : 
    1247        1884 : ivas_error ivas_init_decoder(
    1248             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
    1249             : )
    1250             : {
    1251             :     int16_t i, n, k;
    1252             :     int16_t sce_id, cpe_id;
    1253             :     int16_t numCldfbAnalyses, numCldfbSyntheses;
    1254             :     int16_t granularity, n_channels_transport_jbm;
    1255             :     int16_t nchan_out_buff;
    1256             :     int32_t output_Fs, ivas_total_brate;
    1257             :     int32_t delay_ns;
    1258             :     AUDIO_CONFIG output_config;
    1259             :     DECODER_CONFIG_HANDLE hDecoderConfig;
    1260             :     ivas_error error;
    1261             :     int32_t ism_total_brate;
    1262             : 
    1263        1884 :     error = IVAS_ERR_OK;
    1264             : 
    1265        1884 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1266        1884 :     hDecoderConfig = st_ivas->hDecoderConfig;
    1267        1884 :     output_config = hDecoderConfig->output_config;
    1268        1884 :     ivas_total_brate = hDecoderConfig->ivas_total_brate;
    1269             : 
    1270        1884 :     hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
    1271        1884 :     st_ivas->last_active_ivas_total_brate = ivas_total_brate;
    1272             : 
    1273             :     /*-----------------------------------------------------------------*
    1274             :      * Set number of output channels for EXTERNAL output config.
    1275             :      *-----------------------------------------------------------------*/
    1276             : 
    1277        1884 :     if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    1278             :     {
    1279         150 :         if ( st_ivas->ivas_format == STEREO_FORMAT )
    1280             :         {
    1281           3 :             hDecoderConfig->nchan_out = CPE_CHANNELS;
    1282             :         }
    1283         147 :         else if ( st_ivas->ivas_format == MC_FORMAT )
    1284             :         {
    1285          12 :             hDecoderConfig->nchan_out = audioCfg2channels( st_ivas->transport_config );
    1286             :         }
    1287         135 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == SBA_FORMAT )
    1288             :         {
    1289          27 :             hDecoderConfig->nchan_out = audioCfg2channels( ivas_set_audio_config_from_sba_order( st_ivas->sba_order ) );
    1290          27 :             hDecoderConfig->nchan_out += st_ivas->nchan_ism;
    1291             :         }
    1292         108 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1293             :         {
    1294          63 :             hDecoderConfig->nchan_out = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1295             :         }
    1296          45 :         else if ( !( st_ivas->ism_mode == ISM_MODE_PARAM ) )
    1297             :         {
    1298          39 :             hDecoderConfig->nchan_out = st_ivas->nchan_transport;
    1299             :         }
    1300             : 
    1301         150 :         st_ivas->hOutSetup.nchan_out_woLFE = hDecoderConfig->nchan_out;
    1302             :     }
    1303             : 
    1304             :     /*-----------------------------------------------------------------*
    1305             :      * Set output and intern setup & renderer selection
    1306             :      *-----------------------------------------------------------------*/
    1307             : 
    1308        1884 :     st_ivas->intern_config = output_config;
    1309             : 
    1310        1884 :     if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->ivas_format == MC_FORMAT )
    1311             :     {
    1312          12 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->transport_config );
    1313          12 :         st_ivas->intern_config = st_ivas->transport_config;
    1314             :     }
    1315        1872 :     else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && ( st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == SBA_FORMAT ) )
    1316             :     {
    1317          27 :         st_ivas->intern_config = ivas_set_audio_config_from_sba_order( st_ivas->sba_order );
    1318          27 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->intern_config );
    1319             :     }
    1320             :     else
    1321             :     {
    1322        1845 :         ivas_output_init( &( st_ivas->hOutSetup ), output_config );
    1323             :     }
    1324             : 
    1325        1884 :     if ( st_ivas->ivas_format == SBA_ISM_FORMAT && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    1326             :     {
    1327          12 :         st_ivas->hOutSetup.ambisonics_order = SBA_HOA3_ORDER;
    1328          12 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_7_1_4;
    1329          12 :         st_ivas->hOutSetup.output_config = st_ivas->intern_config;
    1330          12 :         st_ivas->hOutSetup.nchan_out_woLFE = audioCfg2channels( st_ivas->intern_config );
    1331             :     }
    1332             : 
    1333             :     /* Only initialize transport setup if it is used */
    1334        1884 :     if ( st_ivas->transport_config != IVAS_AUDIO_CONFIG_INVALID )
    1335             :     {
    1336         468 :         ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
    1337             :     }
    1338             : 
    1339        1884 :     if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
    1340             :     {
    1341          93 :         ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    1342             : 
    1343          93 :         ivas_mcmasa_set_separate_channel_mode( &( st_ivas->hOutSetup.separateChannelEnabled ), &( st_ivas->hOutSetup.separateChannelIndex ), ivas_total_brate );
    1344             :     }
    1345             : 
    1346        1884 :     ivas_renderer_select( st_ivas );
    1347             : 
    1348        1884 :     if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1349             :     {
    1350           9 :         if ( ( error = ivas_ls_custom_output_init( st_ivas ) ) != IVAS_ERR_OK )
    1351             :         {
    1352           0 :             return error;
    1353             :         }
    1354             :     }
    1355             : 
    1356        1884 :     ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
    1357             : 
    1358        1884 :     if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
    1359             :     {
    1360          93 :         ivas_mcmasa_set_separate_channel_mode( &( st_ivas->hIntSetup.separateChannelEnabled ), &( st_ivas->hIntSetup.separateChannelIndex ), ivas_total_brate );
    1361             : 
    1362          93 :         if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.separateChannelEnabled )
    1363             :         {
    1364           0 :             st_ivas->hLsSetupCustom->separate_ch_found = 0;
    1365           0 :             if ( st_ivas->hOutSetup.nchan_out_woLFE >= MCMASA_MIN_SPEAKERS_SEPARATE_CENTER )
    1366             :             {
    1367             :                 /* check for a speaker at (0, 0) if minimum speaker count is available */
    1368           0 :                 for ( i = 0; i < st_ivas->hOutSetup.nchan_out_woLFE; i++ )
    1369             :                 {
    1370           0 :                     if ( st_ivas->hOutSetup.ls_azimuth[i] == 0.0f && st_ivas->hOutSetup.ls_elevation[i] == 0.0f )
    1371             :                     {
    1372           0 :                         st_ivas->hIntSetup.separateChannelIndex = i;
    1373           0 :                         st_ivas->hLsSetupCustom->separate_ch_found = 1;
    1374           0 :                         break;
    1375             :                     }
    1376             :                 }
    1377             :             }
    1378             :         }
    1379             :     }
    1380             : 
    1381             :     /*--------------------------------------------------------------------*
    1382             :      * Allocate and initialize HRTF Statistics handle
    1383             :      *--------------------------------------------------------------------*/
    1384             : 
    1385        1884 :     if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1386             :     {
    1387         153 :         if ( ( error = ivas_HRTF_statistics_init( &st_ivas->hHrtfStatistics, output_Fs ) ) != IVAS_ERR_OK )
    1388             :         {
    1389           0 :             return error;
    1390             :         }
    1391             :     }
    1392             : 
    1393             :     /*-----------------------------------------------------------------*
    1394             :      * Allocate and initialize SCE/CPE and other handles
    1395             :      *-----------------------------------------------------------------*/
    1396             : 
    1397        1884 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1398             :     {
    1399           9 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
    1400           9 :         st_ivas->nCPE = 0;
    1401           9 :         st_ivas->nchan_transport = 1;
    1402           9 :         sce_id = 0;
    1403             : 
    1404           9 :         if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate ) ) != IVAS_ERR_OK )
    1405             :         {
    1406           0 :             return error;
    1407             :         }
    1408             : 
    1409           9 :         reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1410             :     }
    1411        1875 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
    1412             :     {
    1413         204 :         st_ivas->nchan_transport = CPE_CHANNELS;
    1414         204 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_STEREO;
    1415             : 
    1416         204 :         st_ivas->nSCE = 0;
    1417         204 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
    1418         204 :         cpe_id = 0;
    1419             : 
    1420         204 :         if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
    1421             :         {
    1422           0 :             return error;
    1423             :         }
    1424             : 
    1425         612 :         for ( n = 0; n < st_ivas->nchan_transport; n++ )
    1426             :         {
    1427         408 :             reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1428             :         }
    1429             : 
    1430             :         /* init EFAP for custom LS output and set hTransSetup */
    1431         204 :         if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1432             :         {
    1433           0 :             if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hOutSetup.ls_azimuth, st_ivas->hOutSetup.ls_elevation, st_ivas->hOutSetup.nchan_out_woLFE, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    1434             :             {
    1435           0 :                 return error;
    1436             :             }
    1437             : 
    1438           0 :             ivas_output_init( &( st_ivas->hTransSetup ), IVAS_AUDIO_CONFIG_STEREO );
    1439             :         }
    1440             :     }
    1441        1671 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    1442             :     {
    1443             :         int32_t element_brate_tmp[MAX_NUM_OBJECTS];
    1444             : 
    1445         222 :         st_ivas->nSCE = st_ivas->nchan_transport; /* "st_ivas->nchan_transport" is known from ivas_dec_setup */
    1446         222 :         st_ivas->nCPE = 0;
    1447         222 :         st_ivas->ism_extmeta_active = -1;
    1448         222 :         st_ivas->ism_extmeta_cnt = 0;
    1449             : 
    1450         222 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1451             :         {
    1452          51 :             st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
    1453          51 :             st_ivas->nSCE = MAX_PARAM_ISM_WAVE;
    1454             : 
    1455          51 :             if ( ( error = ivas_param_ism_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1456             :             {
    1457           0 :                 return error;
    1458             :             }
    1459             :         }
    1460             : 
    1461         222 :         if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
    1462             :         {
    1463           0 :             return error;
    1464             :         }
    1465             : 
    1466         783 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1467             :         {
    1468         561 :             if ( ( error = create_sce_dec( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
    1469             :             {
    1470           0 :                 return error;
    1471             :             }
    1472             : 
    1473         561 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1474             : 
    1475         561 :             st_ivas->hSCE[sce_id]->hCoreCoder[0]->is_ism_format = 1;
    1476             :         }
    1477             : 
    1478         222 :         st_ivas->hISMDTX.sce_id_dtx = 0;
    1479             : 
    1480         222 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1481             :         {
    1482          51 :             st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed3;
    1483             : 
    1484          51 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1485             :             {
    1486             :                 /* reusing OMASA function for allocating and initializing MASA_ISM rendering handle (even though not in OMASA) */
    1487          24 :                 if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1488             :                 {
    1489           0 :                     return error;
    1490             :                 }
    1491             :             }
    1492             :         }
    1493         171 :         else if ( st_ivas->ism_mode == ISM_MODE_DISC )
    1494             :         {
    1495         630 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; ++sce_id )
    1496             :             {
    1497         459 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = 2 + sce_id;
    1498             :             }
    1499             :         }
    1500             :     }
    1501        1449 :     else if ( st_ivas->ivas_format == SBA_FORMAT )
    1502             :     {
    1503         735 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1504             :         {
    1505           0 :             return error;
    1506             :         }
    1507             : 
    1508         735 :         if ( ( error = ivas_spar_dec_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1509             :         {
    1510           0 :             return error;
    1511             :         }
    1512             : 
    1513         735 :         if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
    1514         735 :                                               ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) ? IVAS_MAX_NUM_BANDS : ( IVAS_MAX_NUM_BANDS - SPAR_DIRAC_SPLIT_START_BAND ),
    1515             :                                               st_ivas->ivas_format ) ) != IVAS_ERR_OK )
    1516             :         {
    1517           0 :             return error;
    1518             :         }
    1519             : 
    1520         735 :         if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO && !( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA ) )
    1521             :         {
    1522         243 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1523             :             {
    1524           0 :                 return error;
    1525             :             }
    1526             : 
    1527         243 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    1528             :         }
    1529             :         else
    1530             :         {
    1531             :             int16_t band_grouping[IVAS_MAX_NUM_BANDS + 1];
    1532             : 
    1533         492 :             st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    1534         492 :             if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
    1535             :             {
    1536           3 :                 st_ivas->hSpar->enc_param_start_band = 0;
    1537             : 
    1538           3 :                 set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    1539           3 :                 st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    1540             :             }
    1541             : 
    1542         492 :             ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( st_ivas->hDecoderConfig->output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
    1543         492 :                                      st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    1544             :         }
    1545         735 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1546             : 
    1547        1032 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1548             :         {
    1549         297 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1550             :             {
    1551           0 :                 return error;
    1552             :             }
    1553             : 
    1554         297 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1555             :         }
    1556             : 
    1557        1425 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1558             :         {
    1559         690 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1560             :             {
    1561           0 :                 return error;
    1562             :             }
    1563             : 
    1564        2070 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1565             :             {
    1566        1380 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1567             :             }
    1568             :         }
    1569             : 
    1570             :         /* create CPE element for DFT Stereo like upmix */
    1571         735 :         if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nCPE == 0 )
    1572             :         {
    1573          15 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    1574             :             {
    1575           0 :                 return error;
    1576             :             }
    1577             : 
    1578          15 :             st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
    1579          15 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    1580             :         }
    1581             : 
    1582         735 :         if ( st_ivas->nCPE > 1 )
    1583             :         {
    1584         252 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1585             :             {
    1586           0 :                 return error;
    1587             :             }
    1588             :         }
    1589             : 
    1590             :         /* set CNA/CNG flags */
    1591         735 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1592             :     }
    1593         714 :     else if ( st_ivas->ivas_format == MASA_FORMAT )
    1594             :     {
    1595             :         /* if we start in ISM_MODE_NONE in MASA_ISM, that appears as normal MASA, but we may change to a mode with ISMs */
    1596         246 :         st_ivas->ism_extmeta_active = -1;
    1597         246 :         st_ivas->ism_extmeta_cnt = 0;
    1598         246 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1599             :         {
    1600           0 :             return error;
    1601             :         }
    1602             : 
    1603         246 :         if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1604             :         {
    1605           0 :             return error;
    1606             :         }
    1607             : 
    1608         246 :         if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1609             :         {
    1610         183 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1611             :             {
    1612           0 :                 return error;
    1613             :             }
    1614             :         }
    1615             : 
    1616         339 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1617             :         {
    1618          93 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1619             :             {
    1620           0 :                 return error;
    1621             :             }
    1622             : 
    1623          93 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1624             :         }
    1625             : 
    1626         399 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1627             :         {
    1628         153 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1629             :             {
    1630           0 :                 return error;
    1631             :             }
    1632             : 
    1633         459 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1634             :             {
    1635         306 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1636             :             }
    1637             :         }
    1638             : 
    1639             :         /* set CNA/CNG flags */
    1640         246 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1641             :     }
    1642         468 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1643             :     {
    1644             :         int32_t temp_brate[MAX_SCE];
    1645         111 :         st_ivas->ism_extmeta_active = -1;
    1646         111 :         st_ivas->ism_extmeta_cnt = 0;
    1647             : 
    1648         111 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1649             : 
    1650         111 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1651             :         {
    1652           0 :             return error;
    1653             :         }
    1654             : 
    1655         111 :         if ( ( error = ivas_spar_dec_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1656             :         {
    1657           0 :             return error;
    1658             :         }
    1659             : 
    1660         111 :         if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
    1661         111 :                                               ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) ? IVAS_MAX_NUM_BANDS : ( IVAS_MAX_NUM_BANDS - SPAR_DIRAC_SPLIT_START_BAND ),
    1662             :                                               st_ivas->ivas_format ) ) != IVAS_ERR_OK )
    1663             :         {
    1664           0 :             return error;
    1665             :         }
    1666             : 
    1667         111 :         if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO )
    1668             :         {
    1669          90 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1670             :             {
    1671           0 :                 return error;
    1672             :             }
    1673             : 
    1674          90 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    1675             :         }
    1676             :         else
    1677             :         {
    1678             :             int16_t band_grouping[IVAS_MAX_NUM_BANDS + 1];
    1679             : 
    1680          21 :             st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    1681          21 :             if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
    1682             :             {
    1683           3 :                 st_ivas->hSpar->enc_param_start_band = 0;
    1684             : 
    1685           3 :                 set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    1686           3 :                 st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    1687             :             }
    1688             : 
    1689          21 :             ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( st_ivas->hDecoderConfig->output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
    1690          21 :                                      st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    1691             :         }
    1692             : 
    1693         147 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1694             :         {
    1695          36 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1696             :             {
    1697           0 :                 return error;
    1698             :             }
    1699             : 
    1700          36 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1701             :         }
    1702             : 
    1703         111 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1704             :         {
    1705             :             {
    1706             :                 int16_t n_all;
    1707             : 
    1708          57 :                 n_all = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1709          57 :                 st_ivas->nCPE = ( n_all + 1 ) >> 1;
    1710             :             }
    1711          57 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1712             :         }
    1713             : 
    1714         333 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1715             :         {
    1716         222 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1717             :             {
    1718           0 :                 return error;
    1719             :             }
    1720             : 
    1721         666 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1722             :             {
    1723         444 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1724             :             }
    1725             :         }
    1726             : 
    1727             :         /* create CPE element for DFT Stereo like upmix */
    1728         111 :         if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nCPE == 0 )
    1729             :         {
    1730           3 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    1731             :             {
    1732           0 :                 return error;
    1733             :             }
    1734             : 
    1735           3 :             st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
    1736           3 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    1737             :         }
    1738             : 
    1739         111 :         if ( st_ivas->nCPE > 1 )
    1740             :         {
    1741          60 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1742             :             {
    1743           0 :                 return error;
    1744             :             }
    1745             :         }
    1746             : 
    1747         111 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1748             :         {
    1749          57 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, temp_brate ) ) != IVAS_ERR_OK )
    1750             :             {
    1751           0 :                 return error;
    1752             :             }
    1753             : 
    1754          57 :             if ( ( error = ivas_osba_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1755             :             {
    1756           0 :                 return error;
    1757             :             }
    1758             :         }
    1759             : 
    1760             :         /* set CNA/CNG flags */
    1761         111 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1762             :     }
    1763         357 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1764             :     {
    1765         111 :         st_ivas->ism_extmeta_active = -1;
    1766         111 :         st_ivas->ism_extmeta_cnt = 0;
    1767             : 
    1768         111 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1769             :         {
    1770           0 :             return error;
    1771             :         }
    1772             : 
    1773         111 :         k = 0;
    1774         111 :         ism_total_brate = 0;
    1775         999 :         while ( k < SIZE_IVAS_BRATE_TBL && ivas_total_brate != ivas_brate_tbl[k] )
    1776             :         {
    1777         888 :             k++;
    1778             :         }
    1779             : 
    1780         111 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1781             :         {
    1782             :             /* one separated object */
    1783          60 :             st_ivas->nSCE = 1;
    1784             : 
    1785          60 :             ism_total_brate = sep_object_brate[k - 2][0];
    1786          60 :             if ( ( error = create_sce_dec( st_ivas, 0, ism_total_brate ) ) != IVAS_ERR_OK )
    1787             :             {
    1788           0 :                 return error;
    1789             :             }
    1790             : 
    1791          60 :             reset_indices_dec( st_ivas->hSCE[0]->hCoreCoder[0] );
    1792             : 
    1793          60 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, NULL ) ) != IVAS_ERR_OK )
    1794             :             {
    1795           0 :                 return error;
    1796             :             }
    1797             :         }
    1798          51 :         else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1799             :         {
    1800             :             int32_t temp_brate[MAX_SCE];
    1801          51 :             st_ivas->nSCE = st_ivas->nchan_ism; /* number of objects */
    1802             : 
    1803         192 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1804             :             {
    1805         141 :                 temp_brate[sce_id] = sep_object_brate[k - 2][st_ivas->nSCE - 1];
    1806         141 :                 ism_total_brate += temp_brate[sce_id];
    1807             : 
    1808         141 :                 if ( ( error = create_sce_dec( st_ivas, sce_id, temp_brate[sce_id] ) ) != IVAS_ERR_OK )
    1809             :                 {
    1810           0 :                     return error;
    1811             :                 }
    1812             : 
    1813         141 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1814             :             }
    1815             : 
    1816          51 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, temp_brate ) ) != IVAS_ERR_OK )
    1817             :             {
    1818           0 :                 return error;
    1819             :             }
    1820             :         }
    1821             : 
    1822         111 :         if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1823             :         {
    1824           0 :             return error;
    1825             :         }
    1826             : 
    1827         111 :         if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1828             :         {
    1829           0 :             return error;
    1830             :         }
    1831             : 
    1832         111 :         if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1833             :         {
    1834          84 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1835             :             {
    1836           0 :                 return error;
    1837             :             }
    1838             :         }
    1839             : 
    1840         111 :         if ( ( error = create_cpe_dec( st_ivas, 0, ivas_total_brate - ism_total_brate ) ) != IVAS_ERR_OK )
    1841             :         {
    1842           0 :             return error;
    1843             :         }
    1844             : 
    1845         333 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1846             :         {
    1847         222 :             reset_indices_dec( st_ivas->hCPE[0]->hCoreCoder[n] );
    1848             :         }
    1849             :     }
    1850         246 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    1851             :     {
    1852         246 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    1853             :         {
    1854             :             /* init EFAP for custom LS setup */
    1855         108 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1856             :             {
    1857           3 :                 if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth, st_ivas->hLsSetupCustom->ls_elevation, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    1858             :                 {
    1859           0 :                     return error;
    1860             :                 }
    1861             :             }
    1862             : 
    1863         108 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config ) );
    1864         108 :             st_ivas->nSCE = 0;
    1865         108 :             st_ivas->nCPE = st_ivas->nchan_transport / CPE_CHANNELS;
    1866             : 
    1867         108 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1868             : 
    1869         543 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1870             :             {
    1871         435 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1872             :                 {
    1873           0 :                     return error;
    1874             :                 }
    1875             : 
    1876        1305 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    1877             :                 {
    1878         870 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1879             :                 }
    1880             :             }
    1881             : 
    1882         108 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1883             :             {
    1884           0 :                 return error;
    1885             :             }
    1886             :         }
    1887         138 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    1888             :         {
    1889             :             /* init EFAP for custom LS setup */
    1890          15 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1891             :             {
    1892           0 :                 if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth, st_ivas->hLsSetupCustom->ls_elevation, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    1893             :                 {
    1894           0 :                     return error;
    1895             :                 }
    1896             :             }
    1897             : 
    1898          15 :             st_ivas->nSCE = 0;
    1899          15 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / CPE_CHANNELS;
    1900          15 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    1901             : 
    1902          15 :             if ( ( error = ivas_mc_paramupmix_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1903             :             {
    1904           0 :                 return error;
    1905             :             }
    1906             : 
    1907          15 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1908             : 
    1909          75 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1910             :             {
    1911          60 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    1912             :                 {
    1913           0 :                     return error;
    1914             :                 }
    1915             : 
    1916         180 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    1917             :                 {
    1918         120 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1919             :                 }
    1920             :             }
    1921             : 
    1922          15 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1923             :             {
    1924           0 :                 return error;
    1925             :             }
    1926             :         }
    1927         123 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    1928             :         {
    1929             :             /* init EFAP for custom LS setup */
    1930          30 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1931             :             {
    1932           0 :                 if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth, st_ivas->hLsSetupCustom->ls_elevation, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    1933             :                 {
    1934           0 :                     return error;
    1935             :                 }
    1936             :             }
    1937             : 
    1938          30 :             if ( ( error = ivas_param_mc_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1939             :             {
    1940           0 :                 return error;
    1941             :             }
    1942             : 
    1943          60 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1944             :             {
    1945          30 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    1946             :                 {
    1947           0 :                     return error;
    1948             :                 }
    1949             : 
    1950          90 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    1951             :                 {
    1952          60 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1953             :                 }
    1954             :             }
    1955             : 
    1956          30 :             if ( st_ivas->nCPE > 1 )
    1957             :             {
    1958           0 :                 if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1959             :                 {
    1960           0 :                     return error;
    1961             :                 }
    1962             :             }
    1963             :         }
    1964          93 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    1965             :         {
    1966             :             int32_t brate_sce, brate_cpe;
    1967             : 
    1968          93 :             ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    1969             : 
    1970          93 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1971             :             {
    1972           0 :                 return error;
    1973             :             }
    1974             : 
    1975          93 :             if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1976             :             {
    1977           0 :                 return error;
    1978             :             }
    1979             : 
    1980          93 :             st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1981             : 
    1982          93 :             if ( st_ivas->renderer_type != RENDERER_DISABLE && st_ivas->renderer_type != RENDERER_MCMASA_MONO_STEREO )
    1983             :             {
    1984          72 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1985             :                 {
    1986           0 :                     return error;
    1987             :                 }
    1988             :             }
    1989             : 
    1990          93 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.separateChannelEnabled && !st_ivas->hLsSetupCustom->separate_ch_found )
    1991             :             {
    1992             :                 /* If no speaker matching the separated channel, compute panning gains for the separated channel. */
    1993           0 :                 if ( st_ivas->hVBAPdata == NULL )
    1994             :                 {
    1995             :                     /* Distribute signal to all channels if VBAP is not properly initialized. */
    1996           0 :                     set_f( st_ivas->hLsSetupCustom->separate_ch_gains, inv_sqrt( st_ivas->hLsSetupCustom->num_spk ), st_ivas->hLsSetupCustom->num_spk );
    1997             :                 }
    1998             :                 else
    1999             :                 {
    2000           0 :                     vbap_determine_gains( st_ivas->hVBAPdata, st_ivas->hLsSetupCustom->separate_ch_gains, 0, 0, 0 );
    2001             :                 }
    2002             :             }
    2003             : 
    2004          93 :             ivas_mcmasa_split_brate( st_ivas->hOutSetup.separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
    2005             : 
    2006         180 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2007             :             {
    2008          87 :                 if ( ( error = create_sce_dec( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
    2009             :                 {
    2010           0 :                     return error;
    2011             :                 }
    2012             : 
    2013          87 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2014             :             }
    2015             : 
    2016         108 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2017             :             {
    2018          15 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT; /* element_mode_init was IVAS_SCE for SCE initialization */
    2019             : 
    2020          15 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
    2021             :                 {
    2022           0 :                     return error;
    2023             :                 }
    2024             : 
    2025          45 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2026             :                 {
    2027          30 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2028             :                 }
    2029             :             }
    2030             : 
    2031             :             /* create CPE element for DFT Stereo like upmix */
    2032          93 :             if ( st_ivas->sba_dirac_stereo_flag )
    2033             :             {
    2034           9 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    2035             :                 {
    2036           0 :                     return error;
    2037             :                 }
    2038             : 
    2039           9 :                 st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
    2040           9 :                 st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    2041             :             }
    2042             : 
    2043             :             /* set CNA/CNG flags */
    2044          93 :             if ( st_ivas->nchan_transport == 1 && ( ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) )
    2045             :             {
    2046          42 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag = 1;
    2047          42 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag = 1;
    2048             :             }
    2049             :         }
    2050             :     }
    2051             : #ifdef DEBUGGING
    2052             :     else
    2053             :     {
    2054             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Invalid IVAS format. Exiting,\n" );
    2055             :     }
    2056             : #endif
    2057             : 
    2058             : 
    2059             :     /*-----------------------------------------------------------------*
    2060             :      * Allocate and initialize HP20 filter memories
    2061             :      *-----------------------------------------------------------------*/
    2062             : 
    2063             :     /* set number of output channels used for synthesis/decoding */
    2064        1884 :     n = getNumChanSynthesis( st_ivas );
    2065             : 
    2066        1884 :     if ( n > 0 )
    2067             :     {
    2068        1884 :         if ( ( st_ivas->mem_hp20_out = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
    2069             :         {
    2070           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2071             :         }
    2072             :     }
    2073             :     else
    2074             :     {
    2075           0 :         st_ivas->mem_hp20_out = NULL;
    2076             :     }
    2077             : 
    2078        6876 :     for ( i = 0; i < n; i++ )
    2079             :     {
    2080        4992 :         if ( ( st_ivas->mem_hp20_out[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
    2081             :         {
    2082           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2083             :         }
    2084             : 
    2085        4992 :         set_f( st_ivas->mem_hp20_out[i], 0.0f, L_HP20_MEM );
    2086             :     }
    2087             : 
    2088             :     /*-------------------------------------------------------------------*
    2089             :      * Allocate and initialize rendering handles
    2090             :      *--------------------------------------------------------------------*/
    2091             : 
    2092        1884 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    2093             :     {
    2094             : 
    2095         120 :         if ( ( error = ivas_binRenderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2096             :         {
    2097           0 :             return error;
    2098             :         }
    2099             :     }
    2100             : 
    2101             :     /* ParamISM is handled separately from other common config */
    2102        1764 :     else if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) )
    2103             :     {
    2104          24 :         if ( st_ivas->renderer_type != RENDERER_STEREO_PARAMETRIC )
    2105             :         {
    2106          24 :             if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
    2107             :             {
    2108           0 :                 return error;
    2109             :             }
    2110             :         }
    2111             : 
    2112          24 :         if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
    2113             :         {
    2114           0 :             return error;
    2115             :         }
    2116             :     }
    2117        1740 :     else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    2118             :     {
    2119          78 :         if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2120             :         {
    2121           0 :             return error;
    2122             :         }
    2123             : 
    2124          78 :         if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2125             :         {
    2126           9 :             if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    2127             :             {
    2128           0 :                 return error;
    2129             :             }
    2130             :         }
    2131             :     }
    2132        1662 :     else if ( st_ivas->renderer_type == RENDERER_MC )
    2133             :     {
    2134          18 :         if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
    2135             :         {
    2136           0 :             return error;
    2137             :         }
    2138             :     }
    2139        1644 :     else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
    2140             :     {
    2141          27 :         if ( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2142             :         {
    2143           0 :             return error;
    2144             :         }
    2145             :     }
    2146        1617 :     else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    2147             :     {
    2148          90 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM && st_ivas->ivas_format == MC_FORMAT && ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation ) )
    2149             :         {
    2150          12 :             if ( ( error = efap_init_data( &( st_ivas->hEFAPdata ), st_ivas->hIntSetup.ls_azimuth, st_ivas->hIntSetup.ls_elevation, st_ivas->hIntSetup.nchan_out_woLFE, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    2151             :             {
    2152           0 :                 return error;
    2153             :             }
    2154             :         }
    2155             : 
    2156         180 :         if ( ( error = ivas_rend_openCrend( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, st_ivas->hDecoderConfig->output_config,
    2157         180 :                                             st_ivas->hRenderConfig, st_ivas->hHrtfCrend, st_ivas->hHrtfStatistics, st_ivas->hDecoderConfig->output_Fs, 0, ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses ) ) != IVAS_ERR_OK )
    2158             :         {
    2159           0 :             return error;
    2160             :         }
    2161             : 
    2162          90 :         st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
    2163             :     }
    2164             : 
    2165        1884 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2166             :     {
    2167         111 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2168             :         {
    2169             :             /* Allocate TD renderer for the objects in DISC mode */
    2170           9 :             if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2171             :             {
    2172           0 :                 return error;
    2173             :             }
    2174             : 
    2175           9 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2176             :             {
    2177           0 :                 if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, output_Fs ) ) != IVAS_ERR_OK )
    2178             :                 {
    2179           0 :                     return error;
    2180             :                 }
    2181             :             }
    2182             : 
    2183             :             /* Allocate memory for delay buffer within 'hMasaIsmData' */
    2184           9 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2185             :             {
    2186           0 :                 return error;
    2187             :             }
    2188             :         }
    2189             : 
    2190         111 :         if ( ( st_ivas->renderer_type == RENDERER_DIRAC ) &&
    2191          30 :              ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    2192             :         {
    2193             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2194          30 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2195             :             {
    2196           0 :                 return error;
    2197             :             }
    2198          30 :             if ( ( error = ivas_omasa_separate_object_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2199             :             {
    2200           0 :                 return error;
    2201             :             }
    2202             :         }
    2203             : 
    2204         111 :         if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
    2205             :         {
    2206             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2207           3 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2208             :             {
    2209           0 :                 return error;
    2210             :             }
    2211             : 
    2212           3 :             if ( ( error = ivas_spat_hSpatParamRendCom_config( &st_ivas->hSpatParamRendCom, DIRAC_OPEN, 0,
    2213             :                                                                st_ivas->ivas_format, st_ivas->mc_mode, output_Fs, 0, 0 ) ) != IVAS_ERR_OK )
    2214             :             {
    2215           0 :                 return error;
    2216             :             }
    2217             :         }
    2218             : 
    2219         111 :         if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
    2220             :         {
    2221             :             /* Allocate 'hIsmRendererData' handle */
    2222           0 :             if ( ( error = ivas_omasa_combine_separate_ism_with_masa_open( st_ivas ) ) != IVAS_ERR_OK )
    2223             :             {
    2224           0 :                 return error;
    2225             :             }
    2226             :         }
    2227             :     }
    2228             : 
    2229        1884 :     if ( ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) &&
    2230         333 :          ( st_ivas->ism_mode == ISM_MODE_DISC || st_ivas->ism_mode == ISM_SBA_MODE_DISC ) &&
    2231         228 :          ( st_ivas->renderer_type == RENDERER_TD_PANNING ||
    2232         207 :            st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX ||
    2233         204 :            st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ||
    2234         192 :            st_ivas->renderer_type == RENDERER_OSBA_STEREO ||
    2235         189 :            st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
    2236         183 :            st_ivas->renderer_type == RENDERER_OSBA_LS ||
    2237         177 :            st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    2238         156 :            st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    2239         144 :            st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2240             :     {
    2241         105 :         if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2242             :         {
    2243           0 :             return error;
    2244             :         }
    2245             :     }
    2246             : 
    2247        1884 :     if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2248             :     {
    2249         111 :         if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ) && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2250             :         {
    2251             :             /* Allocate TD renderer for the objects in DISC mode */
    2252          21 :             if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2253             :             {
    2254           0 :                 return error;
    2255             :             }
    2256             : 
    2257          21 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2258             :             {
    2259           6 :                 if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    2260             :                 {
    2261           0 :                     return error;
    2262             :                 }
    2263             :             }
    2264             :         }
    2265             :     }
    2266             : 
    2267             :     /*-----------------------------------------------------------------*
    2268             :      * CLDFB handles for rendering
    2269             :      *-----------------------------------------------------------------*/
    2270             : 
    2271        1884 :     ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses, &numCldfbSyntheses );
    2272             : 
    2273        6489 :     for ( i = 0; i < numCldfbAnalyses; i++ )
    2274             :     {
    2275        4605 :         if ( ( error = openCldfb( &( st_ivas->cldfbAnaDec[i] ), CLDFB_ANALYSIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    2276             :         {
    2277           0 :             return error;
    2278             :         }
    2279             :     }
    2280       27423 :     for ( ; i < MAX_INTERN_CHANNELS; i++ )
    2281             :     {
    2282       25539 :         st_ivas->cldfbAnaDec[i] = NULL;
    2283             :     }
    2284             : 
    2285        8847 :     for ( i = 0; i < numCldfbSyntheses; i++ )
    2286             :     {
    2287        6963 :         if ( ( error = openCldfb( &( st_ivas->cldfbSynDec[i] ), CLDFB_SYNTHESIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    2288             :         {
    2289           0 :             return error;
    2290             :         }
    2291             :     }
    2292       25065 :     for ( ; i < MAX_OUTPUT_CHANNELS; i++ )
    2293             :     {
    2294       23181 :         st_ivas->cldfbSynDec[i] = NULL;
    2295             :     }
    2296             : 
    2297             :     /* CLDFB Interpolation weights */
    2298        1884 :     if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) && !st_ivas->sba_dirac_stereo_flag && st_ivas->hDecoderConfig->nchan_out != 1 )
    2299             :     {
    2300         804 :         ivas_spar_get_cldfb_gains( st_ivas->hSpar, st_ivas->cldfbAnaDec[0], st_ivas->cldfbSynDec[0], hDecoderConfig );
    2301             :     }
    2302             : 
    2303             :     /*-----------------------------------------------------------------*
    2304             :      * LFE handles for rendering after rendering to adjust LFE delay to filter delay
    2305             :      *-----------------------------------------------------------------*/
    2306             : 
    2307        1884 :     if ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2308             :     {
    2309         123 :         if ( st_ivas->hIntSetup.index_lfe[0] != -1 )
    2310             :         {
    2311         111 :             delay_ns = st_ivas->binaural_latency_ns;
    2312             :         }
    2313             :         else
    2314             :         {
    2315          12 :             delay_ns = 0;
    2316             :         }
    2317             : 
    2318         123 :         if ( st_ivas->hBinRenderer != NULL )
    2319             :         {
    2320          12 :             if ( st_ivas->hBinRenderer->render_lfe )
    2321             :             {
    2322           3 :                 if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2323             :                 {
    2324             :                     /* Account for filterbank delay */
    2325           3 :                     delay_ns += IVAS_FB_DEC_DELAY_NS;
    2326             :                 }
    2327             :             }
    2328             :             else
    2329             :             {
    2330           9 :                 delay_ns = 0;
    2331             :             }
    2332             :         }
    2333             :         else
    2334             :         {
    2335         111 :             if ( ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) && ( st_ivas->cldfbSynDec[0] != NULL ) )
    2336             :             {
    2337           3 :                 delay_ns += IVAS_FB_DEC_DELAY_NS;
    2338             :             }
    2339             :         }
    2340             : 
    2341         123 :         if ( ( error = ivas_create_lfe_dec( &st_ivas->hLFE, output_Fs, delay_ns ) ) != IVAS_ERR_OK )
    2342             :         {
    2343           0 :             return error;
    2344             :         }
    2345             : 
    2346         123 :         set_zero( st_ivas->hLFE->prevsynth_buf, LFE_PLC_BUFLEN );
    2347         123 :         set_zero( st_ivas->hLFE->prior_out_buffer, L_FRAME48k );
    2348             :     }
    2349             : 
    2350             :     /*-----------------------------------------------------------------*
    2351             :      * Allocate and initialize limiter struct
    2352             :      *-----------------------------------------------------------------*/
    2353             : 
    2354        1884 :     if ( ( error = ivas_limiter_open( &st_ivas->hLimiter, hDecoderConfig->nchan_out, output_Fs ) ) != IVAS_ERR_OK )
    2355             :     {
    2356           0 :         return error;
    2357             :     }
    2358             : 
    2359             :     /*-----------------------------------------------------------------*
    2360             :      * Allocate and initialize JBM struct + buffer
    2361             :      *-----------------------------------------------------------------*/
    2362             : 
    2363        1884 :     if ( st_ivas->hTcBuffer == NULL )
    2364             :     {
    2365             :         /* no module has yet open the TC buffer, open a default one */
    2366         576 :         granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), output_Fs );
    2367         576 :         n_channels_transport_jbm = ivas_jbm_dec_get_num_tc_channels( st_ivas );
    2368             : 
    2369         576 :         if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, ivas_jbm_dec_get_tc_buffer_mode( st_ivas ), n_channels_transport_jbm, n_channels_transport_jbm, n_channels_transport_jbm, granularity ) ) != IVAS_ERR_OK )
    2370             :         {
    2371           0 :             return error;
    2372             :         }
    2373             :     }
    2374             : 
    2375        1884 :     if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && hDecoderConfig->Opt_tsm )
    2376             :     {
    2377          54 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    2378             :         {
    2379           9 :             if ( ( error = ivas_jbm_dec_metadata_open( st_ivas ) ) != IVAS_ERR_OK )
    2380             :             {
    2381           0 :                 return error;
    2382             :             }
    2383             :         }
    2384             :     }
    2385             : 
    2386             :     /*-----------------------------------------------------------------*
    2387             :      * Allocate floating-point output audio buffers
    2388             :      *-----------------------------------------------------------------*/
    2389             : 
    2390        1884 :     nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, ivas_total_brate );
    2391        1884 :     if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff, hDecoderConfig->Opt_tsm, st_ivas->hTcBuffer ) ) != IVAS_ERR_OK )
    2392             :     {
    2393           0 :         return error;
    2394             :     }
    2395             : 
    2396        1884 :     return IVAS_ERR_OK;
    2397             : }
    2398             : 
    2399             : 
    2400             : /*-------------------------------------------------------------------------
    2401             :  * destroy_core_dec()
    2402             :  *
    2403             :  * Close core decoder handles
    2404             :  *-------------------------------------------------------------------------*/
    2405             : 
    2406       26508 : void destroy_core_dec(
    2407             :     DEC_CORE_HANDLE hCoreCoder /* i/o: core decoder structure      */
    2408             : )
    2409             : {
    2410       26508 :     destroy_cldfb_decoder( hCoreCoder );
    2411             : 
    2412       26508 :     if ( hCoreCoder->hGSCDec != NULL )
    2413             :     {
    2414        9201 :         free( hCoreCoder->hGSCDec );
    2415        9201 :         hCoreCoder->hGSCDec = NULL;
    2416             :     }
    2417             : 
    2418       26508 :     if ( hCoreCoder->hPFstat != NULL )
    2419             :     {
    2420        9201 :         free( hCoreCoder->hPFstat );
    2421        9201 :         hCoreCoder->hPFstat = NULL;
    2422             :     }
    2423             : 
    2424       26508 :     if ( hCoreCoder->hMusicPF != NULL )
    2425             :     {
    2426        9201 :         free( hCoreCoder->hMusicPF );
    2427        9201 :         hCoreCoder->hMusicPF = NULL;
    2428             :     }
    2429             : 
    2430       26508 :     if ( hCoreCoder->hBPF != NULL )
    2431             :     {
    2432        9201 :         free( hCoreCoder->hBPF );
    2433        9201 :         hCoreCoder->hBPF = NULL;
    2434             :     }
    2435             : 
    2436       26508 :     if ( hCoreCoder->hBWE_zero != NULL )
    2437             :     {
    2438        9201 :         free( hCoreCoder->hBWE_zero );
    2439        9201 :         hCoreCoder->hBWE_zero = NULL;
    2440             :     }
    2441             : 
    2442       26508 :     if ( hCoreCoder->hTdCngDec != NULL )
    2443             :     {
    2444        4500 :         free( hCoreCoder->hTdCngDec );
    2445        4500 :         hCoreCoder->hTdCngDec = NULL;
    2446             :     }
    2447             : 
    2448       26508 :     if ( hCoreCoder->hSC_VBR != NULL )
    2449             :     {
    2450           9 :         free( hCoreCoder->hSC_VBR );
    2451           9 :         hCoreCoder->hSC_VBR = NULL;
    2452             :     }
    2453             : 
    2454       26508 :     if ( hCoreCoder->hAmrwb_IO != NULL )
    2455             :     {
    2456           9 :         free( hCoreCoder->hAmrwb_IO );
    2457           9 :         hCoreCoder->hAmrwb_IO = NULL;
    2458             :     }
    2459             : 
    2460       26508 :     if ( hCoreCoder->hBWE_TD != NULL )
    2461             :     {
    2462        9285 :         free( hCoreCoder->hBWE_TD );
    2463        9285 :         hCoreCoder->hBWE_TD = NULL;
    2464             :     }
    2465             : 
    2466       26508 :     if ( hCoreCoder->hBWE_FD != NULL )
    2467             :     {
    2468        9285 :         free( hCoreCoder->hBWE_FD );
    2469        9285 :         hCoreCoder->hBWE_FD = NULL;
    2470             :     }
    2471             : 
    2472       26508 :     if ( hCoreCoder->hBWE_FD_HR != NULL )
    2473             :     {
    2474           9 :         free( hCoreCoder->hBWE_FD_HR );
    2475           9 :         hCoreCoder->hBWE_FD_HR = NULL;
    2476             :     }
    2477             : 
    2478       26508 :     if ( hCoreCoder->hWIDec != NULL )
    2479             :     {
    2480           0 :         free( hCoreCoder->hWIDec );
    2481           0 :         hCoreCoder->hWIDec = NULL;
    2482             :     }
    2483             : 
    2484       26508 :     if ( hCoreCoder->hTECDec != NULL )
    2485             :     {
    2486           9 :         free( hCoreCoder->hTECDec );
    2487           9 :         hCoreCoder->hTECDec = NULL;
    2488             :     }
    2489             : 
    2490       26508 :     if ( hCoreCoder->hTcxLtpDec != NULL )
    2491             :     {
    2492       26262 :         free( hCoreCoder->hTcxLtpDec );
    2493       26262 :         hCoreCoder->hTcxLtpDec = NULL;
    2494             :     }
    2495             : 
    2496       26508 :     if ( hCoreCoder->hTcxDec != NULL )
    2497             :     {
    2498       26262 :         free( hCoreCoder->hTcxDec );
    2499       26262 :         hCoreCoder->hTcxDec = NULL;
    2500             :     }
    2501             : 
    2502       26508 :     if ( hCoreCoder->hTcxCfg != NULL )
    2503             :     {
    2504       26262 :         free( hCoreCoder->hTcxCfg );
    2505       26262 :         hCoreCoder->hTcxCfg = NULL;
    2506             :     }
    2507             : 
    2508       26508 :     if ( hCoreCoder->hTonalMDCTConc != NULL )
    2509             :     {
    2510       26262 :         free( hCoreCoder->hTonalMDCTConc );
    2511       26262 :         hCoreCoder->hTonalMDCTConc = NULL;
    2512             :     }
    2513             : 
    2514       26508 :     if ( hCoreCoder->hIGFDec != NULL )
    2515             :     {
    2516       26262 :         free( hCoreCoder->hIGFDec );
    2517       26262 :         hCoreCoder->hIGFDec = NULL;
    2518             :     }
    2519             : 
    2520       26508 :     if ( hCoreCoder->hPlcInfo != NULL )
    2521             :     {
    2522           9 :         free( hCoreCoder->hPlcInfo );
    2523           9 :         hCoreCoder->hPlcInfo = NULL;
    2524             :     }
    2525             : 
    2526       26508 :     if ( hCoreCoder->hHQ_core != NULL )
    2527             :     {
    2528       26262 :         free( hCoreCoder->hHQ_core );
    2529       26262 :         hCoreCoder->hHQ_core = NULL;
    2530             :     }
    2531             : 
    2532       26508 :     if ( hCoreCoder->hHQ_nbfec != NULL )
    2533             :     {
    2534           9 :         free( hCoreCoder->hHQ_nbfec );
    2535           9 :         hCoreCoder->hHQ_nbfec = NULL;
    2536             :     }
    2537             : 
    2538       26508 :     return;
    2539             : }
    2540             : 
    2541             : 
    2542             : /*-------------------------------------------------------------------------
    2543             :  * ivas_initialize_handles_dec()
    2544             :  *
    2545             :  * NULL initialization of handles
    2546             :  *-------------------------------------------------------------------------*/
    2547             : 
    2548        1884 : void ivas_initialize_handles_dec(
    2549             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                  */
    2550             : )
    2551             : {
    2552             :     int16_t i;
    2553             : 
    2554       32028 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    2555             :     {
    2556       30144 :         st_ivas->cldfbAnaDec[i] = NULL;
    2557             :     }
    2558             : 
    2559       32028 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    2560             :     {
    2561       30144 :         st_ivas->cldfbSynDec[i] = NULL;
    2562             :     }
    2563             : 
    2564             :     /* SCE handles */
    2565        9420 :     for ( i = 0; i < MAX_SCE; i++ )
    2566             :     {
    2567        7536 :         st_ivas->hSCE[i] = NULL;
    2568             :     }
    2569             : 
    2570             :     /* CPE handles */
    2571       13188 :     for ( i = 0; i < MAX_CPE; i++ )
    2572             :     {
    2573       11304 :         st_ivas->hCPE[i] = NULL;
    2574             :     }
    2575             : 
    2576        1884 :     st_ivas->bit_stream = NULL;
    2577        1884 :     st_ivas->mem_hp20_out = NULL;
    2578        1884 :     st_ivas->hLimiter = NULL;
    2579             : 
    2580             :     /* ISM metadata handles */
    2581        9420 :     for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
    2582             :     {
    2583        7536 :         st_ivas->hIsmMetaData[i] = NULL;
    2584             :     }
    2585             : 
    2586             :     /* spatial coding handles */
    2587        1884 :     st_ivas->hDirAC = NULL;
    2588        1884 :     st_ivas->hParamIsmDec = NULL;
    2589        1884 :     st_ivas->hSpar = NULL;
    2590        1884 :     st_ivas->hMasa = NULL;
    2591        1884 :     st_ivas->hQMetaData = NULL;
    2592        1884 :     st_ivas->hMCT = NULL;
    2593        1884 :     st_ivas->hMCParamUpmix = NULL;
    2594        1884 :     st_ivas->hParamMC = NULL;
    2595        1884 :     st_ivas->hLFE = NULL;
    2596             : 
    2597             :     /* rendering handles */
    2598        1884 :     st_ivas->hBinRenderer = NULL;
    2599       16956 :     for ( i = 0; i < MAX_HEAD_ROT_POSES; i++ )
    2600             :     {
    2601       15072 :         st_ivas->hDiracDecBin[i] = NULL;
    2602             :     }
    2603        1884 :     st_ivas->hDirACRend = NULL;
    2604        1884 :     st_ivas->hSpatParamRendCom = NULL;
    2605        1884 :     st_ivas->hLsSetUpConversion = NULL;
    2606        1884 :     st_ivas->hEFAPdata = NULL;
    2607        1884 :     st_ivas->hVBAPdata = NULL;
    2608        1884 :     st_ivas->hIsmRendererData = NULL;
    2609        1884 :     st_ivas->hBinRendererTd = NULL;
    2610        1884 :     st_ivas->hMonoDmxRenderer = NULL;
    2611        1884 :     st_ivas->hCrendWrapper = NULL;
    2612        1884 :     st_ivas->hReverb = NULL;
    2613        1884 :     st_ivas->hHrtfCrend = NULL;
    2614        1884 :     st_ivas->hHrtfFastConv = NULL;
    2615        1884 :     st_ivas->hHrtfParambin = NULL;
    2616        1884 :     st_ivas->hHrtfStatistics = NULL;
    2617        1884 :     st_ivas->hoa_dec_mtx = NULL;
    2618        1884 :     st_ivas->hMasaIsmData = NULL;
    2619        1884 :     st_ivas->hSbaIsmData = NULL;
    2620             : 
    2621        1884 :     st_ivas->hHeadTrackData = NULL;
    2622        1884 :     st_ivas->hHrtfTD = NULL;
    2623        1884 :     st_ivas->hLsSetupCustom = NULL;
    2624        1884 :     st_ivas->hRenderConfig = NULL;
    2625        1884 :     st_ivas->hExtOrientationData = NULL;
    2626        1884 :     st_ivas->hCombinedOrientationData = NULL;
    2627             : 
    2628        1884 :     st_ivas->hSplitBinRend = NULL;
    2629       15072 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2630             :     {
    2631       13188 :         st_ivas->hTdRendHandles[i] = NULL;
    2632             :     }
    2633             : 
    2634             :     /* JBM handles */
    2635        1884 :     st_ivas->hTcBuffer = NULL;
    2636        1884 :     st_ivas->hJbmMetadata = NULL;
    2637             : 
    2638             :     /*  floating-point output audio buffers */
    2639       39564 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    2640             :     {
    2641       37680 :         st_ivas->p_output_f[i] = NULL;
    2642             :     }
    2643             : 
    2644        1884 :     return;
    2645             : }
    2646             : 
    2647             : 
    2648             : /*-------------------------------------------------------------------------
    2649             :  * ivas_destroy_dec()
    2650             :  *
    2651             :  * Close IVAS decoder handles
    2652             :  *-------------------------------------------------------------------------*/
    2653             : 
    2654        1884 : void ivas_destroy_dec(
    2655             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle      */
    2656             : )
    2657             : {
    2658             :     int16_t i;
    2659             : 
    2660             :     /* CLDFB handles */
    2661       32028 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    2662             :     {
    2663       30144 :         if ( st_ivas->cldfbAnaDec[i] != NULL )
    2664             :         {
    2665        4710 :             deleteCldfb( &( st_ivas->cldfbAnaDec[i] ) );
    2666             :         }
    2667             :     }
    2668             : 
    2669       32028 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    2670             :     {
    2671       30144 :         if ( st_ivas->cldfbSynDec[i] != NULL )
    2672             :         {
    2673        6966 :             deleteCldfb( &( st_ivas->cldfbSynDec[i] ) );
    2674             :         }
    2675             :     }
    2676             : 
    2677             :     /* SCE handles */
    2678        9420 :     for ( i = 0; i < MAX_SCE; i++ )
    2679             :     {
    2680        7536 :         if ( st_ivas->hSCE[i] != NULL )
    2681             :         {
    2682        1227 :             destroy_sce_dec( st_ivas->hSCE[i] );
    2683        1227 :             st_ivas->hSCE[i] = NULL;
    2684             :         }
    2685             :     }
    2686             : 
    2687             :     /* CPE handles */
    2688       13188 :     for ( i = 0; i < MAX_CPE; i++ )
    2689             :     {
    2690       11304 :         if ( st_ivas->hCPE[i] != NULL )
    2691             :         {
    2692             :             /* set pointer to NULL as core coder already deallocated in destroy_sce_dec() */
    2693        2265 :             if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nchan_transport == 1 )
    2694             :             {
    2695          15 :                 st_ivas->hCPE[i]->hCoreCoder[0] = NULL;
    2696          15 :                 st_ivas->hCPE[i]->hCoreCoder[1] = NULL;
    2697             :             }
    2698        2265 :             destroy_cpe_dec( st_ivas->hCPE[i] );
    2699        2265 :             st_ivas->hCPE[i] = NULL;
    2700             :         }
    2701             :     }
    2702             : 
    2703             :     /* HP20 filter handles */
    2704        1884 :     if ( st_ivas->mem_hp20_out != NULL )
    2705             :     {
    2706        7428 :         for ( i = 0; i < getNumChanSynthesis( st_ivas ); i++ )
    2707             :         {
    2708        5544 :             free( st_ivas->mem_hp20_out[i] );
    2709        5544 :             st_ivas->mem_hp20_out[i] = NULL;
    2710             :         }
    2711        1884 :         free( st_ivas->mem_hp20_out );
    2712        1884 :         st_ivas->mem_hp20_out = NULL;
    2713             :     }
    2714             : 
    2715             :     /* ISM metadata handles */
    2716        1884 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    2717             : 
    2718             :     /* ISM renderer handle */
    2719        1884 :     ivas_ism_renderer_close( &( st_ivas->hIsmRendererData ) );
    2720             : 
    2721             :     /* DirAC handle */
    2722        1884 :     if ( st_ivas->ivas_format == ISM_FORMAT )
    2723             :     {
    2724         222 :         ivas_param_ism_dec_close( &( st_ivas->hParamIsmDec ), &( st_ivas->hSpatParamRendCom ), st_ivas->hDecoderConfig->output_config );
    2725             :     }
    2726             :     else
    2727             :     {
    2728        1662 :         ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
    2729        1662 :         ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
    2730        1662 :         ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
    2731             :     }
    2732             : 
    2733             :     /* SPAR handle */
    2734        1884 :     ivas_spar_dec_close( &( st_ivas->hSpar ), st_ivas->hDecoderConfig->output_Fs, 0 );
    2735             : 
    2736             :     /* HOA decoder matrix */
    2737        1884 :     if ( st_ivas->hoa_dec_mtx != NULL )
    2738             :     {
    2739          84 :         free( st_ivas->hoa_dec_mtx );
    2740          84 :         st_ivas->hoa_dec_mtx = NULL;
    2741             :     }
    2742             : 
    2743             :     /* MASA decoder structure */
    2744        1884 :     ivas_masa_dec_close( &( st_ivas->hMasa ) );
    2745             : 
    2746             :     /* Qmetadata handle */
    2747        1884 :     ivas_qmetadata_close( &st_ivas->hQMetaData );
    2748             : 
    2749             :     /* MCT handle */
    2750        1884 :     ivas_mct_dec_close( &st_ivas->hMCT );
    2751             : 
    2752             :     /* LFE handle */
    2753        1884 :     ivas_lfe_dec_close( &( st_ivas->hLFE ) );
    2754             : 
    2755             :     /* Param-Upmix MC handle */
    2756        1884 :     ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
    2757             : 
    2758             :     /* Parametric MC handle */
    2759        1884 :     ivas_param_mc_dec_close( &st_ivas->hParamMC );
    2760             : 
    2761             :     /* EFAP handle */
    2762        1884 :     efap_free_data( &st_ivas->hEFAPdata );
    2763             : 
    2764             :     /* VBAP handle */
    2765        1884 :     vbap_free_data( &( st_ivas->hVBAPdata ) );
    2766             : 
    2767             :     /* Fastconv binaural renderer handle */
    2768        1884 :     ivas_binRenderer_close( &st_ivas->hBinRenderer );
    2769             : 
    2770             :     /* TD binaural renderer handles */
    2771       15072 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2772             :     {
    2773       13188 :         if ( st_ivas->hTdRendHandles[i] != NULL )
    2774             :         {
    2775           0 :             st_ivas->hTdRendHandles[i]->HrFiltSet_p = NULL;
    2776           0 :             ivas_td_binaural_close( &st_ivas->hTdRendHandles[i] );
    2777             :         }
    2778             :     }
    2779             : 
    2780             :     /* Parametric binaural renderer handle */
    2781        1884 :     ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
    2782             : 
    2783             :     /* Crend handle */
    2784        1884 :     ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ) );
    2785             : 
    2786             :     /* Reverb handle */
    2787        1884 :     ivas_reverb_close( &st_ivas->hReverb );
    2788             : 
    2789             :     /* LS config converter handle */
    2790        1884 :     ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
    2791             : 
    2792             :     /* Custom LS configuration handle */
    2793        1884 :     if ( st_ivas->hLsSetupCustom != NULL )
    2794             :     {
    2795           9 :         free( st_ivas->hLsSetupCustom );
    2796           9 :         st_ivas->hLsSetupCustom = NULL;
    2797             :     }
    2798             : 
    2799             :     /* Mono downmix structure */
    2800        1884 :     ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
    2801             : 
    2802             :     /* OSBA structure */
    2803        1884 :     ivas_osba_data_close( &st_ivas->hSbaIsmData );
    2804             : 
    2805             :     /* OMASA structure */
    2806        1884 :     ivas_omasa_data_close( &st_ivas->hMasaIsmData );
    2807             : 
    2808             :     /* Head track data handle */
    2809        1884 :     ivas_headTrack_close( &st_ivas->hHeadTrackData );
    2810             : 
    2811             :     /* External orientation data handle */
    2812        1884 :     ivas_external_orientation_close( &st_ivas->hExtOrientationData );
    2813             : 
    2814             :     /* Combined orientation data handle */
    2815        1884 :     ivas_combined_orientation_close( &st_ivas->hCombinedOrientationData );
    2816             : 
    2817             :     /* Time Domain binaural renderer handle */
    2818        1884 :     if ( st_ivas->hBinRendererTd != NULL )
    2819             :     {
    2820         120 :         ivas_td_binaural_close( &st_ivas->hBinRendererTd );
    2821             :     }
    2822             : 
    2823        1884 :     if ( st_ivas->hHrtfTD != NULL )
    2824             :     {
    2825           0 :         BSplineModelEvalDealloc( &st_ivas->hHrtfTD->ModelParams, &st_ivas->hHrtfTD->ModelEval );
    2826             : 
    2827           0 :         ivas_HRTF_binary_close( &st_ivas->hHrtfTD );
    2828             :     }
    2829             : 
    2830             :     /* CRend binaural renderer handle */
    2831        1884 :     ivas_HRTF_CRend_binary_close( &st_ivas->hHrtfCrend );
    2832             : 
    2833             :     /* Fastconv HRTF memories */
    2834        1884 :     ivas_binaural_hrtf_close( &st_ivas->hHrtfFastConv );
    2835             : 
    2836             :     /* Fastconv HRTF filters */
    2837        1884 :     ivas_HRTF_fastconv_binary_close( &st_ivas->hHrtfFastConv );
    2838             : 
    2839             :     /* Parametric binauralizer HRTF filters */
    2840        1884 :     ivas_HRTF_parambin_binary_close( &st_ivas->hHrtfParambin );
    2841             : 
    2842             :     /* HRTF statistics */
    2843        1884 :     ivas_HRTF_statistics_close( &st_ivas->hHrtfStatistics );
    2844             : 
    2845             :     /* Config. Renderer */
    2846        1884 :     ivas_render_config_close( &( st_ivas->hRenderConfig ) );
    2847             : 
    2848             :     /* Limiter struct */
    2849        1884 :     ivas_limiter_close( &( st_ivas->hLimiter ) );
    2850             : 
    2851             :     /* Decoder configuration structure */
    2852        1884 :     if ( st_ivas->hDecoderConfig != NULL )
    2853             :     {
    2854        1884 :         free( st_ivas->hDecoderConfig );
    2855        1884 :         st_ivas->hDecoderConfig = NULL;
    2856             :     }
    2857             : 
    2858             :     /* JBM TC buffer structure */
    2859        1884 :     ivas_jbm_dec_tc_buffer_close( &st_ivas->hTcBuffer );
    2860             : 
    2861        1884 :     if ( st_ivas->hJbmMetadata != NULL )
    2862             :     {
    2863           9 :         free( st_ivas->hJbmMetadata );
    2864           9 :         st_ivas->hJbmMetadata = NULL;
    2865             :     }
    2866             : 
    2867             :     /* floating-point output audio buffers */
    2868       39564 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    2869             :     {
    2870       37680 :         st_ivas->p_output_f[i] = NULL;
    2871             :     }
    2872             : 
    2873             :     /* main IVAS handle */
    2874        1884 :     free( st_ivas );
    2875             : 
    2876        1884 :     return;
    2877             : }
    2878             : 
    2879             : 
    2880             : /*-------------------------------------------------------------------*
    2881             :  * ivas_init_dec_get_num_cldfb_instances()
    2882             :  *
    2883             :  * Return number of CLDFB analysis & synthesis instances
    2884             :  *-------------------------------------------------------------------*/
    2885             : 
    2886             : /*! r: number of cldfb instances */
    2887       48615 : void ivas_init_dec_get_num_cldfb_instances(
    2888             :     Decoder_Struct *st_ivas,   /* i  : IVAS decoder structure                     */
    2889             :     int16_t *numCldfbAnalyses, /* o  : number of needed CLDFB analysis instances  */
    2890             :     int16_t *numCldfbSyntheses /* o  : number of needed CLDFB synthesis instances */
    2891             : )
    2892             : {
    2893             :     IVAS_FORMAT ivas_format;
    2894       48615 :     *numCldfbAnalyses = st_ivas->nchan_transport;
    2895       48615 :     *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    2896             : 
    2897       48615 :     ivas_format = ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_NONE ) ? SBA_FORMAT : st_ivas->ivas_format; /* treat ISM_SBA_MODE_NONE just like SBA_FORMAT */
    2898             : 
    2899       48615 :     switch ( st_ivas->renderer_type )
    2900             :     {
    2901       15282 :         case RENDERER_BINAURAL_PARAMETRIC:
    2902             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    2903             :         case RENDERER_STEREO_PARAMETRIC:
    2904       15282 :             if ( st_ivas->nchan_transport == 1 )
    2905             :             {
    2906        3969 :                 *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    2907             :             }
    2908             : 
    2909       15282 :             if ( st_ivas->mc_mode == MC_MODE_MCMASA && st_ivas->hOutSetup.separateChannelEnabled )
    2910             :             {
    2911          48 :                 *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    2912             :             }
    2913             : 
    2914       15282 :             if ( ivas_format == SBA_ISM_FORMAT )
    2915             :             {
    2916           0 :                 if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2917             :                 {
    2918           0 :                     *numCldfbAnalyses += st_ivas->nchan_ism;
    2919             :                 }
    2920             :             }
    2921             : 
    2922       15282 :             if ( ivas_format == MASA_ISM_FORMAT )
    2923             :             {
    2924        4722 :                 if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2925             :                 {
    2926        1875 :                     *numCldfbAnalyses += st_ivas->nchan_ism;
    2927             :                 }
    2928        2847 :                 else if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    2929             :                 {
    2930        2847 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    2931             :                 }
    2932             :             }
    2933             : 
    2934       15282 :             if ( st_ivas->hDiracDecBin[0]->useTdDecorr )
    2935             :             {
    2936        4833 :                 *numCldfbAnalyses += 2;
    2937             :             }
    2938       15282 :             break;
    2939        1014 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    2940             :         case RENDERER_MONO_DOWNMIX:
    2941        1014 :             if ( ivas_format == ISM_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == SBA_ISM_FORMAT )
    2942             :             {
    2943             :                 /* CLDFB not used in rendering */
    2944         897 :                 *numCldfbAnalyses = 0;
    2945         897 :                 *numCldfbSyntheses = 0;
    2946             :             }
    2947        1014 :             break;
    2948       11028 :         case RENDERER_DIRAC:
    2949       11028 :             if ( ivas_format == SBA_FORMAT )
    2950             :             {
    2951         558 :                 *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    2952             : 
    2953         558 :                 if ( st_ivas->hOutSetup.is_loudspeaker_setup && st_ivas->renderer_type == RENDERER_DIRAC )
    2954             :                 {
    2955         558 :                     *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    2956             :                 }
    2957           0 :                 else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA )
    2958             :                 {
    2959           0 :                     *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    2960             :                 }
    2961             :                 else
    2962             :                 {
    2963           0 :                     *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    2964             :                 }
    2965             :             }
    2966       11028 :             if ( ivas_format != SBA_FORMAT )
    2967             :             {
    2968       10470 :                 if ( st_ivas->nchan_transport > 2 && st_ivas->sba_planar )
    2969             :                 {
    2970           0 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    2971             :                 }
    2972       10470 :                 else if ( st_ivas->nchan_transport == 1 && st_ivas->hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    2973             :                 {
    2974        1149 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    2975             :                 }
    2976             :             }
    2977       11028 :             break;
    2978         522 :         case RENDERER_MC_PARAMMC:
    2979         522 :             if ( st_ivas->hDecoderConfig->nchan_out <= 2 )
    2980             :             {
    2981             :                 /* CLDFB not used in rendering */
    2982          12 :                 *numCldfbAnalyses = 0;
    2983          12 :                 *numCldfbSyntheses = 0;
    2984             :             }
    2985             :             else
    2986             :             {
    2987         510 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses( st_ivas );
    2988             :             }
    2989         522 :             break;
    2990         321 :         case RENDERER_PARAM_ISM:
    2991             :             /* Already correct with no exception */
    2992         321 :             break;
    2993        5424 :         case RENDERER_DISABLE:
    2994             :             /* CLDFB not used */
    2995        5424 :             *numCldfbAnalyses = 0;
    2996        5424 :             *numCldfbSyntheses = 0;
    2997        5424 :             break;
    2998       12702 :         case RENDERER_MC:
    2999             :         case RENDERER_SBA_LINEAR_DEC:
    3000             :         case RENDERER_TD_PANNING:
    3001             :         case RENDERER_BINAURAL_OBJECTS_TD:
    3002             :         case RENDERER_MCMASA_MONO_STEREO:
    3003             :         case RENDERER_BINAURAL_MIXER_CONV:
    3004             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    3005             :         case RENDERER_BINAURAL_FASTCONV:
    3006             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    3007             :         case RENDERER_OSBA_STEREO:
    3008             :         case RENDERER_OSBA_AMBI:
    3009             :         case RENDERER_OSBA_LS:
    3010       12702 :             if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
    3011             :             {
    3012        6768 :                 if ( st_ivas->sba_dirac_stereo_flag )
    3013             :                 {
    3014         216 :                     *numCldfbAnalyses = 0;
    3015         216 :                     *numCldfbSyntheses = 0;
    3016             :                 }
    3017             :                 else
    3018             :                 {
    3019        6552 :                     *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3020             : 
    3021        6552 :                     if ( st_ivas->hOutSetup.is_loudspeaker_setup && st_ivas->renderer_type == RENDERER_DIRAC )
    3022             :                     {
    3023           0 :                         *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    3024             :                     }
    3025        6552 :                     else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA )
    3026             :                     {
    3027        1671 :                         *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    3028             :                     }
    3029             :                     else
    3030             :                     {
    3031        4881 :                         *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    3032             :                     }
    3033             : 
    3034        6552 :                     if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    3035             :                     {
    3036          12 :                         *numCldfbAnalyses = st_ivas->nchan_ism + st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3037             :                     }
    3038             :                 }
    3039             :             }
    3040        5934 :             else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    3041             :             {
    3042             :                 /* do nothing for ParamMC */
    3043             :             }
    3044             :             else
    3045             :             {
    3046             :                 /* CLDFB not used in rendering */
    3047        4857 :                 *numCldfbAnalyses = 0;
    3048        4857 :                 *numCldfbSyntheses = 0;
    3049             :             }
    3050       12702 :             break;
    3051        1986 :         case RENDERER_SBA_LINEAR_ENC:
    3052        1986 :             if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    3053             :             {
    3054         432 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses( st_ivas );
    3055             :             }
    3056        1554 :             else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    3057             :             {
    3058         135 :                 *numCldfbSyntheses = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    3059             :             }
    3060        1419 :             else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    3061             :             {
    3062         348 :                 *numCldfbAnalyses = st_ivas->nchan_transport;
    3063         348 :                 *numCldfbSyntheses = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    3064             :             }
    3065             :             else
    3066             :             {
    3067             :                 /* CLDFB not used in rendering */
    3068        1071 :                 *numCldfbAnalyses = 0;
    3069        1071 :                 *numCldfbSyntheses = 0;
    3070             :             }
    3071        1986 :             break;
    3072         174 :         case RENDERER_OMASA_OBJECT_EXT:
    3073         174 :             *numCldfbAnalyses = st_ivas->nchan_transport;
    3074         174 :             *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    3075         174 :             break;
    3076         162 :         case RENDERER_OMASA_MIX_EXT:
    3077         162 :             *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3078         162 :             *numCldfbSyntheses = 0;
    3079         162 :             break;
    3080           0 :         default:
    3081           0 :             assert( 0 && "Renderer not handled for CLDFB reservation." );
    3082             :     }
    3083             : 
    3084       48615 :     if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO )
    3085             :     {
    3086          42 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    3087             :         {
    3088          21 :             *numCldfbAnalyses = max( MC_PARAMUPMIX_MAX_INPUT_CHANS, *numCldfbAnalyses );
    3089             :         }
    3090             :         else
    3091             :         {
    3092          21 :             *numCldfbAnalyses = max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbAnalyses );
    3093             :         }
    3094          42 :         *numCldfbSyntheses = max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbSyntheses );
    3095             :     }
    3096             : 
    3097       48615 :     return;
    3098             : }
    3099             : 
    3100             : 
    3101             : /*---------------------------------------------------------------------*
    3102             :  * doSanityChecks_IVAS()
    3103             :  *
    3104             :  * Sanity checks - verify if the decoder set-up parameters are
    3105             :  *                 not in conflict with the IVAS format
    3106             :  *---------------------------------------------------------------------*/
    3107             : 
    3108        1875 : static ivas_error doSanityChecks_IVAS(
    3109             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    3110             : )
    3111             : {
    3112             :     int32_t output_Fs;
    3113             :     AUDIO_CONFIG output_config;
    3114             : 
    3115        1875 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    3116        1875 :     output_config = st_ivas->hDecoderConfig->output_config;
    3117             : 
    3118             :     /*-----------------------------------------------------------------*
    3119             :      * Sanity checks
    3120             :      *-----------------------------------------------------------------*/
    3121             : 
    3122        1875 :     if ( output_Fs == 8000 )
    3123             :     {
    3124           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz output sampling rate is not supported in IVAS." );
    3125             :     }
    3126             : 
    3127        1875 :     assert( st_ivas->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
    3128        1875 :     assert( st_ivas->ivas_format != MONO_FORMAT && "\n Wrong IVAS format: MONO" );
    3129             : 
    3130             :     /* Verify output configuration compatible with non-diegetic panning */
    3131        1875 :     if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan && ( st_ivas->ivas_format != MONO_FORMAT ) && ( st_ivas->transport_config != IVAS_AUDIO_CONFIG_ISM1 ) )
    3132             :     {
    3133           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Error: Non-diegetic panning not supported in this IVAS format" );
    3134             :     }
    3135             : 
    3136             :     /* Verify stereo output configuration */
    3137        1875 :     if ( st_ivas->ivas_format == STEREO_FORMAT )
    3138             :     {
    3139         204 :         if ( output_config != IVAS_AUDIO_CONFIG_MONO && output_config != IVAS_AUDIO_CONFIG_STEREO && output_config != IVAS_AUDIO_CONFIG_5_1 && output_config != IVAS_AUDIO_CONFIG_7_1 && output_config != IVAS_AUDIO_CONFIG_5_1_2 && output_config != IVAS_AUDIO_CONFIG_5_1_4 && output_config != IVAS_AUDIO_CONFIG_7_1_4 && output_config != IVAS_AUDIO_CONFIG_LS_CUSTOM && output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
    3140             :         {
    3141           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Wrong output configuration specified for Stereo!" );
    3142             :         }
    3143             :     }
    3144             :     /* Verify output configuration for other formats */
    3145             :     else
    3146             :     {
    3147        1671 :         if ( output_config == IVAS_AUDIO_CONFIG_INVALID )
    3148             :         {
    3149           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration specified!" );
    3150             :         }
    3151             :     }
    3152             : 
    3153        1875 :     if ( ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) && output_Fs != 48000 )
    3154             :     {
    3155           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Error: Only 48kHz output sampling rate is supported for split rendering." );
    3156             :     }
    3157             : 
    3158        1875 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    3159             :     {
    3160         249 :         if ( !( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    3161             :         {
    3162           0 :             return IVAS_ERROR( IVAS_ERR_HEAD_ROTATION_NOT_SUPPORTED, "Wrong set-up: Head-rotation not supported in this configuration" );
    3163             :         }
    3164             :     }
    3165             : 
    3166        1875 :     if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    3167             :     {
    3168          93 :         if ( !( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    3169             :         {
    3170           0 :             return IVAS_ERROR( IVAS_ERR_EXT_ORIENTATION_NOT_SUPPORTED, "Wrong set-up: External orientation not supported in this configuration" );
    3171             :         }
    3172             :     }
    3173             : 
    3174        1875 :     if ( st_ivas->hDecoderConfig->Opt_dpid_on )
    3175             :     {
    3176           6 :         if ( !( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    3177             :         {
    3178           0 :             return IVAS_ERROR( IVAS_ERR_DIRECTIVITY_NOT_SUPPORTED, "Wrong set-up: Directivity is not supported in this output configuration." );
    3179             :         }
    3180             :     }
    3181             : 
    3182        1875 :     if ( st_ivas->hDecoderConfig->Opt_aeid_on )
    3183             :     {
    3184          12 :         if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    3185             :         {
    3186           0 :             return IVAS_ERROR( IVAS_ERR_ACOUSTIC_ENVIRONMENT_NOT_SUPPORTED, "Wrong set-up: Acoustic environment is not supported in this output configuration." );
    3187             :         }
    3188             :     }
    3189             : 
    3190        1875 :     if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on )
    3191             :     {
    3192          39 :         if ( !( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) ) )
    3193             :         {
    3194           0 :             return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Wrong set-up: Object editing is not supported in this IVAS format." );
    3195             :         }
    3196             :     }
    3197             : 
    3198        1875 :     if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on & st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    3199             :     {
    3200           0 :         return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_AND_PANNING_NOT_SUPPORTED, "Wrong set-up: Only object editing or Non-diegetic panning can be used." );
    3201             :     }
    3202             : #ifdef DEBUGGING
    3203             :     if ( ( st_ivas->hDecoderConfig->force_rend == FORCE_TD_RENDERER ) && ( ( st_ivas->ivas_format != MC_FORMAT && st_ivas->ivas_format != ISM_FORMAT ) || ( output_config != IVAS_AUDIO_CONFIG_BINAURAL && output_config != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) || ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM ) || ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode != MC_MODE_MCT ) ) )
    3204             :     {
    3205             :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration: Time Domain object renderer not supported in this configuration" );
    3206             :     }
    3207             : 
    3208             :     if ( ( st_ivas->hHrtfTD != NULL && st_ivas->hDecoderConfig->force_rend == FORCE_CLDFB_RENDERER ) )
    3209             :     {
    3210             :         return IVAS_ERROR( IVAS_ERR_INVALID_FORCE_MODE, "Incorrect debug configuration: Cannot force CLDFB renderer in combination with TD renderer HRTF file" );
    3211             :     }
    3212             : #endif
    3213             : 
    3214        1875 :     return IVAS_ERR_OK;
    3215             : }

Generated by: LCOV version 1.14