LCOV - code coverage report
Current view: top level - lib_dec - ivas_mct_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 0c62f5312a76f89f3e6d6ab9a8d50516c9ab4059 Lines: 418 471 88.7 %
Date: 2025-12-17 10:49:08 Functions: 6 6 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 <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "cnst.h"
      38             : #include "ivas_cnst.h"
      39             : #include "rom_com.h"
      40             : #include "prot.h"
      41             : #include "ivas_prot.h"
      42             : #include "ivas_prot_rend.h"
      43             : #include "ivas_rom_com.h"
      44             : #ifdef DEBUGGING
      45             : #include "debug.h"
      46             : #endif
      47             : #include "wmc_auto.h"
      48             : #ifdef DEBUG_PLOT
      49             : #include "deb_out.h"
      50             : #endif
      51             : 
      52             : 
      53             : /*-----------------------------------------------------------------------*
      54             :  * Local function prototypes
      55             :  *-----------------------------------------------------------------------*/
      56             : 
      57             : static ivas_error ivas_mc_dec_reconfig( Decoder_Struct *st_ivas );
      58             : 
      59             : 
      60             : /*--------------------------------------------------------------------------*
      61             :  * ivas_mct_dec()
      62             :  *
      63             :  * Multi-channel Coding Tool (MCT) decoding routine
      64             :  *--------------------------------------------------------------------------*/
      65             : 
      66     5790027 : ivas_error ivas_mct_dec(
      67             :     Decoder_Struct *st_ivas,       /* i/o: IVAS decoder structure          */
      68             :     float *output[],               /* o  : output synthesis signal         */
      69             :     const int16_t output_frame,    /* i  : output frame length per channel */
      70             :     const int16_t nb_bits_metadata /* i  : number of metadata bits         */
      71             : )
      72             : {
      73             :     int16_t n, nCPE, cpe_id;
      74             :     MCT_DEC_HANDLE hMCT;
      75             :     CPE_DEC_HANDLE hCPE;
      76             :     float *x[CPE_CHANNELS][NB_DIV];
      77             :     int16_t param[MCT_MAX_BLOCKS][CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV];
      78             :     int16_t param_lpc[MCT_MAX_BLOCKS][CPE_CHANNELS][NPRM_LPC_NEW];
      79             :     int16_t p_param[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
      80             :     int16_t nTnsBitsTCX10[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
      81             :     float Aq[MCT_MAX_BLOCKS][CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )];
      82             :     int16_t fUseTns[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
      83             :     STnsData tnsData[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
      84             :     Decoder_State **sts;
      85             :     float synth[CPE_CHANNELS][L_FRAME_PLUS];
      86             :     float *p_output_orig[2];
      87             :     float output_lfe_ch[L_FRAME48k];
      88             :     int32_t ivas_total_brate;
      89             :     ivas_error error;
      90             : 
      91     5790027 :     push_wmops( "ivas_mct_dec" );
      92             : 
      93     5790027 :     error = IVAS_ERR_OK;
      94     5790027 :     nCPE = st_ivas->nCPE;
      95     5790027 :     hMCT = st_ivas->hMCT;
      96             : 
      97     5790027 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
      98             : 
      99     5790027 :     if ( st_ivas->ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) )
     100             :     {
     101             :         /* save LFE channel */
     102     1544081 :         mvr2r( output[LFE_CHANNEL], output_lfe_ch, output_frame );
     103             :     }
     104             : 
     105     5790027 :     if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT && !st_ivas->bfi )
     106             :     {
     107             :         /* get the number of channels from the signalled MC LS setup */
     108     1385396 :         n = ivas_mc_ls_setup_get_num_channels( ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config ) );
     109             : 
     110     1385396 :         if ( n != st_ivas->nchan_transport )
     111             :         {
     112             :             /* IVAS_fmToDo: more work needed for switching the number of transport channels */
     113           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: wrong number of transport channels signalled in MC format!" );
     114             :         }
     115             :     }
     116             : 
     117    22268358 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     118             :     {
     119             :         /*initialize param_lpc buffer*/
     120    49434993 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     121             :         {
     122    32956662 :             set_s( param_lpc[cpe_id][n], 0, NPRM_LPC_NEW );
     123             :         }
     124             : 
     125    16478331 :         if ( ( error = ivas_cpe_dec( st_ivas, cpe_id, output, output_frame, 0 ) ) != IVAS_ERR_OK )
     126             :         {
     127           0 :             return error;
     128             :         }
     129             : 
     130    16478331 :         if ( cpe_id == 0 )
     131             :         {
     132     5790027 :             st_ivas->hCPE[0]->hCoreCoder[0]->total_brate = ivas_total_brate; /* set high enough to read the whole side-info; total_brate is rewritten later in ivas_mdct_core_invQ() */
     133             :         }
     134             : 
     135    16478331 :         if ( !st_ivas->bfi )
     136             :         {
     137    15737084 :             ivas_mdct_dec_side_bits_frame_channel( st_ivas->hCPE[cpe_id], param_lpc[cpe_id], p_param[cpe_id], st_ivas->hCPE[0]->hCoreCoder[0], nTnsBitsTCX10[cpe_id], param[cpe_id], 1,
     138    15737084 :                                                    ( ( cpe_id + 1 ) * CPE_CHANNELS > hMCT->nchan_out_woLFE ) );
     139             : 
     140    15737084 :             st_ivas->BER_detect |= st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect;
     141    15737084 :             st_ivas->BER_detect |= st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect;
     142             :         }
     143             :     }
     144             : 
     145             :     /* MCT side bits decoder */
     146     5790027 :     ivas_mct_side_bits( hMCT, st_ivas->hCPE, nCPE, st_ivas->hCPE[0]->hCoreCoder[0], st_ivas->bfi, st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream, ivas_total_brate, nb_bits_metadata );
     147             : 
     148             :     /* in case of switching from an SID frame (with ACELP core) to MCT, buffer of L_FRAME_PLUS samples is needed -> use synth[] as a temporary buffer */
     149     5790027 :     if ( st_ivas->hCPE[0]->hCoreCoder[0]->last_core == ACELP_CORE )
     150             :     {
     151          27 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     152             :         {
     153          18 :             p_output_orig[n] = output[n];
     154          18 :             output[n] = synth[n];
     155             :         }
     156             :     }
     157             : 
     158    22268358 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     159             :     {
     160    16478331 :         st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect |= st_ivas->BER_detect;
     161    16478331 :         st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect |= st_ivas->BER_detect;
     162             : 
     163    49434993 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     164             :         {
     165    32956662 :             x[n][0] = output[n + cpe_id * CPE_CHANNELS];
     166    32956662 :             x[n][1] = output[n + cpe_id * CPE_CHANNELS] + ( L_FRAME48k / 2 );
     167    32956662 :             set_zero( x[n][0], L_FRAME48k / 2 );
     168    32956662 :             set_zero( x[n][1], L_FRAME48k / 2 );
     169             :         }
     170             : 
     171    16478331 :         ivas_mdct_core_invQ( st_ivas->hCPE[cpe_id], nTnsBitsTCX10[cpe_id], p_param[cpe_id], param_lpc[cpe_id], param[cpe_id],
     172    16478331 :                              fUseTns[cpe_id], tnsData[cpe_id], x, x, Aq[cpe_id], NULL, 1 );
     173             : 
     174    16478331 :         st_ivas->BER_detect |= st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect;
     175    16478331 :         st_ivas->BER_detect |= st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect;
     176             :     }
     177             : 
     178             :     /* MCT core decoder */
     179     5790027 :     ivas_mct_core_dec( hMCT, st_ivas->hCPE, nCPE, output );
     180             : 
     181             :     /* for sba to stereo output disable any further processing for TCs > 2 as it is not needed*/
     182     5790027 :     if ( st_ivas->sba_dirac_stereo_flag && st_ivas->ivas_format != SBA_ISM_FORMAT )
     183             :     {
     184      169490 :         for ( cpe_id = 1; cpe_id < nCPE; cpe_id++ )
     185             :         {
     186      254235 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     187             :             {
     188      169490 :                 st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     189             :             }
     190             :         }
     191             :     }
     192             : 
     193             :     /* MCT reconstruction and CoreCoder updates */
     194    22268358 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     195             :     {
     196    16478331 :         hCPE = st_ivas->hCPE[cpe_id];
     197             : 
     198    49434993 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     199             :         {
     200    32956662 :             x[n][0] = output[n + cpe_id * CPE_CHANNELS];
     201    32956662 :             x[n][1] = output[n + cpe_id * CPE_CHANNELS] + ( L_FRAME48k / 2 );
     202             :         }
     203             : 
     204    16478331 :         ivas_mdct_core_tns_ns( hCPE, fUseTns[cpe_id], tnsData[cpe_id], x, Aq[cpe_id], 1 );
     205             :     }
     206             : 
     207     5790027 :     if ( st_ivas->renderer_type == RENDERER_MC )
     208             :     {
     209             :         /* Equalization in MDCT Domain */
     210      249323 :         ivas_ls_setup_conversion_process_mdct( st_ivas, output );
     211             :     }
     212             : 
     213     5540704 :     else if ( st_ivas->renderer_type == RENDERER_MC_PARAMMC && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) )
     214             :     {
     215             :         float *x_all[MAX_LS_CHANNELS][NB_DIV];
     216             : 
     217       17214 :         for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     218             :         {
     219       34428 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     220             :             {
     221       22952 :                 x_all[n + cpe_id * CPE_CHANNELS][0] = output[n + cpe_id * CPE_CHANNELS];
     222       22952 :                 x_all[n + cpe_id * CPE_CHANNELS][1] = output[n + cpe_id * CPE_CHANNELS] + ( L_FRAME48k / 2 );
     223             :             }
     224             :         }
     225             : 
     226        5738 :         ivas_ls_setup_conversion_process_mdct_param_mc( st_ivas, x_all );
     227             :     }
     228             : 
     229    22268358 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     230             :     {
     231    16478331 :         hCPE = st_ivas->hCPE[cpe_id];
     232    16478331 :         sts = hCPE->hCoreCoder;
     233             : 
     234    49434993 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     235             :         {
     236    32956662 :             x[n][0] = output[n + cpe_id * CPE_CHANNELS];
     237    32956662 :             x[n][1] = output[n + cpe_id * CPE_CHANNELS] + ( L_FRAME48k / 2 );
     238             :         }
     239             : 
     240    16478331 :         ivas_mdct_core_reconstruct( hCPE, x, synth, fUseTns[cpe_id], 1 );
     241             : 
     242             :         /* set pointers back */
     243    16478331 :         if ( cpe_id == 0 && st_ivas->hCPE[0]->hCoreCoder[0]->last_core == ACELP_CORE )
     244             :         {
     245          27 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     246             :             {
     247          18 :                 output[n] = p_output_orig[n];
     248             :             }
     249             :         }
     250             : 
     251             :         /*----------------------------------------------------------------*
     252             :          * CoreCoder Post-processing and updates
     253             :          *----------------------------------------------------------------*/
     254             : 
     255    49434993 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     256             :         {
     257    32956662 :             if ( st_ivas->sba_dirac_stereo_flag && ( st_ivas->ivas_format != SBA_ISM_FORMAT || cpe_id >= nCPE - 2 ) )
     258             :             {
     259      812320 :                 ivas_post_proc( NULL, hCPE, n, synth[n], NULL, output_frame, 1 );
     260             :             }
     261             : 
     262             :             /* Postprocessing for ACELP/MDCT core switching and synchronization */
     263    32956662 :             if ( ( error = core_switching_post_dec( sts[n], synth[n], output[cpe_id * CPE_CHANNELS + n], hCPE->output_mem[1], 0, output_frame, 0 /*core_switching_flag*/, ( st_ivas->ivas_format != SBA_ISM_FORMAT || cpe_id >= nCPE - 2 ) ? st_ivas->sba_dirac_stereo_flag : 0, -1, hCPE->last_element_mode ) ) != IVAS_ERR_OK )
     264             :             {
     265           0 :                 return error;
     266             :             }
     267             : 
     268             :             /* final output of synthesis signal */
     269    32956662 :             mvr2r( synth[n], output[cpe_id * CPE_CHANNELS + n], output_frame );
     270             : 
     271             :             /* Save synthesis for HQ FEC */
     272    32956662 :             save_synthesis_hq_fec( sts[n], output[cpe_id * CPE_CHANNELS + n], output_frame, hCPE );
     273             : 
     274             :             /* CoreCoder common updates */
     275    32956662 :             updt_dec_common( sts[n], NORMAL_HQ_CORE, -1, output[cpe_id * CPE_CHANNELS + n] );
     276             : 
     277             : #ifdef DEBUG_PLOT
     278             :             sendDebout( "mct_synth", output_frame, 1, "aftPostPro", MTV_FLOAT, output[cpe_id * CPE_CHANNELS + n] );
     279             : #endif
     280             :         } /* n_channels loop */
     281             : 
     282             :         /* synthesis synchronization between stereo modes */
     283    16478331 :         if ( !st_ivas->sba_dirac_stereo_flag || ( st_ivas->ivas_format == SBA_ISM_FORMAT && cpe_id < nCPE - 2 ) )
     284             :         {
     285    16072171 :             synchro_synthesis( ivas_total_brate, hCPE, output + cpe_id * CPE_CHANNELS, output_frame, 0 );
     286             :         }
     287             : 
     288             : #ifdef DEBUG_PLOT
     289             :         for ( n = 0; n < CPE_CHANNELS; n++ )
     290             :         {
     291             :             setDeboutVars( -1, -1, n, cpe_id );
     292             :             sendDebout( "mct_synth", output_frame, 1, "aftSynchro", MTV_FLOAT, output[cpe_id * CPE_CHANNELS + n] );
     293             :         }
     294             : #endif
     295             :     }
     296             : 
     297             :     /* move channels after LFE to correct output for multi-channel MCT */
     298     5790027 :     if ( st_ivas->ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) )
     299             :     {
     300             :         float tmp[L_FRAME48k];
     301             : 
     302             :         /*save center channel output*/
     303     1544081 :         mvr2r( output[hMCT->nchan_out_woLFE - 1], tmp, output_frame );
     304             : 
     305     7503875 :         for ( n = hMCT->nchan_out_woLFE - 1; n >= LFE_CHANNEL; n-- )
     306             :         {
     307     5959794 :             mvr2r( output[n - 1], output[n + 1], output_frame );
     308             :         }
     309     1544081 :         mvr2r( tmp, output[LFE_CHANNEL - 1], output_frame );
     310             : 
     311             :         /* save LFE channel */
     312     1544081 :         mvr2r( output_lfe_ch, output[LFE_CHANNEL], output_frame );
     313             :     }
     314             : 
     315             : #ifdef DEBUG_MODE_INFO
     316             :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     317             :     {
     318             :         float tmpF = st_ivas->hCPE[cpe_id]->element_brate / 1000.0f;
     319             :         dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "element_brate", 0, cpe_id, DEC ) );
     320             : 
     321             :         dbgwrite( output[0], sizeof( float ), output_frame, 1, fname( debug_dir, "output.mct", 0, cpe_id, DEC ) );
     322             :         tmpF = 0;
     323             :         dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.sce", 0, cpe_id, DEC ) );
     324             :         dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.cpe", 0, cpe_id, DEC ) );
     325             :     }
     326             : #endif
     327             : 
     328     5790027 :     pop_wmops();
     329     5790027 :     return error;
     330             : }
     331             : 
     332             : 
     333             : /*-------------------------------------------------------------------------
     334             :  * create_mct_dec()
     335             :  *
     336             :  * Create, allocate and initialize IVAS decoder MCT handle
     337             :  *-------------------------------------------------------------------------*/
     338             : 
     339       58181 : ivas_error create_mct_dec(
     340             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
     341             : )
     342             : {
     343             :     MCT_DEC_HANDLE hMCT;
     344             :     int16_t n;
     345             :     int32_t cp_bitrate;
     346             :     int16_t max_blocks;
     347             :     int16_t cpe_id;
     348             : 
     349             :     /*---------------------------------------------------------   --------*
     350             :      * Allocate MCT handle
     351             :      *-----------------------------------------------------------------*/
     352             : 
     353       58181 :     if ( ( hMCT = (MCT_DEC_HANDLE) malloc( sizeof( MCT_DEC_DATA ) ) ) == NULL )
     354             :     {
     355           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
     356             :     }
     357             : 
     358             :     /*-----------------------------------------------------------------*
     359             :      * Allocate MCT BlockData handles
     360             :      *-----------------------------------------------------------------*/
     361             : 
     362             :     /* Determine active channels */
     363       58181 :     if ( ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) || st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
     364             :     {
     365       38761 :         hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
     366       38761 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     367             :         {
     368       19028 :             hMCT->nchan_out_woLFE += st_ivas->nchan_ism;
     369             :         }
     370             :     }
     371       19420 :     else if ( st_ivas->mc_mode == MC_MODE_MCT )
     372             :     {
     373       17767 :         hMCT->nchan_out_woLFE = st_ivas->nchan_transport - st_ivas->hTransSetup.num_lfe;
     374             :     }
     375        1653 :     else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     376             :     {
     377        1653 :         hMCT->nchan_out_woLFE = st_ivas->nchan_transport - st_ivas->hTransSetup.num_lfe;
     378             :     }
     379             :     else
     380             :     {
     381           0 :         assert( !"IVAS format currently not supported for MCT" );
     382             :     }
     383             : 
     384       58181 :     cp_bitrate = st_ivas->hDecoderConfig->ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
     385             : 
     386       58181 :     if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     387             :     {
     388       19028 :         cp_bitrate = st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
     389             :     }
     390             : 
     391             :     /* indicate LFE for appropriate core-coder channel */
     392      238547 :     for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     393             :     {
     394      541098 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     395             :         {
     396      360732 :             st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
     397             :         }
     398             :     }
     399             : 
     400             :     /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
     401       58181 :     if ( hMCT->nchan_out_woLFE % 2 )
     402             :     {
     403       42581 :         st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     404             :     }
     405             : 
     406             :     /*Initialize MCT block data */
     407       58181 :     max_blocks = hMCT->nchan_out_woLFE / 2;
     408             : 
     409      195966 :     for ( n = 0; n < max_blocks; n++ )
     410             :     {
     411      137785 :         if ( ( hMCT->hBlockData[n] = (MCT_DEC_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_DEC_BLOCK_DATA ) ) ) == NULL )
     412             :         {
     413           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
     414             :         }
     415             : 
     416             :         /*Initialize all parameters to zero*/
     417      137785 :         hMCT->hBlockData[n]->ch1 = 0;
     418      137785 :         hMCT->hBlockData[n]->ch2 = 0;
     419             : 
     420             :         /*-----------------------------------------------------------------*
     421             :          * MDCT stereo initialization
     422             :          *-----------------------------------------------------------------*/
     423             : 
     424      137785 :         if ( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
     425             :         {
     426           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
     427             :         }
     428             : 
     429      137785 :         initMdctStereoDecData( hMCT->hBlockData[n]->hStereoMdct, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->hIGFDec->igfData.igfInfo.grid, cp_bitrate, SWB );
     430      137785 :         hMCT->hBlockData[n]->hStereoMdct->use_itd = 0;
     431      137785 :         hMCT->hBlockData[n]->hStereoMdct->reverse_dmx = 0;
     432      137785 :         hMCT->hBlockData[n]->hStereoMdct->smooth_ratio = 1.f;
     433             :     }
     434      269482 :     for ( ; n < MCT_MAX_BLOCKS; n++ )
     435             :     {
     436      211301 :         hMCT->hBlockData[n] = NULL;
     437             :     }
     438             : 
     439             :     /*-----------------------------------------------------------------*
     440             :      * Initializations
     441             :      *-----------------------------------------------------------------*/
     442             : 
     443       58181 :     hMCT->currBlockDataCnt = 0;
     444             : 
     445             :     /*Initialize bits required to signal channel-pair index*/
     446       58181 :     hMCT->bitsChannelPairIndex = max( 1, (int16_t) ( floorf( ( logf( (float) hMCT->nchan_out_woLFE * ( hMCT->nchan_out_woLFE - 1 ) / 2 - 1 ) * INV_LOG_2 ) ) + 1 ) );
     447             : 
     448       58181 :     set_s( hMCT->chBitRatios, 0, MCT_MAX_CHANNELS );
     449       58181 :     set_s( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
     450             : 
     451       58181 :     st_ivas->hMCT = hMCT;
     452             : 
     453       58181 :     return IVAS_ERR_OK;
     454             : }
     455             : 
     456             : 
     457             : /*-------------------------------------------------------------------------
     458             :  * mct_dec_reconfigure()
     459             :  *
     460             :  * Reconfigure IVAS decoder MCT handle
     461             :  *-------------------------------------------------------------------------*/
     462             : 
     463       25372 : ivas_error mct_dec_reconfigure(
     464             :     Decoder_Struct *st_ivas,      /* i/o: IVAS decoder structure                  */
     465             :     const uint16_t b_nchan_change /* i  : flag indicating different channel count */
     466             : )
     467             : {
     468             :     MCT_DEC_HANDLE hMCT;
     469             :     Decoder_State *st;
     470             :     int16_t n, cpe_id, max_blocks;
     471             :     int32_t cp_bitrate;
     472             : 
     473       25372 :     hMCT = st_ivas->hMCT;
     474             : 
     475             :     /*-----------------------------------------------------------------*
     476             :      * Allocate and initialize MCT BlockData handles
     477             :      *-----------------------------------------------------------------*/
     478             : 
     479       25372 :     if ( b_nchan_change )
     480             :     {
     481             :         /* Determine active channels */
     482       10602 :         if ( ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) || st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
     483             :         {
     484        8293 :             hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
     485        8293 :             if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     486             :             {
     487        2669 :                 hMCT->nchan_out_woLFE += st_ivas->nchan_ism;
     488             :             }
     489             :         }
     490        2309 :         else if ( st_ivas->mc_mode == MC_MODE_MCT )
     491             :         {
     492        1756 :             hMCT->nchan_out_woLFE = st_ivas->nchan_transport - st_ivas->hTransSetup.num_lfe;
     493             :         }
     494         553 :         else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     495             :         {
     496         553 :             hMCT->nchan_out_woLFE = st_ivas->nchan_transport - st_ivas->hTransSetup.num_lfe;
     497             :         }
     498             :         else
     499             :         {
     500           0 :             assert( !"IVAS format currently not supported for MCT" );
     501             :         }
     502             :     }
     503             : 
     504             :     /* indicate LFE for appropriate core-coder channel */
     505      103809 :     for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     506             :     {
     507      235311 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     508             :         {
     509      156874 :             st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
     510             :         }
     511             :     }
     512             : 
     513             :     /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
     514       25372 :     if ( hMCT->nchan_out_woLFE % 2 )
     515             :     {
     516       19318 :         st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     517             :     }
     518             : 
     519       25372 :     cp_bitrate = st_ivas->hDecoderConfig->ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
     520       25372 :     if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     521             :     {
     522        6086 :         cp_bitrate = st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
     523             :     }
     524             : 
     525             :     /* set correct nominal bitrates and igf config already here, otherwise we
     526             :      * run into a number of problems */
     527      103809 :     for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
     528             :     {
     529       78437 :         st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
     530      235311 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     531             :         {
     532      156874 :             st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
     533             : 
     534      156874 :             st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
     535             : 
     536      156874 :             if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
     537             :             {
     538      137556 :                 st->bits_frame_nominal = (int16_t) ( st_ivas->hCPE[cpe_id]->element_brate / FRAMES_PER_SEC );
     539      137556 :                 st->igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->rf_flag );
     540      137556 :                 if ( st->igf )
     541             :                 {
     542       78533 :                     IGFDecSetMode( st->hIGFDec, st_ivas->hCPE[cpe_id]->element_brate, st->bwidth, st->element_mode, -1, -1, st->rf_flag );
     543             :                 }
     544             :             }
     545             :         }
     546             :     }
     547             : 
     548             :     /*Initialize MCT block data */
     549       25372 :     max_blocks = hMCT->nchan_out_woLFE / CPE_CHANNELS;
     550             : 
     551       84491 :     for ( n = 0; n < max_blocks; n++ )
     552             :     {
     553       59119 :         if ( b_nchan_change )
     554             :         {
     555       25102 :             if ( hMCT->hBlockData[n] == NULL )
     556             :             {
     557        9027 :                 if ( ( hMCT->hBlockData[n] = (MCT_DEC_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
     558             :                 {
     559           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
     560             :                 }
     561             : 
     562             :                 /*Initialize all parameters to zero*/
     563        9027 :                 hMCT->hBlockData[n]->ch1 = 0;
     564        9027 :                 hMCT->hBlockData[n]->ch2 = 0;
     565             : 
     566             :                 /* MDCT stereo initialization */
     567             : #ifdef FIX_2287_MCT_MDCT_STEREO_DATA_MALLOC_SIZE
     568        9027 :                 if ( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
     569             : #else
     570             :                 if ( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
     571             : #endif
     572             :                 {
     573           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
     574             :                 }
     575             :             }
     576             :         }
     577             : 
     578       59119 :         initMdctStereoDecData( hMCT->hBlockData[n]->hStereoMdct, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->hIGFDec->igfData.igfInfo.grid, cp_bitrate, st_ivas->hCPE[0]->hCoreCoder[0]->bwidth );
     579       59119 :         hMCT->hBlockData[n]->hStereoMdct->use_itd = 0;
     580             :     }
     581             : 
     582      118485 :     for ( ; n < MCT_MAX_BLOCKS; n++ )
     583             :     {
     584             :         /* deallocate no longer needed blocks */
     585       93113 :         if ( hMCT->hBlockData[n] != NULL )
     586             :         {
     587        8295 :             if ( hMCT->hBlockData[n]->hStereoMdct != NULL )
     588             :             {
     589        8295 :                 free( hMCT->hBlockData[n]->hStereoMdct );
     590        8295 :                 hMCT->hBlockData[n]->hStereoMdct = NULL;
     591             :             }
     592             : 
     593        8295 :             free( hMCT->hBlockData[n] );
     594        8295 :             hMCT->hBlockData[n] = NULL;
     595             :         }
     596             :     }
     597             : 
     598             :     /*-----------------------------------------------------------------*
     599             :      * Initializations
     600             :      *-----------------------------------------------------------------*/
     601             : 
     602       25372 :     if ( b_nchan_change )
     603             :     {
     604       10602 :         hMCT->currBlockDataCnt = 0;
     605             : 
     606             :         /*Initialize bits required to signal channel-pair index*/
     607       10602 :         hMCT->bitsChannelPairIndex = max( 1, (int16_t) ( floorf( ( logf( (float) hMCT->nchan_out_woLFE * ( hMCT->nchan_out_woLFE - 1 ) / 2 - 1 ) * INV_LOG_2 ) ) + 1 ) );
     608             : 
     609       10602 :         set_s( hMCT->chBitRatios, 0, MCT_MAX_CHANNELS );
     610       10602 :         set_s( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
     611             :     }
     612             : 
     613       25372 :     return IVAS_ERR_OK;
     614             : }
     615             : 
     616             : 
     617             : /*-------------------------------------------------------------------------
     618             :  * create_mct_dec_close()
     619             :  *
     620             :  * Close IVAS decoder MCT handle
     621             :  *-------------------------------------------------------------------------*/
     622             : 
     623      124483 : void ivas_mct_dec_close(
     624             :     MCT_DEC_HANDLE *hMCT /* i/o: MCT decoder structure       */
     625             : )
     626             : {
     627             :     int16_t n, maxBlocks;
     628             : 
     629      124483 :     if ( hMCT == NULL || *hMCT == NULL )
     630             :     {
     631       66302 :         return;
     632             :     }
     633             : 
     634       58181 :     maxBlocks = ( *hMCT )->nchan_out_woLFE / 2;
     635             : 
     636      196698 :     for ( n = 0; n < maxBlocks; n++ )
     637             :     {
     638      138517 :         if ( ( *hMCT )->hBlockData[n] != NULL )
     639             :         {
     640      138517 :             if ( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
     641             :             {
     642      138517 :                 free( ( *hMCT )->hBlockData[n]->hStereoMdct );
     643      138517 :                 ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
     644             :             }
     645             : 
     646      138517 :             free( ( *hMCT )->hBlockData[n] );
     647      138517 :             ( *hMCT )->hBlockData[n] = NULL;
     648             :         }
     649             :     }
     650             : 
     651       58181 :     free( *hMCT );
     652       58181 :     *hMCT = NULL;
     653             : 
     654       58181 :     return;
     655             : }
     656             : 
     657             : 
     658             : /*-------------------------------------------------------------------------
     659             :  * ivas_mc_dec_config()
     660             :  *
     661             :  * - read transported MC LS setup
     662             :  * - select MC format mode
     663             :  * - reconfigure the MC format decoder
     664             :  *-------------------------------------------------------------------------*/
     665             : 
     666             : /*! r : MC format mode */
     667     2610045 : ivas_error ivas_mc_dec_config(
     668             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure                */
     669             :     const int16_t idx        /* i  : LS config. index                      */
     670             : )
     671             : {
     672             :     AUDIO_CONFIG signaled_config;
     673             :     MC_MODE last_mc_mode;
     674             :     ivas_error error;
     675             : 
     676             :     /* store last frame MC mode */
     677     2610045 :     last_mc_mode = st_ivas->mc_mode;
     678             : 
     679     2610045 :     if ( !st_ivas->bfi )
     680             :     {
     681             :         /* set transported MC LS setup */
     682     2610045 :         signaled_config = ivas_mc_map_ls_setup_to_output_config( idx );
     683             : 
     684     2610045 :         if ( st_ivas->ini_frame == 0 )
     685             :         {
     686        8030 :             st_ivas->transport_config = signaled_config;
     687             :         }
     688     2602015 :         else if ( st_ivas->transport_config != signaled_config )
     689             :         {
     690             : #ifdef DEBUGGING
     691             :             fprintf( stderr, "\nError: Switching of MC configurations is not supported!\n" );
     692             : #endif
     693           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong MC configuration signalled!" );
     694             :         }
     695             : 
     696             :         /* select MC format mode */
     697     2610045 :         st_ivas->mc_mode = ivas_mc_mode_select( ivas_mc_map_output_config_to_mc_ls_setup( signaled_config ), st_ivas->hDecoderConfig->ivas_total_brate );
     698             : 
     699             :         /* MC format switching */
     700     2610045 :         if ( st_ivas->ini_frame != 0 )
     701             :         {
     702     2602015 :             if ( st_ivas->hDecoderConfig->last_ivas_total_brate != st_ivas->hDecoderConfig->ivas_total_brate || last_mc_mode != st_ivas->mc_mode )
     703             :             {
     704       60932 :                 if ( ( error = ivas_mc_dec_reconfig( st_ivas ) ) != IVAS_ERR_OK )
     705             :                 {
     706           0 :                     return error;
     707             :                 }
     708             :             }
     709             :         }
     710             : 
     711     2610045 :         st_ivas->transport_config = signaled_config;
     712             :     }
     713             : 
     714     2610045 :     return IVAS_ERR_OK;
     715             : }
     716             : 
     717             : 
     718             : /*-------------------------------------------------------------------------
     719             :  * ivas_mc_dec_reconfig()
     720             :  *
     721             :  * reconfigure the MC format decoder
     722             :  *-------------------------------------------------------------------------*/
     723             : 
     724       60932 : static ivas_error ivas_mc_dec_reconfig(
     725             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
     726             : )
     727             : {
     728             :     int16_t nchan_transport_old, nSCE_old, nCPE_old, sba_dirac_stereo_flag_old, nchan_hp20_old;
     729             :     int16_t numCldfbAnalyses_old, numCldfbSyntheses_old;
     730             :     int32_t new_brate_SCE, new_brate_CPE, ivas_total_brate;
     731             :     RENDERER_TYPE renderer_type_old;
     732             :     Decoder_State *st;
     733             :     ivas_error error;
     734             :     MC_MODE mc_mode, last_mc_mode;
     735             :     TC_BUFFER_MODE tc_buffer_mode_new;
     736             :     int16_t tc_nchan_tc_new;
     737             :     int16_t tc_nchan_allocate_new;
     738             :     int16_t tc_granularity_new;
     739             :     int16_t nchan_out_buff;
     740             : 
     741       60932 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     742       60932 :     nchan_transport_old = st_ivas->nchan_transport;
     743       60932 :     last_mc_mode = ivas_mc_mode_select( ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config ), st_ivas->hDecoderConfig->last_ivas_total_brate ); /* NB: this assumes that LS config remains the same between frames */
     744             : 
     745             :     /* temporally set the current mc_mode back to the previous one to make sure the following call to
     746             :        ivas_init_dec_get_num_cldfb_instances() returns the correct counts */
     747       60932 :     mc_mode = st_ivas->mc_mode;
     748       60932 :     st_ivas->mc_mode = last_mc_mode;
     749       60932 :     ivas_init_dec_get_num_cldfb_instances( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
     750       60932 :     st_ivas->mc_mode = mc_mode;
     751             : 
     752       60932 :     nSCE_old = st_ivas->nSCE;
     753       60932 :     nCPE_old = st_ivas->nCPE;
     754       60932 :     sba_dirac_stereo_flag_old = st_ivas->sba_dirac_stereo_flag;
     755             : 
     756             :     /* special handling needed for the hp20 buffers for McMASA */
     757       60932 :     if ( last_mc_mode == MC_MODE_MCMASA )
     758             :     {
     759       20989 :         nchan_hp20_old = getNumChanSynthesis( st_ivas );
     760             :     }
     761             :     else
     762             :     {
     763       39943 :         nchan_hp20_old = nchan_transport_old;
     764             :     }
     765       60932 :     st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
     766             : 
     767             :     /* renderer might have changed, reselect */
     768       60932 :     renderer_type_old = st_ivas->renderer_type;
     769       60932 :     ivas_renderer_select( st_ivas );
     770             : 
     771             :     /* side effect of the renderer selection can be a changed internal config */
     772       60932 :     ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
     773             : 
     774             :     /* transfer subframe info from DirAC or ParamMC to central tc buffer */
     775       60932 :     if ( st_ivas->hTcBuffer->tc_buffer_mode != TC_BUFFER_MODE_BUFFER )
     776             :     {
     777       54328 :         if ( last_mc_mode == MC_MODE_PARAMMC )
     778             :         {
     779       14358 :             st_ivas->hTcBuffer->nb_subframes = st_ivas->hParamMC->nb_subframes;
     780       14358 :             st_ivas->hTcBuffer->subframes_rendered = st_ivas->hParamMC->subframes_rendered;
     781       14358 :             st_ivas->hTcBuffer->num_slots = st_ivas->hParamMC->num_slots;
     782       14358 :             st_ivas->hTcBuffer->slots_rendered = st_ivas->hParamMC->slots_rendered;
     783       14358 :             mvs2s( st_ivas->hParamMC->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
     784             :         }
     785       39970 :         else if ( last_mc_mode == MC_MODE_MCMASA && st_ivas->hSpatParamRendCom != NULL )
     786             :         {
     787       19618 :             st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
     788       19618 :             st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
     789       19618 :             st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
     790       19618 :             st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
     791       19618 :             mvs2s( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
     792             :         }
     793             :     }
     794             : 
     795             :     /* JBM: when granularity goes down (e.g. MCT with CREND -> ParamMC with binaural fastconv
     796             :            render what still fits in the new granularity */
     797       60932 :     tc_granularity_new = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, st_ivas->hDecoderConfig->output_Fs );
     798             : 
     799       60932 :     if ( tc_granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
     800             :     {
     801             :         /* flush already done in IVAS_DEC_ReadFormat() */
     802             :     }
     803             :     /* JBM: when granularity goes up set samples to discard at the beginning of the frame */
     804       52967 :     else if ( tc_granularity_new > st_ivas->hTcBuffer->n_samples_granularity )
     805             :     {
     806        8008 :         if ( ( error = ivas_jbm_dec_set_discard_samples( st_ivas ) ) != IVAS_ERR_OK )
     807             :         {
     808           0 :             return error;
     809             :         }
     810             :     }
     811             : 
     812       60932 :     if ( st_ivas->mc_mode == MC_MODE_MCT )
     813             :     {
     814       23444 :         st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config ) );
     815       23444 :         st_ivas->nSCE = 0;
     816       23444 :         st_ivas->nCPE = st_ivas->nchan_transport / 2;
     817             : 
     818       23444 :         if ( last_mc_mode != MC_MODE_MCT )
     819             :         {
     820             :             /*De-allocate handles for other MC modes*/
     821       16024 :             if ( st_ivas->hParamMC != NULL )
     822             :             {
     823        7266 :                 ivas_param_mc_dec_close( &st_ivas->hParamMC );
     824             : 
     825             :                 /* remove ls conversion if it was allocated by ParamMC */
     826        7266 :                 ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
     827             :             }
     828             : 
     829       16024 :             if ( last_mc_mode == MC_MODE_PARAMUPMIX )
     830             :             {
     831         501 :                 ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
     832         501 :                 ivas_ls_setup_conversion_close( &( st_ivas->hLsSetUpConversion ) );
     833             :             }
     834             : 
     835             :             /* De-allocate McMasa-related handles */
     836       16024 :             ivas_masa_dec_close( &( st_ivas->hMasa ) );
     837       16024 :             ivas_qmetadata_close( &st_ivas->hQMetaData );
     838             : 
     839       16024 :             ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
     840       16024 :             ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
     841       16024 :             ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
     842       16024 :             vbap_free_data( &( st_ivas->hVBAPdata ) );
     843             : 
     844             :             /* init LS conversion if the renderer type asks for it */
     845       16024 :             if ( st_ivas->renderer_type == RENDERER_MC && st_ivas->hLsSetUpConversion == NULL )
     846             :             {
     847        2294 :                 if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
     848             :                 {
     849           0 :                     return error;
     850             :                 }
     851             :             }
     852             :         }
     853             :     }
     854       37488 :     else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     855             :     {
     856        1975 :         st_ivas->nSCE = 0;
     857        1975 :         st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
     858        1975 :         st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
     859             : 
     860        1975 :         if ( last_mc_mode != MC_MODE_PARAMUPMIX )
     861             :         {
     862             :             /*De-allocate handles for other MC modes*/
     863        1975 :             if ( st_ivas->hParamMC != NULL )
     864             :             {
     865          70 :                 ivas_param_mc_dec_close( &st_ivas->hParamMC );
     866             : 
     867             :                 /* remove ls conversion if it was allocated by ParamMC */
     868          70 :                 ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
     869             :             }
     870             : 
     871        1975 :             ivas_masa_dec_close( &( st_ivas->hMasa ) );
     872        1975 :             ivas_qmetadata_close( &st_ivas->hQMetaData );
     873             : 
     874             :             /* init LS conversion if the renderer type asks for it */
     875        1975 :             if ( ( st_ivas->renderer_type == RENDERER_MC ) && st_ivas->hLsSetUpConversion == NULL )
     876             :             {
     877         208 :                 if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
     878             :                 {
     879           0 :                     return error;
     880             :                 }
     881             :             }
     882             : 
     883        1975 :             if ( ( error = ivas_mc_paramupmix_dec_open( st_ivas ) ) != IVAS_ERR_OK )
     884             :             {
     885           0 :                 return error;
     886             :             }
     887             :         }
     888             : #ifdef DEBUGGING
     889             :         else
     890             :         {
     891             :             assert( 0 );
     892             :         }
     893             : #endif
     894             :     }
     895       35513 :     else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     896             :     {
     897       14625 :         if ( last_mc_mode != MC_MODE_PARAMMC )
     898             :         {
     899             :             /* remove old ls conversion for MCT if open, gets reopened correctly within ivas_param_mc_dec_open when needed */
     900       11822 :             if ( renderer_type_old == RENDERER_MC && st_ivas->hLsSetUpConversion != NULL )
     901             :             {
     902        1132 :                 ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
     903             :             }
     904             : 
     905       11822 :             if ( ( error = ivas_param_mc_dec_open( st_ivas ) ) != IVAS_ERR_OK )
     906             :             {
     907           0 :                 return error;
     908             :             }
     909             :         }
     910             :         else
     911             :         {
     912        2803 :             if ( ( error = ivas_param_mc_dec_reconfig( st_ivas ) ) != IVAS_ERR_OK )
     913             :             {
     914           0 :                 return error;
     915             :             }
     916             :         }
     917             : 
     918             :         /* De-allocate McMasa-related handles */
     919       14625 :         ivas_masa_dec_close( &( st_ivas->hMasa ) );
     920       14625 :         ivas_qmetadata_close( &st_ivas->hQMetaData );
     921             : 
     922       14625 :         ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
     923       14625 :         ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
     924       14625 :         ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
     925             : 
     926       14625 :         vbap_free_data( &( st_ivas->hVBAPdata ) );
     927             : 
     928       14625 :         if ( last_mc_mode == MC_MODE_MCT )
     929             :         {
     930        7124 :             if ( st_ivas->hMCT != NULL && st_ivas->nchan_transport <= CPE_CHANNELS )
     931             :             {
     932        5970 :                 ivas_mct_dec_close( &st_ivas->hMCT );
     933             :             }
     934             :         }
     935        7501 :         else if ( last_mc_mode == MC_MODE_PARAMUPMIX )
     936             :         {
     937          48 :             ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
     938             :         }
     939             : 
     940             :         /* LFE handle */
     941       14625 :         ivas_lfe_dec_close( &( st_ivas->hLFE ) );
     942             :     }
     943       20888 :     else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     944             :     {
     945       20888 :         ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
     946             : 
     947       20888 :         if ( last_mc_mode != MC_MODE_MCMASA )
     948             :         {
     949       14228 :             if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
     950             :             {
     951           0 :                 return error;
     952             :             }
     953             :         }
     954             : 
     955       20888 :         if ( ( error = ivas_mcmasa_dec_reconfig( st_ivas ) ) != IVAS_ERR_OK )
     956             :         {
     957           0 :             return error;
     958             :         }
     959             : 
     960             :         /* LS conversion */
     961       20888 :         if ( st_ivas->hLsSetUpConversion != NULL )
     962             :         {
     963        1940 :             ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
     964             :         }
     965             : 
     966       20888 :         ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
     967             : 
     968       20888 :         if ( st_ivas->hParamMC != NULL )
     969             :         {
     970        4473 :             ivas_param_mc_dec_close( &st_ivas->hParamMC );
     971        4473 :             st_ivas->hParamMC = NULL;
     972             :         }
     973             : 
     974       20888 :         if ( last_mc_mode == MC_MODE_MCT )
     975             :         {
     976        8345 :             ivas_mct_dec_close( &st_ivas->hMCT );
     977             :         }
     978             : 
     979             :         /* LFE handle */
     980       20888 :         ivas_lfe_dec_close( &( st_ivas->hLFE ) );
     981             :     }
     982             : 
     983       60932 :     if ( st_ivas->mc_mode != MC_MODE_MCMASA )
     984             :     {
     985       40044 :         if ( st_ivas->nchan_transport == 1 )
     986             :         {
     987           0 :             st_ivas->element_mode_init = IVAS_SCE;
     988             :         }
     989             :         else
     990             :         {
     991       40044 :             st_ivas->element_mode_init = IVAS_CPE_MDCT;
     992             :         }
     993             :     }
     994             : 
     995             :     /*-----------------------------------------------------------------*
     996             :      * Reconfigure core coder
     997             :      *-----------------------------------------------------------------*/
     998             : 
     999             :     /* special case: MCT->ParamMC with more than 2 TC, CPE 1 stays, but has the wrong mct_chan_mode in channel 1
    1000             :        and might have IGF static memory not allocated and the bit stream index list not set,
    1001             :        set correct mct_chan_mode and init missing static mem (IGF, HQ)  and some config (TNS)  do it here since it is _very_ MC specific */
    1002       60932 :     if ( last_mc_mode == MC_MODE_MCT && st_ivas->mc_mode == MC_MODE_PARAMMC && st_ivas->nchan_transport > CPE_CHANNELS )
    1003             :     {
    1004        1154 :         st = st_ivas->hCPE[1]->hCoreCoder[1];
    1005             : 
    1006        1154 :         if ( st_ivas->nchan_transport == 3 )
    1007             :         {
    1008        1154 :             st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
    1009             :         }
    1010             :         else
    1011             :         {
    1012           0 :             st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
    1013             :         }
    1014             : 
    1015        1154 :         if ( st->hIGFDec == NULL )
    1016             :         {
    1017           0 :             if ( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
    1018             :             {
    1019           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for IGF\n" ) );
    1020             :             }
    1021             : 
    1022           0 :             st->igf = 0;
    1023           0 :             init_igf_dec( st->hIGFDec );
    1024             :         }
    1025             : 
    1026        1154 :         if ( st->hHQ_core == NULL )
    1027             :         {
    1028           0 :             if ( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
    1029             :             {
    1030           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
    1031             :             }
    1032             : 
    1033             :             /* HQ core initialization */
    1034           0 :             HQ_core_dec_init( st->hHQ_core );
    1035             :         }
    1036             : 
    1037             :         /* check if we have a doubly used hTxcCfg, if so, allocate a distint one for the old MCT LFE channel */
    1038        1154 :         if ( st->hTcxCfg == st_ivas->hCPE[1]->hCoreCoder[0]->hTcxCfg )
    1039             :         {
    1040           0 :             if ( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
    1041             :             {
    1042           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxCfg\n" ) );
    1043             :             }
    1044             :         }
    1045             : 
    1046        1154 :         st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( ivas_total_brate, st->igf, st->element_mode );
    1047             :     }
    1048       60932 :     if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    1049             :     {
    1050             :         uint8_t separateChannelEnabled;
    1051             :         int16_t separateChannelIndex;
    1052       20888 :         ivas_mcmasa_set_separate_channel_mode( &separateChannelEnabled, &separateChannelIndex, ivas_total_brate );
    1053       20888 :         ivas_mcmasa_split_brate( separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &new_brate_SCE, &new_brate_CPE );
    1054             :     }
    1055       40044 :     else if ( st_ivas->mc_mode == MC_MODE_MCT )
    1056             :     {
    1057       23444 :         new_brate_SCE = 0;
    1058       23444 :         new_brate_CPE = ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) ) * CPE_CHANNELS;
    1059             :     }
    1060       16600 :     else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    1061             :     {
    1062        1975 :         new_brate_SCE = 0;
    1063        1975 :         new_brate_CPE = ( ivas_total_brate / ( st_ivas->nchan_transport - 1 ) ) * CPE_CHANNELS;
    1064             :     }
    1065             :     else
    1066             :     {
    1067       14625 :         new_brate_SCE = 0; /* ivas_total_brate / st_ivas->nchan_transport;*/
    1068       14625 :         new_brate_CPE = ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS;
    1069             :     }
    1070             : 
    1071       60932 :     if ( ( error = ivas_corecoder_dec_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, sba_dirac_stereo_flag_old, new_brate_SCE, new_brate_CPE ) ) != IVAS_ERR_OK )
    1072             :     {
    1073           0 :         return error;
    1074             :     }
    1075             : 
    1076       60932 :     if ( last_mc_mode == MC_MODE_MCT && st_ivas->mc_mode == MC_MODE_PARAMMC && st_ivas->nchan_transport > CPE_CHANNELS )
    1077             :     {
    1078        1154 :         st = st_ivas->hCPE[1]->hCoreCoder[1];
    1079             : 
    1080             :         /* TCX-LTP */
    1081        1154 :         if ( st->hTcxLtpDec == NULL )
    1082             :         {
    1083           0 :             if ( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
    1084             :             {
    1085           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TCX-LTP handle\n" ) );
    1086             :             }
    1087           0 :             tcxltp_dec_init( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
    1088             :         }
    1089             :     }
    1090             : 
    1091             :     /*-----------------------------------------------------------------*
    1092             :      * re-configure HP20 memories
    1093             :      *-----------------------------------------------------------------*/
    1094             : 
    1095       60932 :     if ( ( error = ivas_hp20_dec_reconfig( st_ivas, nchan_hp20_old ) ) != IVAS_ERR_OK )
    1096             :     {
    1097           0 :         return error;
    1098             :     }
    1099             : 
    1100             :     /*-----------------------------------------------------------------*
    1101             :      * Reconfigure renderers
    1102             :      *-----------------------------------------------------------------*/
    1103             : 
    1104       60932 :     if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    1105             :     {
    1106       20888 :         if ( ( st_ivas->renderer_type != RENDERER_DISABLE ) && ( st_ivas->renderer_type != RENDERER_MCMASA_MONO_STEREO ) )
    1107             :         {
    1108       19521 :             if ( st_ivas->hDirAC != NULL )
    1109             :             {
    1110             :                 /* reconfigure existing DirAC dec */
    1111       19521 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_RECONFIGURE ) ) != IVAS_ERR_OK )
    1112             :                 {
    1113           0 :                     return error;
    1114             :                 }
    1115             :             }
    1116             :             else
    1117             :             {
    1118             :                 /* init a new DirAC dec */
    1119           0 :                 if ( ( error = ivas_dirac_dec_config( st_ivas, DIRAC_OPEN ) ) != IVAS_ERR_OK )
    1120             :                 {
    1121           0 :                     return error;
    1122             :                 }
    1123             :             }
    1124             :         }
    1125        1367 :         else if ( st_ivas->renderer_type == RENDERER_DISABLE )
    1126             :         {
    1127         717 :             ivas_dirac_rend_close( &( st_ivas->hDirACRend ) );
    1128         717 :             ivas_spat_hSpatParamRendCom_close( &( st_ivas->hSpatParamRendCom ) );
    1129         717 :             ivas_dirac_dec_close( &( st_ivas->hDirAC ) );
    1130             : 
    1131         717 :             vbap_free_data( &( st_ivas->hVBAPdata ) );
    1132             :         }
    1133             :     }
    1134             : 
    1135       60932 :     if ( renderer_type_old != st_ivas->renderer_type )
    1136             :     {
    1137             :         AUDIO_CONFIG output_config;
    1138             : 
    1139       36068 :         output_config = st_ivas->hDecoderConfig->output_config;
    1140             : 
    1141             :         /* binaural renderers*/
    1142       36068 :         if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1143             :         {
    1144             :             /* remove unneeded binaural renderers */
    1145       21276 :             if ( st_ivas->hBinRenderer != NULL && ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV && st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM ) )
    1146             :             {
    1147        6680 :                 ivas_binRenderer_close( &st_ivas->hBinRenderer );
    1148             : 
    1149        6680 :                 if ( ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation || st_ivas->hCombinedOrientationData ) )
    1150             :                 {
    1151        1414 :                     efap_free_data( &st_ivas->hEFAPdata );
    1152             :                 }
    1153             :             }
    1154             : 
    1155       21276 :             if ( ( st_ivas->hCrendWrapper != NULL ) && ( st_ivas->hCrendWrapper->hCrend[0] != NULL ) && ( st_ivas->renderer_type != RENDERER_BINAURAL_MIXER_CONV && st_ivas->renderer_type != RENDERER_BINAURAL_MIXER_CONV_ROOM && ( st_ivas->renderer_type != RENDERER_BINAURAL_OBJECTS_TD || st_ivas->hIntSetup.output_config != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) ) )
    1156             :             {
    1157        6675 :                 ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ) );
    1158             :             }
    1159             : 
    1160       21276 :             if ( st_ivas->hBinRendererTd != NULL && ( st_ivas->renderer_type != RENDERER_BINAURAL_OBJECTS_TD ) )
    1161             :             {
    1162        1290 :                 ivas_td_binaural_close( &st_ivas->hBinRendererTd );
    1163             :             }
    1164             : 
    1165       21276 :             if ( st_ivas->hDiracDecBin[0] != NULL )
    1166             :             {
    1167       13208 :                 if ( st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM && st_ivas->renderer_type != RENDERER_STEREO_PARAMETRIC )
    1168             :                 {
    1169        6631 :                     ivas_dirac_dec_close_binaural_data( st_ivas->hDiracDecBin );
    1170             :                 }
    1171             :             }
    1172             : 
    1173             :             /* init necessary new renderers */
    1174       21276 :             if ( st_ivas->hBinRenderer == NULL && ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
    1175             :             {
    1176        6691 :                 if ( ( error = ivas_binRenderer_open( st_ivas ) ) != IVAS_ERR_OK )
    1177             :                 {
    1178           0 :                     return error;
    1179             :                 }
    1180             :             }
    1181       14585 :             else if ( st_ivas->hBinRendererTd == NULL && st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    1182             :             {
    1183        1304 :                 if ( ( error = ivas_td_binaural_open( st_ivas ) ) != IVAS_ERR_OK )
    1184             :                 {
    1185           0 :                     return error;
    1186             :                 }
    1187             : 
    1188        1304 :                 if ( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1189             :                 {
    1190         646 :                     if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, st_ivas->hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    1191             :                     {
    1192           0 :                         return error;
    1193             :                     }
    1194             :                 }
    1195             : 
    1196        1304 :                 if ( st_ivas->hIntSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1197             :                 {
    1198           0 :                     if ( ( error = ivas_rend_initCrendWrapper( &st_ivas->hCrendWrapper, 1 ) ) != IVAS_ERR_OK )
    1199             :                     {
    1200           0 :                         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend Wrapper\n" );
    1201             :                     }
    1202             : 
    1203           0 :                     st_ivas->hCrendWrapper->hCrend[0] = NULL;
    1204           0 :                     st_ivas->hCrendWrapper->hHrtfCrend = NULL;
    1205           0 :                     if ( ( st_ivas->hCrendWrapper->hCrend[0] = (CREND_HANDLE) malloc( sizeof( CREND_DATA ) ) ) == NULL )
    1206             :                     {
    1207           0 :                         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend\n" );
    1208             :                     }
    1209             :                 }
    1210             :             }
    1211       13281 :             else if ( st_ivas->hCrendWrapper == NULL && ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    1212             :             {
    1213        6704 :                 if ( ( error = ivas_rend_openCrend( &( 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, ( st_ivas->hSplitBinRend == NULL ) ? 1 : st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses ) ) != IVAS_ERR_OK )
    1214             :                 {
    1215           0 :                     return error;
    1216             :                 }
    1217        6704 :                 st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
    1218             :             }
    1219             :         }
    1220             :         /* mono/stereo */
    1221             :         else if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
    1222             :         {
    1223             :             /* nothing should happen here... */
    1224             :         }
    1225             :         /* LS */
    1226             :         else if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_5_1_2 || output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1 || output_config == IVAS_AUDIO_CONFIG_7_1_4 || output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1227             :         {
    1228             : #ifdef DEBUGGING
    1229             :             assert( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_MC_PARAMMC || st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_DISABLE );
    1230             : #endif
    1231             :         }
    1232             : #ifdef DEBUGGING
    1233             :         else if ( output_config == IVAS_AUDIO_CONFIG_FOA || output_config == IVAS_AUDIO_CONFIG_HOA2 || output_config == IVAS_AUDIO_CONFIG_HOA3 )
    1234             :         {
    1235             :             /* FOA/HOA output */
    1236             :             /* Nothing to do, renderer is always RENDERER_SBA_LINEAR_ENC */
    1237             :             assert( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC && renderer_type_old == RENDERER_SBA_LINEAR_ENC );
    1238             :         }
    1239             : #endif
    1240             :     }
    1241             : 
    1242             :     /*-----------------------------------------------------------------*
    1243             :      * CLDFB instances
    1244             :      *-----------------------------------------------------------------*/
    1245             : 
    1246       60932 :     if ( ( error = ivas_cldfb_dec_reconfig( st_ivas, nchan_transport_old, numCldfbAnalyses_old, numCldfbSyntheses_old ) ) != IVAS_ERR_OK )
    1247             :     {
    1248           0 :         return error;
    1249             :     }
    1250             : 
    1251             : 
    1252             :     /*-----------------------------------------------------------------*
    1253             :      * Allocate the LFE handle that is coded separately after the allocation of the core coders
    1254             :      *-----------------------------------------------------------------*/
    1255             : 
    1256       60932 :     if ( ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) )
    1257             :     {
    1258             :         int32_t delay_ns;
    1259       25419 :         if ( st_ivas->hIntSetup.index_lfe[0] != -1 )
    1260             :         {
    1261       24519 :             delay_ns = st_ivas->binaural_latency_ns;
    1262             :         }
    1263             :         else
    1264             :         {
    1265         900 :             delay_ns = 0;
    1266             :         }
    1267             : 
    1268       25419 :         if ( st_ivas->hBinRenderer != NULL )
    1269             :         {
    1270         722 :             if ( st_ivas->hBinRenderer->render_lfe )
    1271             :             {
    1272             :                 /* Account for filterbank delay */
    1273         497 :                 delay_ns += IVAS_FB_DEC_DELAY_NS;
    1274             :             }
    1275             :             else
    1276             :             {
    1277         225 :                 delay_ns = 0;
    1278             :             }
    1279             :         }
    1280             :         else
    1281             :         {
    1282       24697 :             if ( ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) && ( st_ivas->cldfbSynDec[0] != NULL ) )
    1283             :             {
    1284         994 :                 delay_ns += IVAS_FB_DEC_DELAY_NS;
    1285             :             }
    1286             :         }
    1287             : 
    1288       25419 :         if ( st_ivas->hLFE != NULL )
    1289             :         {
    1290        8404 :             if ( st_ivas->hLFE->delay_ns != delay_ns )
    1291             :             {
    1292         741 :                 ivas_lfe_dec_close( &( st_ivas->hLFE ) );
    1293             :             }
    1294             :         }
    1295             : 
    1296       25419 :         if ( st_ivas->hLFE == NULL )
    1297             :         {
    1298       17756 :             if ( ( error = ivas_create_lfe_dec( &st_ivas->hLFE, st_ivas->hDecoderConfig->output_Fs, delay_ns ) ) != IVAS_ERR_OK )
    1299             :             {
    1300           0 :                 return error;
    1301             :             }
    1302             : 
    1303       17756 :             set_zero( st_ivas->hLFE->prevsynth_buf, LFE_PLC_BUFLEN );
    1304       17756 :             set_zero( st_ivas->hLFE->prior_out_buffer, L_FRAME48k );
    1305             :         }
    1306             :     }
    1307             : 
    1308             :     /*-----------------------------------------------------------------*
    1309             :      * JBM TC buffers
    1310             :      *-----------------------------------------------------------------*/
    1311             :     {
    1312             :         int16_t tc_nchan_full_new;
    1313             :         DECODER_TC_BUFFER_HANDLE hTcBuffer;
    1314             : 
    1315       60932 :         hTcBuffer = st_ivas->hTcBuffer;
    1316       60932 :         tc_buffer_mode_new = ivas_jbm_dec_get_tc_buffer_mode( st_ivas );
    1317       60932 :         tc_nchan_tc_new = ivas_jbm_dec_get_num_tc_channels( st_ivas );
    1318       60932 :         tc_nchan_allocate_new = tc_nchan_tc_new;
    1319       60932 :         tc_nchan_full_new = tc_nchan_tc_new;
    1320             : 
    1321       60932 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1322             :         {
    1323        9020 :             tc_nchan_allocate_new = BINAURAL_CHANNELS;
    1324        9020 :             if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->useTdDecorr )
    1325             :             {
    1326        6263 :                 tc_nchan_allocate_new = 2 * BINAURAL_CHANNELS;
    1327             :             }
    1328        2757 :             else if ( st_ivas->hOutSetup.separateChannelEnabled )
    1329             :             {
    1330        1574 :                 tc_nchan_allocate_new++;
    1331             :             }
    1332             : 
    1333        9020 :             tc_nchan_full_new = tc_nchan_allocate_new;
    1334             :         }
    1335             : 
    1336       60932 :         if ( st_ivas->mc_mode == MC_MODE_PARAMMC && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO )
    1337             :         {
    1338       14371 :             tc_nchan_full_new = 0;
    1339             :         }
    1340       46561 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    1341             :         {
    1342        1975 :             tc_nchan_allocate_new = MC_PARAMUPMIX_MAX_INPUT_CHANS;
    1343        1975 :             tc_buffer_mode_new = TC_BUFFER_MODE_RENDERER;
    1344        1975 :             if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
    1345             :             {
    1346         259 :                 tc_buffer_mode_new = TC_BUFFER_MODE_BUFFER;
    1347         259 :                 tc_nchan_tc_new = st_ivas->hDecoderConfig->nchan_out;
    1348         259 :                 tc_nchan_allocate_new = tc_nchan_tc_new;
    1349             :             }
    1350        1975 :             tc_nchan_full_new = tc_nchan_allocate_new;
    1351             :         }
    1352             : 
    1353             :         /* reconfigure buffer */
    1354       60932 :         if ( hTcBuffer->tc_buffer_mode != tc_buffer_mode_new || hTcBuffer->nchan_transport_jbm != tc_nchan_tc_new ||
    1355       13746 :              hTcBuffer->nchan_buffer_full != tc_nchan_full_new || hTcBuffer->nchan_transport_internal != tc_nchan_allocate_new ||
    1356       12771 :              tc_granularity_new != hTcBuffer->n_samples_granularity )
    1357             :         {
    1358       48161 :             if ( ( error = ivas_jbm_dec_tc_buffer_reconfigure( st_ivas, tc_buffer_mode_new, tc_nchan_tc_new, tc_nchan_allocate_new, tc_nchan_full_new, tc_granularity_new ) ) != IVAS_ERR_OK )
    1359             :             {
    1360           0 :                 return error;
    1361             :             }
    1362             :         }
    1363             : 
    1364             :         /* transfer subframe info from central tc buffer to ParamMC or McMASA (DirAC) */
    1365       60932 :         if ( st_ivas->hSpatParamRendCom != NULL )
    1366             :         {
    1367       20772 :             st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
    1368       20772 :             st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
    1369       20772 :             st_ivas->hSpatParamRendCom->num_slots = st_ivas->hTcBuffer->num_slots;
    1370       20772 :             st_ivas->hSpatParamRendCom->slots_rendered = st_ivas->hTcBuffer->slots_rendered;
    1371       20772 :             mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
    1372             :         }
    1373       40160 :         else if ( st_ivas->hParamMC != NULL )
    1374             :         {
    1375       14625 :             st_ivas->hParamMC->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
    1376       14625 :             st_ivas->hParamMC->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
    1377       14625 :             st_ivas->hParamMC->num_slots = st_ivas->hTcBuffer->num_slots;
    1378       14625 :             st_ivas->hParamMC->slots_rendered = st_ivas->hTcBuffer->slots_rendered;
    1379       14625 :             mvs2s( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hParamMC->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
    1380             :         }
    1381             :     }
    1382             : 
    1383             :     /*-----------------------------------------------------------------*
    1384             :      * floating-point output audio buffers
    1385             :      *-----------------------------------------------------------------*/
    1386             : 
    1387       60932 :     nchan_out_buff = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
    1388       60932 :     if ( ( error = ivas_output_buff_dec( st_ivas->p_output_f, nchan_out_buff, st_ivas->hDecoderConfig->Opt_tsm, st_ivas->hTcBuffer ) ) != IVAS_ERR_OK )
    1389             :     {
    1390           0 :         return error;
    1391             :     }
    1392             : 
    1393       60932 :     return IVAS_ERR_OK;
    1394             : }

Generated by: LCOV version 1.14