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

Generated by: LCOV version 1.14