LCOV - code coverage report
Current view: top level - lib_dec - ivas_init_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 1087 1263 86.1 %
Date: 2025-11-01 09:50:07 Functions: 13 13 100.0 %

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

Generated by: LCOV version 1.14