LCOV - code coverage report
Current view: top level - lib_dec - ivas_init_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 1133 1346 84.2 %
Date: 2026-02-12 08:06:51 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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        5702 : 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        5702 :     output_config = IVAS_AUDIO_CONFIG_HOA3;
      72             : 
      73        5702 :     switch ( sba_order )
      74             :     {
      75        1902 :         case SBA_FOA_ORDER:
      76        1902 :             output_config = IVAS_AUDIO_CONFIG_FOA;
      77        1902 :             break;
      78        1888 :         case SBA_HOA2_ORDER:
      79        1888 :             output_config = IVAS_AUDIO_CONFIG_HOA2;
      80        1888 :             break;
      81        1912 :         case SBA_HOA3_ORDER:
      82        1912 :             output_config = IVAS_AUDIO_CONFIG_HOA3;
      83        1912 :             break;
      84           0 :         default:
      85           0 :             output_config = IVAS_AUDIO_CONFIG_INVALID;
      86           0 :             break;
      87             :     }
      88             : 
      89        5702 :     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    20440998 : 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    20440998 :     num_bits_read = 0;
     115    20440998 :     element_mode_flag = 0;
     116             : 
     117    20440998 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     118    20440998 :     Opt_tsm = st_ivas->hDecoderConfig->Opt_tsm;
     119    20440998 :     bit_stream_orig = st_ivas->bit_stream;
     120             : 
     121             :     /*-------------------------------------------------------------------*
     122             :      * Read IVAS format
     123             :      *-------------------------------------------------------------------*/
     124             : 
     125    20440998 :     if ( ( error = ivas_read_format( st_ivas, &num_bits_read ) ) != IVAS_ERR_OK )
     126             :     {
     127           0 :         return error;
     128             :     }
     129             : 
     130    20440998 :     if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format != st_ivas->last_ivas_format &&
     131       94105 :          !( st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format == MASA_ISM_FORMAT ) &&
     132       47036 :          !( 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    20440998 :     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    20221152 :         if ( st_ivas->ivas_format == STEREO_FORMAT )
     164             :         {
     165     2279768 :             element_mode_flag = 1;
     166             :         }
     167    17941384 :         else if ( st_ivas->ivas_format == ISM_FORMAT )
     168             :         {
     169             :             /* read the number of objects */
     170     2962933 :             nchan_ism = 1;
     171     2962933 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 );
     172     8656299 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     173             :             {
     174     5693366 :                 nchan_ism++;
     175     5693366 :                 k--;
     176             :             }
     177             : 
     178     2962933 :             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     2962933 :             st_ivas->nchan_ism = nchan_ism;
     196     2962933 :             st_ivas->ism_mode = ivas_ism_mode_select( nchan_ism, ivas_total_brate );
     197             : 
     198     2962933 :             st_ivas->nchan_transport = nchan_ism;
     199     2962933 :             if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     200             :             {
     201      519091 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     202             :             }
     203             :         }
     204    14978451 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     205             :         {
     206             :             /* read Ambisonic (SBA) planar flag */
     207     6203951 :             sba_planar = st_ivas->bit_stream[num_bits_read];
     208     6203951 :             num_bits_read += SBA_PLANAR_BITS;
     209             : 
     210     6203951 :             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     6203951 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     228     6203951 :             sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     229             : 
     230     6203951 :             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     6203951 :             sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
     248     6203951 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_analysis_order );
     249             :         }
     250     8774500 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
     251             :         {
     252     2498365 :             uint8_t masaRestartCandidate = 0;
     253             : 
     254             :             /* read number of MASA transport channels */
     255     2498365 :             if ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] )
     256             :             {
     257     1432667 :                 if ( st_ivas->nchan_transport == 1 && Opt_tsm )
     258             :                 {
     259           0 :                     masaRestartCandidate = 1;
     260             :                 }
     261             : 
     262     1432667 :                 st_ivas->nchan_transport = 2;
     263     1432667 :                 element_mode_flag = 1;
     264             :             }
     265             :             else
     266             :             {
     267     1065698 :                 if ( st_ivas->nchan_transport == 2 && Opt_tsm )
     268             :                 {
     269       77010 :                     masaRestartCandidate = 1;
     270             :                 }
     271             : 
     272     1065698 :                 st_ivas->nchan_transport = 1;
     273             :             }
     274             : 
     275             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     276     2498365 :             st_ivas->ism_mode = ISM_MODE_NONE;
     277     2498365 :             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     2498365 :             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      326994 :                 nchan_ism = 5 - nchan_ism;
     290      326994 :                 if ( st_ivas->nchan_transport == 1 && nchan_ism == 2 )
     291             :                 {
     292       64355 :                     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      326994 :                 st_ivas->nchan_transport = 2;
     297      326994 :                 element_mode_flag = 1;
     298             :             }
     299     2171371 :             else if ( masaRestartCandidate > 0 )
     300             :             {
     301           0 :                 st_ivas->restartNeeded = 1;
     302             : 
     303           0 :                 return IVAS_ERR_OK;
     304             :             }
     305             : 
     306     2498365 :             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     2498365 :             st_ivas->nchan_ism = nchan_ism;
     324             :         }
     325     6276135 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     326             :         {
     327     1163616 :             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     1163616 :             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     1163616 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, nchan_ism );
     332             : 
     333     1163616 :             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     1163616 :             st_ivas->nchan_ism = nchan_ism;
     351             :         }
     352     5112519 :         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     2502474 :             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     2502474 :             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     2502474 :             st_ivas->nchan_ism = nchan_ism;
     375             : 
     376             :             /* read Ambisonic (SBA) planar flag */
     377             :             /*sba_planar = st_ivas->bit_stream[num_bits_read];*/
     378     2502474 :             num_bits_read += SBA_PLANAR_BITS;
     379             : 
     380             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     381     2502474 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     382     2502474 :             sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     383     2502474 :             num_bits_read += SBA_ORDER_BITS;
     384             : 
     385             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     386     2502474 :             if ( ivas_total_brate < IVAS_24k4 )
     387             :             {
     388      310969 :                 sba_order = st_ivas->bit_stream[num_bits_read + 1];
     389      310969 :                 sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     390      310969 :                 num_bits_read += SBA_ORDER_BITS;
     391             :             }
     392             : 
     393     2502474 :             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     2502474 :             st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );
     411             : 
     412     2502474 :             sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
     413     2502474 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_analysis_order );
     414             :         }
     415     2610045 :         else if ( st_ivas->ivas_format == MC_FORMAT )
     416             :         {
     417             :             /* read MC configuration */
     418     2610045 :             idx = 0;
     419    10440180 :             for ( k = 0; k < MC_LS_SETUP_BITS; k++ )
     420             :             {
     421     7830135 :                 if ( st_ivas->bit_stream[num_bits_read + k] )
     422             :                 {
     423     1426648 :                     idx += 1 << ( MC_LS_SETUP_BITS - 1 - k );
     424             :                 }
     425             :             }
     426     2610045 :             num_bits_read += MC_LS_SETUP_BITS;
     427             : 
     428     2610045 :             signaled_config = ivas_mc_map_ls_setup_to_output_config( idx );
     429             : 
     430     2610045 :             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     2610045 :             st_ivas->mc_mode = ivas_mc_mode_select( ivas_mc_map_output_config_to_mc_ls_setup( signaled_config ), ivas_total_brate );
     448     2610045 :             st_ivas->transport_config = signaled_config;
     449             :         }
     450             : 
     451             :         /*-------------------------------------------------------------------*
     452             :          * Read element mode
     453             :          *-------------------------------------------------------------------*/
     454             : 
     455    20221152 :         if ( st_ivas->ini_frame == 0 && element_mode_flag )
     456             :         {
     457             :             /* read stereo technology info */
     458        8133 :             if ( ivas_total_brate < MIN_BRATE_MDCT_STEREO )
     459             :             {
     460             :                 /* 1 bit */
     461        4876 :                 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        4876 :                     st_ivas->element_mode_init = 0 + IVAS_CPE_DFT;
     468             :                 }
     469             :             }
     470             :             else
     471             :             {
     472        3257 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     473             :             }
     474             :         }
     475             :     }
     476      219846 :     else if ( ivas_total_brate == IVAS_SID_5k2 )
     477             :     {
     478       36145 :         switch ( st_ivas->sid_format )
     479             :         {
     480       16595 :             case SID_DFT_STEREO:
     481       16595 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     482       16595 :                 break;
     483        4933 :             case SID_MDCT_STEREO:
     484        4933 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     485        4933 :                 break;
     486        8703 :             case SID_ISM:
     487        8703 :                 st_ivas->element_mode_init = IVAS_SCE;
     488        8703 :                 break;
     489        1913 :             case SID_MASA_1TC:
     490        1913 :                 st_ivas->element_mode_init = IVAS_SCE;
     491        1913 :                 st_ivas->nchan_transport = 1;
     492        1913 :                 break;
     493        1097 :             case SID_MASA_2TC:
     494        1097 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1 - SID_FORMAT_NBITS] == 1 )
     495             :                 {
     496         210 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     497             :                 }
     498             :                 else
     499             :                 {
     500         887 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     501             :                 }
     502        1097 :                 st_ivas->nchan_transport = 2;
     503        1097 :                 break;
     504        1801 :             case SID_SBA_1TC:
     505        1801 :                 st_ivas->element_mode_init = IVAS_SCE;
     506        1801 :                 break;
     507        1103 :             case SID_SBA_2TC:
     508        1103 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     509        1103 :                 break;
     510             :         }
     511             : 
     512       36145 :         if ( st_ivas->ivas_format == ISM_FORMAT )
     513             :         {
     514             :             /* read the number of objects */
     515        8703 :             nchan_ism = 1;
     516        8703 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 - SID_FORMAT_NBITS );
     517       23373 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     518             :             {
     519       14670 :                 nchan_ism++;
     520       14670 :                 k--;
     521             :             }
     522        8703 :             k--;
     523             : 
     524        8703 :             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        8703 :             st_ivas->nchan_ism = nchan_ism;
     542             : 
     543             :             /* read ism_mode */
     544        8703 :             st_ivas->ism_mode = ISM_MODE_DISC;
     545        8703 :             if ( nchan_ism > 2 )
     546             :             {
     547        4628 :                 k -= nchan_ism; /* SID metadata flags */
     548        4628 :                 idx = st_ivas->bit_stream[k];
     549        4628 :                 st_ivas->ism_mode = (ISM_MODE) ( idx + 1 );
     550             :             }
     551             : 
     552        8703 :             st_ivas->nchan_transport = nchan_ism;
     553        8703 :             if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     554             :             {
     555        1557 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     556             :             }
     557             :         }
     558             :     }
     559             : 
     560    20440998 :     st_ivas->bit_stream = bit_stream_orig;
     561             : 
     562    20440998 :     return IVAS_ERR_OK;
     563             : }
     564             : 
     565             : 
     566             : /*-------------------------------------------------------------------*
     567             :  * ivas_dec_setup()
     568             :  *
     569             :  * IVAS decoder setup
     570             :  *-------------------------------------------------------------------*/
     571             : 
     572    20456056 : 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    20456056 :     error = IVAS_ERR_OK;
     583             : 
     584    20456056 :     num_bits_read = 0;
     585    20456056 :     element_mode_flag = 0;
     586             : 
     587    20456056 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     588             : 
     589             :     /*-------------------------------------------------------------------*
     590             :      * Read IVAS format
     591             :      *-------------------------------------------------------------------*/
     592             : 
     593    20456056 :     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    20456056 :     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    20221152 :         if ( st_ivas->ivas_format == STEREO_FORMAT )
     613             :         {
     614     2279768 :             element_mode_flag = 1;
     615             :         }
     616    17941384 :         else if ( st_ivas->ivas_format == ISM_FORMAT )
     617             :         {
     618             :             /* read the number of objects */
     619     2962933 :             st_ivas->nchan_transport = 1;
     620     2962933 :             nchan_ism = 1;
     621     2962933 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 );
     622     8656299 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     623             :             {
     624     5693366 :                 nchan_ism++;
     625     5693366 :                 k--;
     626             :             }
     627             : 
     628     2962933 :             st_ivas->nchan_ism = nchan_ism;
     629             : 
     630     2962933 :             if ( ( error = ivas_ism_dec_config( st_ivas, st_ivas->ism_mode ) ) != IVAS_ERR_OK )
     631             :             {
     632           0 :                 return error;
     633             :             }
     634             :         }
     635    14978451 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     636             :         {
     637             :             /* read Ambisonic (SBA) planar flag */
     638     6203951 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     639     6203951 :             num_bits_read += SBA_PLANAR_BITS;
     640             : 
     641             :             /* read Ambisonic (SBA) order */
     642     6203951 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     643     6203951 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     644             : 
     645     6203951 :             num_bits_read += SBA_ORDER_BITS;
     646     6203951 :             if ( st_ivas->ini_frame > 0 && ivas_total_brate != st_ivas->last_active_ivas_total_brate && ivas_total_brate > IVAS_SID_5k2 )
     647             :             {
     648       37380 :                 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     6166571 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
     657             : 
     658     6166571 :                 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     8774500 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
     662             :         {
     663             :             /* read number of MASA transport channels */
     664     2498365 :             if ( st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 1] )
     665             :             {
     666     1432667 :                 st_ivas->nchan_transport = 2;
     667     1432667 :                 element_mode_flag = 1;
     668             :             }
     669             :             else
     670             :             {
     671     1065698 :                 st_ivas->nchan_transport = 1;
     672             :             }
     673             : 
     674             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     675     2498365 :             st_ivas->ism_mode = ISM_MODE_NONE;
     676     2498365 :             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     2498365 :             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      326994 :                 st_ivas->nchan_ism = 5 - st_ivas->nchan_ism;
     689      326994 :                 if ( st_ivas->nchan_transport == 1 && st_ivas->nchan_ism == 2 )
     690             :                 {
     691       64355 :                     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      326994 :                 st_ivas->nchan_transport = 2;
     696      326994 :                 element_mode_flag = 1;
     697             :             }
     698             : 
     699     2498365 :             if ( st_ivas->ini_frame > 0 )
     700             :             {
     701             :                 /* reconfigure in case a change of operation mode is detected */
     702     2489703 :                 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       89358 :                     if ( st_ivas->last_ivas_format == MASA_FORMAT )
     705             :                     {
     706       42289 :                         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       42289 :                             if ( ( error = ivas_masa_dec_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     713             :                             {
     714           0 :                                 return error;
     715             :                             }
     716             :                         }
     717             :                     }
     718             :                     else
     719             :                     {
     720       47069 :                         if ( ( error = ivas_omasa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     721             :                         {
     722           0 :                             return error;
     723             :                         }
     724             :                     }
     725             :                 }
     726             :             }
     727             :         }
     728     6276135 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     729             :         {
     730     1163616 :             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     1163616 :             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     1163616 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );
     735             : 
     736     1163616 :             if ( st_ivas->ini_frame > 0 )
     737             :             {
     738             :                 /* reconfigure in case a change of operation mode is detected */
     739     1155099 :                 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      124319 :                     if ( ( error = ivas_omasa_dec_config( st_ivas ) ) != IVAS_ERR_OK )
     742             :                     {
     743           0 :                         return error;
     744             :                     }
     745             :                 }
     746             :             }
     747             :         }
     748     5112519 :         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     2502474 :             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     2502474 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     755     2502474 :             num_bits_read += SBA_PLANAR_BITS;
     756             : 
     757             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     758     2502474 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     759     2502474 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     760     2502474 :             num_bits_read += SBA_ORDER_BITS;
     761             : 
     762             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     763     2502474 :             if ( ivas_total_brate < IVAS_24k4 )
     764             :             {
     765      310969 :                 st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     766      310969 :                 st_ivas->sba_order += 2 * st_ivas->bit_stream[num_bits_read];
     767      310969 :                 num_bits_read += SBA_ORDER_BITS;
     768             :             }
     769             : 
     770     2502474 :             if ( st_ivas->ini_frame > 0 && ivas_total_brate != st_ivas->last_active_ivas_total_brate )
     771             :             {
     772       25725 :                 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     2476749 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, st_ivas->sba_order );
     781             : 
     782     2476749 :                 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     2476749 :                 if ( st_ivas->ini_frame > 0 && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     786             :                 {
     787             :                     {
     788             :                         int16_t n;
     789             : 
     790     1255964 :                         n = st_ivas->nchan_transport + st_ivas->nchan_ism;
     791     1255964 :                         st_ivas->nCPE = ( n + 1 ) >> 1;
     792             :                     }
     793             :                 }
     794             :             }
     795     2502474 :             if ( ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism ) == ISM_SBA_MODE_DISC )
     796             :             {
     797     1281078 :                 st_ivas->ism_mode = ISM_SBA_MODE_DISC;
     798             :             }
     799             :             else
     800             :             {
     801     1221396 :                 st_ivas->ism_mode = ISM_MODE_NONE;
     802             :             }
     803             :         }
     804     2610045 :         else if ( st_ivas->ivas_format == MC_FORMAT )
     805             :         {
     806             :             /* read MC configuration */
     807     2610045 :             idx = 0;
     808    10440180 :             for ( k = 0; k < MC_LS_SETUP_BITS; k++ )
     809             :             {
     810     7830135 :                 if ( st_ivas->bit_stream[num_bits_read + k] )
     811             :                 {
     812     1426648 :                     idx += 1 << ( MC_LS_SETUP_BITS - 1 - k );
     813             :                 }
     814             :             }
     815     2610045 :             num_bits_read += MC_LS_SETUP_BITS;
     816             : 
     817             :             /* select MC format mode; reconfigure the MC format decoder */
     818     2610045 :             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    20221152 :         if ( st_ivas->ini_frame == 0 && element_mode_flag )
     829             :         {
     830             :             /* read stereo technology info */
     831        8133 :             if ( ivas_total_brate < MIN_BRATE_MDCT_STEREO )
     832             :             {
     833             :                 /* 1 bit */
     834        4876 :                 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        4876 :                     st_ivas->element_mode_init = 0 + IVAS_CPE_DFT;
     841             :                 }
     842             :             }
     843             :             else
     844             :             {
     845        3257 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     846             :             }
     847             :         }
     848             :     }
     849      234904 :     else if ( ivas_total_brate == IVAS_SID_5k2 )
     850             :     {
     851       36145 :         switch ( st_ivas->sid_format )
     852             :         {
     853       16595 :             case SID_DFT_STEREO:
     854       16595 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     855       16595 :                 break;
     856        4933 :             case SID_MDCT_STEREO:
     857        4933 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     858        4933 :                 break;
     859        8703 :             case SID_ISM:
     860        8703 :                 st_ivas->element_mode_init = IVAS_SCE;
     861        8703 :                 break;
     862        1913 :             case SID_MASA_1TC:
     863        1913 :                 st_ivas->element_mode_init = IVAS_SCE;
     864        1913 :                 st_ivas->nchan_transport = 1;
     865        1913 :                 break;
     866        1097 :             case SID_MASA_2TC:
     867        1097 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1 - SID_FORMAT_NBITS] == 1 )
     868             :                 {
     869         210 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     870             :                 }
     871             :                 else
     872             :                 {
     873         887 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     874             :                 }
     875        1097 :                 st_ivas->nchan_transport = 2;
     876        1097 :                 break;
     877        1801 :             case SID_SBA_1TC:
     878        1801 :                 st_ivas->element_mode_init = IVAS_SCE;
     879        1801 :                 break;
     880        1103 :             case SID_SBA_2TC:
     881        1103 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     882        1103 :                 break;
     883             :         }
     884             : 
     885       36145 :         if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format == SBA_FORMAT )
     886             :         {
     887             :             int16_t nchan_transport_old, nchan_transport;
     888        2904 :             nchan_transport_old = st_ivas->nchan_transport;
     889        2904 :             nchan_transport = ( st_ivas->sid_format == SID_SBA_2TC ) ? 2 : 1;
     890             : 
     891        2904 :             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       36145 :         if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format == MASA_FORMAT )
     914             :         {
     915        3010 :             st_ivas->nchan_ism = 0;
     916        3010 :             st_ivas->ism_mode = ISM_MODE_NONE;
     917             :         }
     918             : 
     919       36145 :         if ( st_ivas->ivas_format == ISM_FORMAT )
     920             :         {
     921        8703 :             ISM_MODE last_ism_mode = st_ivas->ism_mode;
     922             : 
     923             :             /* read the number of objects */
     924        8703 :             st_ivas->nchan_transport = 1;
     925        8703 :             nchan_ism = 1;
     926        8703 :             k = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - 1 ) - SID_FORMAT_NBITS;
     927       23373 :             while ( st_ivas->bit_stream[k] && nchan_ism < MAX_NUM_OBJECTS )
     928             :             {
     929       14670 :                 nchan_ism++;
     930       14670 :                 k--;
     931             :             }
     932        8703 :             k--;
     933             : 
     934        8703 :             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        8703 :             st_ivas->nchan_ism = nchan_ism;
     940             : 
     941             :             /* read ism_mode */
     942        8703 :             st_ivas->ism_mode = ISM_MODE_DISC;
     943        8703 :             if ( nchan_ism > 2 )
     944             :             {
     945        4628 :                 k -= nchan_ism; /* SID metadata flags */
     946        4628 :                 idx = st_ivas->bit_stream[k];
     947        4628 :                 st_ivas->ism_mode = (ISM_MODE) ( idx + 1 );
     948             :             }
     949             : 
     950        8703 :             if ( st_ivas->ini_frame == 0 )
     951             :             {
     952           0 :                 last_ism_mode = st_ivas->ism_mode;
     953             :             }
     954             : 
     955        8703 :             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    20456056 :     if ( st_ivas->ini_frame == 0 && st_ivas->ivas_format != UNDEFINED_FORMAT )
     968             :     {
     969       89475 :         if ( ( error = doSanityChecks_IVAS( st_ivas ) ) != IVAS_ERR_OK )
     970             :         {
     971           0 :             return IVAS_ERROR( error, "Sanity checks failed" );
     972             :         }
     973             : 
     974       89475 :         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    20456056 :     ivas_set_bitstream_pointers( st_ivas );
     985             : 
     986    20456056 :     reset_elements( st_ivas );
     987             : 
     988             :     /* update bitstream buffer pointer -> take into account already read bits */
     989    20456056 :     if ( ( st_ivas->nSCE > 0 ) || ( st_ivas->nCPE > 0 ) )
     990             :     {
     991    20456056 :         st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     992    20456056 :         st->next_bit_pos = num_bits_read;
     993    20456056 :         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    20456056 :     return error;
     997             : }
     998             : 
     999             : 
    1000             : /*-------------------------------------------------------------------*
    1001             :  * ivas_read_format()
    1002             :  *
    1003             :  * Read IVAS format signaling
    1004             :  *-------------------------------------------------------------------*/
    1005             : 
    1006    40897054 : 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    40897054 :     error = IVAS_ERR_OK;
    1016             : 
    1017    40897054 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1018             : 
    1019    40897054 :     *num_bits_read = 0;
    1020             : 
    1021    40897054 :     if ( !st_ivas->bfi && is_DTXrate( ivas_total_brate ) == 0 )
    1022             :     {
    1023             :         /* read IVAS format */
    1024    40442304 :         k = 0;
    1025    40442304 :         if ( st_ivas->bit_stream[*num_bits_read] )
    1026             :         {
    1027    30662678 :             k = 1;
    1028             :         }
    1029    40442304 :         k <<= 1;
    1030    40442304 :         ( *num_bits_read )++;
    1031             : 
    1032    40442304 :         if ( st_ivas->bit_stream[*num_bits_read] )
    1033             :         {
    1034    23246660 :             k += 1;
    1035             :         }
    1036    40442304 :         ( *num_bits_read )++;
    1037             : 
    1038    40442304 :         switch ( k )
    1039             :         {
    1040     4559536 :             case 0:
    1041     4559536 :                 st_ivas->ivas_format = STEREO_FORMAT;
    1042     4559536 :                 break;
    1043     5220090 :             case 1:
    1044     5220090 :                 st_ivas->ivas_format = MC_FORMAT;
    1045     5220090 :                 break;
    1046    12636108 :             case 2:
    1047    12636108 :                 st_ivas->ivas_format = ISM_FORMAT;
    1048             : 
    1049    12636108 :                 if ( ivas_total_brate >= IVAS_24k4 )
    1050             :                 {
    1051    12229010 :                     if ( st_ivas->bit_stream[*num_bits_read] )
    1052             :                     {
    1053     6710242 :                         ( *num_bits_read )++;
    1054     6710242 :                         if ( st_ivas->bit_stream[*num_bits_read] )
    1055             :                         {
    1056     4383010 :                             st_ivas->ivas_format = SBA_ISM_FORMAT;
    1057             :                         }
    1058             :                         else
    1059             :                         {
    1060     2327232 :                             st_ivas->ivas_format = MASA_ISM_FORMAT;
    1061             :                         }
    1062             :                     }
    1063    12229010 :                     ( *num_bits_read )++;
    1064             :                 }
    1065    12636108 :                 break;
    1066    18026570 :             case 3:
    1067    18026570 :                 if ( st_ivas->bit_stream[*num_bits_read] )
    1068             :                 {
    1069     4996730 :                     st_ivas->ivas_format = MASA_FORMAT;
    1070             :                 }
    1071             :                 else
    1072             :                 {
    1073    13029840 :                     st_ivas->ivas_format = SBA_FORMAT;
    1074             :                     /* read Ambisonic (SBA) planar flag */
    1075    13029840 :                     st_ivas->sba_planar = st_ivas->bit_stream[( *num_bits_read ) + 1];
    1076             : 
    1077             :                     /* read Ambisonic (SBA) order */
    1078    13029840 :                     st_ivas->sba_order = st_ivas->bit_stream[( *num_bits_read ) + 2 + SBA_PLANAR_BITS];
    1079    13029840 :                     st_ivas->sba_order += 2 * st_ivas->bit_stream[( *num_bits_read ) + 1 + SBA_PLANAR_BITS];
    1080    13029840 :                     if ( st_ivas->sba_order == 0 )
    1081             :                     {
    1082      621938 :                         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      621938 :                         if ( ivas_total_brate < IVAS_24k4 )
    1086             :                         {
    1087      621938 :                             st_ivas->sba_order = st_ivas->bit_stream[*num_bits_read + 2 + SBA_PLANAR_BITS + SBA_ORDER_BITS];
    1088      621938 :                             st_ivas->sba_order += 2 * st_ivas->bit_stream[*num_bits_read + 1 + SBA_PLANAR_BITS + SBA_ORDER_BITS];
    1089             :                         }
    1090             :                     }
    1091             :                 }
    1092    18026570 :                 ( *num_bits_read )++;
    1093             : 
    1094    18026570 :                 break;
    1095             :         }
    1096    40442304 :     }
    1097      454750 :     else if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
    1098             :     {
    1099             :         /* read IVAS format in SID frame */
    1100       72290 :         idx = 0;
    1101      289160 :         for ( k = 0; k < SID_FORMAT_NBITS; k++ )
    1102             :         {
    1103      216870 :             idx += st_ivas->bit_stream[k] << ( SID_FORMAT_NBITS - 1 - k );
    1104             :         }
    1105             : 
    1106       72290 :         ( *num_bits_read ) += SID_FORMAT_NBITS;
    1107       72290 :         st_ivas->sid_format = idx;
    1108             : 
    1109       72290 :         switch ( idx )
    1110             :         {
    1111       43056 :             case SID_DFT_STEREO:
    1112             :             case SID_MDCT_STEREO:
    1113       43056 :                 st_ivas->ivas_format = STEREO_FORMAT;
    1114       43056 :                 break;
    1115       17406 :             case SID_ISM:
    1116       17406 :                 st_ivas->ivas_format = ISM_FORMAT;
    1117       17406 :                 break;
    1118        3602 :             case SID_SBA_1TC:
    1119        3602 :                 st_ivas->ivas_format = SBA_FORMAT;
    1120        3602 :                 st_ivas->element_mode_init = IVAS_SCE;
    1121        3602 :                 break;
    1122        2206 :             case SID_SBA_2TC:
    1123        2206 :                 st_ivas->ivas_format = SBA_FORMAT;
    1124        2206 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1125        2206 :                 break;
    1126        3826 :             case SID_MASA_1TC:
    1127        3826 :                 st_ivas->ivas_format = MASA_FORMAT;
    1128        3826 :                 st_ivas->element_mode_init = IVAS_SCE;
    1129        3826 :                 break;
    1130        2194 :             case SID_MASA_2TC:
    1131        2194 :                 st_ivas->ivas_format = MASA_FORMAT;
    1132        2194 :                 if ( st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] == 1 )
    1133             :                 {
    1134         420 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1135             :                 }
    1136             :                 else
    1137             :                 {
    1138        1774 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
    1139             :                 }
    1140        2194 :                 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       72290 :         if ( st_ivas->ivas_format == SBA_FORMAT )
    1146             :         {
    1147             :             /* read Ambisonic (SBA) planar flag */
    1148        5808 :             st_ivas->sba_planar = st_ivas->bit_stream[*num_bits_read];
    1149        5808 :             *num_bits_read += SBA_PLANAR_BITS;
    1150             : 
    1151             :             /* read Ambisonic (SBA) order */
    1152        5808 :             st_ivas->sba_order = st_ivas->bit_stream[*num_bits_read + 1];
    1153        5808 :             st_ivas->sba_order += 2 * st_ivas->bit_stream[*num_bits_read];
    1154        5808 :             *num_bits_read += SBA_ORDER_BITS;
    1155        5808 :             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       72290 :         st_ivas->bit_stream += ( *num_bits_read );
    1163       72290 :         ( *num_bits_read ) = 0;
    1164             :     }
    1165             :     else
    1166             :     {
    1167             :         /* In SID/NO_DATA frames, use the previous frame IVAS format */
    1168             :     }
    1169             : 
    1170    40897054 :     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    21786406 : int16_t getNumChanSynthesis(
    1183             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure  */
    1184             : )
    1185             : {
    1186             :     int16_t n;
    1187             : 
    1188    21786406 :     n = st_ivas->nSCE + CPE_CHANNELS * st_ivas->nCPE;
    1189             : 
    1190    21786406 :     if ( st_ivas->sba_dirac_stereo_flag )
    1191             :     {
    1192      767266 :         n = CPE_CHANNELS;
    1193             :     }
    1194    21019140 :     else if ( ( st_ivas->hMCT != NULL || st_ivas->ivas_format == SBA_FORMAT ) && st_ivas->ivas_format != SBA_ISM_FORMAT )
    1195             :     {
    1196      191270 :         n = st_ivas->nchan_transport;
    1197             :     }
    1198    20827870 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1199             :     {
    1200    12789921 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1201             :         {
    1202     9608688 :             n = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1203             :         }
    1204             :         else
    1205             :         {
    1206     3181233 :             n = st_ivas->nchan_transport;
    1207             :         }
    1208             :     }
    1209             : 
    1210    21786406 :     return n;
    1211             : }
    1212             : 
    1213             : 
    1214             : /*-------------------------------------------------------------------*
    1215             :  * copy_decoder_config()
    1216             :  *
    1217             :  * Copy IVAS configuration structure to the CoreCoder state structure
    1218             :  *-------------------------------------------------------------------*/
    1219             : 
    1220      697640 : void copy_decoder_config(
    1221             :     Decoder_Struct *st_ivas, /* i  : IVAS decoder structure      */
    1222             :     Decoder_State *st        /* o  : decoder state structure     */
    1223             : )
    1224             : {
    1225      697640 :     st->output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1226      697640 :     st->Opt_AMR_WB = st_ivas->hDecoderConfig->Opt_AMR_WB;
    1227      697640 :     st->codec_mode = st_ivas->codec_mode;
    1228      697640 :     st->ini_frame = st_ivas->ini_frame;
    1229             : 
    1230      697640 :     st->bfi = st_ivas->bfi;
    1231             : 
    1232      697640 :     st->writeFECoffset = st_ivas->writeFECoffset;
    1233             : 
    1234      697640 :     st->element_mode = st_ivas->element_mode_init;
    1235             : 
    1236      697640 :     return;
    1237             : }
    1238             : 
    1239             : 
    1240             : /*-------------------------------------------------------------------*
    1241             :  * ivas_init_decoder_front()
    1242             :  *
    1243             :  * Set decoder parameters to initial values
    1244             :  *-------------------------------------------------------------------*/
    1245             : 
    1246       90769 : ivas_error ivas_init_decoder_front(
    1247             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
    1248             : )
    1249             : {
    1250             :     ivas_error error;
    1251             : 
    1252       90769 :     error = IVAS_ERR_OK;
    1253             : 
    1254             :     /*-----------------------------------------------------------------*
    1255             :      * Resets
    1256             :      *-----------------------------------------------------------------*/
    1257             : 
    1258       90769 :     st_ivas->nSCE = 0;
    1259       90769 :     st_ivas->nCPE = 0;
    1260       90769 :     st_ivas->nchan_ism = 0;
    1261       90769 :     st_ivas->nchan_transport = -1;
    1262             : 
    1263       90769 :     st_ivas->ism_mode = ISM_MODE_NONE;
    1264       90769 :     st_ivas->mc_mode = MC_MODE_NONE;
    1265             : 
    1266       90769 :     st_ivas->sid_format = SID_FORMAT_NONE;
    1267       90769 :     st_ivas->sba_dirac_stereo_flag = 0;
    1268             : 
    1269             :     /* HRTF binauralization latency in ns */
    1270       90769 :     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       90769 :     if ( st_ivas->hDecoderConfig->Opt_LsCustom )
    1280             :     {
    1281          21 :         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       90769 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    1292             :     {
    1293        3709 :         if ( ( error = ivas_headTrack_open( &( st_ivas->hHeadTrackData ) ) ) != IVAS_ERR_OK )
    1294             :         {
    1295           0 :             return error;
    1296             :         }
    1297        3709 :         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       90769 :     if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1308             :     {
    1309         217 :         if ( ( error = ivas_external_orientation_open( &( st_ivas->hExtOrientationData ), st_ivas->hDecoderConfig->render_num_subframes ) ) != IVAS_ERR_OK )
    1310             :         {
    1311           0 :             return error;
    1312             :         }
    1313             :     }
    1314             : 
    1315             :     /*-------------------------------------------------------------------*
    1316             :      * Allocate and initialize combined orientation handle
    1317             :      *--------------------------------------------------------------------*/
    1318             : 
    1319       90769 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1320             :     {
    1321        3709 :         if ( ( error = ivas_combined_orientation_open( &( st_ivas->hCombinedOrientationData ), st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->render_num_subframes ) ) != IVAS_ERR_OK )
    1322             :         {
    1323           0 :             return error;
    1324             :         }
    1325             :     }
    1326             : 
    1327             :     /*-------------------------------------------------------------------*
    1328             :      * Allocate and initialize Binaural Renderer configuration handle
    1329             :      *--------------------------------------------------------------------*/
    1330             : 
    1331       90769 :     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       62550 :          ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->Opt_non_diegetic_pan ) )
    1333             :     {
    1334       28233 :         if ( ( error = ivas_render_config_open( &( st_ivas->hRenderConfig ) ) ) != IVAS_ERR_OK )
    1335             :         {
    1336           0 :             return error;
    1337             :         }
    1338             : 
    1339       28233 :         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       90769 :     st_ivas->flushing = 0;
    1347             : #endif
    1348             : 
    1349       90769 :     return error;
    1350             : }
    1351             : 
    1352             : 
    1353             : /*-------------------------------------------------------------------*
    1354             :  * ivas_init_decoder()
    1355             :  *
    1356             :  * Initialize IVAS decoder state structure
    1357             :  *-------------------------------------------------------------------*/
    1358             : 
    1359       90741 : 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;
    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       90741 :     error = IVAS_ERR_OK;
    1376             : 
    1377       90741 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1378       90741 :     hDecoderConfig = st_ivas->hDecoderConfig;
    1379       90741 :     output_config = hDecoderConfig->output_config;
    1380       90741 :     ivas_total_brate = hDecoderConfig->ivas_total_brate;
    1381             : 
    1382       90741 :     hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
    1383       90741 :     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       90741 :     if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    1390             :     {
    1391        5874 :         if ( st_ivas->ivas_format == MONO_FORMAT )
    1392             :         {
    1393          54 :             hDecoderConfig->nchan_out = 1;
    1394             :         }
    1395        5820 :         else if ( st_ivas->ivas_format == STEREO_FORMAT )
    1396             :         {
    1397         191 :             hDecoderConfig->nchan_out = CPE_CHANNELS;
    1398             :         }
    1399        5629 :         else if ( st_ivas->ivas_format == MC_FORMAT )
    1400             :         {
    1401         524 :             hDecoderConfig->nchan_out = audioCfg2channels( st_ivas->transport_config );
    1402             :         }
    1403        5105 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == SBA_FORMAT )
    1404             :         {
    1405        2851 :             hDecoderConfig->nchan_out = audioCfg2channels( ivas_set_audio_config_from_sba_order( st_ivas->sba_order ) );
    1406        2851 :             hDecoderConfig->nchan_out += st_ivas->nchan_ism;
    1407             :         }
    1408        2254 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1409             :         {
    1410        1125 :             hDecoderConfig->nchan_out = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1411             :         }
    1412        1129 :         else if ( !( st_ivas->ism_mode == ISM_MODE_PARAM ) )
    1413             :         {
    1414        1019 :             hDecoderConfig->nchan_out = st_ivas->nchan_transport;
    1415             :         }
    1416             : 
    1417        5874 :         st_ivas->hOutSetup.nchan_out_woLFE = hDecoderConfig->nchan_out;
    1418             :     }
    1419             : 
    1420             :     /*-----------------------------------------------------------------*
    1421             :      * Set output and intern setup & renderer selection
    1422             :      *-----------------------------------------------------------------*/
    1423             : 
    1424       90741 :     st_ivas->intern_config = output_config;
    1425             : 
    1426       90741 :     if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->ivas_format == MC_FORMAT )
    1427             :     {
    1428         524 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->transport_config );
    1429         524 :         st_ivas->intern_config = st_ivas->transport_config;
    1430             :     }
    1431       90217 :     else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && ( st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == SBA_FORMAT ) )
    1432             :     {
    1433        2851 :         st_ivas->intern_config = ivas_set_audio_config_from_sba_order( st_ivas->sba_order );
    1434        2851 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->intern_config );
    1435             :     }
    1436             :     else
    1437             :     {
    1438       87366 :         ivas_output_init( &( st_ivas->hOutSetup ), output_config );
    1439             :     }
    1440             : 
    1441       90741 :     if ( st_ivas->ivas_format == SBA_ISM_FORMAT && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    1442             :     {
    1443        3054 :         st_ivas->hOutSetup.ambisonics_order = SBA_HOA3_ORDER;
    1444        3054 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_7_1_4;
    1445        3054 :         st_ivas->hOutSetup.output_config = st_ivas->intern_config;
    1446        3054 :         st_ivas->hOutSetup.nchan_out_woLFE = audioCfg2channels( st_ivas->intern_config );
    1447             :     }
    1448             : 
    1449             :     /* Only initialize transport setup if it is used */
    1450       90741 :     if ( st_ivas->transport_config != IVAS_AUDIO_CONFIG_INVALID )
    1451             :     {
    1452       25838 :         ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
    1453             :     }
    1454             : 
    1455       90741 :     if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
    1456             :     {
    1457        2792 :         ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    1458             : 
    1459        2792 :         ivas_mcmasa_set_separate_channel_mode( &( st_ivas->hOutSetup.separateChannelEnabled ), &( st_ivas->hOutSetup.separateChannelIndex ), ivas_total_brate );
    1460             :     }
    1461             : 
    1462       90741 :     ivas_renderer_select( st_ivas );
    1463             : 
    1464       90741 :     if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1465             :     {
    1466          21 :         if ( ( error = ivas_ls_custom_output_init( st_ivas ) ) != IVAS_ERR_OK )
    1467             :         {
    1468           0 :             return error;
    1469             :         }
    1470             :     }
    1471             : 
    1472       90741 :     ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
    1473             : 
    1474       90741 :     if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA )
    1475             :     {
    1476        2792 :         ivas_mcmasa_set_separate_channel_mode( &( st_ivas->hIntSetup.separateChannelEnabled ), &( st_ivas->hIntSetup.separateChannelIndex ), ivas_total_brate );
    1477             : 
    1478        2792 :         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       90741 :     if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1502             :     {
    1503        8215 :         if ( ( error = ivas_reverb_HRTF_statistics_open( &( st_ivas->hHrtfStatistics ), output_Fs ) ) != IVAS_ERR_OK )
    1504             :         {
    1505           0 :             return error;
    1506             :         }
    1507             : 
    1508             :         /* Get default reverb values based on format, if custom values were not given */
    1509        8215 :         if ( st_ivas->hDecoderConfig->Opt_RendConfigCustom == 0 )
    1510             :         {
    1511             :             IVAS_DefaultReverbSize defaultReverbSize;
    1512        8012 :             switch ( st_ivas->hDecoderConfig->room_size )
    1513             :             {
    1514        8012 :                 case IVAS_ROOM_SIZE_AUTO:
    1515        8012 :                     switch ( st_ivas->ivas_format )
    1516             :                     {
    1517        1547 :                         case ISM_FORMAT:
    1518        1547 :                             defaultReverbSize = DEFAULT_REVERB_LARGE;
    1519        1547 :                             break;
    1520        1117 :                         case SBA_FORMAT:
    1521        1117 :                             defaultReverbSize = DEFAULT_REVERB_SMALL;
    1522        1117 :                             break;
    1523         741 :                         case MASA_FORMAT:
    1524         741 :                             defaultReverbSize = DEFAULT_REVERB_SMALL;
    1525         741 :                             break;
    1526         674 :                         case MC_FORMAT:
    1527         674 :                             defaultReverbSize = DEFAULT_REVERB_MEDIUM;
    1528         674 :                             break;
    1529         752 :                         case MASA_ISM_FORMAT:
    1530         752 :                             defaultReverbSize = DEFAULT_REVERB_MEDIUM;
    1531         752 :                             break;
    1532        3061 :                         case SBA_ISM_FORMAT:
    1533        3061 :                             defaultReverbSize = DEFAULT_REVERB_MEDIUM;
    1534        3061 :                             break;
    1535         120 :                         default:
    1536         120 :                             defaultReverbSize = DEFAULT_REVERB_LARGE;
    1537             :                     }
    1538        8012 :                     break;
    1539           0 :                 case IVAS_ROOM_SIZE_SMALL:
    1540           0 :                     defaultReverbSize = DEFAULT_REVERB_SMALL;
    1541           0 :                     break;
    1542           0 :                 case IVAS_ROOM_SIZE_MEDIUM:
    1543           0 :                     defaultReverbSize = DEFAULT_REVERB_MEDIUM;
    1544           0 :                     break;
    1545           0 :                 case IVAS_ROOM_SIZE_LARGE:
    1546             :                 default:
    1547           0 :                     defaultReverbSize = DEFAULT_REVERB_LARGE;
    1548           0 :                     break;
    1549             :             }
    1550        8012 :             if ( ( error = ivas_render_config_change_defaults( st_ivas->hRenderConfig, defaultReverbSize ) ) != IVAS_ERR_OK )
    1551             :             {
    1552           0 :                 return error;
    1553             :             }
    1554             :         }
    1555             :     }
    1556             : 
    1557             :     /*-----------------------------------------------------------------*
    1558             :      * Allocate and initialize SCE/CPE and other handles
    1559             :      *-----------------------------------------------------------------*/
    1560             : 
    1561       90741 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1562             :     {
    1563        1266 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
    1564        1266 :         st_ivas->nCPE = 0;
    1565        1266 :         st_ivas->nchan_transport = 1;
    1566        1266 :         sce_id = 0;
    1567             : 
    1568        1266 :         if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate ) ) != IVAS_ERR_OK )
    1569             :         {
    1570           0 :             return error;
    1571             :         }
    1572             : 
    1573        1266 :         reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1574             :     }
    1575       89475 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
    1576             :     {
    1577        2235 :         st_ivas->nchan_transport = CPE_CHANNELS;
    1578        2235 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_STEREO;
    1579             : 
    1580        2235 :         st_ivas->nSCE = 0;
    1581        2235 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
    1582        2235 :         cpe_id = 0;
    1583             : 
    1584        2235 :         if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate ) ) != IVAS_ERR_OK )
    1585             :         {
    1586           0 :             return error;
    1587             :         }
    1588             : 
    1589        6705 :         for ( n = 0; n < st_ivas->nchan_transport; n++ )
    1590             :         {
    1591        4470 :             reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1592             :         }
    1593             : 
    1594             :         /* init EFAP for custom LS output and set hTransSetup */
    1595        2235 :         if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1596             :         {
    1597           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 )
    1598             :             {
    1599           0 :                 return error;
    1600             :             }
    1601             : 
    1602           0 :             ivas_output_init( &( st_ivas->hTransSetup ), IVAS_AUDIO_CONFIG_STEREO );
    1603             :         }
    1604             :     }
    1605       87240 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    1606             :     {
    1607             :         int32_t element_brate_tmp[MAX_NUM_OBJECTS];
    1608             : 
    1609       16542 :         st_ivas->nSCE = st_ivas->nchan_transport; /* "st_ivas->nchan_transport" is known from ivas_dec_setup */
    1610       16542 :         st_ivas->nCPE = 0;
    1611       16542 :         st_ivas->ism_extmeta_active = -1;
    1612       16542 :         st_ivas->ism_extmeta_cnt = 0;
    1613             : 
    1614       16542 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1615             :         {
    1616        1627 :             st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
    1617        1627 :             st_ivas->nSCE = MAX_PARAM_ISM_WAVE;
    1618             : 
    1619        1627 :             if ( ( error = ivas_param_ism_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1620             :             {
    1621           0 :                 return error;
    1622             :             }
    1623             :         }
    1624             : 
    1625       16542 :         if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
    1626             :         {
    1627           0 :             return error;
    1628             :         }
    1629             : 
    1630       57951 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1631             :         {
    1632       41409 :             if ( ( error = create_sce_dec( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
    1633             :             {
    1634           0 :                 return error;
    1635             :             }
    1636             : 
    1637       41409 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1638             : 
    1639       41409 :             st_ivas->hSCE[sce_id]->hCoreCoder[0]->is_ism_format = 1;
    1640             :         }
    1641             : 
    1642       16542 :         st_ivas->hISMDTX.sce_id_dtx = 0;
    1643             : 
    1644       16542 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1645             :         {
    1646        1627 :             st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed3;
    1647             : 
    1648        1627 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1649             :             {
    1650             :                 /* reusing OMASA function for allocating and initializing MASA_ISM rendering handle (even though not in OMASA) */
    1651         496 :                 if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1652             :                 {
    1653           0 :                     return error;
    1654             :                 }
    1655             :             }
    1656             :         }
    1657       14915 :         else if ( st_ivas->ism_mode == ISM_MODE_DISC )
    1658             :         {
    1659       53070 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; ++sce_id )
    1660             :             {
    1661       38155 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = 2 + sce_id;
    1662             :             }
    1663             :         }
    1664             :     }
    1665       70698 :     else if ( st_ivas->ivas_format == SBA_FORMAT )
    1666             :     {
    1667       13558 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1668             :         {
    1669           0 :             return error;
    1670             :         }
    1671             : 
    1672       13558 :         if ( ( error = ivas_spar_dec_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1673             :         {
    1674           0 :             return error;
    1675             :         }
    1676             : 
    1677       13558 :         if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
    1678       13558 :                                               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 ),
    1679             :                                               st_ivas->ivas_format ) ) != IVAS_ERR_OK )
    1680             :         {
    1681           0 :             return error;
    1682             :         }
    1683             : 
    1684       13558 :         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 ) )
    1685             :         {
    1686        9970 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1687             :             {
    1688           0 :                 return error;
    1689             :             }
    1690             : 
    1691        9970 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    1692             :         }
    1693             :         else
    1694             :         {
    1695             :             int16_t band_grouping[IVAS_MAX_NUM_BANDS + 1];
    1696             : 
    1697        3588 :             st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    1698        3588 :             if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
    1699             :             {
    1700         151 :                 st_ivas->hSpar->enc_param_start_band = 0;
    1701             : 
    1702         151 :                 set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    1703         151 :                 st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    1704             :             }
    1705             : 
    1706        3588 :             ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
    1707        3588 :                                      st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    1708             :         }
    1709       13558 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1710             : 
    1711       18189 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1712             :         {
    1713        4631 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1714             :             {
    1715           0 :                 return error;
    1716             :             }
    1717             : 
    1718        4631 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1719             :         }
    1720             : 
    1721       27396 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1722             :         {
    1723       13838 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1724             :             {
    1725           0 :                 return error;
    1726             :             }
    1727             : 
    1728       41514 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1729             :             {
    1730       27676 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1731             :             }
    1732             :         }
    1733             : 
    1734             :         /* create CPE element for DFT Stereo like upmix */
    1735       13558 :         if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nCPE == 0 )
    1736             :         {
    1737         543 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    1738             :             {
    1739           0 :                 return error;
    1740             :             }
    1741             : 
    1742         543 :             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 */
    1743         543 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    1744             :         }
    1745             : 
    1746       13558 :         if ( st_ivas->nCPE > 1 )
    1747             :         {
    1748        4911 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1749             :             {
    1750           0 :                 return error;
    1751             :             }
    1752             :         }
    1753             : 
    1754             :         /* set CNA/CNG flags */
    1755       13558 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1756             :     }
    1757       57140 :     else if ( st_ivas->ivas_format == MASA_FORMAT )
    1758             :     {
    1759             :         /* if we start in ISM_MODE_NONE in MASA_ISM, that appears as normal MASA, but we may change to a mode with ISMs */
    1760        8662 :         st_ivas->ism_extmeta_active = -1;
    1761        8662 :         st_ivas->ism_extmeta_cnt = 0;
    1762        8662 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1763             :         {
    1764           0 :             return error;
    1765             :         }
    1766             : 
    1767        8662 :         if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1768             :         {
    1769           0 :             return error;
    1770             :         }
    1771             : 
    1772        8662 :         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 )
    1773             :         {
    1774        7715 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1775             :             {
    1776           0 :                 return error;
    1777             :             }
    1778             :         }
    1779             : 
    1780       11426 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1781             :         {
    1782        2764 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1783             :             {
    1784           0 :                 return error;
    1785             :             }
    1786             : 
    1787        2764 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1788             :         }
    1789             : 
    1790       14560 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1791             :         {
    1792        5898 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1793             :             {
    1794           0 :                 return error;
    1795             :             }
    1796             : 
    1797       17694 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1798             :             {
    1799       11796 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1800             :             }
    1801             :         }
    1802             : 
    1803             :         /* set CNA/CNG flags */
    1804        8662 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1805             :     }
    1806       48478 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1807             :     {
    1808             :         int32_t temp_brate[MAX_SCE];
    1809       31931 :         st_ivas->ism_extmeta_active = -1;
    1810       31931 :         st_ivas->ism_extmeta_cnt = 0;
    1811             : 
    1812       31931 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1813             : 
    1814       31931 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1815             :         {
    1816           0 :             return error;
    1817             :         }
    1818             : 
    1819       31931 :         if ( ( error = ivas_spar_dec_open( st_ivas, 0 ) ) != IVAS_ERR_OK )
    1820             :         {
    1821           0 :             return error;
    1822             :         }
    1823             : 
    1824       31931 :         if ( ( error = ivas_dirac_sba_config( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order,
    1825       31931 :                                               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 ),
    1826             :                                               st_ivas->ivas_format ) ) != IVAS_ERR_OK )
    1827             :         {
    1828           0 :             return error;
    1829             :         }
    1830             : 
    1831       31931 :         if ( output_config != IVAS_AUDIO_CONFIG_FOA && output_config != IVAS_AUDIO_CONFIG_STEREO && output_config != IVAS_AUDIO_CONFIG_MONO )
    1832             :         {
    1833       25832 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1834             :             {
    1835           0 :                 return error;
    1836             :             }
    1837             : 
    1838       25832 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    1839             :         }
    1840             :         else
    1841             :         {
    1842             :             int16_t band_grouping[IVAS_MAX_NUM_BANDS + 1];
    1843             : 
    1844        6099 :             st_ivas->hSpar->enc_param_start_band = min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    1845        6099 :             if ( ivas_get_hodirac_flag( ivas_total_brate, st_ivas->sba_analysis_order ) )
    1846             :             {
    1847         583 :                 st_ivas->hSpar->enc_param_start_band = 0;
    1848             : 
    1849         583 :                 set_c( (int8_t *) st_ivas->hQMetaData->twoDirBands, (int8_t) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    1850         583 :                 st_ivas->hQMetaData->numTwoDirBands = (uint8_t) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    1851             :             }
    1852             : 
    1853        6099 :             ivas_dirac_config_bands( band_grouping, IVAS_MAX_NUM_BANDS, (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ),
    1854        6099 :                                      st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    1855             :         }
    1856             : 
    1857       43181 :         for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1858             :         {
    1859       11250 :             if ( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate / st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
    1860             :             {
    1861           0 :                 return error;
    1862             :             }
    1863             : 
    1864       11250 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1865             :         }
    1866             : 
    1867       31931 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1868             :         {
    1869             :             {
    1870             :                 int16_t n_all;
    1871             : 
    1872       12265 :                 n_all = st_ivas->nchan_transport + st_ivas->nchan_ism;
    1873       12265 :                 st_ivas->nCPE = ( n_all + 1 ) >> 1;
    1874             :             }
    1875       12265 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1876             :         }
    1877             : 
    1878       81893 :         for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1879             :         {
    1880       49962 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS ) ) != IVAS_ERR_OK )
    1881             :             {
    1882           0 :                 return error;
    1883             :             }
    1884             : 
    1885      149886 :             for ( n = 0; n < CPE_CHANNELS; n++ )
    1886             :             {
    1887       99924 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1888             :             }
    1889             :         }
    1890             : 
    1891             :         /* create CPE element for DFT Stereo like upmix */
    1892       31931 :         if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nCPE == 0 )
    1893             :         {
    1894         727 :             if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    1895             :             {
    1896           0 :                 return error;
    1897             :             }
    1898             : 
    1899         727 :             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 */
    1900         727 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    1901             :         }
    1902             : 
    1903       31931 :         if ( st_ivas->nCPE > 1 )
    1904             :         {
    1905       13948 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    1906             :             {
    1907           0 :                 return error;
    1908             :             }
    1909             :         }
    1910             : 
    1911       31931 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1912             :         {
    1913       12265 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, temp_brate ) ) != IVAS_ERR_OK )
    1914             :             {
    1915           0 :                 return error;
    1916             :             }
    1917             : 
    1918       12265 :             if ( ( error = ivas_osba_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1919             :             {
    1920           0 :                 return error;
    1921             :             }
    1922             :         }
    1923             : 
    1924             :         /* set CNA/CNG flags */
    1925       31931 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1926             :     }
    1927       16547 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1928             :     {
    1929        8517 :         st_ivas->ism_extmeta_active = -1;
    1930        8517 :         st_ivas->ism_extmeta_cnt = 0;
    1931             : 
    1932        8517 :         if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    1933             :         {
    1934           0 :             return error;
    1935             :         }
    1936             : 
    1937        8517 :         k = 0;
    1938        8517 :         ism_total_brate = 0;
    1939       90905 :         while ( k < SIZE_IVAS_BRATE_TBL && ivas_total_brate != ivas_brate_tbl[k] )
    1940             :         {
    1941       82388 :             k++;
    1942             :         }
    1943             : 
    1944        8517 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1945             :         {
    1946             :             /* one separated object */
    1947        2210 :             st_ivas->nSCE = 1;
    1948             : 
    1949        2210 :             ism_total_brate = sep_object_brate[k - 2][0];
    1950        2210 :             if ( ( error = create_sce_dec( st_ivas, 0, ism_total_brate ) ) != IVAS_ERR_OK )
    1951             :             {
    1952           0 :                 return error;
    1953             :             }
    1954             : 
    1955        2210 :             reset_indices_dec( st_ivas->hSCE[0]->hCoreCoder[0] );
    1956             : 
    1957        2210 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, NULL ) ) != IVAS_ERR_OK )
    1958             :             {
    1959           0 :                 return error;
    1960             :             }
    1961             :         }
    1962        6307 :         else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1963             :         {
    1964             :             int32_t temp_brate[MAX_SCE];
    1965        6307 :             st_ivas->nSCE = st_ivas->nchan_ism; /* number of objects */
    1966             : 
    1967       19854 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1968             :             {
    1969       13547 :                 temp_brate[sce_id] = sep_object_brate[k - 2][st_ivas->nSCE - 1];
    1970       13547 :                 ism_total_brate += temp_brate[sce_id];
    1971             : 
    1972       13547 :                 if ( ( error = create_sce_dec( st_ivas, sce_id, temp_brate[sce_id] ) ) != IVAS_ERR_OK )
    1973             :                 {
    1974           0 :                     return error;
    1975             :                 }
    1976             : 
    1977       13547 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1978             :             }
    1979             : 
    1980        6307 :             if ( ( error = ivas_ism_metadata_dec_create( st_ivas, st_ivas->nchan_ism, temp_brate ) ) != IVAS_ERR_OK )
    1981             :             {
    1982           0 :                 return error;
    1983             :             }
    1984             :         }
    1985             : 
    1986        8517 :         if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    1987             :         {
    1988           0 :             return error;
    1989             :         }
    1990             : 
    1991        8517 :         if ( ( error = ivas_omasa_data_open( st_ivas ) ) != IVAS_ERR_OK )
    1992             :         {
    1993           0 :             return error;
    1994             :         }
    1995             : 
    1996        8517 :         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 )
    1997             :         {
    1998        7468 :             if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1999             :             {
    2000           0 :                 return error;
    2001             :             }
    2002             :         }
    2003             : 
    2004        8517 :         if ( ( error = create_cpe_dec( st_ivas, 0, ivas_total_brate - ism_total_brate ) ) != IVAS_ERR_OK )
    2005             :         {
    2006           0 :             return error;
    2007             :         }
    2008             : 
    2009       25551 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    2010             :         {
    2011       17034 :             reset_indices_dec( st_ivas->hCPE[0]->hCoreCoder[n] );
    2012             :         }
    2013             :     }
    2014        8030 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    2015             :     {
    2016        8030 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    2017             :         {
    2018             :             /* init EFAP for custom LS setup */
    2019        3499 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2020             :             {
    2021           7 :                 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 )
    2022             :                 {
    2023           0 :                     return error;
    2024             :                 }
    2025             :             }
    2026             : 
    2027        3499 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config ) );
    2028        3499 :             st_ivas->nSCE = 0;
    2029        3499 :             st_ivas->nCPE = st_ivas->nchan_transport / CPE_CHANNELS;
    2030             : 
    2031        3499 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2032             : 
    2033       17819 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2034             :             {
    2035       14320 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    2036             :                 {
    2037           0 :                     return error;
    2038             :                 }
    2039             : 
    2040       42960 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2041             :                 {
    2042       28640 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2043             :                 }
    2044             :             }
    2045             : 
    2046        3499 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    2047             :             {
    2048           0 :                 return error;
    2049             :             }
    2050             :         }
    2051        4531 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2052             :         {
    2053             :             /* init EFAP for custom LS setup */
    2054         231 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2055             :             {
    2056           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 )
    2057             :                 {
    2058           0 :                     return error;
    2059             :                 }
    2060             :             }
    2061             : 
    2062         231 :             st_ivas->nSCE = 0;
    2063         231 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / CPE_CHANNELS;
    2064         231 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    2065             : 
    2066         231 :             if ( ( error = ivas_mc_paramupmix_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    2067             :             {
    2068           0 :                 return error;
    2069             :             }
    2070             : 
    2071         231 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2072             : 
    2073        1155 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2074             :             {
    2075         924 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
    2076             :                 {
    2077           0 :                     return error;
    2078             :                 }
    2079             : 
    2080        2772 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2081             :                 {
    2082        1848 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2083             :                 }
    2084             :             }
    2085             : 
    2086         231 :             if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    2087             :             {
    2088           0 :                 return error;
    2089             :             }
    2090             :         }
    2091        4300 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    2092             :         {
    2093             :             /* init EFAP for custom LS setup */
    2094        1508 :             if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2095             :             {
    2096           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 )
    2097             :                 {
    2098           0 :                     return error;
    2099             :                 }
    2100             :             }
    2101             : 
    2102        1508 :             if ( ( error = ivas_param_mc_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    2103             :             {
    2104           0 :                 return error;
    2105             :             }
    2106             : 
    2107        3595 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2108             :             {
    2109        2087 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    2110             :                 {
    2111           0 :                     return error;
    2112             :                 }
    2113             : 
    2114        6261 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2115             :                 {
    2116        4174 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2117             :                 }
    2118             :             }
    2119             : 
    2120        1508 :             if ( st_ivas->nCPE > 1 )
    2121             :             {
    2122         579 :                 if ( ( error = create_mct_dec( st_ivas ) ) != IVAS_ERR_OK )
    2123             :                 {
    2124           0 :                     return error;
    2125             :                 }
    2126             :             }
    2127             :         }
    2128        2792 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    2129             :         {
    2130             :             int32_t brate_sce, brate_cpe;
    2131             : 
    2132        2792 :             ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    2133             : 
    2134        2792 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
    2135             :             {
    2136           0 :                 return error;
    2137             :             }
    2138             : 
    2139        2792 :             if ( ( error = ivas_masa_dec_open( st_ivas ) ) != IVAS_ERR_OK )
    2140             :             {
    2141           0 :                 return error;
    2142             :             }
    2143             : 
    2144        2792 :             st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    2145             : 
    2146        2792 :             if ( st_ivas->renderer_type != RENDERER_DISABLE && st_ivas->renderer_type != RENDERER_MCMASA_MONO_STEREO )
    2147             :             {
    2148        2417 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    2149             :                 {
    2150           0 :                     return error;
    2151             :                 }
    2152             :             }
    2153             : 
    2154        2792 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.separateChannelEnabled && !st_ivas->hLsSetupCustom->separate_ch_found )
    2155             :             {
    2156             :                 /* If no speaker matching the separated channel, compute panning gains for the separated channel. */
    2157           0 :                 if ( st_ivas->hVBAPdata == NULL )
    2158             :                 {
    2159             :                     /* Distribute signal to all channels if VBAP is not properly initialized. */
    2160           0 :                     set_f( st_ivas->hLsSetupCustom->separate_ch_gains, inv_sqrt( st_ivas->hLsSetupCustom->num_spk ), st_ivas->hLsSetupCustom->num_spk );
    2161             :                 }
    2162             :                 else
    2163             :                 {
    2164           0 :                     vbap_determine_gains( st_ivas->hVBAPdata, st_ivas->hLsSetupCustom->separate_ch_gains, 0, 0, 0 );
    2165             :                 }
    2166             :             }
    2167             : 
    2168        2792 :             ivas_mcmasa_split_brate( st_ivas->hOutSetup.separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
    2169             : 
    2170        5380 :             for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2171             :             {
    2172        2588 :                 if ( ( error = create_sce_dec( st_ivas, sce_id, brate_sce ) ) != IVAS_ERR_OK )
    2173             :                 {
    2174           0 :                     return error;
    2175             :                 }
    2176             : 
    2177        2588 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2178             :             }
    2179             : 
    2180        3494 :             for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2181             :             {
    2182         702 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT; /* element_mode_init was IVAS_SCE for SCE initialization */
    2183             : 
    2184         702 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, brate_cpe ) ) != IVAS_ERR_OK )
    2185             :                 {
    2186           0 :                     return error;
    2187             :                 }
    2188             : 
    2189        2106 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2190             :                 {
    2191        1404 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2192             :                 }
    2193             :             }
    2194             : 
    2195             :             /* create CPE element for DFT Stereo like upmix */
    2196        2792 :             if ( st_ivas->sba_dirac_stereo_flag )
    2197             :             {
    2198         141 :                 if ( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nSCE + st_ivas->nCPE ) ) ) != IVAS_ERR_OK )
    2199             :                 {
    2200           0 :                     return error;
    2201             :                 }
    2202             : 
    2203         141 :                 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 */
    2204         141 :                 st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    2205             :             }
    2206             : 
    2207             :             /* set CNA/CNG flags */
    2208        2792 :             if ( st_ivas->nchan_transport == 1 && ( ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) )
    2209             :             {
    2210         644 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag = 1;
    2211         644 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag = 1;
    2212             :             }
    2213             :         }
    2214             :     }
    2215             : #ifdef DEBUGGING
    2216             :     else
    2217             :     {
    2218             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Invalid IVAS format. Exiting,\n" );
    2219             :     }
    2220             : #endif
    2221             : 
    2222             : 
    2223             :     /*-----------------------------------------------------------------*
    2224             :      * Allocate and initialize HP20 filter memories
    2225             :      *-----------------------------------------------------------------*/
    2226             : 
    2227             :     /* set number of output channels used for synthesis/decoding */
    2228       90741 :     n = getNumChanSynthesis( st_ivas );
    2229             : 
    2230       90741 :     if ( n > 0 )
    2231             :     {
    2232       90741 :         if ( ( st_ivas->mem_hp20_out = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
    2233             :         {
    2234           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2235             :         }
    2236             :     }
    2237             :     else
    2238             :     {
    2239           0 :         st_ivas->mem_hp20_out = NULL;
    2240             :     }
    2241             : 
    2242      354612 :     for ( i = 0; i < n; i++ )
    2243             :     {
    2244      263871 :         if ( ( st_ivas->mem_hp20_out[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
    2245             :         {
    2246           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2247             :         }
    2248             : 
    2249      263871 :         set_f( st_ivas->mem_hp20_out[i], 0.0f, L_HP20_MEM );
    2250             :     }
    2251             : 
    2252             :     /*-------------------------------------------------------------------*
    2253             :      * Allocate and initialize rendering handles
    2254             :      *--------------------------------------------------------------------*/
    2255             : 
    2256       90741 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    2257             :     {
    2258             : 
    2259        6698 :         if ( ( error = ivas_binRenderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2260             :         {
    2261           0 :             return error;
    2262             :         }
    2263             :     }
    2264             : 
    2265             :     /* ParamISM is handled separately from other common config */
    2266       84043 :     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 ) )
    2267             :     {
    2268         496 :         if ( st_ivas->renderer_type != RENDERER_STEREO_PARAMETRIC )
    2269             :         {
    2270         496 :             if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
    2271             :             {
    2272           0 :                 return error;
    2273             :             }
    2274             :         }
    2275             : 
    2276         496 :         if ( ( error = ivas_dirac_dec_init_binaural_data( st_ivas, &( st_ivas->hHrtfParambin ) ) ) != IVAS_ERR_OK )
    2277             :         {
    2278           0 :             return error;
    2279             :         }
    2280             :     }
    2281       83547 :     else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    2282             :     {
    2283        3559 :         if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2284             :         {
    2285           0 :             return error;
    2286             :         }
    2287             : 
    2288        3559 :         if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2289             :         {
    2290        1416 :             if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, output_Fs ) ) != IVAS_ERR_OK )
    2291             :             {
    2292           0 :                 return error;
    2293             :             }
    2294             :         }
    2295             :     }
    2296       79988 :     else if ( st_ivas->renderer_type == RENDERER_MC )
    2297             :     {
    2298        2188 :         if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
    2299             :         {
    2300           0 :             return error;
    2301             :         }
    2302             :     }
    2303       77800 :     else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
    2304             :     {
    2305        2339 :         if ( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2306             :         {
    2307           0 :             return error;
    2308             :         }
    2309             :     }
    2310       75461 :     else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    2311             :     {
    2312        2468 :         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 ) )
    2313             :         {
    2314          28 :             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 )
    2315             :             {
    2316           0 :                 return error;
    2317             :             }
    2318             :         }
    2319             : 
    2320        2468 :         if ( ( error = ivas_rend_openCrend( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, output_config,
    2321        2468 :                                             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 )
    2322             :         {
    2323           0 :             return error;
    2324             :         }
    2325             : 
    2326        2468 :         st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
    2327             :     }
    2328             : 
    2329       90741 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2330             :     {
    2331        8517 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2332             :         {
    2333             :             /* Allocate TD renderer for the objects in DISC mode */
    2334        1001 :             if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2335             :             {
    2336           0 :                 return error;
    2337             :             }
    2338             : 
    2339        1001 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2340             :             {
    2341           0 :                 if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, output_Fs ) ) != IVAS_ERR_OK )
    2342             :                 {
    2343           0 :                     return error;
    2344             :                 }
    2345             :             }
    2346             : 
    2347             :             /* Allocate memory for delay buffer within 'hMasaIsmData' */
    2348        1001 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2349             :             {
    2350           0 :                 return error;
    2351             :             }
    2352             :         }
    2353             : 
    2354        8517 :         if ( ( st_ivas->renderer_type == RENDERER_DIRAC ) &&
    2355        4010 :              ( 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 ) )
    2356             :         {
    2357             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2358        4010 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2359             :             {
    2360           0 :                 return error;
    2361             :             }
    2362        4010 :             if ( ( error = ivas_omasa_separate_object_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2363             :             {
    2364           0 :                 return error;
    2365             :             }
    2366             :         }
    2367             : 
    2368        8517 :         if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
    2369             :         {
    2370             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2371          81 :             if ( ( error = ivas_omasa_objects_delay_open( st_ivas ) ) != IVAS_ERR_OK )
    2372             :             {
    2373           0 :                 return error;
    2374             :             }
    2375             : 
    2376          81 :             if ( ( error = ivas_spat_hSpatParamRendCom_config( &st_ivas->hSpatParamRendCom, DIRAC_OPEN, 0,
    2377             :                                                                st_ivas->ivas_format, st_ivas->mc_mode, output_Fs, 0, 0 ) ) != IVAS_ERR_OK )
    2378             :             {
    2379           0 :                 return error;
    2380             :             }
    2381             :         }
    2382             : 
    2383        8517 :         if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
    2384             :         {
    2385             :             /* Allocate 'hIsmRendererData' handle */
    2386          48 :             if ( ( error = ivas_omasa_combine_separate_ism_with_masa_open( st_ivas ) ) != IVAS_ERR_OK )
    2387             :             {
    2388           0 :                 return error;
    2389             :             }
    2390             :         }
    2391             :     }
    2392             : 
    2393       90741 :     if ( ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) &&
    2394       48473 :          ( st_ivas->ism_mode == ISM_MODE_DISC || st_ivas->ism_mode == ISM_SBA_MODE_DISC ) &&
    2395       27180 :          ( st_ivas->renderer_type == RENDERER_TD_PANNING ||
    2396       21559 :            st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX ||
    2397       21552 :            st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ||
    2398       18736 :            st_ivas->renderer_type == RENDERER_OSBA_STEREO ||
    2399       17973 :            st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
    2400       15691 :            st_ivas->renderer_type == RENDERER_OSBA_LS ||
    2401       11897 :            st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    2402        9168 :            st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    2403        8006 :            st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2404             :     {
    2405       20615 :         if ( ( error = ivas_ism_renderer_open( st_ivas ) ) != IVAS_ERR_OK )
    2406             :         {
    2407           0 :             return error;
    2408             :         }
    2409             :     }
    2410             : 
    2411       90741 :     if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2412             :     {
    2413       31931 :         if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ) && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2414             :         {
    2415             :             /* Allocate TD renderer for the objects in DISC mode */
    2416        2729 :             if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    2417             :             {
    2418           0 :                 return error;
    2419             :             }
    2420             : 
    2421        2729 :             if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2422             :             {
    2423        1150 :                 if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, output_Fs ) ) != IVAS_ERR_OK )
    2424             :                 {
    2425           0 :                     return error;
    2426             :                 }
    2427             :             }
    2428             :         }
    2429             :     }
    2430             : 
    2431             :     /*-----------------------------------------------------------------*
    2432             :      * CLDFB handles for rendering
    2433             :      *-----------------------------------------------------------------*/
    2434             : 
    2435       90741 :     ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses, &numCldfbSyntheses );
    2436             : 
    2437      322035 :     for ( i = 0; i < numCldfbAnalyses; i++ )
    2438             :     {
    2439      231294 :         if ( ( error = openCldfb( &( st_ivas->cldfbAnaDec[i] ), CLDFB_ANALYSIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    2440             :         {
    2441           0 :             return error;
    2442             :         }
    2443             :     }
    2444     1311303 :     for ( ; i < MAX_INTERN_CHANNELS; i++ )
    2445             :     {
    2446     1220562 :         st_ivas->cldfbAnaDec[i] = NULL;
    2447             :     }
    2448             : 
    2449      616340 :     for ( i = 0; i < numCldfbSyntheses; i++ )
    2450             :     {
    2451      525599 :         if ( ( error = openCldfb( &( st_ivas->cldfbSynDec[i] ), CLDFB_SYNTHESIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    2452             :         {
    2453           0 :             return error;
    2454             :         }
    2455             :     }
    2456     1016998 :     for ( ; i < MAX_OUTPUT_CHANNELS; i++ )
    2457             :     {
    2458      926257 :         st_ivas->cldfbSynDec[i] = NULL;
    2459             :     }
    2460             : 
    2461             :     /* CLDFB Interpolation weights */
    2462       90741 :     if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) && !st_ivas->sba_dirac_stereo_flag && hDecoderConfig->nchan_out != 1 )
    2463             :     {
    2464       39913 :         ivas_spar_get_cldfb_gains( st_ivas->hSpar, st_ivas->cldfbAnaDec[0], st_ivas->cldfbSynDec[0], hDecoderConfig );
    2465             :     }
    2466             : 
    2467             :     /*-----------------------------------------------------------------*
    2468             :      * LFE handles for rendering after rendering to adjust LFE delay to
    2469             :      * filter delay
    2470             :      *-----------------------------------------------------------------*/
    2471             : 
    2472       90741 :     if ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2473             :     {
    2474        3730 :         if ( st_ivas->hIntSetup.index_lfe[0] != -1 )
    2475             :         {
    2476        3495 :             delay_ns = st_ivas->binaural_latency_ns;
    2477             :         }
    2478             :         else
    2479             :         {
    2480         235 :             delay_ns = 0;
    2481             :         }
    2482             : 
    2483        3730 :         if ( st_ivas->hBinRenderer != NULL )
    2484             :         {
    2485         154 :             if ( st_ivas->hBinRenderer->render_lfe )
    2486             :             {
    2487          34 :                 if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2488             :                 {
    2489             :                     /* Account for filterbank delay */
    2490          34 :                     delay_ns += IVAS_FB_DEC_DELAY_NS;
    2491             :                 }
    2492             :             }
    2493             :             else
    2494             :             {
    2495         120 :                 delay_ns = 0;
    2496             :             }
    2497             :         }
    2498             :         else
    2499             :         {
    2500        3576 :             if ( ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) && ( st_ivas->cldfbSynDec[0] != NULL ) )
    2501             :             {
    2502          63 :                 delay_ns += IVAS_FB_DEC_DELAY_NS;
    2503             :             }
    2504             :         }
    2505             : 
    2506        3730 :         if ( ( error = ivas_create_lfe_dec( &st_ivas->hLFE, output_Fs, delay_ns ) ) != IVAS_ERR_OK )
    2507             :         {
    2508           0 :             return error;
    2509             :         }
    2510             : 
    2511        3730 :         set_zero( st_ivas->hLFE->prevsynth_buf, LFE_PLC_BUFLEN );
    2512        3730 :         set_zero( st_ivas->hLFE->prior_out_buffer, L_FRAME48k );
    2513             :     }
    2514             : 
    2515             :     /*-----------------------------------------------------------------*
    2516             :      * Allocate and initialize limiter struct
    2517             :      *-----------------------------------------------------------------*/
    2518             : 
    2519       90741 :     if ( ( error = ivas_limiter_open( &st_ivas->hLimiter, hDecoderConfig->nchan_out, output_Fs ) ) != IVAS_ERR_OK )
    2520             :     {
    2521           0 :         return error;
    2522             :     }
    2523             : 
    2524             :     /*-----------------------------------------------------------------*
    2525             :      * Allocate and initialize TC struct + buffer
    2526             :      *-----------------------------------------------------------------*/
    2527             : 
    2528       90741 :     if ( st_ivas->hTcBuffer == NULL )
    2529             :     {
    2530             :         /* no module has yet open the TC buffer, open a default one */
    2531       23703 :         granularity = ivas_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), output_Fs );
    2532       23703 :         n_channels_transport = ivas_dec_get_num_tc_channels( st_ivas );
    2533             : 
    2534       23703 :         if ( ( error = ivas_dec_tc_buffer_open( st_ivas, ivas_dec_get_tc_buffer_mode( st_ivas ), n_channels_transport, n_channels_transport, n_channels_transport, granularity ) ) != IVAS_ERR_OK )
    2535             :         {
    2536           0 :             return error;
    2537             :         }
    2538             :     }
    2539             : 
    2540       90741 :     if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && hDecoderConfig->Opt_tsm )
    2541             :     {
    2542        7576 :         if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    2543             :         {
    2544         510 :             if ( ( error = ivas_jbm_dec_masa_metadata_open( st_ivas ) ) != IVAS_ERR_OK )
    2545             :             {
    2546           0 :                 return error;
    2547             :             }
    2548             :         }
    2549             :     }
    2550             : 
    2551             :     /*-----------------------------------------------------------------*
    2552             :      * Allocate floating-point output audio buffers
    2553             :      *-----------------------------------------------------------------*/
    2554             : 
    2555       90741 :     nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, ivas_total_brate );
    2556       90741 :     if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff, hDecoderConfig->Opt_tsm, st_ivas->hTcBuffer ) ) != IVAS_ERR_OK )
    2557             :     {
    2558           0 :         return error;
    2559             :     }
    2560             : 
    2561       90741 :     return IVAS_ERR_OK;
    2562             : }
    2563             : 
    2564             : 
    2565             : /*-------------------------------------------------------------------------
    2566             :  * destroy_core_dec()
    2567             :  *
    2568             :  * Close core decoder handles
    2569             :  *-------------------------------------------------------------------------*/
    2570             : 
    2571      697640 : void destroy_core_dec(
    2572             :     DEC_CORE_HANDLE hCoreCoder /* i/o: core decoder structure      */
    2573             : )
    2574             : {
    2575      697640 :     destroy_cldfb_decoder( hCoreCoder );
    2576             : 
    2577      697640 :     if ( hCoreCoder->hGSCDec != NULL )
    2578             :     {
    2579      271822 :         free( hCoreCoder->hGSCDec );
    2580      271822 :         hCoreCoder->hGSCDec = NULL;
    2581             :     }
    2582             : 
    2583      697640 :     if ( hCoreCoder->hPFstat != NULL )
    2584             :     {
    2585      271822 :         free( hCoreCoder->hPFstat );
    2586      271822 :         hCoreCoder->hPFstat = NULL;
    2587             :     }
    2588             : 
    2589      697640 :     if ( hCoreCoder->hMusicPF != NULL )
    2590             :     {
    2591      271822 :         free( hCoreCoder->hMusicPF );
    2592      271822 :         hCoreCoder->hMusicPF = NULL;
    2593             :     }
    2594             : 
    2595      697640 :     if ( hCoreCoder->hBPF != NULL )
    2596             :     {
    2597      271822 :         free( hCoreCoder->hBPF );
    2598      271822 :         hCoreCoder->hBPF = NULL;
    2599             :     }
    2600             : 
    2601      697640 :     if ( hCoreCoder->hBWE_zero != NULL )
    2602             :     {
    2603      271822 :         free( hCoreCoder->hBWE_zero );
    2604      271822 :         hCoreCoder->hBWE_zero = NULL;
    2605             :     }
    2606             : 
    2607      697640 :     if ( hCoreCoder->hTdCngDec != NULL )
    2608             :     {
    2609      131372 :         free( hCoreCoder->hTdCngDec );
    2610      131372 :         hCoreCoder->hTdCngDec = NULL;
    2611             :     }
    2612             : 
    2613      697640 :     if ( hCoreCoder->hSC_VBR != NULL )
    2614             :     {
    2615        1266 :         free( hCoreCoder->hSC_VBR );
    2616        1266 :         hCoreCoder->hSC_VBR = NULL;
    2617             :     }
    2618             : 
    2619      697640 :     if ( hCoreCoder->hAmrwb_IO != NULL )
    2620             :     {
    2621        1266 :         free( hCoreCoder->hAmrwb_IO );
    2622        1266 :         hCoreCoder->hAmrwb_IO = NULL;
    2623             :     }
    2624             : 
    2625      697640 :     if ( hCoreCoder->hBWE_TD != NULL )
    2626             :     {
    2627      272222 :         free( hCoreCoder->hBWE_TD );
    2628      272222 :         hCoreCoder->hBWE_TD = NULL;
    2629             :     }
    2630             : 
    2631      697640 :     if ( hCoreCoder->hBWE_FD != NULL )
    2632             :     {
    2633      272222 :         free( hCoreCoder->hBWE_FD );
    2634      272222 :         hCoreCoder->hBWE_FD = NULL;
    2635             :     }
    2636             : 
    2637      697640 :     if ( hCoreCoder->hBWE_FD_HR != NULL )
    2638             :     {
    2639        1266 :         free( hCoreCoder->hBWE_FD_HR );
    2640        1266 :         hCoreCoder->hBWE_FD_HR = NULL;
    2641             :     }
    2642             : 
    2643      697640 :     if ( hCoreCoder->hWIDec != NULL )
    2644             :     {
    2645         546 :         free( hCoreCoder->hWIDec );
    2646         546 :         hCoreCoder->hWIDec = NULL;
    2647             :     }
    2648             : 
    2649      697640 :     if ( hCoreCoder->hTECDec != NULL )
    2650             :     {
    2651        1266 :         free( hCoreCoder->hTECDec );
    2652        1266 :         hCoreCoder->hTECDec = NULL;
    2653             :     }
    2654             : 
    2655      697640 :     if ( hCoreCoder->hTcxLtpDec != NULL )
    2656             :     {
    2657      690241 :         free( hCoreCoder->hTcxLtpDec );
    2658      690241 :         hCoreCoder->hTcxLtpDec = NULL;
    2659             :     }
    2660             : 
    2661      697640 :     if ( hCoreCoder->hTcxDec != NULL )
    2662             :     {
    2663      690241 :         free( hCoreCoder->hTcxDec );
    2664      690241 :         hCoreCoder->hTcxDec = NULL;
    2665             :     }
    2666             : 
    2667      697640 :     if ( hCoreCoder->hTcxCfg != NULL )
    2668             :     {
    2669      690241 :         free( hCoreCoder->hTcxCfg );
    2670      690241 :         hCoreCoder->hTcxCfg = NULL;
    2671             :     }
    2672             : 
    2673      697640 :     if ( hCoreCoder->hTonalMDCTConc != NULL )
    2674             :     {
    2675      690241 :         free( hCoreCoder->hTonalMDCTConc );
    2676      690241 :         hCoreCoder->hTonalMDCTConc = NULL;
    2677             :     }
    2678             : 
    2679      697640 :     if ( hCoreCoder->hIGFDec != NULL )
    2680             :     {
    2681      690241 :         free( hCoreCoder->hIGFDec );
    2682      690241 :         hCoreCoder->hIGFDec = NULL;
    2683             :     }
    2684             : 
    2685      697640 :     if ( hCoreCoder->hPlcInfo != NULL )
    2686             :     {
    2687        1266 :         free( hCoreCoder->hPlcInfo );
    2688        1266 :         hCoreCoder->hPlcInfo = NULL;
    2689             :     }
    2690             : 
    2691      697640 :     if ( hCoreCoder->hHQ_core != NULL )
    2692             :     {
    2693      690241 :         free( hCoreCoder->hHQ_core );
    2694      690241 :         hCoreCoder->hHQ_core = NULL;
    2695             :     }
    2696             : 
    2697      697640 :     if ( hCoreCoder->hHQ_nbfec != NULL )
    2698             :     {
    2699        1266 :         free( hCoreCoder->hHQ_nbfec );
    2700        1266 :         hCoreCoder->hHQ_nbfec = NULL;
    2701             :     }
    2702             : 
    2703      697640 :     return;
    2704             : }
    2705             : 
    2706             : 
    2707             : /*-------------------------------------------------------------------------
    2708             :  * ivas_initialize_handles_dec()
    2709             :  *
    2710             :  * NULL initialization of handles
    2711             :  *-------------------------------------------------------------------------*/
    2712             : 
    2713       90769 : void ivas_initialize_handles_dec(
    2714             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                  */
    2715             : )
    2716             : {
    2717             :     int16_t i;
    2718             : 
    2719     1543073 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    2720             :     {
    2721     1452304 :         st_ivas->cldfbAnaDec[i] = NULL;
    2722             :     }
    2723             : 
    2724     1543073 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    2725             :     {
    2726     1452304 :         st_ivas->cldfbSynDec[i] = NULL;
    2727             :     }
    2728             : 
    2729             :     /* SCE handles */
    2730      453845 :     for ( i = 0; i < MAX_SCE; i++ )
    2731             :     {
    2732      363076 :         st_ivas->hSCE[i] = NULL;
    2733             :     }
    2734             : 
    2735             :     /* CPE handles */
    2736      635383 :     for ( i = 0; i < MAX_CPE; i++ )
    2737             :     {
    2738      544614 :         st_ivas->hCPE[i] = NULL;
    2739             :     }
    2740             : 
    2741       90769 :     st_ivas->bit_stream = NULL;
    2742       90769 :     st_ivas->mem_hp20_out = NULL;
    2743       90769 :     st_ivas->hLimiter = NULL;
    2744             : 
    2745             :     /* ISM metadata handles */
    2746      453845 :     for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
    2747             :     {
    2748      363076 :         st_ivas->hIsmMetaData[i] = NULL;
    2749             :     }
    2750             : 
    2751             :     /* spatial coding handles */
    2752       90769 :     st_ivas->hDirAC = NULL;
    2753       90769 :     st_ivas->hParamIsmDec = NULL;
    2754       90769 :     st_ivas->hSpar = NULL;
    2755       90769 :     st_ivas->hMasa = NULL;
    2756       90769 :     st_ivas->hQMetaData = NULL;
    2757       90769 :     st_ivas->hMCT = NULL;
    2758       90769 :     st_ivas->hMCParamUpmix = NULL;
    2759       90769 :     st_ivas->hParamMC = NULL;
    2760       90769 :     st_ivas->hLFE = NULL;
    2761             : 
    2762             :     /* rendering handles */
    2763       90769 :     st_ivas->hBinRenderer = NULL;
    2764      816921 :     for ( i = 0; i < MAX_HEAD_ROT_POSES; i++ )
    2765             :     {
    2766      726152 :         st_ivas->hDiracDecBin[i] = NULL;
    2767             :     }
    2768       90769 :     st_ivas->hDirACRend = NULL;
    2769       90769 :     st_ivas->hSpatParamRendCom = NULL;
    2770       90769 :     st_ivas->hLsSetUpConversion = NULL;
    2771       90769 :     st_ivas->hEFAPdata = NULL;
    2772       90769 :     st_ivas->hVBAPdata = NULL;
    2773       90769 :     st_ivas->hIsmRendererData = NULL;
    2774       90769 :     st_ivas->hBinRendererTd = NULL;
    2775       90769 :     st_ivas->hMonoDmxRenderer = NULL;
    2776       90769 :     st_ivas->hCrendWrapper = NULL;
    2777       90769 :     st_ivas->hReverb = NULL;
    2778       90769 :     st_ivas->hHrtfCrend = NULL;
    2779       90769 :     st_ivas->hHrtfFastConv = NULL;
    2780       90769 :     st_ivas->hHrtfParambin = NULL;
    2781       90769 :     st_ivas->hHrtfStatistics = NULL;
    2782       90769 :     st_ivas->hoa_dec_mtx = NULL;
    2783       90769 :     st_ivas->hMasaIsmData = NULL;
    2784       90769 :     st_ivas->hSbaIsmData = NULL;
    2785             : 
    2786       90769 :     st_ivas->hHeadTrackData = NULL;
    2787       90769 :     st_ivas->hHrtfTD = NULL;
    2788       90769 :     st_ivas->hLsSetupCustom = NULL;
    2789       90769 :     st_ivas->hRenderConfig = NULL;
    2790       90769 :     st_ivas->hExtOrientationData = NULL;
    2791       90769 :     st_ivas->hCombinedOrientationData = NULL;
    2792       90769 :     st_ivas->acousticEnvironmentsCount = 0;
    2793       90769 :     st_ivas->pAcousticEnvironments = NULL;
    2794             : 
    2795       90769 :     st_ivas->hSplitBinRend = NULL;
    2796      726152 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2797             :     {
    2798      635383 :         st_ivas->hTdRendHandles[i] = NULL;
    2799             :     }
    2800             : 
    2801       90769 :     st_ivas->hTcBuffer = NULL;
    2802       90769 :     st_ivas->hJbmMetadata = NULL;
    2803             : 
    2804             :     /*  floating-point output audio buffers */
    2805     1906149 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    2806             :     {
    2807     1815380 :         st_ivas->p_output_f[i] = NULL;
    2808             :     }
    2809             : 
    2810       90769 :     return;
    2811             : }
    2812             : 
    2813             : 
    2814             : /*-------------------------------------------------------------------------
    2815             :  * ivas_destroy_dec()
    2816             :  *
    2817             :  * Close IVAS decoder handles
    2818             :  *-------------------------------------------------------------------------*/
    2819             : 
    2820       90769 : void ivas_destroy_dec(
    2821             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle      */
    2822             : )
    2823             : {
    2824             :     int16_t i;
    2825             : 
    2826             :     /* CLDFB handles */
    2827     1543073 :     for ( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    2828             :     {
    2829     1452304 :         if ( st_ivas->cldfbAnaDec[i] != NULL )
    2830             :         {
    2831      231478 :             deleteCldfb( &( st_ivas->cldfbAnaDec[i] ) );
    2832             :         }
    2833             :     }
    2834             : 
    2835     1543073 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    2836             :     {
    2837     1452304 :         if ( st_ivas->cldfbSynDec[i] != NULL )
    2838             :         {
    2839      526385 :             deleteCldfb( &( st_ivas->cldfbSynDec[i] ) );
    2840             :         }
    2841             :     }
    2842             : 
    2843             :     /* SCE handles */
    2844      453845 :     for ( i = 0; i < MAX_SCE; i++ )
    2845             :     {
    2846      363076 :         if ( st_ivas->hSCE[i] != NULL )
    2847             :         {
    2848       79248 :             destroy_sce_dec( st_ivas->hSCE[i] );
    2849       79248 :             st_ivas->hSCE[i] = NULL;
    2850             :         }
    2851             :     }
    2852             : 
    2853             :     /* CPE handles */
    2854      635383 :     for ( i = 0; i < MAX_CPE; i++ )
    2855             :     {
    2856      544614 :         if ( st_ivas->hCPE[i] != NULL )
    2857             :         {
    2858             :             /* set pointer to NULL as core coder already deallocated in destroy_sce_dec() */
    2859      100548 :             if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nchan_transport == 1 )
    2860             :             {
    2861        1383 :                 st_ivas->hCPE[i]->hCoreCoder[0] = NULL;
    2862        1383 :                 st_ivas->hCPE[i]->hCoreCoder[1] = NULL;
    2863             :             }
    2864      100548 :             destroy_cpe_dec( st_ivas->hCPE[i] );
    2865      100548 :             st_ivas->hCPE[i] = NULL;
    2866             :         }
    2867             :     }
    2868             : 
    2869             :     /* HP20 filter handles */
    2870       90769 :     if ( st_ivas->mem_hp20_out != NULL )
    2871             :     {
    2872      355449 :         for ( i = 0; i < getNumChanSynthesis( st_ivas ); i++ )
    2873             :         {
    2874      264708 :             free( st_ivas->mem_hp20_out[i] );
    2875      264708 :             st_ivas->mem_hp20_out[i] = NULL;
    2876             :         }
    2877       90741 :         free( st_ivas->mem_hp20_out );
    2878       90741 :         st_ivas->mem_hp20_out = NULL;
    2879             :     }
    2880             : 
    2881             :     /* ISM metadata handles */
    2882       90769 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    2883             : 
    2884             :     /* ISM renderer handle */
    2885       90769 :     ivas_ism_renderer_close( &( st_ivas->hIsmRendererData ) );
    2886             : 
    2887             :     /* DirAC handle */
    2888       90769 :     if ( st_ivas->ivas_format == ISM_FORMAT )
    2889             :     {
    2890       16542 :         ivas_param_ism_dec_close( &( st_ivas->hParamIsmDec ), &( st_ivas->hSpatParamRendCom ), st_ivas->hDecoderConfig->output_config );
    2891             :     }
    2892             :     else
    2893             :     {
    2894       74227 :         ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
    2895       74227 :         ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
    2896       74227 :         ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
    2897             :     }
    2898             : 
    2899             :     /* SPAR handle */
    2900       90769 :     ivas_spar_dec_close( &( st_ivas->hSpar ), st_ivas->hDecoderConfig->output_Fs, 0 );
    2901             : 
    2902             :     /* HOA decoder matrix */
    2903       90769 :     if ( st_ivas->hoa_dec_mtx != NULL )
    2904             :     {
    2905       15996 :         free( st_ivas->hoa_dec_mtx );
    2906       15996 :         st_ivas->hoa_dec_mtx = NULL;
    2907             :     }
    2908             : 
    2909             :     /* MASA decoder structure */
    2910       90769 :     ivas_masa_dec_close( &( st_ivas->hMasa ) );
    2911             : 
    2912             :     /* Qmetadata handle */
    2913       90769 :     ivas_qmetadata_close( &st_ivas->hQMetaData );
    2914             : 
    2915             :     /* MCT handle */
    2916       90769 :     ivas_mct_dec_close( &st_ivas->hMCT );
    2917             : 
    2918             :     /* LFE handle */
    2919       90769 :     ivas_lfe_dec_close( &( st_ivas->hLFE ) );
    2920             : 
    2921             :     /* Param-Upmix MC handle */
    2922       90769 :     ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
    2923             : 
    2924             :     /* Parametric MC handle */
    2925       90769 :     ivas_param_mc_dec_close( &st_ivas->hParamMC );
    2926             : 
    2927             :     /* EFAP handle */
    2928       90769 :     efap_free_data( &st_ivas->hEFAPdata );
    2929             : 
    2930             :     /* VBAP handle */
    2931       90769 :     vbap_free_data( &( st_ivas->hVBAPdata ) );
    2932             : 
    2933             :     /* Fastconv binaural renderer handle */
    2934       90769 :     ivas_binRenderer_close( &st_ivas->hBinRenderer );
    2935             : 
    2936             :     /* TD binaural renderer handles */
    2937      726152 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2938             :     {
    2939      635383 :         if ( st_ivas->hTdRendHandles[i] != NULL )
    2940             :         {
    2941        3964 :             st_ivas->hTdRendHandles[i]->HrFiltSet_p = NULL;
    2942        3964 :             ivas_td_binaural_close( &st_ivas->hTdRendHandles[i] );
    2943             :         }
    2944             :     }
    2945             : 
    2946             :     /* Parametric binaural renderer handle */
    2947       90769 :     ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
    2948             : 
    2949             :     /* Crend handle */
    2950       90769 :     ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ) );
    2951             : 
    2952             :     /* Reverb handle */
    2953       90769 :     ivas_reverb_close( &st_ivas->hReverb );
    2954             : 
    2955             :     /* LS config converter handle */
    2956       90769 :     ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
    2957             : 
    2958             :     /* Custom LS configuration handle */
    2959       90769 :     if ( st_ivas->hLsSetupCustom != NULL )
    2960             :     {
    2961          21 :         free( st_ivas->hLsSetupCustom );
    2962          21 :         st_ivas->hLsSetupCustom = NULL;
    2963             :     }
    2964             : 
    2965             :     /* Mono downmix structure */
    2966       90769 :     ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
    2967             : 
    2968             :     /* OSBA structure */
    2969       90769 :     ivas_osba_data_close( &st_ivas->hSbaIsmData );
    2970             : 
    2971             :     /* OMASA structure */
    2972       90769 :     ivas_omasa_data_close( &st_ivas->hMasaIsmData );
    2973             : 
    2974             :     /* Head track data handle */
    2975       90769 :     ivas_headTrack_close( &st_ivas->hHeadTrackData );
    2976             : 
    2977             :     /* External orientation data handle */
    2978       90769 :     ivas_external_orientation_close( &st_ivas->hExtOrientationData );
    2979             : 
    2980             :     /* Combined orientation data handle */
    2981       90769 :     ivas_combined_orientation_close( &st_ivas->hCombinedOrientationData );
    2982             : 
    2983             :     /* Time Domain binaural renderer handle */
    2984       90769 :     ivas_td_binaural_close( &st_ivas->hBinRendererTd );
    2985       90769 :     ivas_HRTF_td_binary_close( &st_ivas->hHrtfTD );
    2986             : 
    2987             :     /* CRend binaural renderer handle */
    2988       90769 :     ivas_HRTF_CRend_binary_close( &st_ivas->hHrtfCrend );
    2989             : 
    2990             : 
    2991             :     /* Fastconv HRTF filters */
    2992       90769 :     ivas_HRTF_fastconv_binary_close( &st_ivas->hHrtfFastConv );
    2993             : 
    2994             :     /* Parametric binauralizer HRTF filters */
    2995       90769 :     ivas_HRTF_parambin_binary_close( &st_ivas->hHrtfParambin );
    2996             : 
    2997             :     /* HRTF statistics */
    2998       90769 :     ivas_HRTF_statistics_binary_close( &st_ivas->hHrtfStatistics );
    2999             : 
    3000             :     /* Config. Renderer */
    3001       90769 :     ivas_render_config_close( &( st_ivas->hRenderConfig ) );
    3002             : 
    3003             :     /* Acoustic environments */
    3004       90769 :     if ( st_ivas->pAcousticEnvironments != NULL )
    3005             :     {
    3006         203 :         free( st_ivas->pAcousticEnvironments );
    3007         203 :         st_ivas->pAcousticEnvironments = NULL;
    3008             :     }
    3009             : 
    3010             :     /* Limiter struct */
    3011       90769 :     ivas_limiter_close( &( st_ivas->hLimiter ) );
    3012             : 
    3013             :     /* Decoder configuration structure */
    3014       90769 :     if ( st_ivas->hDecoderConfig != NULL )
    3015             :     {
    3016       90769 :         free( st_ivas->hDecoderConfig );
    3017       90769 :         st_ivas->hDecoderConfig = NULL;
    3018             :     }
    3019             : 
    3020             :     /* TC buffer structure */
    3021       90769 :     ivas_dec_tc_buffer_close( &st_ivas->hTcBuffer );
    3022             : 
    3023       90769 :     if ( st_ivas->hJbmMetadata != NULL )
    3024             :     {
    3025         510 :         free( st_ivas->hJbmMetadata );
    3026         510 :         st_ivas->hJbmMetadata = NULL;
    3027             :     }
    3028             : 
    3029             :     /* floating-point output audio buffers */
    3030     1906149 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    3031             :     {
    3032     1815380 :         st_ivas->p_output_f[i] = NULL;
    3033             :     }
    3034             : 
    3035             :     /* main IVAS handle */
    3036       90769 :     free( st_ivas );
    3037             : 
    3038       90769 :     return;
    3039             : }
    3040             : 
    3041             : 
    3042             : /*-------------------------------------------------------------------*
    3043             :  * ivas_init_dec_get_num_cldfb_instances()
    3044             :  *
    3045             :  * Return number of CLDFB analysis & synthesis instances
    3046             :  *-------------------------------------------------------------------*/
    3047             : 
    3048             : /*! r: number of cldfb instances */
    3049      996416 : void ivas_init_dec_get_num_cldfb_instances(
    3050             :     Decoder_Struct *st_ivas,   /* i  : IVAS decoder structure                     */
    3051             :     int16_t *numCldfbAnalyses, /* o  : number of needed CLDFB analysis instances  */
    3052             :     int16_t *numCldfbSyntheses /* o  : number of needed CLDFB synthesis instances */
    3053             : )
    3054             : {
    3055             :     IVAS_FORMAT ivas_format;
    3056      996416 :     *numCldfbAnalyses = st_ivas->nchan_transport;
    3057      996416 :     *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    3058             : 
    3059      996416 :     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 */
    3060             : 
    3061      996416 :     switch ( st_ivas->renderer_type )
    3062             :     {
    3063      369692 :         case RENDERER_BINAURAL_PARAMETRIC:
    3064             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    3065             :         case RENDERER_STEREO_PARAMETRIC:
    3066      369692 :             if ( st_ivas->nchan_transport == 1 )
    3067             :             {
    3068       47493 :                 *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3069             :             }
    3070             : 
    3071      369692 :             if ( st_ivas->mc_mode == MC_MODE_MCMASA && st_ivas->hOutSetup.separateChannelEnabled )
    3072             :             {
    3073        3277 :                 *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3074             :             }
    3075             : 
    3076      369692 :             if ( ivas_format == SBA_ISM_FORMAT )
    3077             :             {
    3078           0 :                 if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    3079             :                 {
    3080           0 :                     *numCldfbAnalyses += st_ivas->nchan_ism;
    3081             :                 }
    3082             :             }
    3083             : 
    3084      369692 :             if ( ivas_format == MASA_ISM_FORMAT )
    3085             :             {
    3086      190825 :                 if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    3087             :                 {
    3088       79395 :                     *numCldfbAnalyses += st_ivas->nchan_ism;
    3089             :                 }
    3090      111430 :                 else if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    3091             :                 {
    3092      111430 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3093             :                 }
    3094             :             }
    3095             : 
    3096      369692 :             if ( st_ivas->hDiracDecBin[0]->useTdDecorr )
    3097             :             {
    3098       99087 :                 *numCldfbAnalyses += 2;
    3099             :             }
    3100      369692 :             break;
    3101       51603 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    3102             :         case RENDERER_MONO_DOWNMIX:
    3103       51603 :             if ( ivas_format == ISM_FORMAT || ivas_format == MASA_ISM_FORMAT || ivas_format == SBA_ISM_FORMAT )
    3104             :             {
    3105             :                 /* CLDFB not used in rendering */
    3106       46064 :                 *numCldfbAnalyses = 0;
    3107       46064 :                 *numCldfbSyntheses = 0;
    3108             :             }
    3109       51603 :             break;
    3110      233768 :         case RENDERER_DIRAC:
    3111      233768 :             if ( ivas_format == SBA_FORMAT )
    3112             :             {
    3113       17554 :                 *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3114             : 
    3115       17554 :                 if ( st_ivas->hOutSetup.is_loudspeaker_setup && st_ivas->renderer_type == RENDERER_DIRAC )
    3116             :                 {
    3117       17554 :                     *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    3118             :                 }
    3119           0 :                 else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA )
    3120             :                 {
    3121           0 :                     *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    3122             :                 }
    3123             :                 else
    3124             :                 {
    3125           0 :                     *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    3126             :                 }
    3127             :             }
    3128      233768 :             if ( ivas_format != SBA_FORMAT )
    3129             :             {
    3130      216214 :                 if ( st_ivas->nchan_transport > 2 && st_ivas->sba_planar )
    3131             :                 {
    3132           0 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3133             :                 }
    3134      216214 :                 else if ( st_ivas->nchan_transport == 1 && st_ivas->hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    3135             :                 {
    3136        8647 :                     *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3137             :                 }
    3138             :             }
    3139      233768 :             break;
    3140       10206 :         case RENDERER_MC_PARAMMC:
    3141       10206 :             if ( st_ivas->hDecoderConfig->nchan_out <= 2 )
    3142             :             {
    3143             :                 /* CLDFB not used in rendering */
    3144         676 :                 *numCldfbAnalyses = 0;
    3145         676 :                 *numCldfbSyntheses = 0;
    3146             :             }
    3147             :             else
    3148             :             {
    3149        9530 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses( st_ivas );
    3150             :             }
    3151       10206 :             break;
    3152        3405 :         case RENDERER_PARAM_ISM:
    3153             :             /* Already correct with no exception */
    3154        3405 :             break;
    3155       85269 :         case RENDERER_DISABLE:
    3156             :             /* CLDFB not used */
    3157       85269 :             *numCldfbAnalyses = 0;
    3158       85269 :             *numCldfbSyntheses = 0;
    3159       85269 :             break;
    3160      192640 :         case RENDERER_MC:
    3161             :         case RENDERER_SBA_LINEAR_DEC:
    3162             :         case RENDERER_TD_PANNING:
    3163             :         case RENDERER_BINAURAL_OBJECTS_TD:
    3164             :         case RENDERER_MCMASA_MONO_STEREO:
    3165             :         case RENDERER_BINAURAL_MIXER_CONV:
    3166             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    3167             :         case RENDERER_BINAURAL_FASTCONV:
    3168             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    3169             :         case RENDERER_OSBA_STEREO:
    3170             :         case RENDERER_OSBA_AMBI:
    3171             :         case RENDERER_OSBA_LS:
    3172      192640 :             if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
    3173             :             {
    3174      107431 :                 if ( st_ivas->sba_dirac_stereo_flag )
    3175             :                 {
    3176        3672 :                     *numCldfbAnalyses = 0;
    3177        3672 :                     *numCldfbSyntheses = 0;
    3178             :                 }
    3179             :                 else
    3180             :                 {
    3181      103759 :                     *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3182             : 
    3183      103759 :                     if ( st_ivas->hOutSetup.is_loudspeaker_setup && st_ivas->renderer_type == RENDERER_DIRAC )
    3184             :                     {
    3185           0 :                         *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    3186             :                     }
    3187      103759 :                     else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA )
    3188             :                     {
    3189       17245 :                         *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    3190             :                     }
    3191             :                     else
    3192             :                     {
    3193       86514 :                         *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    3194             :                     }
    3195             : 
    3196      103759 :                     if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    3197             :                     {
    3198        2188 :                         *numCldfbAnalyses = st_ivas->nchan_ism + st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3199             :                     }
    3200             :                 }
    3201             :             }
    3202       85209 :             else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    3203             :             {
    3204             :                 /* do nothing for ParamMC */
    3205             :             }
    3206             :             else
    3207             :             {
    3208             :                 /* CLDFB not used in rendering */
    3209       69742 :                 *numCldfbAnalyses = 0;
    3210       69742 :                 *numCldfbSyntheses = 0;
    3211             :             }
    3212      192640 :             break;
    3213       33199 :         case RENDERER_SBA_LINEAR_ENC:
    3214       33199 :             if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    3215             :             {
    3216        5072 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses( st_ivas );
    3217             :             }
    3218       28127 :             else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    3219             :             {
    3220        1513 :                 *numCldfbSyntheses = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    3221             :             }
    3222       26614 :             else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    3223             :             {
    3224        8975 :                 *numCldfbAnalyses = st_ivas->nchan_transport;
    3225        8975 :                 *numCldfbSyntheses = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    3226             :             }
    3227             :             else
    3228             :             {
    3229             :                 /* CLDFB not used in rendering */
    3230       17639 :                 *numCldfbAnalyses = 0;
    3231       17639 :                 *numCldfbSyntheses = 0;
    3232             :             }
    3233       33199 :             break;
    3234        8336 :         case RENDERER_OMASA_OBJECT_EXT:
    3235        8336 :             *numCldfbAnalyses = st_ivas->nchan_transport;
    3236        8336 :             *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    3237        8336 :             break;
    3238        8298 :         case RENDERER_OMASA_MIX_EXT:
    3239        8298 :             *numCldfbAnalyses = st_ivas->nchan_transport + 1;
    3240        8298 :             *numCldfbSyntheses = 0;
    3241        8298 :             break;
    3242           0 :         default:
    3243           0 :             assert( 0 && "Renderer not handled for CLDFB reservation." );
    3244             :     }
    3245             : 
    3246      996416 :     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 )
    3247             :     {
    3248        3637 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    3249             :         {
    3250        1594 :             *numCldfbAnalyses = max( MC_PARAMUPMIX_MAX_INPUT_CHANS, *numCldfbAnalyses );
    3251             :         }
    3252             :         else
    3253             :         {
    3254        2043 :             *numCldfbAnalyses = max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbAnalyses );
    3255             :         }
    3256        3637 :         *numCldfbSyntheses = max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbSyntheses );
    3257             :     }
    3258             : 
    3259      996416 :     return;
    3260             : }
    3261             : 
    3262             : 
    3263             : /*---------------------------------------------------------------------*
    3264             :  * doSanityChecks_IVAS()
    3265             :  *
    3266             :  * Sanity checks - verify if the decoder set-up parameters are
    3267             :  *                 not in conflict with the IVAS format
    3268             :  *---------------------------------------------------------------------*/
    3269             : 
    3270       89475 : static ivas_error doSanityChecks_IVAS(
    3271             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    3272             : )
    3273             : {
    3274             :     int32_t output_Fs;
    3275             :     AUDIO_CONFIG output_config;
    3276             : 
    3277       89475 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    3278       89475 :     output_config = st_ivas->hDecoderConfig->output_config;
    3279             : 
    3280             :     /*-----------------------------------------------------------------*
    3281             :      * Sanity checks
    3282             :      *-----------------------------------------------------------------*/
    3283             : 
    3284       89475 :     if ( output_Fs == 8000 )
    3285             :     {
    3286           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz output sampling rate is not supported in IVAS." );
    3287             :     }
    3288             : 
    3289       89475 :     assert( st_ivas->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
    3290       89475 :     assert( st_ivas->ivas_format != MONO_FORMAT && "\n Wrong IVAS format: MONO" );
    3291             : 
    3292             :     /* Verify output configuration compatible with non-diegetic panning */
    3293       89475 :     if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan && ( st_ivas->ivas_format != MONO_FORMAT ) && ( st_ivas->transport_config != IVAS_AUDIO_CONFIG_ISM1 ) )
    3294             :     {
    3295           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Error: Non-diegetic panning not supported in this IVAS format" );
    3296             :     }
    3297             : 
    3298       89475 :     if ( output_config == IVAS_AUDIO_CONFIG_INVALID ||
    3299       89475 :          output_config == IVAS_AUDIO_CONFIG_ISM1 ||
    3300       89475 :          output_config == IVAS_AUDIO_CONFIG_ISM2 ||
    3301       89475 :          output_config == IVAS_AUDIO_CONFIG_ISM3 ||
    3302       89475 :          output_config == IVAS_AUDIO_CONFIG_ISM4 ||
    3303       89475 :          output_config == IVAS_AUDIO_CONFIG_MASA1 ||
    3304             :          output_config == IVAS_AUDIO_CONFIG_MASA2 )
    3305             :     {
    3306           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration specified!" );
    3307             :     }
    3308       89475 :     if ( ( st_ivas->ivas_format == MONO_FORMAT || st_ivas->ivas_format == STEREO_FORMAT ) &&
    3309        2235 :          ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    3310             :     {
    3311           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration specified!" );
    3312             :     }
    3313             : 
    3314       89475 :     if ( ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) && output_Fs != 48000 )
    3315             :     {
    3316           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Error: Only 48kHz output sampling rate is supported for split rendering." );
    3317             :     }
    3318             : 
    3319       89475 :     if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    3320             :     {
    3321        3709 :         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 ) )
    3322             :         {
    3323           0 :             return IVAS_ERROR( IVAS_ERR_HEAD_ROTATION_NOT_SUPPORTED, "Wrong set-up: Head-rotation not supported in this configuration" );
    3324             :         }
    3325             :     }
    3326             : 
    3327       89475 :     if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    3328             :     {
    3329         217 :         if ( !( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    3330             :         {
    3331           0 :             return IVAS_ERROR( IVAS_ERR_EXT_ORIENTATION_NOT_SUPPORTED, "Wrong set-up: External orientation not supported in this configuration" );
    3332             :         }
    3333             :     }
    3334             : 
    3335       89475 :     if ( st_ivas->hDecoderConfig->Opt_dpid_on )
    3336             :     {
    3337          14 :         if ( !( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    3338             :         {
    3339           0 :             return IVAS_ERROR( IVAS_ERR_DIRECTIVITY_NOT_SUPPORTED, "Wrong set-up: Directivity is not supported in this output configuration." );
    3340             :         }
    3341             :     }
    3342             : 
    3343       89475 :     if ( st_ivas->hDecoderConfig->Opt_aeid_on )
    3344             :     {
    3345          28 :         if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    3346             :         {
    3347           0 :             return IVAS_ERROR( IVAS_ERR_ACOUSTIC_ENVIRONMENT_NOT_SUPPORTED, "Wrong set-up: Acoustic environment is not supported in this output configuration." );
    3348             :         }
    3349             :     }
    3350             : 
    3351       89475 :     if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on )
    3352             :     {
    3353          91 :         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 ) ) )
    3354             :         {
    3355           0 :             return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Wrong set-up: Object editing is not supported in this IVAS format." );
    3356             :         }
    3357             :     }
    3358             : 
    3359       89475 :     if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on && st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    3360             :     {
    3361           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." );
    3362             :     }
    3363             : 
    3364       89475 :     return IVAS_ERR_OK;
    3365             : }

Generated by: LCOV version 1.14