LCOV - code coverage report
Current view: top level - lib_dec - ivas_init_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ da9cc8ead0679b4682d329fdff98cf1616159273 Lines: 1599 1819 87.9 %
Date: 2025-10-13 22:24:20 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 <assert.h>
      34             : #include <math.h>
      35             : #include <stdint.h>
      36             : #include "options.h"
      37             : #include "ivas_cnst.h"
      38             : #include "ivas_prot_rend_fx.h"
      39             : #include "rom_com.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "ivas_stat_enc.h"
      42             : #include "prot_fx.h"
      43             : #include "wmc_auto.h"
      44             : #include "ivas_prot_fx.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * Local function prototypes
      49             :  *-------------------------------------------------------------------*/
      50             : 
      51             : static ivas_error ivas_read_format( Decoder_Struct *st_ivas, Word16 *num_bits_read );
      52             : 
      53             : static ivas_error doSanityChecks_IVAS( Decoder_Struct *st_ivas );
      54             : 
      55             : static AUDIO_CONFIG ivas_set_audio_config_from_sba_order( const Word16 sba_order );
      56             : 
      57             : 
      58             : /*-------------------------------------------------------------------*
      59             :  * ivas_set_audio_config_from_sba_order()
      60             :  *
      61             :  *
      62             :  *-------------------------------------------------------------------*/
      63             : 
      64             : /*! r: audio configuration */
      65          18 : static AUDIO_CONFIG ivas_set_audio_config_from_sba_order(
      66             :     const Word16 sba_order /* i  : Ambisonic (SBA) order           */
      67             : )
      68             : {
      69             :     AUDIO_CONFIG output_config;
      70             : 
      71          18 :     output_config = IVAS_AUDIO_CONFIG_HOA3;
      72          18 :     move32();
      73             : 
      74          18 :     SWITCH( sba_order )
      75             :     {
      76           6 :         case SBA_FOA_ORDER:
      77           6 :             output_config = IVAS_AUDIO_CONFIG_FOA;
      78           6 :             move32();
      79           6 :             BREAK;
      80           4 :         case SBA_HOA2_ORDER:
      81           4 :             output_config = IVAS_AUDIO_CONFIG_HOA2;
      82           4 :             move32();
      83           4 :             BREAK;
      84           8 :         case SBA_HOA3_ORDER:
      85           8 :             output_config = IVAS_AUDIO_CONFIG_HOA3;
      86           8 :             move32();
      87           8 :             BREAK;
      88           0 :         default:
      89           0 :             output_config = IVAS_AUDIO_CONFIG_INVALID;
      90           0 :             move32();
      91           0 :             BREAK;
      92             :     }
      93             : 
      94          18 :     return output_config;
      95             : }
      96             : 
      97             : 
      98             : /*---------------------------------------------------------------------*
      99             :  * ivas_dec_get_format( )
     100             :  *
     101             :  * Read main parameters from the bitstream to set-up the decoder:
     102             :  * - IVAS format
     103             :  * - IVAS format specific signaling
     104             :  *---------------------------------------------------------------------*/
     105             : 
     106      400398 : ivas_error ivas_dec_get_format_fx(
     107             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure     */
     108             : )
     109             : {
     110             :     Word16 k, idx, num_bits_read;
     111             :     Word16 nchan_ism, element_mode_flag;
     112             :     Word16 sba_order, sba_planar, sba_analysis_order;
     113             :     Word32 ivas_total_brate;
     114             :     UWord16 *bit_stream_orig;
     115             :     AUDIO_CONFIG signaled_config;
     116             :     ivas_error error;
     117             : 
     118      400398 :     num_bits_read = 0;
     119      400398 :     move16();
     120      400398 :     element_mode_flag = 0;
     121      400398 :     move16();
     122             : 
     123      400398 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     124      400398 :     move32();
     125      400398 :     bit_stream_orig = st_ivas->bit_stream;
     126             : 
     127             :     /*-------------------------------------------------------------------*
     128             :      * Read IVAS format
     129             :      *-------------------------------------------------------------------*/
     130             : 
     131      400398 :     IF( ( error = ivas_read_format( st_ivas, &num_bits_read ) ) != IVAS_ERR_OK )
     132             :     {
     133           0 :         return error;
     134             :     }
     135             : 
     136      400398 :     test();
     137      400398 :     test();
     138      400398 :     test();
     139      400398 :     test();
     140      400398 :     test();
     141      400398 :     IF( st_ivas->ini_frame > 0 && NE_32( st_ivas->ivas_format, st_ivas->last_ivas_format ) &&
     142             :         !( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && EQ_32( st_ivas->last_ivas_format, MASA_ISM_FORMAT ) ) &&
     143             :         !( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) && EQ_32( st_ivas->last_ivas_format, MASA_FORMAT ) ) )
     144             :     {
     145             : #ifdef DEBUGGING
     146             :         fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     147             : #endif
     148           0 :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     149             :     }
     150             : 
     151             :     /*-------------------------------------------------------------------*
     152             :      * Read other signaling (ISM/MC mode, number of channels, etc.)
     153             :      *-------------------------------------------------------------------*/
     154             : #ifndef FIX_HRTF_LOAD
     155             :     k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     156             : #endif
     157             : 
     158      400398 :     IF( is_DTXrate( ivas_total_brate ) == 0 )
     159             :     {
     160             :         /*-------------------------------------------------------------------*
     161             :          * Read IVAS format related signaling:
     162             :          * - in ISM  : read number of objects
     163             :          * - in SBA  : read SBA planar flag and SBA order
     164             :          * - in MASA : read number of TC
     165             :          * - in MC   : read LS setup
     166             :          *-------------------------------------------------------------------*/
     167             : 
     168      384309 :         IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
     169             :         {
     170       58666 :             element_mode_flag = 1;
     171       58666 :             move16();
     172             :         }
     173      325643 :         ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
     174             :         {
     175             :             /* read the number of objects */
     176       91989 :             nchan_ism = 1;
     177       91989 :             move16();
     178             : #ifdef FIX_HRTF_LOAD
     179       91989 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     180       91989 :             k = sub( k, 1 );
     181      262905 :             WHILE( st_ivas->bit_stream[k] && ( nchan_ism < MAX_NUM_OBJECTS ) )
     182             : #else
     183             :             WHILE( st_ivas->bit_stream[k - 1] && nchan_ism < MAX_NUM_OBJECTS )
     184             : #endif
     185             :             {
     186      170916 :                 nchan_ism = add( nchan_ism, 1 );
     187      170916 :                 k = sub( k, 1 );
     188             :             }
     189             : 
     190       91989 :             test();
     191       91989 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
     192             :             {
     193             : #ifdef DEBUGGING
     194             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     195             : #endif
     196           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     197             :             }
     198             : 
     199       91989 :             st_ivas->nchan_ism = nchan_ism;
     200       91989 :             move16();
     201       91989 :             st_ivas->ism_mode = ivas_ism_mode_select( nchan_ism, ivas_total_brate );
     202             : 
     203       91989 :             st_ivas->nchan_transport = nchan_ism;
     204       91989 :             move16();
     205       91989 :             if ( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     206             :             {
     207       15685 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     208       15685 :                 move16();
     209             :             }
     210             :         }
     211      233654 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
     212             :         {
     213             :             /* read Ambisonic (SBA) planar flag */
     214      114185 :             sba_planar = st_ivas->bit_stream[num_bits_read];
     215      114185 :             num_bits_read = add( num_bits_read, SBA_PLANAR_BITS );
     216             : 
     217      114185 :             test();
     218      114185 :             IF( st_ivas->ini_frame > 0 && NE_16( sba_planar, st_ivas->sba_planar ) )
     219             :             {
     220             : #ifdef DEBUGGING
     221             :                 fprintf( stderr, "\nError: Changing the SBA planar/3D layout is not supported!\n" );
     222             : #endif
     223           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA planar flag signalled!" );
     224             :             }
     225             : 
     226             :             /* read Ambisonic (SBA) order */
     227      114185 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     228      114185 :             move16();
     229      114185 :             sba_order = add( sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     230             : 
     231      114185 :             test();
     232      114185 :             IF( st_ivas->ini_frame > 0 && NE_16( sba_order, st_ivas->sba_order ) )
     233             :             {
     234             : #ifdef DEBUGGING
     235             :                 fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
     236             : #endif
     237           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
     238             :             }
     239             : 
     240      114185 :             sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, sba_order );
     241      114185 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs_fx( ivas_total_brate, sba_analysis_order );
     242             :         }
     243      119469 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
     244             :         {
     245             :             /* read number of MASA transport channels */
     246       30693 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     247       30693 :             IF( st_ivas->bit_stream[k - 1] )
     248             :             {
     249       16014 :                 st_ivas->nchan_transport = 2;
     250       16014 :                 move16();
     251       16014 :                 element_mode_flag = 1;
     252       16014 :                 move16();
     253             :             }
     254             :             ELSE
     255             :             {
     256       14679 :                 st_ivas->nchan_transport = 1;
     257       14679 :                 move16();
     258             :             }
     259             : 
     260             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     261       30693 :             st_ivas->ism_mode = ISM_MODE_NONE;
     262       30693 :             move16();
     263       30693 :             nchan_ism = add( st_ivas->bit_stream[k - 3], shl( st_ivas->bit_stream[k - 2], 1 ) );
     264             : 
     265       30693 :             IF( nchan_ism > 0 )
     266             :             {
     267             :                 /* the input_ivas_format should be MASA_ISM_FORMAT, but we cannot initialize it now */
     268             :                 /* info about the number of objects:
     269             :                           '00' - MASA format at the encoder
     270             :                           '01' - MASA_ISM_FORMAT at the encoder, with 4 objects
     271             :                           '10' - MASA_ISM_FORMAT at the encoder, with 3 objects
     272             :                           '11' - MASA_ISM_FORMAT at the encoder, with 1 or 2 objects
     273             :                           reading if 1 or 2 objects is performed later
     274             :                 */
     275        1859 :                 nchan_ism = sub( 5, nchan_ism );
     276        1859 :                 test();
     277             : #ifdef FIX_HRTF_LOAD
     278        1859 :                 IF( EQ_16( st_ivas->nchan_transport, 1 ) && EQ_16( nchan_ism, 2 ) )
     279             :                 {
     280         221 :                     nchan_ism = 1;
     281         221 :                     move16();
     282             :                 }
     283             : #else
     284             :                 IF( EQ_16( st_ivas->nchan_transport, 1 ) && EQ_16( st_ivas->nchan_ism, 2 ) )
     285             :                 {
     286             :                     st_ivas->nchan_ism = 1;
     287             :                     move16();
     288             :                 }
     289             : #endif
     290             : 
     291             :                 /* for MASA_ISM_FORMAT at input the number of MASA transport channels is always 2 and the corresponding bit is not used here*/
     292        1859 :                 st_ivas->nchan_transport = 2;
     293        1859 :                 move16();
     294        1859 :                 element_mode_flag = 1;
     295        1859 :                 move16();
     296             :             }
     297             : 
     298       30693 :             test();
     299       30693 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
     300             :             {
     301             : #ifdef DEBUGGING
     302             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     303             : #endif
     304           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     305             :             }
     306             : 
     307       30693 :             st_ivas->nchan_ism = nchan_ism;
     308       30693 :             move16();
     309             :         }
     310       88776 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     311             :         {
     312        6423 :             st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */
     313        6423 :             move16();
     314             : 
     315             :             /* the number of objects are written at the end of the bitstream */
     316        6423 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     317        6423 :             nchan_ism = add( add( shl( st_ivas->bit_stream[k - 1], 1 ), st_ivas->bit_stream[k - 2] ), 1 );
     318        6423 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( ivas_total_brate, nchan_ism );
     319             : 
     320        6423 :             test();
     321        6423 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
     322             :             {
     323             : #ifdef DEBUGGING
     324             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     325             : #endif
     326           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     327             :             }
     328             : 
     329        6423 :             st_ivas->nchan_ism = nchan_ism;
     330        6423 :             move16();
     331             :         }
     332       82353 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
     333             :         {
     334             :             /* the number of objects is written at the end of the bitstream, in the SBA metadata */
     335       33885 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     336       33885 :             nchan_ism = add( add( shl( st_ivas->bit_stream[k - 1], 1 ), st_ivas->bit_stream[k - 2] ), 1 );
     337             : 
     338       33885 :             test();
     339       33885 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
     340             :             {
     341             : #ifdef DEBUGGING
     342             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     343             : #endif
     344           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     345             :             }
     346             : 
     347       33885 :             st_ivas->nchan_ism = nchan_ism;
     348       33885 :             move16();
     349             : 
     350             :             /* read Ambisonic (SBA) planar flag */
     351             :             /*sba_planar = st_ivas->bit_stream[num_bits_read];*/
     352       33885 :             num_bits_read = add( num_bits_read, SBA_PLANAR_BITS );
     353             : 
     354             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     355       33885 :             sba_order = st_ivas->bit_stream[num_bits_read + 1];
     356       33885 :             move16();
     357             : #ifdef FIX_HRTF_LOAD
     358       33885 :             sba_order = add( sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     359             : #else
     360             :             sba_order = shl( st_ivas->bit_stream[num_bits_read], 1 );
     361             : #endif
     362       33885 :             num_bits_read = add( num_bits_read, SBA_ORDER_BITS );
     363             : 
     364             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     365       33885 :             IF( LT_32( ivas_total_brate, IVAS_24k4 ) )
     366             :             {
     367        2641 :                 sba_order = st_ivas->bit_stream[num_bits_read + 1];
     368        2641 :                 move16();
     369             : #ifdef FIX_HRTF_LOAD
     370        2641 :                 sba_order = add( sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     371             : #else
     372             :                 sba_order = shl( st_ivas->bit_stream[num_bits_read], 1 );
     373             : #endif
     374        2641 :                 num_bits_read = add( num_bits_read, SBA_ORDER_BITS );
     375             :             }
     376             : 
     377       33885 :             test();
     378       33885 :             IF( st_ivas->ini_frame > 0 && NE_16( sba_order, st_ivas->sba_order ) )
     379             :             {
     380             : #ifdef DEBUGGING
     381             :                 fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
     382             : #endif
     383           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
     384             :             }
     385             : 
     386       33885 :             st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );
     387             : 
     388       33885 :             sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, sba_order );
     389       33885 :             st_ivas->nchan_transport = ivas_get_sba_num_TCs_fx( ivas_total_brate, sba_analysis_order );
     390             :         }
     391       48468 :         ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
     392             :         {
     393             :             /* read MC configuration */
     394       48468 :             idx = 0;
     395       48468 :             move16();
     396      193872 :             FOR( k = 0; k < MC_LS_SETUP_BITS; k++ )
     397             :             {
     398      145404 :                 IF( st_ivas->bit_stream[num_bits_read + k] )
     399             :                 {
     400        6570 :                     idx = add( idx, shl( 1, sub( ( MC_LS_SETUP_BITS - 1 ), k ) ) );
     401             :                 }
     402             :             }
     403       48468 :             num_bits_read = add( num_bits_read, MC_LS_SETUP_BITS );
     404             : 
     405       48468 :             signaled_config = ivas_mc_map_ls_setup_to_output_config_fx( idx );
     406             : 
     407       48468 :             test();
     408       48468 :             IF( st_ivas->ini_frame > 0 && NE_32( st_ivas->transport_config, signaled_config ) )
     409             :             {
     410             : #ifdef DEBUGGING
     411             :                 fprintf( stderr, "\nError: Switching of MC configurations is not supported!\n" );
     412             : #endif
     413           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong MC configuration signalled!" );
     414             :             }
     415             : 
     416       48468 :             st_ivas->mc_mode = ivas_mc_mode_select_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( signaled_config ), st_ivas->hDecoderConfig->ivas_total_brate );
     417       48468 :             st_ivas->transport_config = signaled_config;
     418       48468 :             move16();
     419             :         }
     420             : 
     421             :         /*-------------------------------------------------------------------*
     422             :          * Read element mode
     423             :          *-------------------------------------------------------------------*/
     424             : 
     425      384309 :         test();
     426      384309 :         IF( st_ivas->ini_frame == 0 && element_mode_flag )
     427             :         {
     428             :             /* read stereo technology info */
     429         119 :             if ( LT_32( ivas_total_brate, MIN_BRATE_MDCT_STEREO ) )
     430             :             {
     431             :                 /* 1 bit */
     432          79 :                 IF( st_ivas->bit_stream[num_bits_read] )
     433             :                 {
     434           0 :                     st_ivas->element_mode_init = add( 1, IVAS_CPE_DFT );
     435             :                 }
     436             :                 ELSE
     437             :                 {
     438          79 :                     st_ivas->element_mode_init = add( 0, IVAS_CPE_DFT );
     439             :                 }
     440             :             }
     441             :             ELSE
     442             :             {
     443          40 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     444          40 :                 move16();
     445             :             }
     446             :         }
     447             :     }
     448       16089 :     ELSE IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
     449             :     {
     450        2677 :         SWITCH( st_ivas->sid_format )
     451             :         {
     452        1432 :             case SID_DFT_STEREO:
     453        1432 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     454        1432 :                 move16();
     455        1432 :                 BREAK;
     456         410 :             case SID_MDCT_STEREO:
     457         410 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     458         410 :                 move16();
     459         410 :                 BREAK;
     460         531 :             case SID_ISM:
     461         531 :                 st_ivas->element_mode_init = IVAS_SCE;
     462         531 :                 move16();
     463         531 :                 BREAK;
     464         117 :             case SID_MASA_1TC:
     465         117 :                 st_ivas->element_mode_init = IVAS_SCE;
     466         117 :                 move16();
     467         117 :                 st_ivas->nchan_transport = 1;
     468         117 :                 move16();
     469         117 :                 BREAK;
     470          74 :             case SID_MASA_2TC:
     471          74 :                 k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     472          74 :                 IF( st_ivas->bit_stream[k - 1 - SID_FORMAT_NBITS] == 1 )
     473             :                 {
     474           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     475           0 :                     move16();
     476             :                 }
     477             :                 ELSE
     478             :                 {
     479          74 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     480          74 :                     move16();
     481             :                 }
     482          74 :                 st_ivas->nchan_transport = 2;
     483          74 :                 move16();
     484          74 :                 BREAK;
     485          67 :             case SID_SBA_1TC:
     486          67 :                 st_ivas->element_mode_init = IVAS_SCE;
     487          67 :                 move16();
     488          67 :                 BREAK;
     489          46 :             case SID_SBA_2TC:
     490          46 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     491          46 :                 move16();
     492          46 :                 BREAK;
     493             :         }
     494             : 
     495        2677 :         IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
     496             :         {
     497             :             /* read the number of objects */
     498         531 :             nchan_ism = 1;
     499         531 :             move16();
     500             : #ifdef FIX_HRTF_LOAD
     501         531 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     502         531 :             k = sub( sub( k, 1 ), SID_FORMAT_NBITS );
     503        1752 :             WHILE( st_ivas->bit_stream[k] && ( nchan_ism < MAX_NUM_OBJECTS ) )
     504             : #else
     505             :             WHILE( st_ivas->bit_stream[k - 1 - SID_FORMAT_NBITS] && nchan_ism < MAX_NUM_OBJECTS )
     506             : #endif
     507             :             {
     508        1221 :                 nchan_ism = add( nchan_ism, 1 );
     509        1221 :                 k = sub( k, 1 );
     510             :             }
     511         531 :             k = sub( k, 1 );
     512             : 
     513         531 :             test();
     514         531 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
     515             :             {
     516             : #ifdef DEBUGGING
     517             :                 fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
     518             : #endif
     519           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
     520             :             }
     521             : 
     522         531 :             st_ivas->nchan_ism = nchan_ism;
     523         531 :             move16();
     524             : 
     525             :             /* read ism_mode */
     526         531 :             st_ivas->ism_mode = ISM_MODE_DISC;
     527         531 :             move32();
     528         531 :             IF( GT_16( nchan_ism, 2 ) )
     529             :             {
     530         372 :                 k = sub( k, nchan_ism ); /* SID metadata flags */
     531         372 :                 idx = st_ivas->bit_stream[k];
     532         372 :                 move16();
     533         372 :                 st_ivas->ism_mode = (ISM_MODE) add( idx, 1 );
     534         372 :                 move32();
     535             :             }
     536             : 
     537         531 :             st_ivas->nchan_transport = nchan_ism;
     538         531 :             move16();
     539         531 :             if ( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     540             :             {
     541          94 :                 st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
     542          94 :                 move16();
     543             :             }
     544             :         }
     545             :     }
     546             : 
     547      400398 :     st_ivas->bit_stream = bit_stream_orig;
     548             : 
     549      400398 :     return IVAS_ERR_OK;
     550             : }
     551             : 
     552             : 
     553             : /*-------------------------------------------------------------------*
     554             :  * ivas_dec_setup()
     555             :  *
     556             :  * IVAS decoder setup
     557             :  *-------------------------------------------------------------------*/
     558             : 
     559      401402 : ivas_error ivas_dec_setup(
     560             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                                  */
     561             : #ifndef FIX_HRTF_LOAD
     562             :     ,
     563             :     UWord16 *nSamplesRendered, /* o  : number of samples flushed from the previous frame (JBM) */
     564             :     Word16 *data               /* o  : output synthesis signal                                 */
     565             : #endif
     566             : )
     567             : {
     568             :     Word16 k, idx, num_bits_read;
     569             :     Word16 nchan_ism, element_mode_flag;
     570             :     Decoder_State *st;
     571             :     Word32 ivas_total_brate;
     572             :     ivas_error error;
     573             :     Word16 SrcInd[MAX_NUM_TDREND_CHANNELS];
     574      401402 :     Word16 num_src = 0;
     575      401402 :     move16();
     576             : 
     577      401402 :     error = IVAS_ERR_OK;
     578      401402 :     move32();
     579      401402 :     num_bits_read = 0;
     580      401402 :     move16();
     581      401402 :     element_mode_flag = 0;
     582      401402 :     move16();
     583             : 
     584      401402 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     585      401402 :     move32();
     586             : 
     587             :     /*-------------------------------------------------------------------*
     588             :      * Read IVAS format
     589             :      *-------------------------------------------------------------------*/
     590             : 
     591      401402 :     IF( ( error = ivas_read_format( st_ivas, &num_bits_read ) ) != IVAS_ERR_OK )
     592             :     {
     593           0 :         return error;
     594             :     }
     595             : 
     596             :     /*-------------------------------------------------------------------*
     597             :      * Read other signling (ISM/MC mode, number of channels, etc.)
     598             :      *-------------------------------------------------------------------*/
     599             : 
     600      401402 :     IF( is_DTXrate( ivas_total_brate ) == 0 )
     601             :     {
     602             :         /*-------------------------------------------------------------------*
     603             :          * Read IVAS format related signaling:
     604             :          * - in ISM  : read number of objects
     605             :          * - in SBA  : read SBA planar flag and SBA order
     606             :          * - in MASA : read number of TC
     607             :          * - in MC   : read LS setup
     608             :          *-------------------------------------------------------------------*/
     609             : 
     610      384309 :         IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
     611             :         {
     612       58666 :             element_mode_flag = 1;
     613       58666 :             move16();
     614             :         }
     615      325643 :         ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
     616             :         {
     617             :             /* read the number of objects */
     618       91989 :             st_ivas->nchan_transport = 1;
     619       91989 :             move16();
     620       91989 :             nchan_ism = 1;
     621       91989 :             move16();
     622       91989 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     623       91989 :             k = sub( k, 1 );
     624      262905 :             WHILE( st_ivas->bit_stream[k] && ( nchan_ism < MAX_NUM_OBJECTS ) )
     625             :             {
     626      170916 :                 nchan_ism = add( nchan_ism, 1 );
     627      170916 :                 k = sub( k, 1 );
     628             :             }
     629             : 
     630       91989 :             st_ivas->nchan_ism = nchan_ism;
     631       91989 :             move16();
     632             : 
     633             : #ifdef FIX_HRTF_LOAD
     634       91989 :             IF( NE_32( ( error = ivas_ism_dec_config_fx( st_ivas, st_ivas->ism_mode ) ), IVAS_ERR_OK ) )
     635             : #else
     636             :             IF( NE_32( ( error = ivas_ism_dec_config_fx( st_ivas, st_ivas->ism_mode, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     637             : #endif
     638             :             {
     639           0 :                 return error;
     640             :             }
     641             :         }
     642      233654 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
     643             :         {
     644             :             /* read Ambisonic (SBA) planar flag */
     645      114185 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     646      114185 :             move16();
     647      114185 :             num_bits_read = add( num_bits_read, SBA_PLANAR_BITS );
     648             : 
     649             :             /* read Ambisonic (SBA) order */
     650      114185 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     651      114185 :             move16();
     652      114185 :             st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     653      114185 :             move16();
     654      114185 :             num_bits_read = add( num_bits_read, SBA_ORDER_BITS );
     655             : 
     656      114185 :             test();
     657      114185 :             test();
     658      114185 :             IF( st_ivas->ini_frame > 0 && NE_32( ivas_total_brate, st_ivas->last_active_ivas_total_brate ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
     659             :             {
     660             : #ifdef FIX_HRTF_LOAD
     661        1020 :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
     662             : #else
     663             :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     664             : #endif
     665             :                 {
     666           0 :                     return error;
     667             :                 }
     668             :             }
     669             :             ELSE
     670             :             {
     671             :                 /* set Ambisonic (SBA) order used for analysis and coding */
     672      113165 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->sba_order );
     673      113165 :                 move16();
     674      113165 :                 ivas_sba_config_fx( 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 );
     675             :             }
     676             :         }
     677      119469 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
     678             :         {
     679             :             /* read number of MASA transport channels */
     680       30693 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     681       30693 :             IF( st_ivas->bit_stream[k - 1] )
     682             :             {
     683       16014 :                 st_ivas->nchan_transport = 2;
     684       16014 :                 move16();
     685       16014 :                 element_mode_flag = 1;
     686       16014 :                 move16();
     687             :             }
     688             :             ELSE
     689             :             {
     690       14679 :                 st_ivas->nchan_transport = 1;
     691       14679 :                 move16();
     692             :             }
     693             :             /* this should be non-zero if original input format was MASA_ISM_FORMAT */
     694       30693 :             st_ivas->nchan_ism = add( st_ivas->bit_stream[sub( k, 3 )], shl( st_ivas->bit_stream[sub( k, 2 )], 1 ) );
     695             : 
     696       30693 :             IF( GT_16( st_ivas->nchan_ism, 0 ) )
     697             :             {
     698             :                 /* the input_ivas_format should be MASA_ISM_FORMAT, but we cannot initialize it now */
     699             :                 /* info about the number of objects:
     700             :                           '00' - MASA format at the encoder
     701             :                           '01' - MASA_ISM_FORMAT at the encoder, with 4 objects
     702             :                           '10' - MASA_ISM_FORMAT at the encoder, with 3 objects
     703             :                           '11' - MASA_ISM_FORMAT at the encoder, with 1 or 2 objects
     704             :                           reading if 1 or 2 objects is performed later
     705             :                 */
     706        1859 :                 st_ivas->nchan_ism = sub( 5, st_ivas->nchan_ism );
     707             : 
     708        1859 :                 test();
     709        1859 :                 IF( EQ_16( st_ivas->nchan_transport, 1 ) && EQ_16( st_ivas->nchan_ism, 2 ) )
     710             :                 {
     711         221 :                     st_ivas->nchan_ism = 1;
     712         221 :                     move16();
     713             :                 }
     714             : 
     715             :                 /* for MASA_ISM_FORMAT at input the number of MASA transport channels is always 2 and the corresponding bit is not used here*/
     716        1859 :                 st_ivas->nchan_transport = 2;
     717        1859 :                 element_mode_flag = 1;
     718        1859 :                 move16();
     719        1859 :                 move16();
     720             :             }
     721             : 
     722       30693 :             IF( st_ivas->ini_frame > 0 )
     723             :             {
     724             :                 /* reconfigure in case a change of operation mode is detected */
     725       30611 :                 test();
     726       30611 :                 test();
     727       30611 :                 IF( ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) || ( st_ivas->ini_active_frame == 0 ) )
     728             :                 {
     729        1723 :                     IF( EQ_32( st_ivas->last_ivas_format, MASA_FORMAT ) )
     730             :                     {
     731        1310 :                         test();
     732        1310 :                         test();
     733        1310 :                         test();
     734        1310 :                         IF( ( st_ivas->ini_active_frame == 0 ) && NE_32( ivas_total_brate, FRAME_NO_DATA ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && EQ_16( st_ivas->nCPE, 1 ) )
     735             :                         {
     736           0 :                             st_ivas->hCPE[0]->nchan_out = 1;
     737           0 :                             move16();
     738             :                         }
     739             :                         ELSE
     740             :                         {
     741             : #ifdef FIX_HRTF_LOAD
     742        1310 :                             IF( NE_32( ( error = ivas_masa_dec_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
     743             : #else
     744             :                             IF( NE_32( ( error = ivas_masa_dec_reconfigure_fx( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     745             : #endif
     746             :                             {
     747           0 :                                 return error;
     748             :                             }
     749             :                         }
     750             :                     }
     751             :                     ELSE
     752             :                     {
     753             : #ifdef FIX_HRTF_LOAD
     754         413 :                         IF( NE_32( ( error = ivas_omasa_dec_config_fx( st_ivas, &num_src, SrcInd ) ), IVAS_ERR_OK ) )
     755             : #else
     756             :                         IF( NE_32( ( error = ivas_omasa_dec_config_fx( st_ivas, nSamplesRendered, &num_src, SrcInd, data ) ), IVAS_ERR_OK ) )
     757             : #endif
     758             :                         {
     759           0 :                             return error;
     760             :                         }
     761             :                     }
     762             :                 }
     763             :             }
     764             :         }
     765       88776 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     766             :         {
     767        6423 :             st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */
     768        6423 :             move16();
     769             : 
     770             :             /* for the DISC mode the number of objects are written at the end of the bitstream, in the MASA metadata */
     771        6423 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     772        6423 :             st_ivas->nchan_ism = add( add( shl( st_ivas->bit_stream[k - 1], 1 ), st_ivas->bit_stream[k - 2] ), 1 );
     773        6423 :             move16();
     774        6423 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( ivas_total_brate, st_ivas->nchan_ism );
     775        6423 :             move16();
     776        6423 :             IF( st_ivas->ini_frame > 0 )
     777             :             {
     778             :                 /* reconfigure in case a change of operation mode is detected */
     779        6390 :                 test();
     780        6390 :                 test();
     781        6390 :                 IF( ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) || ( st_ivas->ini_active_frame == 0 ) )
     782             :                 {
     783             : #ifdef FIX_HRTF_LOAD
     784        1121 :                     IF( NE_32( ( error = ivas_omasa_dec_config_fx( st_ivas, &num_src, SrcInd ) ), IVAS_ERR_OK ) )
     785             : #else
     786             :                     IF( NE_32( ( error = ivas_omasa_dec_config_fx( st_ivas, nSamplesRendered, &num_src, SrcInd, data ) ), IVAS_ERR_OK ) )
     787             : #endif
     788             :                     {
     789           0 :                         return error;
     790             :                     }
     791             :                 }
     792             :             }
     793             :         }
     794       82353 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
     795             :         {
     796             :             /* the number of objects is written at the end of the bitstream, in the SBA metadata */
     797       33885 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     798       33885 :             st_ivas->nchan_ism = add( add( shl( st_ivas->bit_stream[k - 1], 1 ), st_ivas->bit_stream[k - 2] ), 1 );
     799       33885 :             move16();
     800             : 
     801             :             /* read Ambisonic (SBA) planar flag */
     802       33885 :             st_ivas->sba_planar = st_ivas->bit_stream[num_bits_read];
     803       33885 :             move16();
     804       33885 :             num_bits_read = add( num_bits_read, SBA_PLANAR_BITS );
     805             : 
     806             :             /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
     807       33885 :             st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     808       33885 :             move16();
     809       33885 :             st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     810       33885 :             move16();
     811       33885 :             num_bits_read = add( num_bits_read, SBA_ORDER_BITS );
     812             : 
     813             :             /* read Ambisonic (SBA) order */
     814             :             /* read the real Ambisonic order when the above bits are used to signal OSBA format */
     815       33885 :             IF( LT_32( ivas_total_brate, IVAS_24k4 ) )
     816             :             {
     817        2641 :                 st_ivas->sba_order = st_ivas->bit_stream[num_bits_read + 1];
     818        2641 :                 move16();
     819        2641 :                 st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[num_bits_read], 1 ) );
     820        2641 :                 num_bits_read = add( num_bits_read, SBA_ORDER_BITS );
     821             :             }
     822             : 
     823       33885 :             test();
     824       33885 :             IF( st_ivas->ini_frame > 0 && NE_32( ivas_total_brate, st_ivas->last_active_ivas_total_brate ) )
     825             :             {
     826             : #ifdef FIX_HRTF_LOAD
     827         562 :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
     828             : #else
     829             :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     830             : #endif
     831             :                 {
     832           0 :                     return error;
     833             :                 }
     834             :             }
     835             :             ELSE
     836             :             {
     837             :                 /* set Ambisonic (SBA) order used for analysis and coding */
     838       33323 :                 st_ivas->sba_analysis_order = ivas_sba_get_analysis_order_fx( ivas_total_brate, st_ivas->sba_order );
     839       33323 :                 move16();
     840             : 
     841       33323 :                 ivas_sba_config_fx( 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 );
     842             : 
     843             :                 /*correct number of CPEs for discrete ISM coding*/
     844       33323 :                 test();
     845       33323 :                 IF( st_ivas->ini_frame > 0 && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
     846             :                 {
     847             :                     Word16 n;
     848             : 
     849       19397 :                     n = add( st_ivas->nchan_transport, st_ivas->nchan_ism );
     850       19397 :                     st_ivas->nCPE = shr_r( n, 1 );
     851             :                 }
     852             :             }
     853             : 
     854       33885 :             IF( EQ_16( ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism ), ISM_SBA_MODE_DISC ) )
     855             :             {
     856       19633 :                 st_ivas->ism_mode = ISM_SBA_MODE_DISC;
     857       19633 :                 move32();
     858             :             }
     859             :             ELSE
     860             :             {
     861       14252 :                 st_ivas->ism_mode = ISM_MODE_NONE;
     862       14252 :                 move32();
     863             :             }
     864             :         }
     865       48468 :         ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
     866             :         {
     867             :             /* read MC configuration */
     868       48468 :             idx = 0;
     869       48468 :             move16();
     870      193872 :             FOR( k = 0; k < MC_LS_SETUP_BITS; k++ )
     871             :             {
     872      145404 :                 IF( st_ivas->bit_stream[num_bits_read + k] )
     873             :                 {
     874        6570 :                     idx = add( idx, shl( 1, sub( ( MC_LS_SETUP_BITS - 1 ), k ) ) );
     875             :                 }
     876             :             }
     877       48468 :             num_bits_read = add( num_bits_read, MC_LS_SETUP_BITS );
     878             : 
     879             :             /* select MC format mode; reconfigure the MC format decoder */
     880             : #ifdef FIX_HRTF_LOAD
     881       48468 :             IF( NE_32( ( error = ivas_mc_dec_config_fx( st_ivas, idx ) ), IVAS_ERR_OK ) )
     882             : #else
     883             :             IF( NE_32( ( error = ivas_mc_dec_config_fx( st_ivas, idx, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     884             : #endif
     885             :             {
     886           0 :                 return error;
     887             :             }
     888             :         }
     889             : 
     890             :         /*-------------------------------------------------------------------*
     891             :          * Read element mode
     892             :          *-------------------------------------------------------------------*/
     893             : 
     894      384309 :         IF( st_ivas->ini_frame == 0 && element_mode_flag )
     895             :         {
     896             :             /* read stereo technology info */
     897         119 :             IF( LT_32( ivas_total_brate, MIN_BRATE_MDCT_STEREO ) )
     898             :             {
     899             :                 /* 1 bit */
     900          79 :                 IF( st_ivas->bit_stream[num_bits_read] )
     901             :                 {
     902           0 :                     st_ivas->element_mode_init = add( 1, IVAS_CPE_DFT );
     903           0 :                     move16();
     904             :                 }
     905             :                 ELSE
     906             :                 {
     907          79 :                     st_ivas->element_mode_init = add( 0, IVAS_CPE_DFT );
     908          79 :                     move16();
     909             :                 }
     910             :             }
     911             :             ELSE
     912             :             {
     913          40 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     914          40 :                 move16();
     915             :             }
     916             :         }
     917             :     }
     918       17093 :     ELSE IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
     919             :     {
     920        2677 :         SWITCH( st_ivas->sid_format )
     921             :         {
     922        1432 :             case SID_DFT_STEREO:
     923        1432 :                 st_ivas->element_mode_init = IVAS_CPE_DFT;
     924        1432 :                 move16();
     925        1432 :                 BREAK;
     926         410 :             case SID_MDCT_STEREO:
     927         410 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     928         410 :                 move16();
     929         410 :                 BREAK;
     930         531 :             case SID_ISM:
     931         531 :                 st_ivas->element_mode_init = IVAS_SCE;
     932         531 :                 move16();
     933         531 :                 BREAK;
     934         117 :             case SID_MASA_1TC:
     935         117 :                 st_ivas->element_mode_init = IVAS_SCE;
     936         117 :                 move16();
     937         117 :                 st_ivas->nchan_transport = 1;
     938         117 :                 move16();
     939         117 :                 BREAK;
     940          74 :             case SID_MASA_2TC:
     941          74 :                 k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     942          74 :                 IF( EQ_16( st_ivas->bit_stream[( k - 1 ) - SID_FORMAT_NBITS], 1 ) )
     943             :                 {
     944           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
     945           0 :                     move16();
     946             :                 }
     947             :                 ELSE
     948             :                 {
     949          74 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
     950          74 :                     move16();
     951             :                 }
     952          74 :                 st_ivas->nchan_transport = 2;
     953          74 :                 move16();
     954          74 :                 BREAK;
     955          67 :             case SID_SBA_1TC:
     956          67 :                 st_ivas->element_mode_init = IVAS_SCE;
     957          67 :                 move16();
     958          67 :                 BREAK;
     959          46 :             case SID_SBA_2TC:
     960          46 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
     961          46 :                 move16();
     962          46 :                 BREAK;
     963             :         }
     964             : 
     965        2677 :         test();
     966        2677 :         IF( st_ivas->ini_frame > 0 && EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
     967             :         {
     968             :             Word16 nchan_transport_old, nchan_transport;
     969         113 :             nchan_transport_old = st_ivas->nchan_transport;
     970         113 :             move16();
     971         113 :             IF( ( EQ_16( st_ivas->sid_format, SID_SBA_2TC ) ) )
     972             :             {
     973          46 :                 nchan_transport = 2;
     974             :             }
     975             :             ELSE
     976             :             {
     977          67 :                 nchan_transport = 1;
     978             :             }
     979         113 :             move16();
     980             : 
     981         113 :             IF( NE_16( nchan_transport_old, nchan_transport ) )
     982             :             {
     983             :                 /*Setting the default bitrate for the reconfig function*/
     984           0 :                 IF( EQ_16( st_ivas->sid_format, SID_SBA_2TC ) )
     985             :                 {
     986           0 :                     st_ivas->hDecoderConfig->ivas_total_brate = IVAS_48k;
     987           0 :                     move16();
     988             :                 }
     989             :                 ELSE
     990             :                 {
     991           0 :                     st_ivas->hDecoderConfig->ivas_total_brate = IVAS_24k4;
     992           0 :                     move16();
     993             :                 }
     994             : 
     995             : #ifdef FIX_HRTF_LOAD
     996           0 :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
     997             : #else
     998             :                 IF( NE_32( ( error = ivas_sba_dec_reconfigure_fx( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
     999             : #endif
    1000             :                 {
    1001           0 :                     return error;
    1002             :                 }
    1003             : 
    1004           0 :                 st_ivas->last_active_ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1005           0 :                 move32();
    1006           0 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
    1007           0 :                 move32();
    1008             :             }
    1009             :         }
    1010             : 
    1011        2677 :         IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
    1012             :         {
    1013         531 :             ISM_MODE last_ism_mode = st_ivas->ism_mode;
    1014         531 :             move32();
    1015             :             /* read the number of objects */
    1016         531 :             st_ivas->nchan_transport = 1;
    1017         531 :             move16();
    1018         531 :             nchan_ism = 1;
    1019         531 :             move16();
    1020         531 :             k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
    1021         531 :             k = sub( sub( k, 1 ), SID_FORMAT_NBITS );
    1022        1752 :             WHILE( st_ivas->bit_stream[k] && ( nchan_ism < MAX_NUM_OBJECTS ) )
    1023             :             {
    1024        1221 :                 nchan_ism = add( nchan_ism, 1 );
    1025        1221 :                 k = sub( k, 1 );
    1026             :             }
    1027         531 :             k = sub( k, 1 );
    1028             : 
    1029         531 :             test();
    1030         531 :             IF( st_ivas->ini_frame > 0 && NE_16( nchan_ism, st_ivas->nchan_ism ) )
    1031             :             {
    1032           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong number of objects signalled!" );
    1033             :             }
    1034             : 
    1035         531 :             st_ivas->nchan_ism = nchan_ism;
    1036         531 :             move16();
    1037             : 
    1038             :             /* read ism_mode */
    1039         531 :             st_ivas->ism_mode = ISM_MODE_DISC;
    1040         531 :             move32();
    1041         531 :             IF( GT_16( nchan_ism, 2 ) )
    1042             :             {
    1043         372 :                 k = sub( k, nchan_ism ); /* SID metadata flags */
    1044         372 :                 idx = st_ivas->bit_stream[k];
    1045         372 :                 move16();
    1046         372 :                 st_ivas->ism_mode = (ISM_MODE) add( idx, 1 );
    1047         372 :                 move32();
    1048             :             }
    1049             : 
    1050         531 :             if ( st_ivas->ini_frame == 0 )
    1051             :             {
    1052           0 :                 last_ism_mode = st_ivas->ism_mode;
    1053           0 :                 move32();
    1054             :             }
    1055             : 
    1056             : #ifdef FIX_HRTF_LOAD
    1057         531 :             IF( NE_32( ( error = ivas_ism_dec_config_fx( st_ivas, last_ism_mode ) ), IVAS_ERR_OK ) )
    1058             : #else
    1059             :             IF( NE_32( ( error = ivas_ism_dec_config_fx( st_ivas, last_ism_mode, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
    1060             : #endif
    1061             :             {
    1062           0 :                 return error;
    1063             :             }
    1064             :         }
    1065             :     }
    1066             : 
    1067             :     /*-------------------------------------------------------------------*
    1068             :      * Initialize decoder in the first good frame based on IVAS format
    1069             :      * and number of transport channels
    1070             :      *-------------------------------------------------------------------*/
    1071      401402 :     test();
    1072      401402 :     IF( st_ivas->ini_frame == 0 && NE_32( st_ivas->ivas_format, UNDEFINED_FORMAT ) )
    1073             :     {
    1074         611 :         IF( NE_32( ( error = doSanityChecks_IVAS( st_ivas ) ), IVAS_ERR_OK ) )
    1075             :         {
    1076           0 :             return IVAS_ERROR( error, "Sanity checks failed" );
    1077             :         }
    1078             : 
    1079         611 :         IF( NE_32( ( error = ivas_init_decoder_fx( st_ivas ) ), IVAS_ERR_OK ) )
    1080             :         {
    1081           0 :             return error;
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*----------------------------------------------------------------*
    1086             :      * Reset bitstream pointers
    1087             :      *----------------------------------------------------------------*/
    1088             : 
    1089      401402 :     ivas_set_bitstream_pointers( st_ivas );
    1090             : 
    1091      401402 :     reset_elements( st_ivas );
    1092             : 
    1093             :     /* update bitstream buffer pointer -> take into account already read bits */
    1094      401402 :     test();
    1095      401402 :     IF( ( st_ivas->nSCE > 0 ) || ( st_ivas->nCPE > 0 ) )
    1096             :     {
    1097      401402 :         IF( st_ivas->nSCE > 0 )
    1098             :         {
    1099      164429 :             st = st_ivas->hSCE[0]->hCoreCoder[0];
    1100             :         }
    1101             :         ELSE
    1102             :         {
    1103      236973 :             st = st_ivas->hCPE[0]->hCoreCoder[0];
    1104             :         }
    1105      401402 :         st->next_bit_pos = num_bits_read;
    1106      401402 :         move16();
    1107      401402 :         st->total_brate = ACELP_8k00; /* only temporary initialization - this is needed for get_next_indice() in the frame following NO_DATA frame */
    1108      401402 :         move32();
    1109             :     }
    1110             : 
    1111      401402 :     return error;
    1112             : }
    1113             : 
    1114             : 
    1115             : /*-------------------------------------------------------------------*
    1116             :  * ivas_read_format()
    1117             :  *
    1118             :  * Read IVAS format signaling
    1119             :  *-------------------------------------------------------------------*/
    1120             : 
    1121      801800 : static ivas_error ivas_read_format(
    1122             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure                                 */
    1123             :     Word16 *num_bits_read    /* o  : number of IVAS signaling bits read from the bitstream */
    1124             : )
    1125             : {
    1126             :     Word16 k, idx;
    1127             :     Word32 ivas_total_brate;
    1128             :     ivas_error error;
    1129             : 
    1130      801800 :     error = IVAS_ERR_OK;
    1131      801800 :     move32();
    1132             : 
    1133      801800 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1134      801800 :     move32();
    1135             : 
    1136      801800 :     *num_bits_read = 0;
    1137      801800 :     move16();
    1138      801800 :     test();
    1139      801800 :     test();
    1140      801800 :     IF( !st_ivas->bfi && is_DTXrate( ivas_total_brate ) == 0 )
    1141             :     {
    1142             :         /* read IVAS format */
    1143      768618 :         k = 0;
    1144      768618 :         move16();
    1145      768618 :         if ( st_ivas->bit_stream[*num_bits_read] )
    1146             :         {
    1147      554350 :             k = 1;
    1148      554350 :             move16();
    1149             :         }
    1150      768618 :         k = shl( k, 1 );
    1151      768618 :         ( *num_bits_read ) = add( ( *num_bits_read ), 1 );
    1152      768618 :         move16();
    1153      768618 :         if ( st_ivas->bit_stream[*num_bits_read] )
    1154             :         {
    1155      391974 :             k = add( k, 1 );
    1156             :         }
    1157      768618 :         ( *num_bits_read ) = add( ( *num_bits_read ), 1 );
    1158      768618 :         move16();
    1159      768618 :         SWITCH( k )
    1160             :         {
    1161      117332 :             case 0:
    1162      117332 :                 st_ivas->ivas_format = STEREO_FORMAT;
    1163      117332 :                 move32();
    1164      117332 :                 BREAK;
    1165       96936 :             case 1:
    1166       96936 :                 st_ivas->ivas_format = MC_FORMAT;
    1167       96936 :                 move32();
    1168       96936 :                 BREAK;
    1169      259312 :             case 2:
    1170      259312 :                 st_ivas->ivas_format = ISM_FORMAT;
    1171      259312 :                 move32();
    1172             : 
    1173      259312 :                 IF( GE_32( ivas_total_brate, IVAS_24k4 ) )
    1174             :                 {
    1175      242630 :                     IF( st_ivas->bit_stream[*num_bits_read] )
    1176             :                     {
    1177       75334 :                         ( *num_bits_read ) = add( ( *num_bits_read ), 1 );
    1178       75334 :                         move16();
    1179       75334 :                         IF( st_ivas->bit_stream[*num_bits_read] )
    1180             :                         {
    1181       62488 :                             st_ivas->ivas_format = SBA_ISM_FORMAT;
    1182       62488 :                             move32();
    1183             :                         }
    1184             :                         ELSE
    1185             :                         {
    1186       12846 :                             st_ivas->ivas_format = MASA_ISM_FORMAT;
    1187       12846 :                             move32();
    1188             :                         }
    1189             :                     }
    1190             : 
    1191      242630 :                     ( *num_bits_read ) = add( ( *num_bits_read ), 1 );
    1192      242630 :                     move16();
    1193             :                 }
    1194      259312 :                 BREAK;
    1195      295038 :             case 3:
    1196      295038 :                 IF( st_ivas->bit_stream[*num_bits_read] )
    1197             :                 {
    1198       61386 :                     st_ivas->ivas_format = MASA_FORMAT;
    1199       61386 :                     move32();
    1200             :                 }
    1201             :                 ELSE
    1202             :                 {
    1203      233652 :                     st_ivas->ivas_format = SBA_FORMAT;
    1204      233652 :                     move32();
    1205             :                     /* read Ambisonic (SBA) planar flag */
    1206      233652 :                     st_ivas->sba_planar = st_ivas->bit_stream[( *num_bits_read ) + 1];
    1207      233652 :                     move16();
    1208             : 
    1209             :                     /* read Ambisonic (SBA) order */
    1210      233652 :                     st_ivas->sba_order = st_ivas->bit_stream[( *num_bits_read ) + 2 + SBA_PLANAR_BITS];
    1211      233652 :                     move16();
    1212      233652 :                     st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[( *num_bits_read ) + 1 + SBA_PLANAR_BITS], 1 ) );
    1213      233652 :                     move16();
    1214      233652 :                     IF( st_ivas->sba_order == 0 )
    1215             :                     {
    1216        5282 :                         st_ivas->ivas_format = SBA_ISM_FORMAT;
    1217        5282 :                         move32();
    1218             : 
    1219             : #ifdef FIX_CREND_SIMPLIFY_CODE
    1220             :                         /* read the real Ambisonic order when the above bits are used to signal OSBA format */
    1221        5282 :                         IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
    1222             :                         {
    1223        5282 :                             st_ivas->sba_order = st_ivas->bit_stream[add( *num_bits_read, 2 + SBA_PLANAR_BITS + SBA_ORDER_BITS )];
    1224        5282 :                             st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[add( *num_bits_read, 1 + SBA_PLANAR_BITS + SBA_ORDER_BITS )], 1 ) );
    1225             :                         }
    1226             : #endif
    1227             :                     }
    1228             :                 }
    1229      295038 :                 ( *num_bits_read ) = add( ( *num_bits_read ), 1 );
    1230      295038 :                 move16();
    1231             : 
    1232      295038 :                 BREAK;
    1233             :         }
    1234      768618 :     }
    1235       33182 :     ELSE IF( ( st_ivas->bfi == 0 ) && EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
    1236             :     {
    1237             :         /* read IVAS format in SID frame */
    1238        5354 :         idx = 0;
    1239        5354 :         move16();
    1240       21416 :         FOR( k = 0; k < SID_FORMAT_NBITS; k++ )
    1241             :         {
    1242       16062 :             idx += st_ivas->bit_stream[k] << ( SID_FORMAT_NBITS - 1 - k );
    1243             :         }
    1244             : 
    1245        5354 :         ( *num_bits_read ) = add( *num_bits_read, SID_FORMAT_NBITS );
    1246        5354 :         move16();
    1247        5354 :         st_ivas->sid_format = idx;
    1248        5354 :         move16();
    1249             : 
    1250        5354 :         SWITCH( idx )
    1251             :         {
    1252        3684 :             case SID_DFT_STEREO:
    1253             :             case SID_MDCT_STEREO:
    1254        3684 :                 st_ivas->ivas_format = STEREO_FORMAT;
    1255        3684 :                 move32();
    1256        3684 :                 BREAK;
    1257        1062 :             case SID_ISM:
    1258        1062 :                 st_ivas->ivas_format = ISM_FORMAT;
    1259        1062 :                 move32();
    1260        1062 :                 BREAK;
    1261         134 :             case SID_SBA_1TC:
    1262         134 :                 st_ivas->ivas_format = SBA_FORMAT;
    1263         134 :                 move32();
    1264         134 :                 st_ivas->element_mode_init = IVAS_SCE;
    1265         134 :                 move16();
    1266         134 :                 BREAK;
    1267          92 :             case SID_SBA_2TC:
    1268          92 :                 st_ivas->ivas_format = SBA_FORMAT;
    1269          92 :                 move32();
    1270          92 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1271          92 :                 move16();
    1272          92 :                 BREAK;
    1273         234 :             case SID_MASA_1TC:
    1274         234 :                 st_ivas->ivas_format = MASA_FORMAT;
    1275         234 :                 move32();
    1276         234 :                 st_ivas->element_mode_init = IVAS_SCE;
    1277         234 :                 move16();
    1278         234 :                 BREAK;
    1279         148 :             case SID_MASA_2TC:
    1280         148 :                 st_ivas->ivas_format = MASA_FORMAT;
    1281         148 :                 move32();
    1282         148 :                 k = extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
    1283         148 :                 IF( EQ_32( st_ivas->bit_stream[k - 1], 1 ) )
    1284             :                 {
    1285           0 :                     st_ivas->element_mode_init = IVAS_CPE_MDCT;
    1286           0 :                     move16();
    1287             :                 }
    1288             :                 ELSE
    1289             :                 {
    1290         148 :                     st_ivas->element_mode_init = IVAS_CPE_DFT;
    1291         148 :                     move16();
    1292             :                 }
    1293         148 :                 BREAK;
    1294           0 :             default:
    1295           0 :                 return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Invalid value %c found in SID format field.", st_ivas->sid_format );
    1296             :         }
    1297             : 
    1298        5354 :         IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
    1299             :         {
    1300             :             /* read Ambisonic (SBA) planar flag */
    1301         226 :             st_ivas->sba_planar = st_ivas->bit_stream[*num_bits_read];
    1302         226 :             move16();
    1303         226 :             *num_bits_read = add( *num_bits_read, SBA_PLANAR_BITS );
    1304         226 :             move16();
    1305             : 
    1306             :             /* read Ambisonic (SBA) order */
    1307         226 :             st_ivas->sba_order = st_ivas->bit_stream[*num_bits_read + 1];
    1308         226 :             move16();
    1309         226 :             st_ivas->sba_order = add( st_ivas->sba_order, shl( st_ivas->bit_stream[*num_bits_read], 1 ) );
    1310         226 :             move16();
    1311         226 :             *num_bits_read = add( *num_bits_read, SBA_ORDER_BITS );
    1312         226 :             move16();
    1313             : 
    1314         226 :             if ( st_ivas->sba_analysis_order == 0 )
    1315             :             {
    1316           0 :                 st_ivas->sba_analysis_order = SBA_FOA_ORDER;
    1317           0 :                 move16();
    1318             :             }
    1319             :         }
    1320             : 
    1321             :         /* reset bitstream handle to avoid BER detection after reading the 2400 kbps for ch0 */
    1322        5354 :         st_ivas->bit_stream = st_ivas->bit_stream + ( *num_bits_read );
    1323        5354 :         ( *num_bits_read ) = 0;
    1324        5354 :         move16();
    1325             :     }
    1326             :     ELSE
    1327             :     {
    1328             :         /* In SID/NO_DATA frames, use the previous frame IVAS format */
    1329             :     }
    1330             : 
    1331      801800 :     return error;
    1332             : }
    1333             : 
    1334             : 
    1335             : /*-------------------------------------------------------------------*
    1336             :  * getNumChanSynthesis()
    1337             :  *
    1338             :  * get number of output channels used for synthesis/decoding
    1339             :  * (often different from number of output channels!)
    1340             :  *-------------------------------------------------------------------*/
    1341             : 
    1342             : /*! r: number of channels to be synthesised */
    1343             : 
    1344      257532 : Word16 getNumChanSynthesis(
    1345             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure  */
    1346             : )
    1347             : {
    1348             :     Word16 n;
    1349             : 
    1350      257532 :     n = add( st_ivas->nSCE, imult1616( CPE_CHANNELS, st_ivas->nCPE ) );
    1351      257532 :     test();
    1352      257532 :     test();
    1353      257532 :     IF( st_ivas->sba_dirac_stereo_flag )
    1354             :     {
    1355       13026 :         n = CPE_CHANNELS;
    1356       13026 :         move16();
    1357             :     }
    1358      244506 :     ELSE IF( ( st_ivas->hMCT != NULL || EQ_32( st_ivas->ivas_format, SBA_FORMAT ) ) && NE_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    1359             :     {
    1360        3825 :         n = st_ivas->nchan_transport;
    1361        3825 :         move16();
    1362             :     }
    1363      240681 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    1364             :     {
    1365      176707 :         IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    1366             :         {
    1367      143717 :             n = add( st_ivas->nchan_transport, st_ivas->nchan_ism );
    1368             :         }
    1369             :         ELSE
    1370             :         {
    1371       32990 :             n = st_ivas->nchan_transport;
    1372       32990 :             move16();
    1373             :         }
    1374             :     }
    1375             : 
    1376      257532 :     return n;
    1377             : }
    1378             : 
    1379             : /*-------------------------------------------------------------------*
    1380             :  * copy_decoder_config()
    1381             :  *
    1382             :  * Copy IVAS configuration structure to the CoreCoder state structure
    1383             :  *-------------------------------------------------------------------*/
    1384             : 
    1385        8164 : void copy_decoder_config(
    1386             :     Decoder_Struct *st_ivas, /* i  : IVAS decoder structure      */
    1387             :     Decoder_State *st        /* o  : decoder state structure     */
    1388             : )
    1389             : {
    1390        8164 :     st->output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1391        8164 :     move32();
    1392        8164 :     st->Opt_AMR_WB = st_ivas->hDecoderConfig->Opt_AMR_WB;
    1393        8164 :     move16();
    1394        8164 :     st->codec_mode = st_ivas->codec_mode;
    1395        8164 :     move16();
    1396        8164 :     st->ini_frame = st_ivas->ini_frame;
    1397        8164 :     move16();
    1398             : 
    1399        8164 :     st->bfi = st_ivas->bfi;
    1400        8164 :     move16();
    1401             : 
    1402        8164 :     st->writeFECoffset = st_ivas->writeFECoffset;
    1403        8164 :     move16();
    1404             : 
    1405        8164 :     st->element_mode = st_ivas->element_mode_init;
    1406        8164 :     move16();
    1407             : 
    1408        8164 :     return;
    1409             : }
    1410             : 
    1411             : 
    1412             : /*-------------------------------------------------------------------*
    1413             :  * ivas_init_decoder_front()
    1414             :  *
    1415             :  * Set decoder parameters to initial values
    1416             :  *-------------------------------------------------------------------*/
    1417             : 
    1418         614 : ivas_error ivas_init_decoder_front(
    1419             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
    1420             : )
    1421             : {
    1422             :     ivas_error error;
    1423             : 
    1424         614 :     error = IVAS_ERR_OK;
    1425         614 :     move32();
    1426             :     /*-----------------------------------------------------------------*
    1427             :      * Resets
    1428             :      *-----------------------------------------------------------------*/
    1429             : 
    1430         614 :     st_ivas->nSCE = 0;
    1431         614 :     move16();
    1432         614 :     st_ivas->nCPE = 0;
    1433         614 :     move16();
    1434         614 :     st_ivas->nchan_ism = 0;
    1435         614 :     move16();
    1436         614 :     st_ivas->nCPE_old = 0;
    1437         614 :     move16();
    1438         614 :     st_ivas->nchan_transport = -1;
    1439         614 :     move16();
    1440         614 :     st_ivas->ism_mode = ISM_MODE_NONE;
    1441         614 :     move32();
    1442         614 :     st_ivas->mc_mode = MC_MODE_NONE;
    1443         614 :     move32();
    1444         614 :     st_ivas->sba_dirac_stereo_flag = 0;
    1445         614 :     move16();
    1446             : 
    1447             :     /* HRTF binauralization latency in ns */
    1448         614 :     st_ivas->binaural_latency_ns = 0;
    1449         614 :     move32();
    1450             : 
    1451             :     /*-------------------------------------------------------------------*
    1452             :      * Allocate and initialize Custom loudspeaker layout handle
    1453             :      *--------------------------------------------------------------------*/
    1454             : 
    1455         614 :     IF( st_ivas->hDecoderConfig->Opt_LsCustom )
    1456             :     {
    1457           3 :         IF( EQ_32( ( error = ivas_ls_custom_open_fx( &( st_ivas->hLsSetupCustom ) ) ), IVAS_ERR_OK ) )
    1458             :         {
    1459           3 :             set_zero_fx( ( st_ivas->hLsSetupCustom )->ls_azimuth_fx, MAX_OUTPUT_CHANNELS );
    1460           3 :             set_zero_fx( ( st_ivas->hLsSetupCustom )->ls_elevation_fx, MAX_OUTPUT_CHANNELS );
    1461             :         }
    1462             :         ELSE
    1463             :         {
    1464           0 :             return error;
    1465             :         }
    1466             :     }
    1467             : 
    1468             :     /*-------------------------------------------------------------------*
    1469             :      * Allocate and initialize Head-Tracking handle
    1470             :      *--------------------------------------------------------------------*/
    1471             : 
    1472         614 :     IF( st_ivas->hDecoderConfig->Opt_Headrotation )
    1473             :     {
    1474          82 :         IF( NE_32( ( error = ivas_headTrack_open_fx( &( st_ivas->hHeadTrackData ) ) ), IVAS_ERR_OK ) )
    1475             :         {
    1476           0 :             return error;
    1477             :         }
    1478          82 :         error = ivas_orient_trk_SetTrackingType_fx( st_ivas->hHeadTrackData->OrientationTracker, st_ivas->hDecoderConfig->orientation_tracking );
    1479          82 :         IF( NE_32( ( error ), IVAS_ERR_OK ) )
    1480             :         {
    1481           0 :             return error;
    1482             :         }
    1483             :     }
    1484             : 
    1485             :     /*-------------------------------------------------------------------*
    1486             :      * Allocate and initialize external orientation handle
    1487             :      *--------------------------------------------------------------------*/
    1488             : 
    1489         614 :     IF( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1490             :     {
    1491          31 :         IF( NE_32( ( error = ivas_external_orientation_open( &( st_ivas->hExtOrientationData ), st_ivas->hDecoderConfig->render_framesize ) ), IVAS_ERR_OK ) )
    1492             :         {
    1493           0 :             return error;
    1494             :         }
    1495             :     }
    1496             : 
    1497             :     /*-------------------------------------------------------------------*
    1498             :      * Allocate and initialize combined orientation handle
    1499             :      *--------------------------------------------------------------------*/
    1500             : 
    1501         614 :     test();
    1502         614 :     IF( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    1503             :     {
    1504          82 :         IF( NE_32( ( error = ivas_combined_orientation_open( &( st_ivas->hCombinedOrientationData ), st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->render_framesize ) ), IVAS_ERR_OK ) )
    1505             :         {
    1506           0 :             return error;
    1507             :         }
    1508             :     }
    1509             : 
    1510             : #ifndef FIX_CREND_SIMPLIFY_CODE
    1511             :     /*-------------------------------------------------------------------*
    1512             :      * Allocate HRTF binary handle
    1513             :      *--------------------------------------------------------------------*/
    1514             : 
    1515             :     IF( st_ivas->hDecoderConfig->Opt_HRTF_binary )
    1516             :     {
    1517             :         IF( NE_32( ( error = ivas_HRTF_binary_open_fx( &( st_ivas->hHrtfTD ) ) ), IVAS_ERR_OK ) )
    1518             :         {
    1519             :             return error;
    1520             :         }
    1521             :         IF( NE_32( ( error = ivas_HRTF_CRend_binary_open_fx( &( st_ivas->hSetOfHRTF ) ) ), IVAS_ERR_OK ) )
    1522             :         {
    1523             :             return error;
    1524             :         }
    1525             : 
    1526             :         IF( NE_32( ( error = ivas_HRTF_fastconv_binary_open_fx( &st_ivas->hHrtfFastConv ) ), IVAS_ERR_OK ) )
    1527             :         {
    1528             :             return error;
    1529             :         }
    1530             : 
    1531             :         IF( NE_32( ( error = ivas_HRTF_parambin_binary_open_fx( &st_ivas->hHrtfParambin ) ), IVAS_ERR_OK ) )
    1532             :         {
    1533             :             return error;
    1534             :         }
    1535             : 
    1536             :         IF( NE_32( ( error = ivas_HRTF_statistics_binary_open_fx( &st_ivas->hHrtfStatistics ) ), IVAS_ERR_OK ) )
    1537             :         {
    1538             :             return error;
    1539             :         }
    1540             :     }
    1541             : #endif
    1542             :     /*-------------------------------------------------------------------*
    1543             :      * Allocate and initialize Binaural Renderer configuration handle
    1544             :      *--------------------------------------------------------------------*/
    1545             : 
    1546         614 :     test();
    1547         614 :     test();
    1548         614 :     test();
    1549         614 :     test();
    1550         614 :     test();
    1551         614 :     test();
    1552         614 :     IF( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ||
    1553             :         ( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) && st_ivas->hDecoderConfig->Opt_non_diegetic_pan ) )
    1554             :     {
    1555         203 :         IF( NE_32( ( error = ivas_render_config_open_fx( &( st_ivas->hRenderConfig ) ) ), IVAS_ERR_OK ) )
    1556             :         {
    1557           0 :             return error;
    1558             :         }
    1559             : 
    1560         203 :         IF( NE_32( ( error = ivas_render_config_init_from_rom_fx( &st_ivas->hRenderConfig ) ), IVAS_ERR_OK ) )
    1561             :         {
    1562           0 :             return error;
    1563             :         }
    1564             :     }
    1565             : 
    1566         614 :     return error;
    1567             : }
    1568             : 
    1569             : 
    1570             : /*-------------------------------------------------------------------*
    1571             :  * ivas_init_decoder()
    1572             :  *
    1573             :  * Initialize IVAS decoder state structure
    1574             :  *-------------------------------------------------------------------*/
    1575         614 : ivas_error ivas_init_decoder_fx(
    1576             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
    1577             : )
    1578             : {
    1579             :     Word16 i, n, k;
    1580             :     Word16 sce_id, cpe_id;
    1581             :     Word16 numCldfbAnalyses, numCldfbSyntheses;
    1582             :     Word16 granularity, n_channels_transport_jbm;
    1583             :     Word32 output_Fs, ivas_total_brate, tmp_br, tmp32;
    1584             :     Word32 delay_ns;
    1585             :     AUDIO_CONFIG output_config;
    1586             :     DECODER_CONFIG_HANDLE hDecoderConfig;
    1587             :     ivas_error error;
    1588             :     Word32 ism_total_brate;
    1589             : 
    1590         614 :     error = IVAS_ERR_OK;
    1591         614 :     move32();
    1592         614 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1593         614 :     move32();
    1594         614 :     hDecoderConfig = st_ivas->hDecoderConfig;
    1595         614 :     output_config = hDecoderConfig->output_config;
    1596         614 :     ivas_total_brate = hDecoderConfig->ivas_total_brate;
    1597         614 :     move32();
    1598         614 :     hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
    1599         614 :     move32();
    1600         614 :     st_ivas->last_active_ivas_total_brate = ivas_total_brate;
    1601         614 :     move32();
    1602             : 
    1603             :     /*-----------------------------------------------------------------*
    1604             :      * Set number of output channels for EXTERNAL output config.
    1605             :      *-----------------------------------------------------------------*/
    1606             : 
    1607         614 :     test();
    1608         614 :     test();
    1609         614 :     IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    1610             :     {
    1611          50 :         test();
    1612          50 :         IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
    1613             :         {
    1614           1 :             hDecoderConfig->nchan_out = CPE_CHANNELS;
    1615             :         }
    1616          49 :         ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
    1617             :         {
    1618           4 :             hDecoderConfig->nchan_out = audioCfg2channels( st_ivas->transport_config );
    1619             :         }
    1620          45 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) || EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
    1621             :         {
    1622           9 :             hDecoderConfig->nchan_out = audioCfg2channels( ivas_set_audio_config_from_sba_order( st_ivas->sba_order ) );
    1623           9 :             move16();
    1624           9 :             hDecoderConfig->nchan_out = add( hDecoderConfig->nchan_out, st_ivas->nchan_ism );
    1625           9 :             move16();
    1626             :         }
    1627          36 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
    1628             :         {
    1629          21 :             hDecoderConfig->nchan_out = add( st_ivas->nchan_transport, st_ivas->nchan_ism );
    1630          21 :             move16();
    1631             :         }
    1632          15 :         ELSE IF( !EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1633             :         {
    1634          13 :             hDecoderConfig->nchan_out = st_ivas->nchan_transport;
    1635          13 :             move16();
    1636             :         }
    1637             : 
    1638          50 :         st_ivas->hOutSetup.nchan_out_woLFE = hDecoderConfig->nchan_out;
    1639          50 :         move16();
    1640             :     }
    1641             : 
    1642             :     /*-----------------------------------------------------------------*
    1643             :      * Set output and intern setup & renderer selection
    1644             :      *-----------------------------------------------------------------*/
    1645             : 
    1646         614 :     st_ivas->intern_config = output_config;
    1647         614 :     move32();
    1648             : 
    1649         614 :     test();
    1650         614 :     test();
    1651         614 :     IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) && EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
    1652             :     {
    1653           4 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->transport_config );
    1654           4 :         st_ivas->intern_config = st_ivas->transport_config;
    1655           4 :         move32();
    1656             :     }
    1657         610 :     ELSE IF( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && ( st_ivas->ivas_format == SBA_ISM_FORMAT || st_ivas->ivas_format == SBA_FORMAT ) )
    1658             :     {
    1659           9 :         st_ivas->intern_config = ivas_set_audio_config_from_sba_order( st_ivas->sba_order );
    1660           9 :         ivas_output_init( &( st_ivas->hOutSetup ), st_ivas->intern_config );
    1661             :     }
    1662             :     ELSE
    1663             :     {
    1664         601 :         ivas_output_init( &( st_ivas->hOutSetup ), output_config );
    1665             :     }
    1666             : 
    1667         614 :     test();
    1668         614 :     IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) )
    1669             :     {
    1670           3 :         st_ivas->hOutSetup.ambisonics_order = SBA_HOA3_ORDER;
    1671           3 :         move16();
    1672           3 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_7_1_4;
    1673           3 :         move32();
    1674           3 :         st_ivas->hOutSetup.output_config = st_ivas->intern_config;
    1675           3 :         move32();
    1676           3 :         st_ivas->hOutSetup.nchan_out_woLFE = audioCfg2channels( st_ivas->intern_config );
    1677           3 :         move16();
    1678             :     }
    1679             : 
    1680             :     /* Only initialize transport setup if it is used */
    1681         614 :     IF( NE_32( st_ivas->transport_config, IVAS_AUDIO_CONFIG_INVALID ) )
    1682             :     {
    1683         149 :         ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
    1684             :     }
    1685             : 
    1686         614 :     test();
    1687         614 :     IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1688             :     {
    1689          30 :         ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    1690             : 
    1691          30 :         ivas_mcmasa_set_separate_channel_mode_fx( &( st_ivas->hOutSetup.separateChannelEnabled ), &( st_ivas->hOutSetup.separateChannelIndex ), ivas_total_brate );
    1692             :     }
    1693             : 
    1694         614 :     ivas_renderer_select( st_ivas );
    1695             : 
    1696         614 :     IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    1697             :     {
    1698           3 :         IF( EQ_16( ( error = ivas_ls_custom_output_init_fx( st_ivas ) ), IVAS_ERR_OK ) )
    1699             :         {
    1700           3 :             st_ivas->hOutSetup.ls_azimuth_fx = st_ivas->hLsSetupCustom->ls_azimuth_fx;
    1701           3 :             move32();
    1702           3 :             st_ivas->hOutSetup.ls_elevation_fx = st_ivas->hLsSetupCustom->ls_elevation_fx;
    1703           3 :             move32();
    1704           3 :             st_ivas->hIntSetup.ls_azimuth_fx = st_ivas->hLsSetupCustom->ls_azimuth_fx;
    1705           3 :             move32();
    1706           3 :             st_ivas->hIntSetup.ls_elevation_fx = st_ivas->hLsSetupCustom->ls_elevation_fx;
    1707           3 :             move32();
    1708             :         }
    1709             :         ELSE
    1710             :         {
    1711           0 :             return error;
    1712             :         }
    1713             :     }
    1714             : 
    1715         614 :     ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
    1716             : 
    1717         614 :     test();
    1718         614 :     IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1719             :     {
    1720          30 :         ivas_mcmasa_set_separate_channel_mode_fx( &( st_ivas->hIntSetup.separateChannelEnabled ), &( st_ivas->hIntSetup.separateChannelIndex ), ivas_total_brate );
    1721             : 
    1722          30 :         test();
    1723          30 :         IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.separateChannelEnabled )
    1724             :         {
    1725           0 :             st_ivas->hLsSetupCustom->separate_ch_found = 0;
    1726           0 :             move16();
    1727           0 :             IF( GE_16( st_ivas->hOutSetup.nchan_out_woLFE, MCMASA_MIN_SPEAKERS_SEPARATE_CENTER ) )
    1728             :             {
    1729             :                 /* check for a speaker at (0, 0) if minimum speaker count is available */
    1730           0 :                 FOR( i = 0; i < st_ivas->hOutSetup.nchan_out_woLFE; i++ )
    1731             :                 {
    1732           0 :                     test();
    1733           0 :                     IF( ( L_shr( st_ivas->hOutSetup.ls_azimuth_fx[i], Q22 ) == 0 ) && ( L_shr( st_ivas->hOutSetup.ls_elevation_fx[i], Q22 ) == 0 ) )
    1734             :                     {
    1735           0 :                         st_ivas->hIntSetup.separateChannelIndex = i;
    1736           0 :                         move16();
    1737           0 :                         st_ivas->hLsSetupCustom->separate_ch_found = 1;
    1738           0 :                         move16();
    1739           0 :                         BREAK;
    1740             :                     }
    1741             :                 }
    1742             :             }
    1743             :         }
    1744             :     }
    1745             : 
    1746             :     /*--------------------------------------------------------------------*
    1747             :      * Allocate and initialize HRTF Statistics handle
    1748             :      *--------------------------------------------------------------------*/
    1749             : 
    1750         614 :     IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    1751             :     {
    1752          51 :         IF( NE_32( ( error = ivas_HRTF_statistics_init_fx( &st_ivas->hHrtfStatistics, output_Fs ) ), IVAS_ERR_OK ) )
    1753             :         {
    1754           0 :             return error;
    1755             :         }
    1756             :     }
    1757             : 
    1758             :     /*-----------------------------------------------------------------*
    1759             :      * Allocate and initialize SCE/CPE and other handles
    1760             :      *-----------------------------------------------------------------*/
    1761             : 
    1762         614 :     IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) )
    1763             :     {
    1764           3 :         st_ivas->nSCE = 1; /* in mono, there is always only one SCE */
    1765           3 :         move16();
    1766           3 :         st_ivas->nCPE = 0;
    1767           3 :         move16();
    1768           3 :         st_ivas->nCPE_old = 0;
    1769           3 :         move16();
    1770           3 :         st_ivas->nchan_transport = 1;
    1771           3 :         move16();
    1772           3 :         sce_id = 0;
    1773           3 :         move16();
    1774             : 
    1775           3 :         IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
    1776             :         {
    1777           0 :             return error;
    1778             :         }
    1779             : 
    1780           3 :         reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1781             :     }
    1782         611 :     ELSE IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
    1783             :     {
    1784          68 :         st_ivas->nchan_transport = CPE_CHANNELS;
    1785          68 :         move16();
    1786          68 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_STEREO;
    1787          68 :         move32();
    1788             : 
    1789          68 :         st_ivas->nSCE = 0;
    1790          68 :         move16();
    1791          68 :         st_ivas->nCPE = 1; /* in stereo, there is always only one CPE */
    1792          68 :         move16();
    1793          68 :         st_ivas->nCPE_old = 0;
    1794          68 :         move16();
    1795          68 :         cpe_id = 0;
    1796          68 :         move16();
    1797             : 
    1798          68 :         IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, ivas_total_brate ) ), IVAS_ERR_OK ) )
    1799             :         {
    1800           0 :             return error;
    1801             :         }
    1802             : 
    1803         204 :         FOR( n = 0; n < st_ivas->nchan_transport; n++ )
    1804             :         {
    1805         136 :             reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1806             :         }
    1807             : 
    1808             :         /* init EFAP for custom LS output and set hTransSetup */
    1809          68 :         IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    1810             :         {
    1811           0 :             IF( NE_32( ( error = efap_init_data_fx( &( st_ivas->hEFAPdata ), st_ivas->hOutSetup.ls_azimuth_fx, st_ivas->hOutSetup.ls_elevation_fx, st_ivas->hOutSetup.nchan_out_woLFE, EFAP_MODE_EFAP ) ), IVAS_ERR_OK ) )
    1812             :             {
    1813           0 :                 return error;
    1814             :             }
    1815             : 
    1816           0 :             ivas_output_init( &( st_ivas->hTransSetup ), IVAS_AUDIO_CONFIG_STEREO );
    1817             :         }
    1818             :     }
    1819         543 :     ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
    1820             :     {
    1821             :         Word32 element_brate_tmp[MAX_NUM_OBJECTS];
    1822             : 
    1823          68 :         st_ivas->nSCE = st_ivas->nchan_transport; /* "st_ivas->nchan_transport" is known from ivas_dec_setup */
    1824          68 :         move16();
    1825          68 :         st_ivas->nCPE = 0;
    1826          68 :         move16();
    1827          68 :         st_ivas->nCPE_old = 0;
    1828          68 :         move16();
    1829          68 :         st_ivas->ism_extmeta_active = -1;
    1830          68 :         move16();
    1831          68 :         st_ivas->ism_extmeta_cnt = 0;
    1832          68 :         move16();
    1833          68 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1834             :         {
    1835          14 :             st_ivas->nchan_transport = MAX_PARAM_ISM_WAVE;
    1836          14 :             move16();
    1837          14 :             st_ivas->nSCE = MAX_PARAM_ISM_WAVE;
    1838          14 :             move16();
    1839             : 
    1840          14 :             IF( NE_32( ( error = ivas_param_ism_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    1841             :             {
    1842           0 :                 return error;
    1843             :             }
    1844             :         }
    1845             : 
    1846          68 :         IF( ( error = ivas_ism_metadata_dec_create_fx( st_ivas, st_ivas->nchan_ism, element_brate_tmp ) ) != IVAS_ERR_OK )
    1847             :         {
    1848           0 :             return error;
    1849             :         }
    1850             : 
    1851         238 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1852             :         {
    1853         170 :             IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, element_brate_tmp[sce_id] ) ), IVAS_ERR_OK ) )
    1854             :             {
    1855           0 :                 return error;
    1856             :             }
    1857             : 
    1858         170 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1859             : 
    1860         170 :             st_ivas->hSCE[sce_id]->hCoreCoder[0]->is_ism_format = 1;
    1861         170 :             move16();
    1862             :         }
    1863             : 
    1864          68 :         st_ivas->hISMDTX.sce_id_dtx = 0;
    1865          68 :         move16();
    1866             : 
    1867          68 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1868             :         {
    1869          14 :             st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed3;
    1870          14 :             move16();
    1871             :         }
    1872          54 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_DISC ) )
    1873             :         {
    1874         196 :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; ++sce_id )
    1875             :             {
    1876         142 :                 st_ivas->hSCE[sce_id]->hCoreCoder[0]->hFdCngDec->hFdCngCom->seed2 = add( 2, sce_id );
    1877         142 :                 move16();
    1878             :             }
    1879             :         }
    1880             :     }
    1881         475 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
    1882             :     {
    1883         245 :         IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
    1884             :         {
    1885           0 :             return error;
    1886             :         }
    1887             : 
    1888         245 :         IF( NE_32( ( error = ivas_spar_dec_open_fx( st_ivas, 0 ) ), IVAS_ERR_OK ) )
    1889             :         {
    1890           0 :             return error;
    1891             :         }
    1892         245 :         set16_fx( st_ivas->hSpar->hFbMixer->cldfb_cross_fade_fx, 0, CLDFB_NO_COL_MAX );
    1893             : 
    1894         245 :         Word16 hodirac_flag = ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order );
    1895         245 :         IF( hodirac_flag )
    1896             :         {
    1897           8 :             IF( NE_32( ( error = ivas_dirac_sba_config_fx( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order, IVAS_MAX_NUM_BANDS, st_ivas->ivas_format ) ), IVAS_ERR_OK ) )
    1898             :             {
    1899           0 :                 return error;
    1900             :             }
    1901             :         }
    1902             :         ELSE
    1903             :         {
    1904         237 :             IF( NE_32( ( error = ivas_dirac_sba_config_fx( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order, ( IVAS_MAX_NUM_BANDS - SPAR_DIRAC_SPLIT_START_BAND ), st_ivas->ivas_format ) ), IVAS_ERR_OK ) )
    1905             :             {
    1906           0 :                 return error;
    1907             :             }
    1908             :         }
    1909             : 
    1910         245 :         test();
    1911         245 :         test();
    1912         245 :         test();
    1913         245 :         IF( NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) && !( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) && EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_FOA ) ) )
    1914             :         {
    1915          81 :             IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
    1916             :             {
    1917           0 :                 return error;
    1918             :             }
    1919             : 
    1920          81 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    1921          81 :             move16();
    1922             :         }
    1923             :         ELSE
    1924             :         {
    1925             :             Word16 band_grouping[IVAS_MAX_NUM_BANDS + 1];
    1926             : 
    1927         164 :             st_ivas->hSpar->enc_param_start_band = s_min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    1928         164 :             move16();
    1929         164 :             IF( ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order ) )
    1930             :             {
    1931           1 :                 st_ivas->hSpar->enc_param_start_band = 0;
    1932           1 :                 move16();
    1933           1 :                 set8_fx( (Word8 *) st_ivas->hQMetaData->twoDirBands, (Word8) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    1934           1 :                 st_ivas->hQMetaData->numTwoDirBands = (UWord8) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    1935           1 :                 move16();
    1936             :             }
    1937             : 
    1938         164 :             ivas_dirac_config_bands_fx( band_grouping, IVAS_MAX_NUM_BANDS, extract_l( Mpy_32_32_r( st_ivas->hDecoderConfig->output_Fs, INV_CLDFB_BANDWIDTH_Q31 ) ),
    1939         164 :                                         st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    1940             :         }
    1941         245 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    1942         245 :         move16();
    1943             : 
    1944         245 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
    1945         344 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    1946             :         {
    1947          99 :             IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, tmp_br ) ), IVAS_ERR_OK ) )
    1948             :             {
    1949           0 :                 return error;
    1950             :             }
    1951             : 
    1952          99 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    1953             :         }
    1954             : 
    1955         245 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
    1956         245 :         tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
    1957         475 :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    1958             :         {
    1959         230 :             IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    1960             :             {
    1961           0 :                 return error;
    1962             :             }
    1963             : 
    1964         690 :             FOR( n = 0; n < CPE_CHANNELS; n++ )
    1965             :             {
    1966         460 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    1967             :             }
    1968             :         }
    1969             : 
    1970             :         /* create CPE element for DFT Stereo like upmix */
    1971         245 :         iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nSCE, st_ivas->nCPE ), &tmp_br, &tmp32, 0 );
    1972         245 :         test();
    1973         245 :         IF( st_ivas->sba_dirac_stereo_flag && ( st_ivas->nCPE == 0 ) )
    1974             :         {
    1975           5 :             IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    1976             :             {
    1977           0 :                 return error;
    1978             :             }
    1979             : 
    1980           5 :             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 */
    1981           5 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    1982             :         }
    1983             : 
    1984         245 :         IF( GT_16( st_ivas->nCPE, 1 ) )
    1985             :         {
    1986          84 :             IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
    1987             :             {
    1988           0 :                 return error;
    1989             :             }
    1990             :         }
    1991             : 
    1992             :         /* set CNA/CNG flags */
    1993         245 :         ivas_sba_set_cna_cng_flag( st_ivas );
    1994             :     }
    1995         230 :     ELSE IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
    1996             :     {
    1997             :         /* if we start in ISM_MODE_NONE in MASA_ISM, that appears as normal MASA, but we may change to a mode with ISMs */
    1998          82 :         st_ivas->ism_extmeta_active = -1;
    1999          82 :         move16();
    2000          82 :         st_ivas->ism_extmeta_cnt = 0;
    2001          82 :         move16();
    2002          82 :         IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
    2003             :         {
    2004           0 :             return error;
    2005             :         }
    2006             : 
    2007          82 :         IF( NE_32( ( error = ivas_masa_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2008             :         {
    2009           0 :             return error;
    2010             :         }
    2011             : 
    2012          82 :         test();
    2013          82 :         test();
    2014          82 :         test();
    2015          82 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
    2016             :         {
    2017          61 :             IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
    2018             :             {
    2019           0 :                 return error;
    2020             :             }
    2021             :         }
    2022             : 
    2023          82 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
    2024         113 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2025             :         {
    2026          31 :             IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, tmp_br ) ), IVAS_ERR_OK ) )
    2027             :             {
    2028           0 :                 return error;
    2029             :             }
    2030             : 
    2031          31 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2032             :         }
    2033             : 
    2034          82 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
    2035          82 :         tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
    2036         133 :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2037             :         {
    2038          51 :             IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2039             :             {
    2040           0 :                 return error;
    2041             :             }
    2042             : 
    2043         153 :             FOR( n = 0; n < CPE_CHANNELS; n++ )
    2044             :             {
    2045         102 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2046             :             }
    2047             :         }
    2048             : 
    2049             :         /* set CNA/CNG flags */
    2050          82 :         ivas_sba_set_cna_cng_flag( st_ivas );
    2051             :     }
    2052         148 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    2053             :     {
    2054             :         Word32 temp_brate[MAX_SCE];
    2055          34 :         st_ivas->ism_extmeta_active = -1;
    2056          34 :         move16();
    2057          34 :         st_ivas->ism_extmeta_cnt = 0;
    2058          34 :         move16();
    2059             : 
    2060          34 :         st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    2061          34 :         move16();
    2062          34 :         IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
    2063             :         {
    2064           0 :             return error;
    2065             :         }
    2066             : 
    2067          34 :         IF( NE_32( ( error = ivas_spar_dec_open_fx( st_ivas, 0 ) ), IVAS_ERR_OK ) )
    2068             :         {
    2069           0 :             return error;
    2070             :         }
    2071             : 
    2072          34 :         Word16 hodirac_flag = ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order );
    2073          34 :         IF( hodirac_flag )
    2074             :         {
    2075           6 :             IF( NE_32( ( error = ivas_dirac_sba_config_fx( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order, IVAS_MAX_NUM_BANDS, st_ivas->ivas_format ) ), IVAS_ERR_OK ) )
    2076             :             {
    2077           0 :                 return error;
    2078             :             }
    2079             :         }
    2080             :         ELSE
    2081             :         {
    2082          28 :             IF( NE_32( ( error = ivas_dirac_sba_config_fx( st_ivas->hQMetaData, &st_ivas->element_mode_init, ivas_total_brate, st_ivas->sba_analysis_order, ( IVAS_MAX_NUM_BANDS - SPAR_DIRAC_SPLIT_START_BAND ), st_ivas->ivas_format ) ), IVAS_ERR_OK ) )
    2083             :             {
    2084           0 :                 return error;
    2085             :             }
    2086             :         }
    2087             : 
    2088          34 :         test();
    2089          34 :         test();
    2090          34 :         IF( NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) )
    2091             :         {
    2092          28 :             IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
    2093             :             {
    2094           0 :                 return error;
    2095             :             }
    2096             : 
    2097          28 :             st_ivas->hSpar->enc_param_start_band = st_ivas->hDirAC->hConfig->enc_param_start_band;
    2098          28 :             move16();
    2099             :         }
    2100             :         ELSE
    2101             :         {
    2102             :             Word16 band_grouping[IVAS_MAX_NUM_BANDS + 1];
    2103             : 
    2104           6 :             st_ivas->hSpar->enc_param_start_band = s_min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
    2105           6 :             move16();
    2106           6 :             IF( ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order ) )
    2107             :             {
    2108           1 :                 st_ivas->hSpar->enc_param_start_band = 0;
    2109           1 :                 move16();
    2110           1 :                 set8_fx( (Word8 *) st_ivas->hQMetaData->twoDirBands, (Word8) 1, st_ivas->hQMetaData->q_direction[0].cfg.nbands );
    2111           1 :                 st_ivas->hQMetaData->numTwoDirBands = (UWord8) st_ivas->hQMetaData->q_direction[0].cfg.nbands;
    2112           1 :                 move16();
    2113             :             }
    2114             : 
    2115           6 :             ivas_dirac_config_bands_fx( band_grouping, IVAS_MAX_NUM_BANDS, extract_l( Mpy_32_32_r( st_ivas->hDecoderConfig->output_Fs, INV_CLDFB_BANDWIDTH_Q31 ) ),
    2116           6 :                                         st_ivas->hSpar->dirac_to_spar_md_bands, st_ivas->hQMetaData->useLowerBandRes, st_ivas->hSpar->enc_param_start_band, 0, 1 );
    2117             :         }
    2118             : 
    2119          45 :         FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2120             :         {
    2121             :             Word32 res_dec, res_frac;
    2122          11 :             iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
    2123          11 :             IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, res_dec ) ), IVAS_ERR_OK ) )
    2124             :             {
    2125           0 :                 return error;
    2126             :             }
    2127             : 
    2128          11 :             reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2129             :         }
    2130             : 
    2131          34 :         IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    2132             :         {
    2133          17 :             st_ivas->nCPE_old = st_ivas->nCPE;
    2134          17 :             move16();
    2135             :             {
    2136             :                 Word16 n_all;
    2137             : 
    2138          17 :                 n_all = add( st_ivas->nchan_transport, st_ivas->nchan_ism );
    2139          17 :                 st_ivas->nCPE = shr( add( n_all, 1 ), 1 );
    2140             :             }
    2141          17 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2142          17 :             move16();
    2143             :         }
    2144             : 
    2145          34 :         iDiv_and_mod_32( ivas_total_brate, st_ivas->nchan_transport, &tmp_br, &tmp32, 0 );
    2146          34 :         tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
    2147         101 :         FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2148             :         {
    2149          67 :             IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2150             :             {
    2151           0 :                 return error;
    2152             :             }
    2153             : 
    2154         201 :             FOR( n = 0; n < CPE_CHANNELS; n++ )
    2155             :             {
    2156         134 :                 reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2157             :             }
    2158             :         }
    2159             : 
    2160             :         /* create CPE element for DFT Stereo like upmix */
    2161          34 :         iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nSCE, st_ivas->nCPE ), &tmp_br, &tmp32, 0 );
    2162          34 :         test();
    2163          34 :         IF( st_ivas->sba_dirac_stereo_flag && st_ivas->nCPE == 0 )
    2164             :         {
    2165           1 :             IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2166             :             {
    2167           0 :                 return error;
    2168             :             }
    2169             : 
    2170           1 :             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 */
    2171           1 :             st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    2172             :         }
    2173             : 
    2174          34 :         IF( GT_16( st_ivas->nCPE, 1 ) )
    2175             :         {
    2176          18 :             IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2177             :             {
    2178           0 :                 return error;
    2179             :             }
    2180             :         }
    2181             : 
    2182          34 :         IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    2183             :         {
    2184          17 :             IF( NE_32( ( error = ivas_ism_metadata_dec_create_fx( st_ivas, st_ivas->nchan_ism, temp_brate ) ), IVAS_ERR_OK ) )
    2185             :             {
    2186           0 :                 return error;
    2187             :             }
    2188             : 
    2189          17 :             IF( NE_32( ( error = ivas_osba_data_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2190             :             {
    2191           0 :                 return error;
    2192             :             }
    2193             :         }
    2194             : 
    2195             :         /* set CNA/CNG flags */
    2196          34 :         ivas_sba_set_cna_cng_flag( st_ivas );
    2197             :     }
    2198         114 :     ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    2199             :     {
    2200          33 :         st_ivas->ism_extmeta_active = -1;
    2201          33 :         move16();
    2202          33 :         st_ivas->ism_extmeta_cnt = 0;
    2203          33 :         move16();
    2204             : 
    2205          33 :         IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
    2206             :         {
    2207           0 :             return error;
    2208             :         }
    2209             : 
    2210          33 :         k = 0;
    2211          33 :         move16();
    2212          33 :         ism_total_brate = 0;
    2213          33 :         move32();
    2214             : 
    2215         293 :         WHILE( ( k < SIZE_IVAS_BRATE_TBL ) && ( ivas_total_brate != ivas_brate_tbl[k] ) )
    2216             :         {
    2217         260 :             k = add( k, 1 );
    2218             :         }
    2219             : 
    2220          33 :         test();
    2221          33 :         IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
    2222             :         {
    2223             :             /* one separated object */
    2224          19 :             st_ivas->nSCE = 1;
    2225          19 :             move16();
    2226          19 :             ism_total_brate = sep_object_brate[k - 2][0];
    2227          19 :             move32();
    2228             : 
    2229          19 :             IF( NE_32( ( error = create_sce_dec( st_ivas, 0, ism_total_brate ) ), IVAS_ERR_OK ) )
    2230             :             {
    2231           0 :                 return error;
    2232             :             }
    2233             : 
    2234          19 :             reset_indices_dec( st_ivas->hSCE[0]->hCoreCoder[0] );
    2235             : 
    2236          19 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    2237             :             {
    2238           1 :                 if ( ( error = ivas_ism_metadata_dec_create_fx( st_ivas, st_ivas->nchan_ism, NULL ) ) != IVAS_ERR_OK )
    2239             :                 {
    2240           0 :                     return error;
    2241             :                 }
    2242             :             }
    2243             :             else
    2244             :             {
    2245          18 :                 IF( NE_32( ( error = ivas_ism_metadata_dec_create_fx( st_ivas, 1, NULL ) ), IVAS_ERR_OK ) )
    2246             :                 {
    2247           0 :                     return error;
    2248             :                 }
    2249             :             }
    2250             :         }
    2251          14 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    2252             :         {
    2253             :             Word32 temp_brate[MAX_SCE];
    2254          14 :             st_ivas->nSCE = st_ivas->nchan_ism; /* number of objects */
    2255          14 :             move16();
    2256          53 :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2257             :             {
    2258          39 :                 temp_brate[sce_id] = sep_object_brate[k - 2][st_ivas->nSCE - 1];
    2259          39 :                 move32();
    2260          39 :                 ism_total_brate = L_add( ism_total_brate, temp_brate[sce_id] );
    2261             : 
    2262          39 :                 IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, temp_brate[sce_id] ) ), IVAS_ERR_OK ) )
    2263             :                 {
    2264           0 :                     return error;
    2265             :                 }
    2266             : 
    2267          39 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2268             :             }
    2269             : 
    2270          14 :             IF( NE_32( ( error = ivas_ism_metadata_dec_create_fx( st_ivas, st_ivas->nchan_ism, temp_brate ) ), IVAS_ERR_OK ) )
    2271             :             {
    2272           0 :                 return error;
    2273             :             }
    2274             :         }
    2275             : 
    2276          33 :         IF( NE_32( ( error = ivas_masa_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2277             :         {
    2278           0 :             return error;
    2279             :         }
    2280             : 
    2281          33 :         IF( NE_32( ( error = ivas_omasa_data_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2282             :         {
    2283           0 :             return error;
    2284             :         }
    2285             : 
    2286          33 :         test();
    2287          33 :         test();
    2288          33 :         test();
    2289          33 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
    2290             :         {
    2291          24 :             IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
    2292             :             {
    2293           0 :                 return error;
    2294             :             }
    2295             :         }
    2296             : 
    2297          33 :         IF( NE_32( ( error = create_cpe_dec( st_ivas, 0, ivas_total_brate - ism_total_brate ) ), IVAS_ERR_OK ) )
    2298             :         {
    2299           0 :             return error;
    2300             :         }
    2301             : 
    2302          99 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    2303             :         {
    2304          66 :             reset_indices_dec( st_ivas->hCPE[0]->hCoreCoder[n] );
    2305             :         }
    2306             :     }
    2307          81 :     ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
    2308             :     {
    2309          81 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
    2310             :         {
    2311             :             /* init EFAP for custom LS setup */
    2312          36 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2313             :             {
    2314           1 :                 IF( NE_32( ( error = efap_init_data_fx( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth_fx, st_ivas->hLsSetupCustom->ls_elevation_fx, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ), IVAS_ERR_OK ) )
    2315             :                 {
    2316           0 :                     return error;
    2317             :                 }
    2318             :             }
    2319             : 
    2320          36 :             st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( st_ivas->transport_config ) );
    2321          36 :             move16();
    2322          36 :             st_ivas->nSCE = 0;
    2323          36 :             move16();
    2324          36 :             st_ivas->nCPE = shr( st_ivas->nchan_transport, 1 );
    2325          36 :             move16();
    2326          36 :             st_ivas->nCPE_old = 0;
    2327          36 :             move16();
    2328          36 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2329          36 :             move16();
    2330             : 
    2331          36 :             iDiv_and_mod_32( ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &tmp_br, &tmp32, 0 );
    2332          36 :             tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
    2333         181 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2334             :             {
    2335         145 :                 IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2336             :                 {
    2337           0 :                     return error;
    2338             :                 }
    2339             : 
    2340         435 :                 FOR( n = 0; n < CPE_CHANNELS; n++ )
    2341             :                 {
    2342         290 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2343             :                 }
    2344             :             }
    2345             : 
    2346          36 :             IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2347             :             {
    2348           0 :                 return error;
    2349             :             }
    2350             :         }
    2351          45 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    2352             :         {
    2353             :             /* init EFAP for custom LS setup */
    2354           5 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2355             :             {
    2356           0 :                 IF( NE_32( ( error = efap_init_data_fx( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth_fx, st_ivas->hLsSetupCustom->ls_elevation_fx, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ), IVAS_ERR_OK ) )
    2357             :                 {
    2358           0 :                     return error;
    2359             :                 }
    2360             :             }
    2361             : 
    2362           5 :             st_ivas->nSCE = 0;
    2363           5 :             move16();
    2364           5 :             st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS >> 1;
    2365           5 :             move16();
    2366           5 :             st_ivas->nCPE_old = 0;
    2367           5 :             move16();
    2368           5 :             st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    2369           5 :             move16();
    2370             : 
    2371           5 :             IF( NE_32( ( error = ivas_mc_paramupmix_dec_open( st_ivas ) ), IVAS_ERR_OK ) )
    2372             :             {
    2373           0 :                 return error;
    2374             :             }
    2375             : 
    2376           5 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
    2377           5 :             move16();
    2378             : 
    2379           5 :             iDiv_and_mod_32( ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &tmp_br, &tmp32, 0 );
    2380           5 :             tmp_br = L_shl( tmp_br, CPE_CHANNELS_LOG2 );
    2381          25 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2382             :             {
    2383          20 :                 IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2384             : 
    2385             :                 {
    2386           0 :                     return error;
    2387             :                 }
    2388             : 
    2389          60 :                 FOR( n = 0; n < CPE_CHANNELS; n++ )
    2390             :                 {
    2391          40 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2392             :                 }
    2393             :             }
    2394             : 
    2395           5 :             IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2396             :             {
    2397           0 :                 return error;
    2398             :             }
    2399             :         }
    2400          40 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    2401             :         {
    2402             :             /* init EFAP for custom LS setup */
    2403          10 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2404             :             {
    2405           0 :                 IF( NE_32( ( error = efap_init_data_fx( &( st_ivas->hEFAPdata ), st_ivas->hLsSetupCustom->ls_azimuth_fx, st_ivas->hLsSetupCustom->ls_elevation_fx, st_ivas->hLsSetupCustom->num_spk, EFAP_MODE_EFAP ) ), IVAS_ERR_OK ) )
    2406             :                 {
    2407           0 :                     return error;
    2408             :                 }
    2409             :             }
    2410          10 :             IF( NE_32( ( error = ivas_param_mc_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2411             :             {
    2412           0 :                 return error;
    2413             :             }
    2414             : 
    2415          10 :             st_ivas->hParamMC->proto_matrix_int_e = 0;
    2416          10 :             move16();
    2417             : 
    2418          10 :             iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nSCE, st_ivas->nCPE ), &tmp_br, &tmp32, 0 );
    2419          20 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2420             :             {
    2421          10 :                 IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2422             :                 {
    2423           0 :                     return error;
    2424             :                 }
    2425             : 
    2426          30 :                 FOR( n = 0; n < CPE_CHANNELS; n++ )
    2427             :                 {
    2428          20 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2429             :                 }
    2430             :             }
    2431             : 
    2432          10 :             IF( GT_16( st_ivas->nCPE, 1 ) )
    2433             :             {
    2434           0 :                 IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2435             :                 {
    2436           0 :                     return error;
    2437             :                 }
    2438             :             }
    2439             :         }
    2440          30 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    2441             :         {
    2442             :             Word32 brate_sce, brate_cpe;
    2443             : 
    2444          30 :             ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
    2445             : 
    2446          30 :             IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
    2447             :             {
    2448           0 :                 return error;
    2449             :             }
    2450             : 
    2451          30 :             IF( NE_32( ( error = ivas_masa_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2452             : 
    2453             :             {
    2454           0 :                 return error;
    2455             :             }
    2456             : 
    2457          30 :             st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
    2458          30 :             move16();
    2459          30 :             test();
    2460          30 :             IF( NE_32( st_ivas->renderer_type, RENDERER_DISABLE ) && NE_32( st_ivas->renderer_type, RENDERER_MCMASA_MONO_STEREO ) )
    2461             :             {
    2462          23 :                 IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
    2463             :                 {
    2464           0 :                     return error;
    2465             :                 }
    2466             :             }
    2467          30 :             test();
    2468          30 :             test();
    2469          30 :             IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.separateChannelEnabled && !st_ivas->hLsSetupCustom->separate_ch_found )
    2470             :             {
    2471             :                 /* If no speaker matching the separated channel, compute panning gains for the separated channel. */
    2472           0 :                 IF( st_ivas->hVBAPdata == NULL )
    2473             :                 {
    2474             :                     /* Distribute signal to all channels if VBAP is not properly initialized. */
    2475           0 :                     Word16 inv_sqr, sqr, exp = 15, exp_sqr;
    2476           0 :                     move16();
    2477           0 :                     IF( EQ_16( st_ivas->hLsSetupCustom->num_spk, 1 ) )
    2478             :                     {
    2479           0 :                         inv_sqr = 32767; // (1.0f in Q15)-1
    2480           0 :                         move16();
    2481             :                     }
    2482             :                     ELSE
    2483             :                     {
    2484           0 :                         sqr = Sqrt16( st_ivas->hLsSetupCustom->num_spk, &exp );
    2485           0 :                         inv_sqr = BASOP_Util_Divide1616_Scale( 32767, sqr, &exp_sqr );
    2486           0 :                         exp_sqr = sub( exp_sqr, exp );
    2487           0 :                         IF( ( exp < 0 ) )
    2488             :                         {
    2489           0 :                             inv_sqr = shr( inv_sqr, exp ); // exp_sqr
    2490             :                         }
    2491             :                         ELSE
    2492             :                         {
    2493           0 :                             inv_sqr = shl( inv_sqr, exp ); // exp_sqr
    2494             :                         }
    2495             :                     }
    2496           0 :                     set16_fx( st_ivas->hLsSetupCustom->separate_ch_gains_fx, inv_sqr, st_ivas->hLsSetupCustom->num_spk );
    2497             :                 }
    2498             :             }
    2499             : 
    2500          30 :             ivas_mcmasa_split_brate_fx( st_ivas->hOutSetup.separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &brate_sce, &brate_cpe );
    2501             : 
    2502          58 :             FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
    2503             :             {
    2504          28 :                 IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, brate_sce ) ), IVAS_ERR_OK ) )
    2505             :                 {
    2506           0 :                     return error;
    2507             :                 }
    2508             : 
    2509          28 :                 reset_indices_dec( st_ivas->hSCE[sce_id]->hCoreCoder[0] );
    2510             :             }
    2511             : 
    2512          35 :             FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
    2513             :             {
    2514           5 :                 st_ivas->element_mode_init = IVAS_CPE_MDCT; /* element_mode_init was IVAS_SCE for SCE initialization */
    2515           5 :                 move16();
    2516           5 :                 IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, brate_cpe ) ), IVAS_ERR_OK ) )
    2517             :                 {
    2518           0 :                     return error;
    2519             :                 }
    2520             : 
    2521          15 :                 FOR( n = 0; n < CPE_CHANNELS; n++ )
    2522             :                 {
    2523          10 :                     reset_indices_dec( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
    2524             :                 }
    2525             :             }
    2526             : 
    2527             :             /* create CPE element for DFT Stereo like upmix */
    2528          30 :             iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nSCE, st_ivas->nCPE ), &tmp_br, &tmp32, 0 );
    2529          30 :             IF( st_ivas->sba_dirac_stereo_flag )
    2530             :             {
    2531           3 :                 IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, tmp_br ) ), IVAS_ERR_OK ) )
    2532             :                 {
    2533           0 :                     return error;
    2534             :                 }
    2535             : 
    2536           3 :                 st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
    2537           3 :                 st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
    2538             :             }
    2539             : 
    2540             :             /* set CNA/CNG flags */
    2541          30 :             test();
    2542          30 :             test();
    2543          30 :             IF( EQ_16( st_ivas->nchan_transport, 1 ) && ( ( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) ) )
    2544             :             {
    2545          13 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag = 1;
    2546          13 :                 move16();
    2547          13 :                 st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag = 1;
    2548          13 :                 move16();
    2549             :             }
    2550             :         }
    2551             :     }
    2552             : 
    2553             :     /*-----------------------------------------------------------------*
    2554             :      * Allocate and initialize HP20 filter memories
    2555             :      *-----------------------------------------------------------------*/
    2556             : 
    2557             :     /* set number of output channels used for synthesis/decoding */
    2558         614 :     n = getNumChanSynthesis( st_ivas );
    2559             : 
    2560         614 :     IF( n > 0 )
    2561             :     {
    2562         614 :         IF( ( st_ivas->mem_hp20_out_fx = (Word32 **) malloc( n * sizeof( Word32 * ) ) ) == NULL )
    2563             :         {
    2564           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2565             :         }
    2566             :     }
    2567             :     ELSE
    2568             :     {
    2569           0 :         st_ivas->mem_hp20_out_fx = NULL;
    2570             :     }
    2571             : 
    2572        2228 :     FOR( i = 0; i < n; i++ )
    2573             :     {
    2574        1614 :         IF( ( st_ivas->mem_hp20_out_fx[i] = (Word32 *) malloc( ( L_HP20_MEM + 2 ) * sizeof( Word32 ) ) ) == NULL )
    2575             :         {
    2576           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
    2577             :         }
    2578        1614 :         set32_fx( st_ivas->mem_hp20_out_fx[i], 0, L_HP20_MEM + 2 );
    2579             :     }
    2580             : 
    2581             :     /*-------------------------------------------------------------------*
    2582             :      * Allocate and initialize rendering handles
    2583             :      *--------------------------------------------------------------------*/
    2584         614 :     test();
    2585         614 :     test();
    2586         614 :     test();
    2587         614 :     test();
    2588         614 :     test();
    2589         614 :     test();
    2590         614 :     IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
    2591             :     {
    2592          39 :         IF( NE_32( ( error = ivas_binRenderer_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2593             :         {
    2594           0 :             return error;
    2595             :         }
    2596             :     }
    2597             :     /* ParamISM is handled separately from other common config */
    2598         575 :     ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) && ( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) ) )
    2599             :     {
    2600           6 :         IF( NE_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    2601             :         {
    2602           6 :             IF( NE_32( ( error = ivas_dirac_dec_binaural_copy_hrtfs_fx( &st_ivas->hHrtfParambin ) ), IVAS_ERR_OK ) )
    2603             :             {
    2604           0 :                 return error;
    2605             :             }
    2606             :         }
    2607             : 
    2608           6 :         IF( NE_32( ( error = ivas_dirac_dec_init_binaural_data_fx( st_ivas, &( st_ivas->hHrtfParambin ) ) ), IVAS_ERR_OK ) )
    2609             :         {
    2610           0 :             return error;
    2611             :         }
    2612             :     }
    2613         569 :     ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
    2614             :     {
    2615             :         Word16 SrcInd[MAX_NUM_TDREND_CHANNELS];
    2616             :         Word16 num_src;
    2617          24 :         IF( NE_32( ( error = ivas_td_binaural_open_fx( st_ivas, SrcInd, &num_src ) ), IVAS_ERR_OK ) )
    2618             :         {
    2619           0 :             return error;
    2620             :         }
    2621          24 :         Word16 nchan_rend = num_src;
    2622          24 :         move16();
    2623          24 :         test();
    2624          24 :         IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && NE_32( st_ivas->transport_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2625             :         {
    2626           3 :             nchan_rend = sub( nchan_rend, 1 ); /* Skip LFE channel -- added to the others */
    2627             :         }
    2628          98 :         FOR( Word16 nS = 0; nS < nchan_rend; nS++ )
    2629             :         {
    2630          74 :             TDREND_SRC_t *Src_p = st_ivas->hBinRendererTd->Sources[SrcInd[nS]];
    2631          74 :             if ( Src_p->SrcSpatial_p != NULL )
    2632             :             {
    2633          74 :                 Src_p->SrcSpatial_p->q_Pos_p = Q31;
    2634          74 :                 move16();
    2635             :             }
    2636          74 :             TDREND_SRC_SPATIAL_t *SrcSpatial_p = st_ivas->hBinRendererTd->Sources[nS]->SrcSpatial_p;
    2637          74 :             SrcSpatial_p->q_Pos_p = Q31;
    2638          74 :             move16();
    2639             :         }
    2640             : 
    2641          24 :         IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    2642             :         {
    2643           3 :             if ( NE_32( ( error = ivas_reverb_open_fx( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ), IVAS_ERR_OK ) )
    2644             :             {
    2645           0 :                 return error;
    2646             :             }
    2647             :         }
    2648             : 
    2649          24 :         granularity = NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
    2650             : 
    2651          24 :         n_channels_transport_jbm = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas );
    2652             : 
    2653             : 
    2654          24 :         IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_open_fx( st_ivas, TC_BUFFER_MODE_RENDERER, n_channels_transport_jbm, n_channels_transport_jbm, n_channels_transport_jbm, granularity ) ), IVAS_ERR_OK ) )
    2655             :         {
    2656           0 :             return error;
    2657             :         }
    2658             :     }
    2659         545 :     ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) )
    2660             :     {
    2661           6 :         IF( NE_32( ( error = ivas_ls_setup_conversion_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2662             :         {
    2663           0 :             return error;
    2664             :         }
    2665             :     }
    2666         539 :     ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_MONO_DOWNMIX ) )
    2667             :     {
    2668           9 :         IF( NE_32( ( error = ivas_mono_dmx_renderer_open( st_ivas ) ), IVAS_ERR_OK ) )
    2669             :         {
    2670           0 :             return error;
    2671             :         }
    2672             :     }
    2673         530 :     ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2674             :     {
    2675          30 :         test();
    2676          30 :         test();
    2677          30 :         test();
    2678          30 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) && EQ_32( st_ivas->ivas_format, MC_FORMAT ) && ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation ) )
    2679             :         {
    2680           4 :             IF( NE_32( ( error = efap_init_data_fx( &( st_ivas->hEFAPdata ), st_ivas->hIntSetup.ls_azimuth_fx, st_ivas->hIntSetup.ls_elevation_fx, st_ivas->hIntSetup.nchan_out_woLFE, EFAP_MODE_EFAP ) ), IVAS_ERR_OK ) )
    2681             :             {
    2682           0 :                 return error;
    2683             :             }
    2684             :         }
    2685             : 
    2686             : #ifdef FIX_CREND_SIMPLIFY_CODE
    2687          30 :         Word16 num_poses = 1;
    2688          30 :         move16();
    2689          30 :         if ( st_ivas->hSplitBinRend != NULL )
    2690             :         {
    2691           0 :             num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    2692           0 :             move16();
    2693             :         }
    2694             : 
    2695          30 :         IF( NE_32( ( error = ivas_rend_openCrend_fx( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, st_ivas->hDecoderConfig->output_config, st_ivas->hRenderConfig, st_ivas->hHrtfCrend, st_ivas->hHrtfStatistics, st_ivas->hDecoderConfig->output_Fs, 0, num_poses ) ), IVAS_ERR_OK ) )
    2696             : #else
    2697             :         IF( NE_32( ( error = ivas_rend_openCrend_fx( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, st_ivas->hDecoderConfig->output_config,
    2698             :                                                      st_ivas->hRenderConfig, st_ivas->hSetOfHRTF, st_ivas->hHrtfStatistics, st_ivas->hDecoderConfig->output_Fs, ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses ) ),
    2699             :                    IVAS_ERR_OK ) )
    2700             : #endif
    2701             :         {
    2702           0 :             return error;
    2703             :         }
    2704             : 
    2705          30 :         st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
    2706          30 :         move32();
    2707             : 
    2708          30 :         test();
    2709          30 :         IF( ( EQ_32( st_ivas->ivas_format, MC_FORMAT ) ) && ( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) )
    2710             :         {
    2711           0 :             granularity = NS2SA_FX2( output_Fs, CLDFB_SLOT_NS );
    2712           0 :             n_channels_transport_jbm = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas );
    2713             : 
    2714           0 :             IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_open_fx( st_ivas, TC_BUFFER_MODE_RENDERER, n_channels_transport_jbm, MC_PARAMUPMIX_MAX_INPUT_CHANS, MC_PARAMUPMIX_MAX_INPUT_CHANS, granularity ) ), IVAS_ERR_OK ) )
    2715             :             {
    2716           0 :                 return error;
    2717             :             }
    2718             :         }
    2719             :         ELSE
    2720             :         {
    2721          30 :             granularity = NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
    2722             : 
    2723          30 :             n_channels_transport_jbm = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas );
    2724             : 
    2725          30 :             IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_open_fx( st_ivas, TC_BUFFER_MODE_RENDERER, n_channels_transport_jbm, n_channels_transport_jbm, n_channels_transport_jbm, granularity ) ), IVAS_ERR_OK ) )
    2726             :             {
    2727           0 :                 return error;
    2728             :             }
    2729             :         }
    2730             :     }
    2731             : 
    2732         614 :     IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    2733             :     {
    2734          33 :         test();
    2735          33 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    2736             :         {
    2737             :             /* Allocate TD renderer for the objects in DISC mode */
    2738             :             Word16 SrcInd[MAX_NUM_TDREND_CHANNELS];
    2739             :             Word16 num_src;
    2740             : 
    2741           1 :             IF( NE_32( ( error = ivas_td_binaural_open_fx( st_ivas, SrcInd, &num_src ) ), IVAS_ERR_OK ) )
    2742             :             {
    2743           0 :                 return error;
    2744             :             }
    2745             : 
    2746           1 :             Word16 nchan_rend = num_src;
    2747           1 :             move16();
    2748           1 :             test();
    2749           1 :             if ( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && NE_32( st_ivas->transport_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2750             :             {
    2751           0 :                 nchan_rend = sub( nchan_rend, 1 ); /* Skip LFE channel -- added to the others */
    2752             :             }
    2753           5 :             FOR( Word16 nS = 0; nS < nchan_rend; nS++ )
    2754             :             {
    2755           4 :                 TDREND_SRC_t *Src_p = st_ivas->hBinRendererTd->Sources[SrcInd[nS]];
    2756           4 :                 if ( Src_p->SrcSpatial_p != NULL )
    2757             :                 {
    2758           4 :                     Src_p->SrcSpatial_p->q_Pos_p = Q31;
    2759           4 :                     move16();
    2760             :                 }
    2761           4 :                 TDREND_SRC_SPATIAL_t *SrcSpatial_p = st_ivas->hBinRendererTd->Sources[nS]->SrcSpatial_p;
    2762           4 :                 SrcSpatial_p->q_Pos_p = Q31;
    2763           4 :                 move16();
    2764             :             }
    2765             : 
    2766           1 :             IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    2767             :             {
    2768           0 :                 IF( NE_32( ( error = ivas_reverb_open_fx( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ), IVAS_ERR_OK ) )
    2769             :                 {
    2770           0 :                     return error;
    2771             :                 }
    2772             :             }
    2773             : 
    2774             :             /* Allocate memory for delay buffer within 'hMasaIsmData' */
    2775           1 :             IF( NE_32( ( error = ivas_omasa_objects_delay_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2776             :             {
    2777           0 :                 return error;
    2778             :             }
    2779             :         }
    2780             : 
    2781          33 :         test();
    2782          33 :         test();
    2783          33 :         test();
    2784          33 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) && ( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
    2785             :         {
    2786             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2787           9 :             IF( NE_32( ( error = ivas_omasa_objects_delay_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2788             :             {
    2789           0 :                 return error;
    2790             :             }
    2791             : 
    2792           9 :             IF( NE_32( ( error = ivas_omasa_separate_object_renderer_open( st_ivas ) ), IVAS_ERR_OK ) )
    2793             :             {
    2794           0 :                 return error;
    2795             :             }
    2796             :         }
    2797             : 
    2798          33 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_OMASA_OBJECT_EXT ) )
    2799             :         {
    2800             :             /* Allocate 'hIsmRendererData' handle and memory for delay buffer within 'hMasaIsmData' */
    2801           1 :             IF( NE_32( ( error = ivas_omasa_objects_delay_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2802             :             {
    2803           0 :                 return error;
    2804             :             }
    2805             : 
    2806           1 :             IF( ( ( error = ivas_spat_hSpatParamRendCom_config_fx( &st_ivas->hSpatParamRendCom, DIRAC_OPEN, 0, st_ivas->ivas_format, st_ivas->mc_mode, output_Fs, 0, 0 ) ), IVAS_ERR_OK ) )
    2807             :             {
    2808             :                 return error;
    2809             :             }
    2810             :         }
    2811             : 
    2812          33 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_OMASA_MIX_EXT ) )
    2813             :         {
    2814             :             /* Allocate 'hIsmRendererData' handle */
    2815           0 :             IF( ( ( error = ivas_omasa_combine_separate_ism_with_masa_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2816             :             {
    2817             :                 return error;
    2818             :             }
    2819             :         }
    2820             :     }
    2821             : 
    2822         614 :     test();
    2823         614 :     test();
    2824         614 :     test();
    2825         614 :     test();
    2826         614 :     test();
    2827         614 :     test();
    2828         614 :     test();
    2829         614 :     test();
    2830         614 :     test();
    2831         614 :     test();
    2832         614 :     test();
    2833         614 :     test();
    2834         614 :     IF( ( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) || EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) ) &&
    2835             :         ( EQ_32( st_ivas->ism_mode, ISM_MODE_DISC ) || EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) ) &&
    2836             :         ( EQ_32( st_ivas->renderer_type, RENDERER_TD_PANNING ) ||
    2837             :           EQ_32( st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) ||
    2838             :           EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) ||
    2839             :           EQ_32( st_ivas->renderer_type, RENDERER_OSBA_STEREO ) ||
    2840             :           EQ_32( st_ivas->renderer_type, RENDERER_OSBA_AMBI ) ||
    2841             :           EQ_32( st_ivas->renderer_type, RENDERER_OSBA_LS ) ||
    2842             :           EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) ||
    2843             :           EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ||
    2844             :           EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) ) )
    2845             :     {
    2846          32 :         IF( NE_32( ( error = ivas_ism_renderer_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
    2847             :         {
    2848           0 :             return error;
    2849             :         }
    2850             :     }
    2851             : 
    2852         614 :     IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    2853             :     {
    2854          34 :         test();
    2855          34 :         test();
    2856          34 :         IF( ( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    2857             :         {
    2858             :             /* Allocate TD renderer for the objects in DISC mode */
    2859             :             Word16 SrcInd[MAX_NUM_TDREND_CHANNELS];
    2860             :             Word16 num_src;
    2861           7 :             IF( NE_32( ( error = ivas_td_binaural_open_fx( st_ivas, SrcInd, &num_src ) ), IVAS_ERR_OK ) )
    2862             :             {
    2863           0 :                 return error;
    2864             :             }
    2865           7 :             Word16 nchan_rend = num_src;
    2866           7 :             move16();
    2867             : 
    2868           7 :             test();
    2869           7 :             if ( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && NE_32( st_ivas->transport_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    2870             :             {
    2871           0 :                 nchan_rend = sub( nchan_rend, 1 ); /* Skip LFE channel -- added to the others */
    2872             :             }
    2873          29 :             FOR( Word16 nS = 0; nS < nchan_rend; nS++ )
    2874             :             {
    2875          22 :                 TDREND_SRC_t *Src_p = st_ivas->hBinRendererTd->Sources[SrcInd[nS]];
    2876          22 :                 if ( Src_p->SrcSpatial_p != NULL )
    2877             :                 {
    2878          22 :                     Src_p->SrcSpatial_p->q_Pos_p = Q31;
    2879          22 :                     move16();
    2880             :                 }
    2881          22 :                 TDREND_SRC_SPATIAL_t *SrcSpatial_p = st_ivas->hBinRendererTd->Sources[nS]->SrcSpatial_p;
    2882          22 :                 SrcSpatial_p->q_Pos_p = Q31;
    2883          22 :                 move16();
    2884             :             }
    2885           7 :             IF( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    2886             :             {
    2887           2 :                 IF( NE_32( ( error = ivas_reverb_open_fx( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ), IVAS_ERR_OK ) )
    2888             :                 {
    2889           0 :                     return error;
    2890             :                 }
    2891             :             }
    2892             :         }
    2893             :     }
    2894             : 
    2895             :     /*-----------------------------------------------------------------*
    2896             :      * CLDFB handles for rendering
    2897             :      *-----------------------------------------------------------------*/
    2898             : 
    2899         614 :     ivas_init_dec_get_num_cldfb_instances_fx( st_ivas, &numCldfbAnalyses, &numCldfbSyntheses );
    2900             : 
    2901        2104 :     FOR( i = 0; i < numCldfbAnalyses; i++ )
    2902             :     {
    2903        1490 :         IF( NE_32( ( error = openCldfb_ivas_fx( &( st_ivas->cldfbAnaDec[i] ), CLDFB_ANALYSIS, output_Fs, CLDFB_PROTOTYPE_5_00MS, DEC ) ), IVAS_ERR_OK ) )
    2904             :         {
    2905           0 :             return error;
    2906             :         }
    2907             :     }
    2908        8948 :     FOR( ; i < MAX_INTERN_CHANNELS; i++ )
    2909             :     {
    2910        8334 :         st_ivas->cldfbAnaDec[i] = NULL;
    2911             :     }
    2912             : 
    2913        2885 :     FOR( i = 0; i < numCldfbSyntheses; i++ )
    2914             :     {
    2915        2271 :         IF( NE_32( ( error = openCldfb_ivas_fx( &( st_ivas->cldfbSynDec[i] ), CLDFB_SYNTHESIS, output_Fs, CLDFB_PROTOTYPE_5_00MS, DEC ) ), IVAS_ERR_OK ) )
    2916             :         {
    2917           0 :             return error;
    2918             :         }
    2919             :     }
    2920        8167 :     FOR( ; i < MAX_OUTPUT_CHANNELS; i++ )
    2921             :     {
    2922        7553 :         st_ivas->cldfbSynDec[i] = NULL;
    2923             :     }
    2924             : 
    2925             :     /* CLDFB Interpolation weights */
    2926         614 :     test();
    2927         614 :     test();
    2928         614 :     test();
    2929         614 :     IF( ( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) ) && !st_ivas->sba_dirac_stereo_flag && NE_16( st_ivas->hDecoderConfig->nchan_out, 1 ) )
    2930             :     {
    2931         265 :         Word16 Q_cldfbSynDec = Q11;
    2932         265 :         move16();
    2933         265 :         ivas_spar_get_cldfb_gains_fx( st_ivas->hSpar, st_ivas->cldfbAnaDec[0], st_ivas->cldfbSynDec[0], hDecoderConfig );
    2934             : 
    2935      109705 :         FOR( i = 0; i < st_ivas->cldfbAnaDec[0]->cldfb_state_length; i++ )
    2936             :         {
    2937      109440 :             st_ivas->cldfbAnaDec[0]->cldfb_state_fx[i] = L_shr( st_ivas->cldfbAnaDec[0]->cldfb_state_fx[i], 16 ); // Scaling down from 27 to 11
    2938      109440 :             move32();
    2939             :         }
    2940         265 :         st_ivas->cldfbAnaDec[0]->Q_cldfb_state = Q11;
    2941         265 :         move16();
    2942      121865 :         FOR( i = 0; i < st_ivas->cldfbSynDec[0]->cldfb_state_length; i++ )
    2943             :         {
    2944      121600 :             st_ivas->cldfbSynDec[0]->cldfb_state_fx[i] = L_shr( st_ivas->cldfbSynDec[0]->cldfb_state_fx[i], sub( 21, Q_cldfbSynDec ) ); // Scaling down from 21 to Q_cldfbSynDec
    2945      121600 :             move32();
    2946             :         }
    2947         265 :         st_ivas->cldfbSynDec[0]->Q_cldfb_state = Q11;
    2948         265 :         move16();
    2949             :     }
    2950             : 
    2951             :     /*-----------------------------------------------------------------*
    2952             :      * LFE handles for rendering after rendering to adjust LFE delay to filter delay
    2953             :      *-----------------------------------------------------------------*/
    2954             : 
    2955         614 :     test();
    2956         614 :     IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    2957             :     {
    2958          41 :         IF( NE_16( st_ivas->hIntSetup.index_lfe[0], -1 ) )
    2959             :         {
    2960          37 :             delay_ns = st_ivas->binaural_latency_ns;
    2961             :         }
    2962             :         ELSE
    2963             :         {
    2964           4 :             delay_ns = 0;
    2965             :         }
    2966          41 :         move32();
    2967          41 :         IF( st_ivas->hBinRenderer != NULL )
    2968             :         {
    2969           4 :             IF( st_ivas->hBinRenderer->render_lfe )
    2970             :             {
    2971           1 :                 IF( NE_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) && NE_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    2972             :                 {
    2973             :                     /* Account for filterbank delay */
    2974           1 :                     delay_ns = L_add( delay_ns, IVAS_FB_DEC_DELAY_NS );
    2975             :                 }
    2976             :             }
    2977             :             ELSE
    2978             :             {
    2979           3 :                 delay_ns = 0;
    2980           3 :                 move32();
    2981             :             }
    2982             :         }
    2983             :         ELSE
    2984             :         {
    2985          37 :             IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) && ( st_ivas->cldfbSynDec[0] != NULL ) )
    2986             :             {
    2987           1 :                 delay_ns = L_add( delay_ns, IVAS_FB_DEC_DELAY_NS );
    2988             :             }
    2989             :         }
    2990             : 
    2991          41 :         IF( NE_32( ( error = ivas_create_lfe_dec_fx( &st_ivas->hLFE, output_Fs, delay_ns ) ), IVAS_ERR_OK ) )
    2992             :         {
    2993           0 :             return error;
    2994             :         }
    2995             : 
    2996          41 :         set32_fx( st_ivas->hLFE->prevsynth_buf_fx, 0, LFE_PLC_BUFLEN );
    2997          41 :         set32_fx( st_ivas->hLFE->prior_out_buffer_fx, 0, L_FRAME48k );
    2998             :     }
    2999             : 
    3000             :     /*-----------------------------------------------------------------*
    3001             :      * Allocate and initialize limiter struct
    3002             :      *-----------------------------------------------------------------*/
    3003             : 
    3004         614 :     IF( NE_32( ( error = ivas_limiter_open_fx( &st_ivas->hLimiter, hDecoderConfig->nchan_out, output_Fs ) ), IVAS_ERR_OK ) )
    3005             :     {
    3006           0 :         return error;
    3007             :     }
    3008             : 
    3009             :     /*-----------------------------------------------------------------*
    3010             :      * Allocate and initialize JBM struct + buffer
    3011             :      *-----------------------------------------------------------------*/
    3012             : 
    3013         614 :     IF( st_ivas->hTcBuffer == NULL )
    3014             :     {
    3015             :         /* no module has yet open the TC buffer, open a default one */
    3016         135 :         n_channels_transport_jbm = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas );
    3017             : 
    3018         135 :         IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_open_fx( st_ivas, ivas_jbm_dec_get_tc_buffer_mode( st_ivas ), n_channels_transport_jbm, n_channels_transport_jbm, n_channels_transport_jbm, NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS ) ) ), IVAS_ERR_OK ) )
    3019             :         {
    3020           0 :             return error;
    3021             :         }
    3022             :     }
    3023             : 
    3024         614 :     test();
    3025         614 :     test();
    3026         614 :     IF( ( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) ) && st_ivas->hDecoderConfig->Opt_tsm )
    3027             :     {
    3028          16 :         IF( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    3029             :         {
    3030           3 :             IF( NE_32( ( error = ivas_jbm_dec_metadata_open( st_ivas ) ), IVAS_ERR_OK ) )
    3031             :             {
    3032           0 :                 return error;
    3033             :             }
    3034             :         }
    3035             :     }
    3036             : 
    3037             :     /*-----------------------------------------------------------------*
    3038             :      * Allocate floating-point output audio buffers
    3039             :      *-----------------------------------------------------------------*/
    3040             : 
    3041        4290 :     FOR( n = 0; n < ivas_get_nchan_buffers_dec_fx( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); n++ )
    3042             :     {
    3043             :         /* note: these are intra-frame heap memories */
    3044        3676 :         IF( ( st_ivas->p_output_fx[n] = (Word32 *) malloc( ( 48000 / FRAMES_PER_SEC ) * sizeof( Word32 ) ) ) == NULL )
    3045             :         {
    3046           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for floating-point output audio buffer!\n" ) );
    3047             :         }
    3048        3676 :         set32_fx( st_ivas->p_output_fx[n], 0, 48000 / FRAMES_PER_SEC );
    3049             :     }
    3050        9218 :     FOR( ; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    3051             :     {
    3052        8604 :         st_ivas->p_output_fx[n] = NULL;
    3053             :     }
    3054             : 
    3055         614 :     return error;
    3056             : }
    3057             : 
    3058             : 
    3059             : /*-------------------------------------------------------------------------
    3060             :  * destroy_core_dec()
    3061             :  *
    3062             :  * Close core decoder handles
    3063             :  *-------------------------------------------------------------------------*/
    3064             : 
    3065        8164 : void destroy_core_dec_fx(
    3066             :     DEC_CORE_HANDLE hCoreCoder /* i/o: core decoder structure      */
    3067             : )
    3068             : {
    3069        8164 :     IF( EQ_16( hCoreCoder->element_mode, EVS_MONO ) )
    3070             :     {
    3071           3 :         destroy_cldfb_decoder_fx( hCoreCoder );
    3072             :     }
    3073             :     ELSE
    3074             :     {
    3075        8161 :         destroy_cldfb_decoder_ivas_fx( hCoreCoder );
    3076             :     }
    3077             : 
    3078        8164 :     IF( hCoreCoder->hGSCDec != NULL )
    3079             :     {
    3080        2664 :         free( hCoreCoder->hGSCDec );
    3081        2664 :         hCoreCoder->hGSCDec = NULL;
    3082             :     }
    3083             : 
    3084        8164 :     IF( hCoreCoder->hPFstat != NULL )
    3085             :     {
    3086        2664 :         free( hCoreCoder->hPFstat );
    3087        2664 :         hCoreCoder->hPFstat = NULL;
    3088             :     }
    3089             : 
    3090        8164 :     IF( hCoreCoder->hMusicPF != NULL )
    3091             :     {
    3092        2664 :         free( hCoreCoder->hMusicPF );
    3093        2664 :         hCoreCoder->hMusicPF = NULL;
    3094             :     }
    3095             : 
    3096        8164 :     IF( hCoreCoder->hBPF != NULL )
    3097             :     {
    3098        2664 :         free( hCoreCoder->hBPF );
    3099        2664 :         hCoreCoder->hBPF = NULL;
    3100             :     }
    3101             : 
    3102        8164 :     IF( hCoreCoder->hBWE_zero != NULL )
    3103             :     {
    3104        2664 :         free( hCoreCoder->hBWE_zero );
    3105        2664 :         hCoreCoder->hBWE_zero = NULL;
    3106             :     }
    3107             : 
    3108        8164 :     IF( hCoreCoder->hTdCngDec != NULL )
    3109             :     {
    3110        1433 :         free( hCoreCoder->hTdCngDec );
    3111        1433 :         hCoreCoder->hTdCngDec = NULL;
    3112             :     }
    3113             : 
    3114        8164 :     IF( hCoreCoder->hSC_VBR != NULL )
    3115             :     {
    3116           3 :         free( hCoreCoder->hSC_VBR );
    3117           3 :         hCoreCoder->hSC_VBR = NULL;
    3118             :     }
    3119             : 
    3120        8164 :     IF( hCoreCoder->hAmrwb_IO != NULL )
    3121             :     {
    3122           3 :         free( hCoreCoder->hAmrwb_IO );
    3123           3 :         hCoreCoder->hAmrwb_IO = NULL;
    3124             :     }
    3125             : 
    3126        8164 :     IF( hCoreCoder->hBWE_TD != NULL )
    3127             :     {
    3128        2692 :         free( hCoreCoder->hBWE_TD );
    3129        2692 :         hCoreCoder->hBWE_TD = NULL;
    3130             :     }
    3131             : 
    3132        8164 :     IF( hCoreCoder->hBWE_FD != NULL )
    3133             :     {
    3134        2692 :         free( hCoreCoder->hBWE_FD );
    3135        2692 :         hCoreCoder->hBWE_FD = NULL;
    3136             :     }
    3137             : 
    3138        8164 :     IF( hCoreCoder->hBWE_FD_HR != NULL )
    3139             :     {
    3140           3 :         free( hCoreCoder->hBWE_FD_HR );
    3141           3 :         hCoreCoder->hBWE_FD_HR = NULL;
    3142             :     }
    3143             : 
    3144        8164 :     IF( hCoreCoder->hWIDec != NULL )
    3145             :     {
    3146           0 :         free( hCoreCoder->hWIDec );
    3147           0 :         hCoreCoder->hWIDec = NULL;
    3148             :     }
    3149             : 
    3150        8164 :     IF( hCoreCoder->hTECDec != NULL )
    3151             :     {
    3152           3 :         free( hCoreCoder->hTECDec );
    3153           3 :         hCoreCoder->hTECDec = NULL;
    3154             :     }
    3155             : 
    3156        8164 :     IF( hCoreCoder->hTcxLtpDec != NULL )
    3157             :     {
    3158        8083 :         free( hCoreCoder->hTcxLtpDec );
    3159        8083 :         hCoreCoder->hTcxLtpDec = NULL;
    3160             :     }
    3161             : 
    3162        8164 :     IF( hCoreCoder->hTcxDec != NULL )
    3163             :     {
    3164        8083 :         free( hCoreCoder->hTcxDec );
    3165        8083 :         hCoreCoder->hTcxDec = NULL;
    3166             :     }
    3167             : 
    3168        8164 :     IF( hCoreCoder->hTcxCfg != NULL )
    3169             :     {
    3170        8083 :         free( hCoreCoder->hTcxCfg );
    3171        8083 :         hCoreCoder->hTcxCfg = NULL;
    3172             :     }
    3173             : 
    3174        8164 :     IF( hCoreCoder->hTonalMDCTConc != NULL )
    3175             :     {
    3176        8083 :         free( hCoreCoder->hTonalMDCTConc );
    3177        8083 :         hCoreCoder->hTonalMDCTConc = NULL;
    3178             :     }
    3179             : 
    3180        8164 :     IF( hCoreCoder->hIGFDec != NULL )
    3181             :     {
    3182        8083 :         free( hCoreCoder->hIGFDec );
    3183        8083 :         hCoreCoder->hIGFDec = NULL;
    3184             :     }
    3185             : 
    3186        8164 :     IF( hCoreCoder->hPlcInfo != NULL )
    3187             :     {
    3188           3 :         free( hCoreCoder->hPlcInfo );
    3189           3 :         hCoreCoder->hPlcInfo = NULL;
    3190             :     }
    3191             : 
    3192        8164 :     IF( hCoreCoder->hHQ_core != NULL )
    3193             :     {
    3194        8083 :         free( hCoreCoder->hHQ_core );
    3195        8083 :         hCoreCoder->hHQ_core = NULL;
    3196             :     }
    3197             : 
    3198        8164 :     IF( hCoreCoder->hHQ_nbfec != NULL )
    3199             :     {
    3200           3 :         free( hCoreCoder->hHQ_nbfec );
    3201           3 :         hCoreCoder->hHQ_nbfec = NULL;
    3202             :     }
    3203             : 
    3204        8164 :     return;
    3205             : }
    3206             : 
    3207             : /*-------------------------------------------------------------------------
    3208             :  * ivas_initialize_handles_dec()
    3209             :  *
    3210             :  * NULL initialization of handles
    3211             :  *-------------------------------------------------------------------------*/
    3212             : 
    3213         614 : void ivas_initialize_handles_dec(
    3214             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                  */
    3215             : )
    3216             : {
    3217             :     Word16 i;
    3218             : 
    3219       10438 :     FOR( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    3220             :     {
    3221        9824 :         st_ivas->cldfbAnaDec[i] = NULL;
    3222             :     }
    3223             : 
    3224       10438 :     FOR( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    3225             :     {
    3226        9824 :         st_ivas->cldfbSynDec[i] = NULL;
    3227             :     }
    3228             : 
    3229             :     /* SCE handles */
    3230        3070 :     FOR( i = 0; i < MAX_SCE; i++ )
    3231             :     {
    3232        2456 :         st_ivas->hSCE[i] = NULL;
    3233             :     }
    3234             : 
    3235             :     /* CPE handles */
    3236        4298 :     FOR( i = 0; i < MAX_CPE; i++ )
    3237             :     {
    3238        3684 :         st_ivas->hCPE[i] = NULL;
    3239             :     }
    3240             : 
    3241         614 :     st_ivas->bit_stream = NULL;
    3242         614 :     st_ivas->mem_hp20_out_fx = NULL;
    3243         614 :     st_ivas->hLimiter = NULL;
    3244             : 
    3245             :     /* ISM metadata handles */
    3246        3070 :     FOR( i = 0; i < MAX_NUM_OBJECTS; i++ )
    3247             :     {
    3248        2456 :         st_ivas->hIsmMetaData[i] = NULL;
    3249             :     }
    3250             : 
    3251             :     /* spatial coding handles */
    3252         614 :     st_ivas->hDirAC = NULL;
    3253         614 :     st_ivas->hParamIsmDec = NULL;
    3254         614 :     st_ivas->hSpar = NULL;
    3255         614 :     st_ivas->hMasa = NULL;
    3256         614 :     st_ivas->hQMetaData = NULL;
    3257         614 :     st_ivas->hMCT = NULL;
    3258         614 :     st_ivas->hMCParamUpmix = NULL;
    3259         614 :     st_ivas->hParamMC = NULL;
    3260         614 :     st_ivas->hLFE = NULL;
    3261             : 
    3262             :     /* rendering handles */
    3263         614 :     st_ivas->hBinRenderer = NULL;
    3264        5526 :     for ( i = 0; i < MAX_HEAD_ROT_POSES; i++ )
    3265             :     {
    3266        4912 :         st_ivas->hDiracDecBin[i] = NULL;
    3267             :     }
    3268         614 :     st_ivas->hDirACRend = NULL;
    3269         614 :     st_ivas->hSpatParamRendCom = NULL;
    3270         614 :     st_ivas->hLsSetUpConversion = NULL;
    3271         614 :     st_ivas->hEFAPdata = NULL;
    3272         614 :     st_ivas->hVBAPdata = NULL;
    3273         614 :     st_ivas->hIsmRendererData = NULL;
    3274         614 :     st_ivas->hBinRendererTd = NULL;
    3275         614 :     st_ivas->hMonoDmxRenderer = NULL;
    3276         614 :     st_ivas->hCrendWrapper = NULL;
    3277         614 :     st_ivas->hReverb = NULL;
    3278             : #ifdef FIX_CREND_SIMPLIFY_CODE
    3279         614 :     st_ivas->hHrtfCrend = NULL;
    3280             : #else
    3281             :     st_ivas->hSetOfHRTF = NULL;
    3282             : #endif
    3283         614 :     st_ivas->hHrtfFastConv = NULL;
    3284         614 :     st_ivas->hHrtfParambin = NULL;
    3285         614 :     st_ivas->hHrtfStatistics = NULL;
    3286         614 :     st_ivas->hoa_dec_mtx = NULL;
    3287         614 :     st_ivas->hMasaIsmData = NULL;
    3288         614 :     st_ivas->hSbaIsmData = NULL;
    3289             : 
    3290         614 :     st_ivas->hHeadTrackData = NULL;
    3291         614 :     st_ivas->hHrtfTD = NULL;
    3292         614 :     st_ivas->hLsSetupCustom = NULL;
    3293         614 :     st_ivas->hRenderConfig = NULL;
    3294         614 :     st_ivas->hExtOrientationData = NULL;
    3295         614 :     st_ivas->hCombinedOrientationData = NULL;
    3296             : 
    3297         614 :     st_ivas->hSplitBinRend = NULL;
    3298        4912 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    3299             :     {
    3300        4298 :         st_ivas->hTdRendHandles[i] = NULL;
    3301             :     }
    3302             :     /* JBM handles */
    3303         614 :     st_ivas->hTcBuffer = NULL;
    3304         614 :     st_ivas->hJbmMetadata = NULL;
    3305             : 
    3306             :     /*  floating-point output audio buffers */
    3307       12894 :     FOR( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    3308             :     {
    3309       12280 :         st_ivas->p_output_fx[i] = NULL;
    3310             :     }
    3311             : 
    3312         614 :     return;
    3313             : }
    3314             : 
    3315             : 
    3316             : /*-------------------------------------------------------------------------
    3317             :  * ivas_destroy_dec()
    3318             :  *
    3319             :  * Close IVAS decoder handles
    3320             :  *-------------------------------------------------------------------------*/
    3321             : 
    3322         614 : void ivas_destroy_dec_fx(
    3323             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder handle      */
    3324             : )
    3325             : {
    3326             :     Word16 i;
    3327             : 
    3328             :     /* CLDFB handles */
    3329       10438 :     FOR( i = 0; i < MAX_INTERN_CHANNELS; i++ )
    3330             :     {
    3331        9824 :         IF( st_ivas->cldfbAnaDec[i] != NULL )
    3332             :         {
    3333        1525 :             deleteCldfb_ivas_fx( &( st_ivas->cldfbAnaDec[i] ) );
    3334             :         }
    3335             :     }
    3336             : 
    3337       10438 :     FOR( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    3338             :     {
    3339        9824 :         IF( st_ivas->cldfbSynDec[i] != NULL )
    3340             :         {
    3341        2260 :             deleteCldfb_ivas_fx( &( st_ivas->cldfbSynDec[i] ) );
    3342             :         }
    3343             :     }
    3344             : 
    3345             :     /* SCE handles */
    3346        3070 :     FOR( i = 0; i < MAX_SCE; i++ )
    3347             :     {
    3348        2456 :         IF( st_ivas->hSCE[i] != NULL )
    3349             :         {
    3350         384 :             destroy_sce_dec( st_ivas->hSCE[i] );
    3351         384 :             st_ivas->hSCE[i] = NULL;
    3352             :         }
    3353             :     }
    3354             : 
    3355             :     /* CPE handles */
    3356        4298 :     FOR( i = 0; i < MAX_CPE; i++ )
    3357             :     {
    3358        3684 :         IF( st_ivas->hCPE[i] != NULL )
    3359             :         {
    3360             :             /* set pointer to NULL as core coder already deallocated in destroy_sce_dec() */
    3361         734 :             test();
    3362         734 :             IF( st_ivas->sba_dirac_stereo_flag && EQ_16( st_ivas->nchan_transport, 1 ) )
    3363             :             {
    3364           5 :                 st_ivas->hCPE[i]->hCoreCoder[0] = NULL;
    3365           5 :                 st_ivas->hCPE[i]->hCoreCoder[1] = NULL;
    3366             :             }
    3367         734 :             destroy_cpe_dec( st_ivas->hCPE[i] );
    3368         734 :             st_ivas->hCPE[i] = NULL;
    3369             :         }
    3370             :     }
    3371             : 
    3372             :     /* HP20 filter handles */
    3373         614 :     IF( st_ivas->mem_hp20_out_fx != NULL )
    3374             :     {
    3375        2396 :         FOR( i = 0; i < getNumChanSynthesis( st_ivas ); i++ )
    3376             :         {
    3377        1782 :             free( st_ivas->mem_hp20_out_fx[i] );
    3378        1782 :             st_ivas->mem_hp20_out_fx[i] = NULL;
    3379             :         }
    3380         614 :         free( st_ivas->mem_hp20_out_fx );
    3381         614 :         st_ivas->mem_hp20_out_fx = NULL;
    3382             :     }
    3383             : 
    3384             :     /* ISM metadata handles */
    3385         614 :     ivas_ism_metadata_close( st_ivas->hIsmMetaData, 0 );
    3386             : 
    3387             :     /* ISM renderer handle */
    3388         614 :     ivas_ism_renderer_close( &( st_ivas->hIsmRendererData ) );
    3389             : 
    3390             :     /* DirAC handle */
    3391         614 :     IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
    3392             :     {
    3393          68 :         ivas_param_ism_dec_close_fx( &( st_ivas->hParamIsmDec ), &( st_ivas->hSpatParamRendCom ), st_ivas->hDecoderConfig->output_config );
    3394             :     }
    3395             :     ELSE
    3396             :     {
    3397         546 :         ivas_dirac_rend_close_fx( &( st_ivas->hDirACRend ) );
    3398         546 :         ivas_spat_hSpatParamRendCom_close_fx( &( st_ivas->hSpatParamRendCom ) );
    3399         546 :         ivas_dirac_dec_close_fx( &( st_ivas->hDirAC ) );
    3400             :     }
    3401             : 
    3402             :     /* SPAR handle */
    3403         614 :     ivas_spar_dec_close_fx( &( st_ivas->hSpar ), st_ivas->hDecoderConfig->output_Fs, 0 );
    3404             : 
    3405             :     /* HOA decoder matrix */
    3406         614 :     IF( st_ivas->hoa_dec_mtx != NULL )
    3407             :     {
    3408          27 :         free( st_ivas->hoa_dec_mtx );
    3409          27 :         st_ivas->hoa_dec_mtx = NULL;
    3410             :     }
    3411             : 
    3412             :     /* MASA decoder structure */
    3413         614 :     ivas_masa_dec_close_fx( &( st_ivas->hMasa ) );
    3414             : 
    3415             :     /* Qmetadata handle */
    3416         614 :     ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
    3417             : 
    3418             :     /* MCT handle */
    3419         614 :     ivas_mct_dec_close( &st_ivas->hMCT );
    3420             : 
    3421             :     /* LFE handle */
    3422         614 :     ivas_lfe_dec_close_fx( &( st_ivas->hLFE ) );
    3423             : 
    3424             :     /* Param-Upmix MC handle */
    3425         614 :     ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
    3426             : 
    3427             :     /* Parametric MC handle */
    3428         614 :     ivas_param_mc_dec_close_fx( &st_ivas->hParamMC );
    3429             : 
    3430             :     /* EFAP handle */
    3431         614 :     efap_free_data_fx( &st_ivas->hEFAPdata );
    3432             : 
    3433             :     /* VBAP handle */
    3434         614 :     vbap_free_data_fx( &( st_ivas->hVBAPdata ) );
    3435             : 
    3436             :     /* Fastconv binaural renderer handle */
    3437         614 :     ivas_binRenderer_close_fx( &st_ivas->hBinRenderer );
    3438             : 
    3439             :     /* TD binaural renderer handles */
    3440        4912 :     for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    3441             :     {
    3442        4298 :         if ( st_ivas->hTdRendHandles[i] != NULL )
    3443             :         {
    3444           0 :             st_ivas->hTdRendHandles[i]->HrFiltSet_p = NULL;
    3445           0 :             ivas_td_binaural_close_fx( &st_ivas->hTdRendHandles[i] );
    3446             :         }
    3447             :     }
    3448             : 
    3449             :     /* Parametric binaural renderer handle */
    3450         614 :     ivas_dirac_dec_close_binaural_data_fx( st_ivas->hDiracDecBin );
    3451             : 
    3452             :     /* Crend handle */
    3453             : #ifdef FIX_CREND_SIMPLIFY_CODE
    3454         614 :     ivas_rend_closeCrend_fx( &( st_ivas->hCrendWrapper ) );
    3455             : #else
    3456             :     ivas_rend_closeCrend_fx( &( st_ivas->hCrendWrapper ), ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses );
    3457             : #endif
    3458             : 
    3459             :     /* Reverb handle */
    3460         614 :     ivas_reverb_close_fx( &st_ivas->hReverb );
    3461             : 
    3462             :     /* LS config converter handle */
    3463         614 :     ivas_ls_setup_conversion_close_fx( &st_ivas->hLsSetUpConversion );
    3464             : 
    3465             :     /* Custom LS configuration handle */
    3466         614 :     IF( st_ivas->hLsSetupCustom != NULL )
    3467             :     {
    3468           3 :         free( st_ivas->hLsSetupCustom );
    3469           3 :         st_ivas->hLsSetupCustom = NULL;
    3470             :     }
    3471             : 
    3472             :     /* Mono downmix structure */
    3473         614 :     ivas_mono_dmx_renderer_close( &st_ivas->hMonoDmxRenderer );
    3474             : 
    3475             :     /* OSBA structure */
    3476         614 :     ivas_osba_data_close_fx( &st_ivas->hSbaIsmData );
    3477             : 
    3478             :     /* OMASA structure */
    3479         614 :     ivas_omasa_data_close_fx( &st_ivas->hMasaIsmData );
    3480             : 
    3481             :     /* Head track data handle */
    3482         614 :     ivas_headTrack_close_fx( &st_ivas->hHeadTrackData );
    3483             : 
    3484             :     /* External orientation data handle */
    3485         614 :     ivas_external_orientation_close_fx( &st_ivas->hExtOrientationData );
    3486             : 
    3487             :     /* Combined orientation data handle */
    3488         614 :     ivas_combined_orientation_close_fx( &st_ivas->hCombinedOrientationData );
    3489             : 
    3490             :     /* Time Domain binaural renderer handle */
    3491         614 :     IF( st_ivas->hBinRendererTd != NULL )
    3492             :     {
    3493          37 :         ivas_td_binaural_close_fx( &st_ivas->hBinRendererTd );
    3494             :     }
    3495             : 
    3496             : #ifdef FIX_CREND_SIMPLIFY_CODE
    3497         614 :     IF( st_ivas->hHrtfTD != NULL )
    3498             : #else
    3499             :     ELSE IF( st_ivas->hHrtfTD != NULL )
    3500             : #endif
    3501             :     {
    3502           0 :         BSplineModelEvalDealloc_fx( &st_ivas->hHrtfTD->ModelParams, &st_ivas->hHrtfTD->ModelEval );
    3503             : 
    3504           0 :         ivas_HRTF_binary_close_fx( &st_ivas->hHrtfTD );
    3505             :     }
    3506             : 
    3507             :     /* CRend binaural renderer handle */
    3508             : #ifdef FIX_CREND_SIMPLIFY_CODE
    3509         614 :     ivas_HRTF_CRend_binary_close_fx( &st_ivas->hHrtfCrend );
    3510             : #else
    3511             :     ivas_HRTF_CRend_binary_close_fx( &st_ivas->hSetOfHRTF );
    3512             : #endif
    3513             : 
    3514             :     /* Fastconv HRTF memories */
    3515         614 :     ivas_binaural_hrtf_close_fx( &st_ivas->hHrtfFastConv );
    3516             : 
    3517             :     /* Fastconv HRTF filters */
    3518         614 :     ivas_HRTF_fastconv_binary_close_fx( &st_ivas->hHrtfFastConv );
    3519             : 
    3520             :     /* Parametric binauralizer HRTF filters */
    3521         614 :     ivas_HRTF_parambin_binary_close_fx( &st_ivas->hHrtfParambin );
    3522             : 
    3523             :     /* HRTF statistics */
    3524         614 :     ivas_HRTF_statistics_close_fx( &st_ivas->hHrtfStatistics );
    3525             : 
    3526             :     /* Config. Renderer */
    3527         614 :     ivas_render_config_close_fx( &( st_ivas->hRenderConfig ) );
    3528             : 
    3529             :     /* Limiter struct */
    3530         614 :     ivas_limiter_close_fx( &( st_ivas->hLimiter ) );
    3531             : 
    3532         614 :     IF( st_ivas->hDecoderConfig != NULL )
    3533             :     {
    3534         614 :         free( st_ivas->hDecoderConfig );
    3535         614 :         st_ivas->hDecoderConfig = NULL;
    3536             :     }
    3537             : 
    3538         614 :     ivas_jbm_dec_tc_buffer_close( &st_ivas->hTcBuffer );
    3539             : 
    3540         614 :     IF( st_ivas->hJbmMetadata != NULL )
    3541             :     {
    3542           3 :         free( st_ivas->hJbmMetadata );
    3543           3 :         st_ivas->hJbmMetadata = NULL;
    3544             :     }
    3545             : 
    3546             :     /*  floating-point output audio buffers */
    3547       12894 :     FOR( i = 0; i < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    3548             :     {
    3549             : 
    3550       12280 :         IF( st_ivas->p_output_fx[i] != NULL )
    3551             :         {
    3552        3770 :             free( st_ivas->p_output_fx[i] );
    3553        3770 :             st_ivas->p_output_fx[i] = NULL;
    3554             :         }
    3555             :     }
    3556             : 
    3557             :     /* main IVAS handle */
    3558         614 :     free( st_ivas );
    3559             : 
    3560         614 :     return;
    3561             : }
    3562             : 
    3563             : 
    3564             : /*-------------------------------------------------------------------*
    3565             :  * ivas_init_dec_get_num_cldfb_instances()
    3566             :  *
    3567             :  * Return number of CLDFB analysis & synthesis instances
    3568             :  *-------------------------------------------------------------------*/
    3569             : 
    3570             : /*! r: number of cldfb instances */
    3571       15117 : void ivas_init_dec_get_num_cldfb_instances_fx(
    3572             :     Decoder_Struct *st_ivas,  /* i  : IVAS decoder structure                     */
    3573             :     Word16 *numCldfbAnalyses, /* o  : number of needed CLDFB analysis instances  */
    3574             :     Word16 *numCldfbSyntheses /* o  : number of needed CLDFB synthesis instances */
    3575             : )
    3576             : {
    3577             :     IVAS_FORMAT ivas_format;
    3578       15117 :     *numCldfbAnalyses = st_ivas->nchan_transport;
    3579       15117 :     move16();
    3580       15117 :     *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    3581       15117 :     move16();
    3582             : 
    3583       15117 :     test();
    3584       15117 :     IF( ( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_MODE_NONE ) ) )
    3585             :     {
    3586         708 :         ivas_format = SBA_FORMAT;
    3587         708 :         move32();
    3588             :     }
    3589             :     ELSE
    3590             :     {
    3591       14409 :         ivas_format = st_ivas->ivas_format;
    3592       14409 :         move32();
    3593             :     }
    3594             : 
    3595       15117 :     SWITCH( st_ivas->renderer_type )
    3596             :     {
    3597        4429 :         case RENDERER_BINAURAL_PARAMETRIC:
    3598             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    3599             :         case RENDERER_STEREO_PARAMETRIC:
    3600        4429 :             IF( EQ_16( st_ivas->nchan_transport, 1 ) )
    3601             :             {
    3602        1243 :                 *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3603        1243 :                 move16();
    3604             :             }
    3605             : 
    3606        4429 :             test();
    3607        4429 :             IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) && st_ivas->hOutSetup.separateChannelEnabled )
    3608             :             {
    3609          10 :                 *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3610          10 :                 move16();
    3611             :             }
    3612             : 
    3613        4429 :             IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
    3614             :             {
    3615           0 :                 IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    3616             :                 {
    3617           0 :                     *numCldfbAnalyses = add( *numCldfbAnalyses, st_ivas->nchan_ism );
    3618           0 :                     move16();
    3619             :                 }
    3620             :             }
    3621             : 
    3622        4429 :             IF( EQ_32( ivas_format, MASA_ISM_FORMAT ) )
    3623             :             {
    3624        1430 :                 test();
    3625        1430 :                 IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    3626             :                 {
    3627         553 :                     *numCldfbAnalyses = add( *numCldfbAnalyses, st_ivas->nchan_ism );
    3628         553 :                     move16();
    3629             :                 }
    3630         877 :                 ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
    3631             :                 {
    3632         877 :                     *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3633         877 :                     move16();
    3634             :                 }
    3635             :             }
    3636        4429 :             IF( st_ivas->hDiracDecBin[0]->useTdDecorr )
    3637             :             {
    3638        1480 :                 *numCldfbAnalyses = add( *numCldfbAnalyses, 2 );
    3639        1480 :                 move16();
    3640             :             }
    3641        4429 :             BREAK;
    3642         338 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    3643             :         case RENDERER_MONO_DOWNMIX:
    3644         338 :             test();
    3645         338 :             test();
    3646         338 :             IF( EQ_32( ivas_format, ISM_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || EQ_32( ivas_format, SBA_ISM_FORMAT ) )
    3647             :             {
    3648             :                 /* CLDFB not used in rendering */
    3649         299 :                 *numCldfbAnalyses = 0;
    3650         299 :                 move16();
    3651         299 :                 *numCldfbSyntheses = 0;
    3652         299 :                 move16();
    3653             :             }
    3654         338 :             BREAK;
    3655        3669 :         case RENDERER_DIRAC:
    3656        3669 :             IF( EQ_32( ivas_format, SBA_FORMAT ) )
    3657             :             {
    3658         186 :                 *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3659         186 :                 move16();
    3660             : 
    3661         186 :                 test();
    3662         186 :                 IF( st_ivas->hOutSetup.is_loudspeaker_setup && EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) )
    3663             :                 {
    3664         186 :                     *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    3665         186 :                     move16();
    3666             :                 }
    3667           0 :                 ELSE IF( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) )
    3668             :                 {
    3669           0 :                     *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    3670           0 :                     move16();
    3671             :                 }
    3672             :                 ELSE
    3673             :                 {
    3674           0 :                     *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    3675           0 :                     move16();
    3676             :                 }
    3677             :             }
    3678        3669 :             IF( NE_32( ivas_format, SBA_FORMAT ) )
    3679             :             {
    3680        3483 :                 test();
    3681        3483 :                 test();
    3682        3483 :                 IF( GT_16( st_ivas->nchan_transport, 2 ) && ( st_ivas->sba_planar != 0 ) )
    3683             :                 {
    3684           0 :                     *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3685           0 :                     move16();
    3686             :                 }
    3687        3483 :                 ELSE IF( EQ_16( st_ivas->nchan_transport, 1 ) && EQ_32( st_ivas->hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    3688             :                 {
    3689         383 :                     *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3690         383 :                     move16();
    3691             :                 }
    3692             :             }
    3693        3669 :             BREAK;
    3694         174 :         case RENDERER_MC_PARAMMC:
    3695         174 :             IF( LE_16( st_ivas->hDecoderConfig->nchan_out, 2 ) )
    3696             :             {
    3697             :                 /* CLDFB not used in rendering */
    3698           4 :                 *numCldfbAnalyses = 0;
    3699           4 :                 move16();
    3700           4 :                 *numCldfbSyntheses = 0;
    3701           4 :                 move16();
    3702             :             }
    3703             :             ELSE
    3704             :             {
    3705         170 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses_fx( st_ivas );
    3706         170 :                 move16();
    3707             :             }
    3708         174 :             BREAK;
    3709         107 :         case RENDERER_PARAM_ISM:
    3710             :             /* Already correct with no exception */
    3711         107 :             BREAK;
    3712        1796 :         case RENDERER_DISABLE:
    3713             :             /* CLDFB not used */
    3714        1796 :             *numCldfbAnalyses = 0;
    3715        1796 :             move16();
    3716        1796 :             *numCldfbSyntheses = 0;
    3717        1796 :             move16();
    3718        1796 :             BREAK;
    3719        3838 :         case RENDERER_MC:
    3720             :         case RENDERER_SBA_LINEAR_DEC:
    3721             :         case RENDERER_TD_PANNING:
    3722             :         case RENDERER_BINAURAL_OBJECTS_TD:
    3723             :         case RENDERER_MCMASA_MONO_STEREO:
    3724             :         case RENDERER_BINAURAL_MIXER_CONV:
    3725             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    3726             :         case RENDERER_BINAURAL_FASTCONV:
    3727             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    3728             :         case RENDERER_OSBA_STEREO:
    3729             :         case RENDERER_OSBA_AMBI:
    3730             :         case RENDERER_OSBA_LS:
    3731        3838 :             test();
    3732        3838 :             IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, SBA_ISM_FORMAT ) )
    3733             :             {
    3734        2171 :                 IF( st_ivas->sba_dirac_stereo_flag != 0 )
    3735             :                 {
    3736          72 :                     *numCldfbAnalyses = 0;
    3737          72 :                     move16();
    3738          72 :                     *numCldfbSyntheses = 0;
    3739          72 :                     move16();
    3740             :                 }
    3741             :                 ELSE
    3742             :                 {
    3743        2099 :                     *numCldfbAnalyses = st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans;
    3744        2099 :                     move16();
    3745             : 
    3746        2099 :                     test();
    3747        2099 :                     IF( st_ivas->hOutSetup.is_loudspeaker_setup && EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) )
    3748             :                     {
    3749           0 :                         *numCldfbSyntheses = st_ivas->hOutSetup.nchan_out_woLFE;
    3750           0 :                         move16();
    3751             :                     }
    3752        2099 :                     ELSE IF( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) )
    3753             :                     {
    3754         556 :                         *numCldfbSyntheses = st_ivas->hSpar->hFbMixer->fb_cfg->num_out_chans;
    3755         556 :                         move16();
    3756             :                     }
    3757             :                     ELSE
    3758             :                     {
    3759        1543 :                         *numCldfbSyntheses = MAX_OUTPUT_CHANNELS;
    3760        1543 :                         move16();
    3761             :                     }
    3762        2099 :                     test();
    3763        2099 :                     IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
    3764             :                     {
    3765           3 :                         *numCldfbAnalyses = add( st_ivas->nchan_ism, st_ivas->hSpar->hFbMixer->fb_cfg->num_in_chans );
    3766           3 :                         move16();
    3767             :                     }
    3768             :                 }
    3769             :             }
    3770        1667 :             ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    3771             :             {
    3772             :                 /* do nothing for ParamMC */
    3773             :             }
    3774             :             ELSE
    3775             :             {
    3776             :                 /* CLDFB not used in rendering */
    3777        1308 :                 *numCldfbAnalyses = 0;
    3778        1308 :                 move16();
    3779        1308 :                 *numCldfbSyntheses = 0;
    3780        1308 :                 move16();
    3781             :             }
    3782        3838 :             BREAK;
    3783         654 :         case RENDERER_SBA_LINEAR_ENC:
    3784         654 :             IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    3785             :             {
    3786         144 :                 *numCldfbSyntheses = param_mc_get_num_cldfb_syntheses_fx( st_ivas );
    3787         144 :                 move16();
    3788             :             }
    3789         510 :             ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    3790             :             {
    3791          44 :                 *numCldfbSyntheses = add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe );
    3792          44 :                 move16();
    3793             :             }
    3794         466 :             ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    3795             :             {
    3796         116 :                 *numCldfbAnalyses = st_ivas->nchan_transport;
    3797         116 :                 move16();
    3798         116 :                 *numCldfbSyntheses = add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe );
    3799         116 :                 move16();
    3800             :             }
    3801             :             ELSE
    3802             :             {
    3803             :                 /* CLDFB not used in rendering */
    3804         350 :                 *numCldfbAnalyses = 0;
    3805         350 :                 move16();
    3806         350 :                 *numCldfbSyntheses = 0;
    3807         350 :                 move16();
    3808             :             }
    3809         654 :             BREAK;
    3810          58 :         case RENDERER_OMASA_OBJECT_EXT:
    3811          58 :             *numCldfbAnalyses = st_ivas->nchan_transport;
    3812          58 :             *numCldfbSyntheses = st_ivas->hDecoderConfig->nchan_out;
    3813          58 :             move16();
    3814          58 :             move16();
    3815          58 :             BREAK;
    3816          54 :         case RENDERER_OMASA_MIX_EXT:
    3817          54 :             *numCldfbAnalyses = add( st_ivas->nchan_transport, 1 );
    3818          54 :             *numCldfbSyntheses = 0;
    3819          54 :             move16();
    3820          54 :             BREAK;
    3821           0 :         default:
    3822           0 :             assert( 0 && "Renderer not handled for CLDFB reservation." );
    3823             :     }
    3824             : 
    3825       15117 :     test();
    3826       15117 :     test();
    3827       15117 :     IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) && NE_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) )
    3828             :     {
    3829          14 :         test();
    3830          14 :         test();
    3831          14 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    3832             :         {
    3833           7 :             *numCldfbAnalyses = s_max( MC_PARAMUPMIX_MAX_INPUT_CHANS, *numCldfbAnalyses );
    3834           7 :             move16();
    3835             :         }
    3836             :         ELSE
    3837             :         {
    3838           7 :             *numCldfbAnalyses = s_max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbAnalyses );
    3839           7 :             move16();
    3840             :         }
    3841          14 :         *numCldfbSyntheses = s_max( MC_PARAMUPMIX_MIN_CLDFB, *numCldfbSyntheses );
    3842          14 :         move16();
    3843             :     }
    3844             : 
    3845       15117 :     return;
    3846             : }
    3847             : 
    3848             : 
    3849             : /*---------------------------------------------------------------------*
    3850             :  * doSanityChecks_IVAS()
    3851             :  *
    3852             :  * Sanity checks - verify if the decoder set-up parameters are
    3853             :  *                 not in conflict with the IVAS format
    3854             :  *---------------------------------------------------------------------*/
    3855             : 
    3856         611 : static ivas_error doSanityChecks_IVAS(
    3857             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    3858             : )
    3859             : {
    3860             :     Word32 output_Fs;
    3861             :     AUDIO_CONFIG output_config;
    3862             : 
    3863         611 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    3864         611 :     move32();
    3865         611 :     output_config = st_ivas->hDecoderConfig->output_config;
    3866         611 :     move32();
    3867             :     /*-----------------------------------------------------------------*
    3868             :      * Sanity checks
    3869             :      *-----------------------------------------------------------------*/
    3870             : 
    3871         611 :     IF( EQ_32( output_Fs, 8000 ) )
    3872             :     {
    3873           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz output sampling rate is not supported in IVAS." );
    3874             :     }
    3875             : 
    3876         611 :     assert( st_ivas->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
    3877         611 :     assert( st_ivas->ivas_format != MONO_FORMAT && "\n Wrong IVAS format: MONO" );
    3878             : 
    3879             :     /* Verify output configuration compatible with non-diegetic panning */
    3880         611 :     test();
    3881         611 :     test();
    3882         611 :     IF( st_ivas->hDecoderConfig->Opt_non_diegetic_pan && NE_32( st_ivas->ivas_format, MONO_FORMAT ) && NE_32( st_ivas->transport_config, IVAS_AUDIO_CONFIG_ISM1 ) )
    3883             :     {
    3884           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Error: Non-diegetic panning not supported in this IVAS format" );
    3885             :     }
    3886             : 
    3887             :     /* Verify stereo output configuration */
    3888         611 :     IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
    3889             :     {
    3890          68 :         test();
    3891          68 :         test();
    3892          68 :         test();
    3893          68 :         test();
    3894          68 :         test();
    3895          68 :         test();
    3896          68 :         test();
    3897          68 :         test();
    3898          68 :         IF( NE_32( output_config, IVAS_AUDIO_CONFIG_MONO ) && NE_32( output_config, IVAS_AUDIO_CONFIG_STEREO ) && NE_32( output_config, IVAS_AUDIO_CONFIG_5_1 ) && NE_32( output_config, IVAS_AUDIO_CONFIG_7_1 ) && NE_32( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) && NE_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) && NE_32( output_config, IVAS_AUDIO_CONFIG_7_1_4 ) && NE_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && NE_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    3899             :         {
    3900           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Wrong output configuration specified for Stereo!" );
    3901             :         }
    3902             :     }
    3903             :     /* Verify output configuration for other formats */
    3904             :     ELSE
    3905             :     {
    3906         543 :         IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_INVALID ) )
    3907             :         {
    3908           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration specified!" );
    3909             :         }
    3910             :     }
    3911             : 
    3912         611 :     IF( ( EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ) && NE_32( output_Fs, 48000 ) )
    3913             :     {
    3914           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Error: Only 48kHz output sampling rate is supported for split rendering." );
    3915             :     }
    3916             : 
    3917         611 :     IF( st_ivas->hDecoderConfig->Opt_Headrotation )
    3918             :     {
    3919          82 :         test();
    3920          82 :         test();
    3921          82 :         IF( !( EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ) )
    3922             :         {
    3923           0 :             return IVAS_ERROR( IVAS_ERR_HEAD_ROTATION_NOT_SUPPORTED, "Wrong set-up: Head-rotation not supported in this configuration" );
    3924             :         }
    3925             :     }
    3926             : 
    3927         611 :     IF( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    3928             :     {
    3929          31 :         test();
    3930          31 :         test();
    3931          31 :         IF( !( EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) ) )
    3932             :         {
    3933           0 :             return IVAS_ERROR( IVAS_ERR_EXT_ORIENTATION_NOT_SUPPORTED, "Wrong set-up: External orientation not supported in this configuration" );
    3934             :         }
    3935             :     }
    3936             : 
    3937         611 :     IF( st_ivas->hDecoderConfig->Opt_dpid_on )
    3938             :     {
    3939           2 :         test();
    3940           2 :         IF( !( EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) ) )
    3941             :         {
    3942           0 :             return IVAS_ERROR( IVAS_ERR_DIRECTIVITY_NOT_SUPPORTED, "Wrong set-up: Directivity is not supported in this output configuration." );
    3943             :         }
    3944             :     }
    3945             : 
    3946         611 :     IF( st_ivas->hDecoderConfig->Opt_aeid_on )
    3947             :     {
    3948           4 :         IF( NE_16( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    3949             :         {
    3950           0 :             return IVAS_ERROR( IVAS_ERR_ACOUSTIC_ENVIRONMENT_NOT_SUPPORTED, "Wrong set-up: Acoustic environment is not supported in this output configuration." );
    3951             :         }
    3952             :     }
    3953             : 
    3954         611 :     return IVAS_ERR_OK;
    3955             : }

Generated by: LCOV version 1.14