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

Generated by: LCOV version 1.14