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

Generated by: LCOV version 1.14