LCOV - code coverage report
Current view: top level - lib_dec - ivas_jbm_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 0c62f5312a76f89f3e6d6ab9a8d50516c9ab4059 Lines: 982 1069 91.9 %
Date: 2025-12-17 10:49:08 Functions: 24 24 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 <stdint.h>
      34             : #include "options.h"
      35             : #include "cnst.h"
      36             : #include "ivas_cnst.h"
      37             : #include "rom_com.h"
      38             : #include "prot.h"
      39             : #include "ivas_prot.h"
      40             : #include "ivas_prot_rend.h"
      41             : #include "ivas_rom_com.h"
      42             : #include <math.h>
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-----------------------------------------------------------------------*
      50             :  * Local function prototypes
      51             :  *-----------------------------------------------------------------------*/
      52             : 
      53             : static void ivas_jbm_dec_tc_buffer_playout( Decoder_Struct *st_ivas, const uint16_t nSamplesAsked, uint16_t *nSamplesRendered, float *output[] );
      54             : 
      55             : static void ivas_jbm_dec_copy_masa_meta_to_buffer( Decoder_Struct *st_ivas );
      56             : 
      57             : static void ivas_jbm_masa_sf_to_slot_map( Decoder_Struct *st_ivas, const int16_t nCldfbTs );
      58             : 
      59             : 
      60             : /*--------------------------------------------------------------------------*
      61             :  * ivas_jbm_dec_tc()
      62             :  *
      63             :  * Principal IVAS JBM decoder routine, decoding of metadata and transport channels
      64             :  *--------------------------------------------------------------------------*/
      65             : 
      66    21635067 : ivas_error ivas_jbm_dec_tc(
      67             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure         */
      68             : )
      69             : {
      70             :     int16_t n, output_frame, nchan_out;
      71             :     Decoder_State *st;                       /* used for bitstream handling */
      72             :     float *p_output[MAX_TRANSPORT_CHANNELS]; /* 'float' buffer for output synthesis */
      73             :     int16_t nchan_remapped;
      74             :     int16_t nb_bits_metadata[MAX_SCE + 1];
      75             :     int32_t output_Fs, ivas_total_brate;
      76             :     AUDIO_CONFIG output_config;
      77             :     ivas_error error;
      78             :     int16_t num_md_sub_frames;
      79             :     int32_t ism_total_brate;
      80             : 
      81    21635067 :     push_wmops( "ivas_jbm_dec_tc" );
      82             : 
      83             :     /*----------------------------------------------------------------*
      84             :      * Initialization of local vars after struct has been set
      85             :      *----------------------------------------------------------------*/
      86             : 
      87    21635067 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
      88    21635067 :     nchan_out = st_ivas->hTcBuffer->nchan_transport_jbm;
      89    21635067 :     output_config = st_ivas->hDecoderConfig->output_config;
      90    21635067 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
      91             : 
      92    21635067 :     output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
      93             : 
      94             :     /* set pointers to transport channels audio */
      95   281255871 :     for ( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
      96             :     {
      97   259620804 :         p_output[n] = st_ivas->p_output_f[n];
      98   259620804 :         if ( p_output[n] != NULL )
      99             :         {
     100   118743511 :             set_zero( p_output[n], L_FRAME48k );
     101             :         }
     102             :     }
     103             : 
     104             :     /*----------------------------------------------------------------*
     105             :      * Decoding + pre-rendering
     106             :      *----------------------------------------------------------------*/
     107             : 
     108    21635067 :     if ( st_ivas->bfi && st_ivas->ini_frame == 0 )
     109             :     {
     110             :         /* zero output when first frame(s) is lost */
     111           0 :         for ( n = 0; n < nchan_out; n++ )
     112             :         {
     113           0 :             set_f( p_output[n], 0.0f, output_frame );
     114             :         }
     115             : 
     116             : #ifdef DEBUG_MODE_INFO
     117             :         create_sce_dec( st_ivas, 0, ivas_total_brate );
     118             :         output_debug_mode_info_dec( st_ivas->hSCE[0]->hCoreCoder, 1, output_frame, NULL );
     119             :         destroy_sce_dec( st_ivas->hSCE[0] );
     120             :         st_ivas->hSCE[0] = NULL;
     121             : #endif
     122             :     }
     123    21635067 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
     124             :     {
     125     2489430 :         st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     126     2489430 :         if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
     127             :         {
     128           0 :             return error;
     129             :         }
     130             : 
     131             :         /* HP filtering */
     132     6678817 :         for ( n = 0; n < min( nchan_out, st_ivas->nchan_transport ); n++ )
     133             :         {
     134     4189387 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     135             :         }
     136             :     }
     137    19145637 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
     138             :     {
     139             :         /* Metadata decoding and configuration */
     140     3119699 :         if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
     141             :         {
     142       45144 :             ivas_ism_dtx_dec( st_ivas, nb_bits_metadata );
     143             : 
     144             :             /* decode dominant object first so the noise energy of the other objects can be limited */
     145       45144 :             if ( ( error = ivas_sce_dec( st_ivas, st_ivas->hISMDTX.sce_id_dtx, &p_output[st_ivas->hISMDTX.sce_id_dtx], output_frame, nb_bits_metadata[st_ivas->hISMDTX.sce_id_dtx] ) ) != IVAS_ERR_OK )
     146             :             {
     147           0 :                 return error;
     148             :             }
     149             : 
     150       45144 :             ivas_ism_dtx_limit_noise_energy_for_near_silence( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport );
     151             : 
     152       45144 :             ivas_param_ism_dec_dequant_md( st_ivas );
     153             :         }
     154     3074555 :         else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     155             :         {
     156      534522 :             if ( ( error = ivas_ism_metadata_dec( ivas_total_brate, st_ivas->nchan_ism, &( st_ivas->nchan_transport ), st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->bfi, nb_bits_metadata, st_ivas->ism_mode, st_ivas->hISMDTX, st_ivas->hParamIsmDec->hParamIsm, &st_ivas->ism_extmeta_active, &st_ivas->ism_extmeta_cnt, st_ivas->hSCE[0]->hCoreCoder[0] ) ) != IVAS_ERR_OK )
     157             :             {
     158           0 :                 return error;
     159             :             }
     160             : 
     161      534522 :             ivas_param_ism_dec_dequant_md( st_ivas );
     162             :         }
     163             :         else /* ISM_MODE_DISC */
     164             :         {
     165     2540033 :             if ( ( error = ivas_ism_metadata_dec( ivas_total_brate, st_ivas->nchan_ism, &( st_ivas->nchan_transport ), st_ivas->hIsmMetaData, st_ivas->hSCE, st_ivas->bfi, nb_bits_metadata, st_ivas->ism_mode, st_ivas->hISMDTX, NULL, &st_ivas->ism_extmeta_active, &st_ivas->ism_extmeta_cnt, st_ivas->hSCE[0]->hCoreCoder[0] ) ) != IVAS_ERR_OK )
     166             :             {
     167           0 :                 return error;
     168             :             }
     169             :         }
     170             : 
     171    11279438 :         for ( n = 0; n < st_ivas->nchan_transport; n++ )
     172             :         {
     173             :             /* for DTX frames, dominant object has already been decoded before */
     174     8159739 :             if ( !( ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) && n == st_ivas->hISMDTX.sce_id_dtx ) )
     175             :             {
     176     8114595 :                 if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[n], output_frame, nb_bits_metadata[n] ) ) != IVAS_ERR_OK )
     177             :                 {
     178           0 :                     return error;
     179             :                 }
     180             :             }
     181             : 
     182             :             /* HP filtering */
     183     8159739 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     184             :         }
     185             : 
     186     3119699 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     187             :         {
     188      144481 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     189             :         }
     190     2975218 :         else if ( st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) )
     191             :         {
     192             :             /* loudness correction */
     193      267595 :             ivas_dirac_dec_binaural_sba_gain( p_output, st_ivas->nchan_transport, output_frame );
     194             :         }
     195             :     }
     196    16025938 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
     197             :     {
     198     9467059 :         set_s( nb_bits_metadata, 0, MAX_SCE );
     199             : 
     200             : 
     201             :         /* read parameters from the bitstream */
     202     9467059 :         if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hQMetaData != NULL )
     203             :         {
     204     2584621 :             st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     205             : 
     206     2584621 :             if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     207             :             {
     208           0 :                 return error;
     209             :             }
     210             :         }
     211     6882438 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     212             :         {
     213     6882438 :             if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
     214             :             {
     215           0 :                 return error;
     216             :             }
     217             :         }
     218             : 
     219     9467059 :         if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
     220             :         {
     221     3641621 :             st_ivas->hCPE[0]->brate_surplus = 0;
     222     3641621 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     223             :         }
     224             : 
     225             :         /* core-decoding of transport channels */
     226     9467059 :         if ( st_ivas->nSCE == 1 )
     227             :         {
     228     3132320 :             if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     229             :             {
     230           0 :                 return error;
     231             :             }
     232             :         }
     233     6334739 :         else if ( st_ivas->nCPE == 1 )
     234             :         {
     235     3641621 :             if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     236             :             {
     237           0 :                 return error;
     238             :             }
     239             :         }
     240     2693118 :         else if ( st_ivas->nCPE > 1 )
     241             :         {
     242     2693118 :             if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     243             :             {
     244           0 :                 return error;
     245             :             }
     246             :         }
     247             : 
     248             : #ifdef DEBUG_SBA_AUDIO_DUMP
     249             :         /* Dump audio signal after core-decoding */
     250             :         ivas_spar_dump_signal_wav( output_frame, NULL, output, st_ivas->nchan_transport, spar_foa_dec_wav[0], "core-decoding" );
     251             : #endif
     252             :         /* TCs remapping */
     253     9467059 :         nchan_remapped = st_ivas->nchan_transport;
     254     9467059 :         if ( st_ivas->sba_dirac_stereo_flag )
     255             :         {
     256      250160 :             nchan_remapped = nchan_out;
     257             : 
     258      250160 :             if ( st_ivas->ivas_format == SBA_FORMAT )
     259             :             {
     260      250160 :                 ivas_agc_dec_process( st_ivas->hSpar->hAgcDec, p_output, p_output, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
     261             : 
     262      250160 :                 if ( st_ivas->hSpar->hPCA != NULL )
     263             :                 {
     264         594 :                     ivas_pca_dec( st_ivas->hSpar->hPCA, output_frame, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, p_output );
     265             :                 }
     266             : 
     267      250160 :                 ivas_spar_dec_gen_umx_mat( st_ivas->hSpar->hMdDec, st_ivas->nchan_transport, IVAS_MAX_NUM_BANDS, st_ivas->bfi, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
     268             :             }
     269             : 
     270      250160 :             ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, st_ivas->ivas_format == MC_FORMAT );
     271             :         }
     272     9216899 :         else if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ( ivas_total_brate > IVAS_SID_5k2 || ( ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE > 0 && st_ivas->hCPE[0]->nchan_out == 1 ) ) )
     273             :         {
     274      568035 :             nchan_remapped = 1; /* Only one channel transported */
     275             :         }
     276             : 
     277             :         /* HP filtering */
     278             : #ifndef DEBUG_SPAR_BYPASS_EVS_CODEC
     279    28923406 :         for ( n = 0; n < nchan_remapped; n++ )
     280             :         {
     281    19456347 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     282             :         }
     283             : #endif
     284             : 
     285     9467059 :         if ( st_ivas->ivas_format == SBA_FORMAT )
     286             :         {
     287     6882438 :             nchan_remapped = ivas_sba_remapTCs( p_output, st_ivas, output_frame );
     288             : 
     289     6882438 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     290             :             {
     291     1152096 :                 num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( st_ivas->sba_analysis_order, ivas_total_brate, st_ivas->last_active_ivas_total_brate );
     292     1152096 :                 ivas_sba_mix_matrix_determiner( st_ivas->hSpar, p_output, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
     293             :             }
     294     5730342 :             else if ( st_ivas->renderer_type != RENDERER_DISABLE )
     295             :             {
     296     5408229 :                 ivas_spar_dec_agc_pca( st_ivas, p_output, output_frame );
     297             :             }
     298             :         }
     299             : 
     300     9467059 :         if ( st_ivas->ivas_format == MASA_FORMAT )
     301             :         {
     302     2584621 :             ivas_masa_prerender( st_ivas, p_output, output_frame, nchan_remapped );
     303             : 
     304             :             /* external output */
     305     2584621 :             if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
     306             :             {
     307       54452 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
     308             :                 {
     309       41506 :                     set_zero( p_output[st_ivas->nchan_transport + n], output_frame );
     310             :                 }
     311             : 
     312       12946 :                 ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, output_frame );
     313             :             }
     314             :         }
     315     6882438 :         else if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
     316             :         {
     317             :             /* loudness correction */
     318     1152096 :             ivas_dirac_dec_binaural_sba_gain( p_output, nchan_remapped, output_frame );
     319             :         }
     320             :     }
     321     6558879 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     322             :     {
     323             :         int16_t nchan_ism, nchan_transport_ism;
     324             :         int16_t dirac_bs_md_write_idx;
     325             : 
     326     1224556 :         set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
     327             : 
     328             :         /* Set the number of objects for the parametric rendering */
     329     1224556 :         dirac_bs_md_write_idx = 0;
     330     1224556 :         if ( st_ivas->hSpatParamRendCom != NULL )
     331             :         {
     332     1000352 :             st_ivas->hSpatParamRendCom->numIsmDirections = 0;
     333     1000352 :             if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     334             :             {
     335      242831 :                 st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
     336             :             }
     337             : 
     338     1000352 :             dirac_bs_md_write_idx = st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx; /* Store the write-index for this frame */
     339             :         }
     340             : 
     341             :         /* MASA metadata decoding */
     342     1224556 :         if ( ( error = ivas_masa_decode( st_ivas, st_ivas->hCPE[0]->hCoreCoder[0], &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     343             :         {
     344           0 :             return error;
     345             :         }
     346             : 
     347             :         /* Configuration of combined-format bit-budget distribution */
     348     1224556 :         ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
     349             : 
     350     1224556 :         st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream = &( st_ivas->bit_stream[( ism_total_brate / FRAMES_PER_SEC )] );
     351             : 
     352             :         /* set ISM parameters and decode ISM metadata in OMASA format */
     353     1224556 :         if ( ( error = ivas_omasa_ism_metadata_dec( st_ivas, ism_total_brate, &nchan_ism, &nchan_transport_ism, dirac_bs_md_write_idx, &nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     354             :         {
     355           0 :             return error;
     356             :         }
     357             : 
     358             :         /* decode ISM channels */
     359     3440401 :         for ( n = 0; n < nchan_transport_ism; n++ )
     360             :         {
     361     2215845 :             if ( ( error = ivas_sce_dec( st_ivas, n, &p_output[st_ivas->nchan_transport + n], output_frame, nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     362             :             {
     363           0 :                 return error;
     364             :             }
     365             :         }
     366             : 
     367             :         /* decode MASA channels */
     368     1224556 :         if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     369             :         {
     370           0 :             return error;
     371             :         }
     372             : 
     373     1224556 :         if ( st_ivas->hCPE[0]->nchan_out == 1 )
     374             :         {
     375      211418 :             mvr2r( p_output[0], p_output[1], output_frame ); /* Copy mono signal to stereo output channels */
     376             :         }
     377             : 
     378             :         /* HP filtering */
     379     5889513 :         for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     380             :         {
     381     4664957 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     382             :         }
     383             : 
     384     1224556 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     385             :         {
     386      124915 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     387             :         }
     388     1099641 :         else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     389             :         {
     390      108661 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     391             :             {
     392       91413 :                 ivas_omasa_rearrange_channels( p_output, nchan_transport_ism, output_frame );
     393             :             }
     394       17248 :             else if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ )
     395             :             {
     396             :                 /* Convert separate object to MASA, combine with the original MASA, and output combined MASA + empty objects. */
     397        7876 :                 ivas_omasa_combine_separate_ism_with_masa( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
     398             :             }
     399        9372 :             else if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
     400             :             {
     401             :                 /* Extract objects from MASA, output MASA + all objects (i.e., extracted and separated objects) */
     402        9372 :                 ivas_omasa_render_objects_from_mix( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
     403             :             }
     404             :         }
     405             :     }
     406     5334323 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
     407             :     {
     408             :         int16_t nchan_ism, sba_ch_idx;
     409             : 
     410     2667903 :         set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
     411     2667903 :         nchan_ism = st_ivas->nchan_ism;
     412     2667903 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     413             :         {
     414             :             /* set ISM parameters and decode ISM metadata in OSBA format */
     415     1357230 :             if ( ( error = ivas_osba_ism_metadata_dec( st_ivas, ivas_total_brate, &nchan_ism, &nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     416             :             {
     417           0 :                 return error;
     418             :             }
     419     1357230 :             sba_ch_idx = st_ivas->nchan_ism;
     420             :         }
     421             :         else
     422             :         {
     423     1310673 :             nb_bits_metadata[1] += NO_BITS_MASA_ISM_NO_OBJ;
     424     1310673 :             sba_ch_idx = 0;
     425             :         }
     426             : 
     427             :         /* SBA metadata decoding */
     428     2667903 :         if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
     429             :         {
     430           0 :             return error;
     431             :         }
     432             : 
     433     2667903 :         if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
     434             :         {
     435      510928 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     436             :         }
     437             : 
     438             :         /* core-decoding of transport channels */
     439     2667903 :         if ( st_ivas->nSCE == 1 )
     440             :         {
     441      668805 :             if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     442             :             {
     443           0 :                 return error;
     444             :             }
     445             :         }
     446     1999098 :         else if ( st_ivas->nCPE == 1 )
     447             :         {
     448      510928 :             if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     449             :             {
     450           0 :                 return error;
     451             :             }
     452             :         }
     453     1488170 :         else if ( st_ivas->nCPE > 1 )
     454             :         {
     455     1488170 :             if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ) != IVAS_ERR_OK )
     456             :             {
     457           0 :                 return error;
     458             :             }
     459             :         }
     460             : 
     461     2667903 :         if ( st_ivas->sba_dirac_stereo_flag )
     462             :         {
     463      184215 :             ivas_agc_dec_process( st_ivas->hSpar->hAgcDec, &p_output[sba_ch_idx], &p_output[sba_ch_idx], st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
     464             : 
     465      184215 :             if ( st_ivas->hSpar->hPCA != NULL )
     466             :             {
     467        2376 :                 ivas_pca_dec( st_ivas->hSpar->hPCA, output_frame, st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, &p_output[sba_ch_idx] );
     468             :             }
     469             : 
     470      184215 :             ivas_spar_dec_gen_umx_mat( st_ivas->hSpar->hMdDec, st_ivas->nchan_transport, IVAS_MAX_NUM_BANDS, st_ivas->bfi, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
     471             : 
     472      184215 :             ivas_sba_dirac_stereo_dec( st_ivas, &p_output[sba_ch_idx], output_frame, 0 );
     473             :         }
     474             : 
     475             :         /* HP filtering */
     476    13140000 :         for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     477             :         {
     478    10472097 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     479             :         }
     480             : 
     481     2667903 :         nchan_remapped = ivas_sba_remapTCs( &p_output[sba_ch_idx], st_ivas, output_frame );
     482             : 
     483             : #ifdef DEBUG_OSBA
     484             :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     485             :         {
     486             :             int16_t nchan = st_ivas->nchan_transport + st_ivas->nchan_ism;
     487             :             for ( int16_t t = 0; t < output_frame; t++ )
     488             :             {
     489             :                 for ( int16_t c = 0; c < nchan; c++ )
     490             :                 {
     491             :                     int16_t val = (int16_t) ( output[c][t] + 0.5f );
     492             :                     dbgwrite( &val, sizeof( int16_t ), 1, 1, "./res/TC_dec_core_out.raw" );
     493             :                 }
     494             :             }
     495             :         }
     496             : #endif
     497     2667903 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     498             :         {
     499      463100 :             num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
     500             : 
     501      463100 :             ivas_sba_mix_matrix_determiner( st_ivas->hSpar, &p_output[sba_ch_idx], st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
     502             :         }
     503     2204803 :         else if ( st_ivas->renderer_type != RENDERER_DISABLE && !st_ivas->sba_dirac_stereo_flag )
     504             :         {
     505     1933525 :             ivas_spar_dec_agc_pca( st_ivas, &p_output[sba_ch_idx], output_frame );
     506             :         }
     507             : 
     508     2667903 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     509             :         {
     510             :             /* loudness correction */
     511      463100 :             ivas_dirac_dec_binaural_sba_gain( &p_output[sba_ch_idx], nchan_remapped, output_frame );
     512             :         }
     513     2204803 :         else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     514             :         {
     515       69742 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     516             : 
     517             :             /* add W */
     518      139484 :             for ( n = 0; n < nchan_out; n++ )
     519             :             {
     520       69742 :                 v_add( p_output[n], p_output[n + max( nchan_out, nchan_ism )], p_output[n], output_frame );
     521             :             }
     522             :         }
     523             :     }
     524     2666420 :     else if ( st_ivas->ivas_format == MC_FORMAT )
     525             :     {
     526     2666420 :         st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     527             : 
     528     2666420 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
     529             :         {
     530             :             /* LFE channel decoder */
     531     1413194 :             ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
     532             : 
     533     1413194 :             if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, 0 ) ) != IVAS_ERR_OK )
     534             :             {
     535           0 :                 return error;
     536             :             }
     537             : 
     538             :             /* HP filtering */
     539    12502216 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     540             :             {
     541    11089022 :                 if ( n != LFE_CHANNEL )
     542             :                 {
     543     9675828 :                     hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     544             :                 }
     545             :             }
     546             : 
     547     1413194 :             if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
     548             :             {
     549       45521 :                 if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
     550             :                 {
     551           0 :                     ivas_mc2sba( st_ivas->hTransSetup, p_output, p_output, output_frame, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
     552             :                 }
     553             :             }
     554             : 
     555     1413194 :             if ( ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
     556             :             {
     557      139748 :                 if ( st_ivas->renderer_type == RENDERER_MC )
     558             :                 {
     559      127478 :                     ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
     560             :                 }
     561       12270 :                 else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     562             :                 {
     563       12270 :                     ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, output_frame, st_ivas->hOutSetup.ambisonics_order, 0.f );
     564             :                 }
     565             :             }
     566             :         }
     567     1253226 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     568             :         {
     569             :             /* LFE channel decoder */
     570      130887 :             ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
     571             : 
     572      130887 :             ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
     573             : 
     574      130887 :             if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     575             :             {
     576           0 :                 return error;
     577             :             }
     578             : 
     579             :             /* HP filtering */
     580     1177983 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     581             :             {
     582     1047096 :                 if ( n != LFE_CHANNEL )
     583             :                 {
     584      916209 :                     hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     585             :                 }
     586             :             }
     587             : 
     588             :             /* Rendering */
     589      130887 :             if ( st_ivas->renderer_type == RENDERER_MC && ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO ) )
     590             :             {
     591             :                 /* Compensate loudness for not doing full upmix */
     592       18170 :                 for ( n = 4; n < 8; n++ )
     593             :                 {
     594       14536 :                     v_multc( p_output[n], 2.0f, p_output[n], output_frame );
     595             :                 }
     596             : 
     597        3634 :                 if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
     598             :                 {
     599        3634 :                     ivas_ls_setup_conversion( st_ivas, audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 ), output_frame, p_output, p_output );
     600             :                 }
     601             :             }
     602             :         }
     603     1122339 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     604             :         {
     605             :             /* read Parametric MC parameters from the bitstream */
     606      459633 :             ivas_param_mc_dec_read_BS( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
     607             : 
     608      459633 :             if ( st_ivas->nCPE == 1 )
     609             :             {
     610      394975 :                 if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     611             :                 {
     612           0 :                     return error;
     613             :                 }
     614             :             }
     615       64658 :             else if ( st_ivas->nCPE > 1 )
     616             :             {
     617       64658 :                 if ( ( error = ivas_mct_dec( st_ivas, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     618             :                 {
     619           0 :                     return error;
     620             :                 }
     621             :             }
     622             : 
     623             :             /* HP filtering */
     624     1443557 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     625             :             {
     626      983924 :                 hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     627             :             }
     628             : 
     629             :             /* Rendering */
     630      459633 :             if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
     631             :             {
     632       11084 :                 ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
     633             :             }
     634             :         }
     635      662706 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     636             :         {
     637      662706 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
     638             :             {
     639      130567 :                 st = st_ivas->hCPE[0]->hCoreCoder[0]; /* Metadata is always with CPE in the case of separated channel */
     640             :             }
     641             : 
     642             :             /* read McMASA parameters from the bitstream */
     643      662706 :             if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     644             :             {
     645           0 :                 return error;
     646             :             }
     647             : 
     648      662706 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
     649             :             {
     650             :                 /* Decode the transport audio signals */
     651      130567 :                 if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     652             :                 {
     653           0 :                     return error;
     654             :                 }
     655             : 
     656             :                 /* Identify the index of the separated channel, always LFE_CHANNEL-1 here */
     657      130567 :                 n = LFE_CHANNEL - 1;
     658             : 
     659             :                 /* Decode the separated channel to output[n] to be combined with the synthesized channels */
     660      130567 :                 if ( ( error = ivas_sce_dec( st_ivas, 0, &p_output[n], output_frame, 0 ) ) != IVAS_ERR_OK )
     661             :                 {
     662           0 :                     return error;
     663             :                 }
     664             : 
     665             :                 /* Delay the separated channel to sync with CLDFB delay of the DirAC synthesis, and synthesize the LFE signal. */
     666      130567 :                 if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
     667      100900 :                      output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
     668       97780 :                      output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) || output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     669             :                 {
     670       42481 :                     ivas_lfe_synth_with_filters( st_ivas->hMasa->hMasaLfeSynth, p_output, output_frame, n, LFE_CHANNEL );
     671             :                 }
     672       88086 :                 else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
     673             :                 {
     674             :                     /* Delay the separated channel to sync with the DirAC rendering */
     675           0 :                     delay_signal( p_output[n], output_frame, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size );
     676             :                 }
     677             :             }
     678             :             else
     679             :             {
     680      532139 :                 if ( st_ivas->nSCE == 1 )
     681             :                 {
     682      451434 :                     if ( ( error = ivas_sce_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     683             :                     {
     684           0 :                         return error;
     685             :                     }
     686             :                 }
     687       80705 :                 else if ( st_ivas->nCPE == 1 )
     688             :                 {
     689       80705 :                     if ( ( error = ivas_cpe_dec( st_ivas, 0, p_output, output_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     690             :                     {
     691           0 :                         return error;
     692             :                     }
     693             :                 }
     694             :             }
     695             : 
     696      662706 :             if ( st_ivas->sba_dirac_stereo_flag ) /* use the flag to trigger the DFT upmix */
     697             :             {
     698       62597 :                 ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, 1 );
     699             :             }
     700             : 
     701             :             /* HP filtering */
     702     1729848 :             for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     703             :             {
     704     1067142 :                 hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     705             :             }
     706             : 
     707      662706 :             if ( st_ivas->renderer_type == RENDERER_MCMASA_MONO_STEREO )
     708             :             {
     709       86482 :                 ivas_mono_stereo_downmix_mcmasa( st_ivas, p_output, output_frame );
     710             :             }
     711             :         }
     712             :     }
     713             : 
     714             :     /*----------------------------------------------------------------*
     715             :      * Common updates
     716             :      *----------------------------------------------------------------*/
     717             : 
     718    21635067 :     if ( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
     719             :     {
     720    20438632 :         st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
     721    20438632 :         st_ivas->last_active_ivas_total_brate = ( ivas_total_brate <= IVAS_SID_5k2 ) ? st_ivas->last_active_ivas_total_brate : ivas_total_brate;
     722             :     }
     723             : 
     724    21635067 :     if ( st_ivas->ini_frame < MAX_FRAME_COUNTER && !( st_ivas->bfi && st_ivas->ini_frame == 0 ) ) /* keep "st_ivas->ini_frame = 0" until first good received frame */
     725             :     {
     726     5360040 :         st_ivas->ini_frame++;
     727             :     }
     728             : 
     729    21635067 :     if ( st_ivas->ini_active_frame < MAX_FRAME_COUNTER && !( st_ivas->bfi && st_ivas->ini_frame == 0 ) && ivas_total_brate > IVAS_SID_5k2 ) /* needed in MASA decoder in case the first active frame is BFI, and there were SID-frames decoded before */
     730             :     {
     731     5358190 :         st_ivas->ini_active_frame++;
     732             :     }
     733             : 
     734    21635067 :     st_ivas->last_ivas_format = st_ivas->ivas_format;
     735             : 
     736             : #ifdef DEBUG_MODE_INFO
     737             :     dbgwrite( &st_ivas->bfi, sizeof( int16_t ), 1, output_frame, "res/bfi" );
     738             :     dbgwrite( &st_ivas->BER_detect, sizeof( int16_t ), 1, output_frame, "res/BER_detect" );
     739             :     {
     740             :         float tmpF = ivas_total_brate / 1000.0f;
     741             :         dbgwrite( &tmpF, sizeof( float ), 1, output_frame, "res/ivas_total_brate.dec" );
     742             :     }
     743             : #endif
     744             : 
     745    21635067 :     pop_wmops();
     746    21635067 :     return IVAS_ERR_OK;
     747             : }
     748             : 
     749             : 
     750             : /*--------------------------------------------------------------------------*
     751             :  * ivas_dec_feed_tc_to_renderer()
     752             :  *
     753             :  * Feed decoded transport channels to the IVAS renderer routine
     754             :  * + digest TC channels in ParamISM and ParamMC
     755             :  *--------------------------------------------------------------------------*/
     756             : 
     757    21828523 : void ivas_jbm_dec_feed_tc_to_renderer(
     758             :     Decoder_Struct *st_ivas,            /* i/o: IVAS decoder structure                          */
     759             :     const int16_t nSamplesForRendering, /* i  : number of TC samples available for rendering    */
     760             :     int16_t *nSamplesResidual           /* o  : number of samples not fitting into the renderer grid and buffer for the next call*/
     761             : )
     762             : {
     763             :     float tmp_buf[MAX_JBM_L_FRAME48k];
     764             :     float *p_data_f[FOA_CHANNELS + MAX_NUM_OBJECTS];
     765             :     int16_t n, n_render_timeslots, n_ch_cldfb;
     766             :     int16_t ch, offset, len_offset;
     767             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
     768             : 
     769    21828523 :     hTcBuffer = st_ivas->hTcBuffer;
     770    21828523 :     n_ch_cldfb = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
     771             : 
     772    21828523 :     if ( st_ivas->hDecoderConfig->Opt_tsm )
     773             :     {
     774             :         int16_t n_samples_still_available;
     775             :         int16_t n_ch_full_copy, n_ch_res_copy;
     776             : 
     777     3099848 :         n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
     778     3099848 :         hTcBuffer->n_samples_buffered = n_samples_still_available + nSamplesForRendering + hTcBuffer->n_samples_discard;
     779     3099848 :         hTcBuffer->n_samples_available = hTcBuffer->n_samples_granularity * ( hTcBuffer->n_samples_buffered / hTcBuffer->n_samples_granularity );
     780     3099848 :         *nSamplesResidual = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_available;
     781     3099848 :         n_ch_full_copy = min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
     782     3099848 :         n_ch_res_copy = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
     783             : 
     784             :         /* buffers are shared between 'hTcBuffer->tc[]' and 'p_output_f[]':
     785             :            in case of 'length(hTcBuffer->tc[]) < length(p_output_f[])', reset of TC buffers
     786             :            pointers is needed after ivas_buffer_interleaved_to_deinterleaved() */
     787     3099848 :         len_offset = NS2SA( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS );
     788     3099848 :         if ( len_offset < L_FRAME48k )
     789             :         {
     790      713513 :             offset = 0;
     791     3389407 :             for ( ch = 0; ch < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch++ )
     792             :             {
     793     2675894 :                 hTcBuffer->tc[ch] = &hTcBuffer->tc_buffer[offset];
     794     2675894 :                 st_ivas->p_output_f[ch] = hTcBuffer->tc[ch];
     795     2675894 :                 offset += len_offset;
     796             :             }
     797             :         }
     798             : 
     799    12669432 :         for ( ch = 0; ch < n_ch_full_copy; ch++ )
     800             :         {
     801     9569584 :             mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
     802     9569584 :             set_zero( hTcBuffer->tc[ch], hTcBuffer->n_samples_discard );
     803     9569584 :             mvr2r( hTcBuffer->tc_buffer_old[ch], hTcBuffer->tc[ch] + hTcBuffer->n_samples_discard, n_samples_still_available );
     804     9569584 :             mvr2r( tmp_buf, hTcBuffer->tc[ch] + n_samples_still_available + hTcBuffer->n_samples_discard, nSamplesForRendering - *nSamplesResidual );
     805     9569584 :             mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
     806             :         }
     807             : 
     808     3099848 :         if ( n_ch_res_copy > 0 )
     809             :         {
     810      413726 :             for ( ; ch < hTcBuffer->nchan_transport_jbm; ch++ )
     811             :             {
     812      282444 :                 p_data_f[ch] = hTcBuffer->tc[ch];
     813      282444 :                 mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
     814      282444 :                 mvr2r( hTcBuffer->tc_buffer_old[ch], p_data_f[ch], n_samples_still_available );
     815      282444 :                 mvr2r( tmp_buf, p_data_f[ch] + n_samples_still_available, nSamplesForRendering - *nSamplesResidual );
     816      282444 :                 mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
     817             :             }
     818             :         }
     819             : 
     820     3099848 :         n_render_timeslots = hTcBuffer->n_samples_available / hTcBuffer->n_samples_granularity;
     821             :     }
     822             :     else
     823             :     {
     824    19789901 :         for ( n = 0; n < n_ch_cldfb; n++ )
     825             :         {
     826     1061226 :             p_data_f[n] = &st_ivas->p_output_f[n][0];
     827             :         }
     828             : 
     829    18728675 :         ch = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
     830    88645421 :         for ( n = 0; n < ch; n++ )
     831             :         {
     832    69916746 :             hTcBuffer->tc[n] = st_ivas->p_output_f[n]; /* note: buffers needed in the TD decorellator */
     833             :         }
     834             : 
     835    18728675 :         hTcBuffer->n_samples_buffered = nSamplesForRendering;
     836    18728675 :         hTcBuffer->n_samples_available = hTcBuffer->n_samples_buffered;
     837    18728675 :         *nSamplesResidual = 0;
     838             : 
     839    18728675 :         n_render_timeslots = DEFAULT_JBM_CLDFB_TIMESLOTS;
     840             :     }
     841             : 
     842             :     /* CLDFB analysis for ParamMC/ParamISM */
     843    21828523 :     if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM && ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) )
     844             :     {
     845      193826 :         ivas_param_ism_dec_digest_tc( st_ivas, n_render_timeslots, p_data_f );
     846             :     }
     847    21634697 :     else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC && hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_RENDERER )
     848             :     {
     849      448549 :         ivas_param_mc_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, p_data_f );
     850             :     }
     851             : 
     852    21828523 :     hTcBuffer->n_samples_rendered = 0;
     853    21828523 :     hTcBuffer->subframes_rendered = 0;
     854             : 
     855    21828523 :     return;
     856             : }
     857             : 
     858             : 
     859             : /*--------------------------------------------------------------------------*
     860             :  * ivas_dec_render()
     861             :  *
     862             :  * Principal IVAS JBM rendering routine
     863             :  *--------------------------------------------------------------------------*/
     864             : 
     865    42892672 : ivas_error ivas_jbm_dec_render(
     866             :     Decoder_Struct *st_ivas,             /* i/o: IVAS decoder structure                                      */
     867             :     const uint16_t nSamplesAsked,        /* i  : number of samples wanted                                    */
     868             :     uint16_t *nSamplesRendered,          /* o  : number of samples rendered                                  */
     869             :     uint16_t *nSamplesAvailableNext,     /* o  : number of samples still available in the rendering pipeline */
     870             :     const PCM_RESOLUTION pcm_resolution, /* i  : type for the decoded PCM resolution                         */
     871             :     void *data                           /* o  : output synthesis signal                                     */
     872             : )
     873             : {
     874             :     int16_t n, nchan_out;
     875             :     int16_t nchan_transport;
     876             :     int16_t nchan_remapped;
     877             :     int32_t output_Fs;
     878             :     AUDIO_CONFIG output_config;
     879             :     int16_t nSamplesAskedLocal;
     880             :     ivas_error error;
     881             :     float *p_output[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
     882             :     float *p_tc[MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS];
     883             :     int16_t nchan_out_syn_output;
     884             : 
     885    42892672 :     push_wmops( "ivas_dec_render" );
     886             :     /*----------------------------------------------------------------*
     887             :      * Initialization of local vars after struct has been set
     888             :      *----------------------------------------------------------------*/
     889             : 
     890    42892672 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     891    42892672 :     nchan_out = st_ivas->hDecoderConfig->nchan_out;
     892    42892672 :     nchan_transport = st_ivas->hTcBuffer->nchan_transport_jbm;
     893    42892672 :     output_config = st_ivas->hDecoderConfig->output_config;
     894    42892672 :     nSamplesAskedLocal = nSamplesAsked + st_ivas->hTcBuffer->n_samples_discard;
     895             : 
     896   900746112 :     for ( n = 0; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     897             :     {
     898   857853440 :         p_output[n] = st_ivas->p_output_f[n];
     899             :     }
     900             : 
     901    42892672 :     if ( !st_ivas->hDecoderConfig->Opt_tsm )
     902             :     {
     903   645665695 :         for ( n = 0; n < MAX_INTERN_CHANNELS; n++ )
     904             :         {
     905   607685360 :             st_ivas->hTcBuffer->tc[n] = p_output[n];
     906             :         }
     907             :     }
     908             : 
     909   200845605 :     for ( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
     910             :     {
     911   157952933 :         p_tc[n] = &st_ivas->hTcBuffer->tc[n][st_ivas->hTcBuffer->n_samples_rendered];
     912             :     }
     913             : 
     914             :     /*----------------------------------------------------------------*
     915             :      * Update combined orientation access index
     916             :      *----------------------------------------------------------------*/
     917             : 
     918    42892672 :     if ( st_ivas->hCombinedOrientationData != NULL )
     919             :     {
     920             :         /* take the discard samples into account here to make sure head rotation stays on the correct 5ms grid */
     921     5900863 :         st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start -= st_ivas->hTcBuffer->n_samples_discard;
     922             : 
     923     5900863 :         ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
     924             :     }
     925             : 
     926             :     /*----------------------------------------------------------------*
     927             :      * Rendering
     928             :      *----------------------------------------------------------------*/
     929             : 
     930    42892672 :     *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
     931             : 
     932    42892672 :     if ( st_ivas->ivas_format == UNDEFINED_FORMAT )
     933             :     {
     934           0 :         assert( 0 );
     935             :     }
     936    42892672 :     else if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
     937             :     {
     938     9748951 :         ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
     939             :     }
     940    33143721 :     else if ( st_ivas->ivas_format == MONO_FORMAT || st_ivas->ivas_format == STEREO_FORMAT )
     941             :     {
     942             :         /* Rendering */
     943       80352 :         if ( st_ivas->renderer_type == RENDERER_MC )
     944             :         {
     945           0 :             ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
     946             :         }
     947             : #ifdef FIX_1435_MOVE_STEREO_PANNING
     948       80352 :         else if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
     949             :         {
     950       80352 :             ivas_apply_non_diegetic_panning( p_tc[0], p_output, st_ivas->hDecoderConfig->non_diegetic_pan_gain, *nSamplesRendered );
     951             :         }
     952             : #endif
     953           0 :         else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     954             :         {
     955             : #ifdef FIX_1454_FIX_STEREO_TO_FOA_JBM
     956           0 :             ivas_stereo2sba( p_tc, p_output, *nSamplesRendered );
     957             : #else
     958             :             ivas_stereo2sba( p_output, p_output, *nSamplesRendered );
     959             : #endif
     960             :         }
     961             :     }
     962    33063369 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
     963             :     {
     964             :         /* Rendering */
     965     4717924 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     966             :         {
     967     1030294 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
     968             :             {
     969      606707 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, st_ivas->nchan_transport, p_output );
     970             :             }
     971      423587 :             else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     972             :             {
     973      423587 :                 ivas_param_ism_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
     974             : 
     975      423587 :                 if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     976             :                 {
     977             :                     /* Convert CICP19 -> Ambisonics */
     978      309705 :                     ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
     979             :                 }
     980             :             }
     981             :         }
     982             :         else /* ISM_MODE_DISC */
     983             :         {
     984             :             /* Loudspeaker or Ambisonics rendering */
     985     3687630 :             if ( st_ivas->renderer_type == RENDERER_TD_PANNING || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
     986             :             {
     987             :                 /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
     988     1372464 :                 ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
     989             :             }
     990     2315166 :             else if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
     991             :             {
     992       65865 :                 ivas_apply_non_diegetic_panning( p_tc[0], p_output, st_ivas->hDecoderConfig->non_diegetic_pan_gain, *nSamplesRendered );
     993             :             }
     994             : #ifdef DEBUGGING
     995             :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
     996             : #else
     997     2249301 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     998             : #endif
     999             :             {
    1000             :                 /* Convert to Ambisonics */
    1001      508560 :                 ivas_ism2sba_sf( p_tc, p_output, st_ivas->hIsmRendererData, st_ivas->nchan_transport, *nSamplesRendered, st_ivas->hTcBuffer->n_samples_rendered, st_ivas->hIntSetup.ambisonics_order );
    1002             :             }
    1003             : 
    1004             :             /* Binaural rendering */
    1005     3687630 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    1006             :             {
    1007     1740741 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1008             :                 {
    1009       27156 :                     if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1010             :                     {
    1011           0 :                         return error;
    1012             :                     }
    1013             :                 }
    1014             :                 else
    1015             :                 {
    1016     1713585 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1017             :                     {
    1018           0 :                         return error;
    1019             :                     }
    1020             :                 }
    1021             :             }
    1022     1946889 :             else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1023             :             {
    1024      604552 :                 if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
    1025      604552 :                                                                NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
    1026             :                 {
    1027           0 :                     return error;
    1028             :                 }
    1029             :             }
    1030             :         }
    1031             :     }
    1032    28345445 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1033             :     {
    1034    18472234 :         nchan_remapped = nchan_transport;
    1035             : 
    1036             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    1037    18472234 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1038             :         {
    1039     4931701 :             ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1040             :         }
    1041    13540533 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
    1042             :         {
    1043     1809320 :             if ( st_ivas->renderer_type == RENDERER_DIRAC )
    1044             :             {
    1045     1809320 :                 ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1046             :             }
    1047             :         }
    1048             :         else
    1049             :         {
    1050    11731213 :             if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1051             :             {
    1052           0 :                 return error;
    1053             :             }
    1054             :         }
    1055             :     }
    1056     9873211 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1057             :     {
    1058     1836525 :         nchan_remapped = st_ivas->nchan_transport;
    1059             : 
    1060     1836525 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1061             :         {
    1062     1052199 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    1063             :             {
    1064      204964 :                 if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output ) ) != IVAS_ERR_OK )
    1065             :                 {
    1066           0 :                     return error;
    1067             :                 }
    1068             :             }
    1069             :             else
    1070             :             {
    1071      847235 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1072             :             }
    1073             :         }
    1074      784326 :         else if ( st_ivas->renderer_type == RENDERER_DIRAC )
    1075             :         {
    1076      767078 :             ivas_omasa_dirac_rend_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1077             :         }
    1078       17248 :         else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT || st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
    1079             :         {
    1080       17248 :             ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
    1081       17248 :             ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, *nSamplesRendered );
    1082             :         }
    1083             :     }
    1084     8036686 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1085             :     {
    1086     3538003 :         nchan_remapped = nchan_transport;
    1087             : 
    1088             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    1089     3538003 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1090             :         {
    1091     1995991 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
    1092             :             {
    1093      826024 :                 if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1094             :                 {
    1095           0 :                     return error;
    1096             :                 }
    1097             :             }
    1098     1169967 :             else if ( st_ivas->renderer_type == RENDERER_OSBA_STEREO )
    1099             :             {
    1100             :                 /* shift SBA channels to avoid overwrite by ISM upmix in 1 object case and non-TSM unified channel memory*/
    1101      135733 :                 if ( st_ivas->nchan_ism == 1 && st_ivas->hDecoderConfig->Opt_tsm == 0 )
    1102             :                 {
    1103        5940 :                     mvr2r( p_tc[2], p_output[3], *nSamplesRendered );
    1104        5940 :                     mvr2r( p_tc[1], p_output[2], *nSamplesRendered );
    1105        5940 :                     p_tc[1] = p_output[2];
    1106        5940 :                     p_tc[2] = p_output[3];
    1107             :                 }
    1108             : 
    1109             :                 /* render objects */
    1110      135733 :                 ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
    1111             : 
    1112             :                 /* add already rendered SBA part */
    1113      135733 :                 ivas_osba_stereo_add_channels( p_tc, p_output, st_ivas->hSbaIsmData->gain_bed, nchan_out, st_ivas->nchan_ism, *nSamplesRendered );
    1114             :             }
    1115     1034234 :             else if ( st_ivas->renderer_type == RENDERER_OSBA_AMBI || st_ivas->renderer_type == RENDERER_OSBA_LS || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    1116             :             {
    1117      922290 :                 if ( ( error = ivas_osba_render_sf( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1118             :                 {
    1119           0 :                     return error;
    1120             :                 }
    1121             :             }
    1122      111944 :             else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) /*EXT output = individual objects + HOA3*/
    1123             :             {
    1124      111944 :                 if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, &p_output[st_ivas->nchan_ism] ) ) != IVAS_ERR_OK )
    1125             :                 {
    1126           0 :                     return error;
    1127             :                 }
    1128             : 
    1129      411071 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    1130             :                 {
    1131      299127 :                     mvr2r( p_tc[n], p_output[n], *nSamplesRendered );
    1132             :                 }
    1133             :             }
    1134             :             else
    1135             :             {
    1136           0 :                 if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1137             :                 {
    1138           0 :                     return error;
    1139             :                 }
    1140             :             }
    1141             :         }
    1142     1542012 :         else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1143             :         {
    1144      757620 :             ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1145     2272860 :             for ( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
    1146             :             {
    1147     1515240 :                 v_multc( p_output[n], 2.0f, p_output[n], *nSamplesRendered );
    1148             :             }
    1149             :         }
    1150             :         else
    1151             :         {
    1152      784392 :             if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1153             :             {
    1154           0 :                 return error;
    1155             :             }
    1156             : 
    1157      784392 :             if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    1158             :             {
    1159             : #ifdef DEBUGGING
    1160             :                 assert( st_ivas->ism_mode == ISM_MODE_NONE );
    1161             : #endif
    1162      881082 :                 for ( n = st_ivas->hIntSetup.nchan_out_woLFE - 1; n >= 0; n-- )
    1163             :                 {
    1164      807654 :                     mvr2r( p_output[n], p_output[n + st_ivas->nchan_ism], *nSamplesRendered );
    1165             :                 }
    1166      282846 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    1167             :                 {
    1168      209418 :                     set_zero( p_output[n], *nSamplesRendered );
    1169             :                 }
    1170             :             }
    1171     7781306 :             for ( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
    1172             :             {
    1173     6996914 :                 v_multc( p_output[n], 2.0f, p_output[n], *nSamplesRendered );
    1174             :             }
    1175             :         }
    1176             :     }
    1177     4498683 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    1178             :     {
    1179     4498683 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    1180             :         {
    1181     2370150 :             int16_t crendInPlaceRotation = FALSE;
    1182             : 
    1183     2370150 :             if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
    1184             :             {
    1185       89836 :                 if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
    1186             :                 {
    1187       89836 :                     crendInPlaceRotation = TRUE;
    1188       89836 :                     ivas_mc2sba( st_ivas->hTransSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
    1189             :                 }
    1190             :             }
    1191             : 
    1192             :             /* Rendering */
    1193     2370150 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1194             :             {
    1195     1616672 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1196             :                 {
    1197       27070 :                     if ( ( error = ivas_rend_crendProcessSubframesSplitBin( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, ( st_ivas->hSplitBinRend == NULL ) ? NULL : &st_ivas->hSplitBinRend->splitrend.multiBinPoseData, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    1198       27070 :                                                                             &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs ) ) != IVAS_ERR_OK )
    1199             :                     {
    1200           0 :                         return error;
    1201             :                     }
    1202             :                 }
    1203             :                 else
    1204             :                 {
    1205     3206274 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    1206     3206274 :                                                                    &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
    1207             :                     {
    1208           0 :                         return error;
    1209             :                     }
    1210             : 
    1211     1603137 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
    1212             :                 }
    1213             :             }
    1214      753478 :             else if ( st_ivas->renderer_type == RENDERER_MC )
    1215             :             {
    1216      271644 :                 ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
    1217             :             }
    1218      481834 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1219             :             {
    1220       57276 :                 ivas_mc2sba( st_ivas->hIntSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1221             :             }
    1222      424558 :             else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    1223             :             {
    1224      424558 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1225             :                 {
    1226      115589 :                     if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1227             :                     {
    1228           0 :                         return error;
    1229             :                     }
    1230             :                 }
    1231             :                 else
    1232             :                 {
    1233      308969 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1234             :                     {
    1235           0 :                         return error;
    1236             :                     }
    1237             : 
    1238      308969 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
    1239             :                 }
    1240             :             }
    1241             :         }
    1242     2128533 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    1243             :         {
    1244      277385 :             ivas_mc_paramupmix_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_tc, p_output );
    1245             : 
    1246             : 
    1247             :             /* Rendering */
    1248      277385 :             if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && !st_ivas->hDecoderConfig->Opt_Headrotation )
    1249             :             {
    1250             :                 /* handled in CLDFB domain already */
    1251       65537 :                 if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1252             :                 {
    1253       65537 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_output, p_output );
    1254             :                 }
    1255             :             }
    1256      211848 :             else if ( st_ivas->renderer_type == RENDERER_MC )
    1257             :             {
    1258        1188 :                 ivas_ls_setup_conversion( st_ivas, MC_PARAMUPMIX_MAX_INPUT_CHANS, *nSamplesRendered, p_output, p_output );
    1259             :             }
    1260      210660 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1261             :             {
    1262        6676 :                 ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1263             :             }
    1264             :         }
    1265     1851148 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    1266             :         {
    1267      915179 :             ivas_param_mc_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1268             :         }
    1269      935969 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    1270             :         {
    1271      935969 :             nchan_remapped = st_ivas->nchan_transport;
    1272      935969 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1273             :             {
    1274      648519 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1275             :             }
    1276      287450 :             else if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) /* rendering to CICPxx and Ambisonics */
    1277             :             {
    1278      287450 :                 ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1279             : 
    1280      287450 :                 if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1281             :                 {
    1282             :                     /* we still need to copy the separate channel if available  */
    1283       69398 :                     if ( st_ivas->hOutSetup.separateChannelEnabled )
    1284             :                     {
    1285       16086 :                         mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1286             :                     }
    1287             : 
    1288       69398 :                     ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1289             :                 }
    1290      218052 :                 else if ( st_ivas->intern_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 ) )
    1291             :                 {
    1292        8040 :                     for ( n = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe; n < st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe; n++ )
    1293             :                     {
    1294        6030 :                         set_zero( p_output[n], *nSamplesRendered );
    1295             :                     }
    1296             :                 }
    1297             :             }
    1298             : 
    1299             :             /* copy discrete C and TD LFE from internal TC to output  */
    1300      935969 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
    1301             :             {
    1302      130670 :                 if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
    1303       67719 :                      output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    1304       64599 :                      output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) )
    1305             :                 {
    1306       66071 :                     mvr2r( p_tc[LFE_CHANNEL], p_output[LFE_CHANNEL], *nSamplesRendered );
    1307       66071 :                     mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1308             :                 }
    1309       64599 :                 else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
    1310             :                 {
    1311           0 :                     mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1312             :                 }
    1313             :             }
    1314             :         }
    1315             :     }
    1316             : 
    1317             :     /*----------------------------------------------------------------*
    1318             :      * Write IVAS output channels
    1319             :      *   - compensation for saturation
    1320             :      *   - float to integer conversion
    1321             :      *----------------------------------------------------------------*/
    1322             : 
    1323    42892672 :     st_ivas->hTcBuffer->n_samples_available -= *nSamplesRendered;
    1324    42892672 :     st_ivas->hTcBuffer->n_samples_rendered += *nSamplesRendered;
    1325             : 
    1326             :     /* update global combined orientation start index */
    1327    42892672 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    1328             : 
    1329    42892672 :     if ( st_ivas->hTcBuffer->n_samples_discard > 0 )
    1330             :     {
    1331       52476 :         for ( n = 0; n < min( MAX_OUTPUT_CHANNELS, ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ) ); n++ )
    1332             :         {
    1333       46534 :             p_output[n] += st_ivas->hTcBuffer->n_samples_discard;
    1334             :         }
    1335        5942 :         *nSamplesRendered -= st_ivas->hTcBuffer->n_samples_discard;
    1336        5942 :         st_ivas->hTcBuffer->n_samples_discard = 0;
    1337             :     }
    1338             : 
    1339    42892672 :     if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1340             :     {
    1341      606433 :         nchan_out_syn_output = BINAURAL_CHANNELS * st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    1342             : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
    1343      606433 :         if ( st_ivas->flushing )
    1344             :         {
    1345         736 :             nchan_out_syn_output = BINAURAL_CHANNELS;
    1346             :         }
    1347             : #endif
    1348             :     }
    1349             :     else
    1350             :     {
    1351    42286239 :         nchan_out_syn_output = nchan_out;
    1352             :     }
    1353             : 
    1354    42892672 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    1355             :     {
    1356    42286239 :         if ( st_ivas->ivas_format != MONO_FORMAT )
    1357             :         {
    1358             : #ifndef DISABLE_LIMITER
    1359    41945166 :             ivas_limiter_dec( st_ivas->hLimiter, p_output, nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    1360             : #endif
    1361             :         }
    1362             :     }
    1363             : 
    1364    42892672 :     switch ( pcm_resolution )
    1365             :     {
    1366    42286975 :         case PCM_INT16:
    1367             : #ifdef DEBUGGING
    1368             :             st_ivas->noClipping +=
    1369             : #endif
    1370    42286975 :                 ivas_syn_output( p_output, *nSamplesRendered, nchan_out_syn_output, (int16_t *) data );
    1371             : 
    1372    42286975 :             break;
    1373      605697 :         case PCM_FLOAT32:
    1374      605697 :             ivas_buffer_deinterleaved_to_interleaved( p_output, nchan_out_syn_output, *nSamplesRendered, (float *) data );
    1375      605697 :             break;
    1376           0 :         default:
    1377           0 :             error = IVAS_ERR_UNKNOWN;
    1378           0 :             break;
    1379             :     }
    1380             : 
    1381    42892672 :     *nSamplesAvailableNext = st_ivas->hTcBuffer->n_samples_available;
    1382             : 
    1383    42892672 :     pop_wmops();
    1384    42892672 :     return IVAS_ERR_OK;
    1385             : }
    1386             : 
    1387             : 
    1388             : /*--------------------------------------------------------------------------*
    1389             :  * ivas_jbm_dec_flush_renderer()
    1390             :  *
    1391             :  * Flush samples if renderer granularity changes on a bitrate change
    1392             :  *--------------------------------------------------------------------------*/
    1393             : 
    1394        8728 : ivas_error ivas_jbm_dec_flush_renderer(
    1395             :     Decoder_Struct *st_ivas,                     /* i/o: IVAS decoder structure    */
    1396             :     const int16_t tc_granularity_new,            /* i  : new renderer granularity  */
    1397             :     const RENDERER_TYPE renderer_type_old,       /* i  : old renderer type         */
    1398             :     const AUDIO_CONFIG intern_config_old,        /* i  : old internal config       */
    1399             :     const IVAS_OUTPUT_SETUP_HANDLE hIntSetupOld, /* i  : old internal output setup */
    1400             :     const MC_MODE mc_mode_old,                   /* i  : old MC mode               */
    1401             :     const ISM_MODE ism_mode_old,                 /* i  : old ISM mode              */
    1402             :     uint16_t *nSamplesRendered,                  /* o  : number of samples flushed */
    1403             :     const PCM_RESOLUTION pcm_resolution,         /* i  : type for the decoded PCM resolution */
    1404             :     void *data                                   /* o  : output synthesis signal   */
    1405             : )
    1406             : {
    1407             :     ivas_error error;
    1408             :     int16_t n_samples_still_available;
    1409             :     int16_t n_slots_still_available;
    1410             :     int16_t n_samples_to_render;
    1411             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    1412             :     float *p_output[MAX_LS_CHANNELS + MAX_NUM_OBJECTS];
    1413             : 
    1414        8728 :     if ( !st_ivas->hDecoderConfig->Opt_tsm )
    1415             :     {
    1416           0 :         return IVAS_ERR_OK;
    1417             :     }
    1418             : 
    1419        8728 :     *nSamplesRendered = 0;
    1420        8728 :     hTcBuffer = st_ivas->hTcBuffer;
    1421             : 
    1422             :     /* get number of possible slots in new granularity */
    1423        8728 :     n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
    1424        8728 :     n_slots_still_available = n_samples_still_available / tc_granularity_new;
    1425        8728 :     *nSamplesRendered = n_slots_still_available * tc_granularity_new;
    1426        8728 :     n_samples_to_render = *nSamplesRendered;
    1427        8728 :     n_samples_still_available -= n_samples_to_render;
    1428        8728 :     assert( n_samples_still_available < tc_granularity_new );
    1429             : 
    1430             :     /* update combined orientation access index */
    1431        8728 :     ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
    1432             : 
    1433        8728 :     if ( n_slots_still_available )
    1434             :     {
    1435             :         int16_t ch_idx;
    1436             : 
    1437             :         /* render available full slots (with new lower granularity) */
    1438       39909 :         for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
    1439             :         {
    1440             :             /* move it at the beginning of the TC buffer with zero padding */
    1441       33977 :             mvr2r( hTcBuffer->tc_buffer_old[ch_idx], hTcBuffer->tc[ch_idx], n_samples_to_render );
    1442       33977 :             set_zero( hTcBuffer->tc[ch_idx] + n_samples_to_render, hTcBuffer->n_samples_granularity - n_samples_to_render );
    1443             :         }
    1444             : 
    1445             :         /* simple change of the slot info */
    1446        5932 :         hTcBuffer->num_slots = 1;
    1447        5932 :         hTcBuffer->nb_subframes = 1;
    1448        5932 :         hTcBuffer->subframes_rendered = 0;
    1449        5932 :         hTcBuffer->slots_rendered = 0;
    1450        5932 :         hTcBuffer->subframe_nbslots[0] = 1;
    1451        5932 :         hTcBuffer->n_samples_buffered = hTcBuffer->n_samples_granularity + n_samples_still_available;
    1452        5932 :         hTcBuffer->n_samples_available = 0;
    1453        5932 :         hTcBuffer->n_samples_flushed = n_samples_to_render;
    1454        5932 :         hTcBuffer->n_samples_rendered = 0;
    1455             : 
    1456      124572 :         for ( ch_idx = 0; ch_idx < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    1457             :         {
    1458      118640 :             p_output[ch_idx] = st_ivas->p_output_f[ch_idx];
    1459             :         }
    1460             : 
    1461        5932 :         if ( st_ivas->ivas_format == ISM_FORMAT )
    1462             :         {
    1463        2521 :             if ( ism_mode_old == ISM_MODE_DISC )
    1464             :             {
    1465             :                 /* Binaural rendering */
    1466        2521 :                 if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
    1467             :                 {
    1468        1374 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
    1469             :                     {
    1470           0 :                         return error;
    1471             :                     }
    1472             :                 }
    1473        1147 :                 else if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1474             :                 {
    1475             :                     /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
    1476        1147 :                     set_f( st_ivas->hIsmRendererData->interpolator, 1.0f, hTcBuffer->n_samples_granularity );
    1477             : 
    1478        1147 :                     ivas_ism_render_sf( st_ivas, renderer_type_old, p_output, hTcBuffer->n_samples_granularity );
    1479             : 
    1480        1147 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
    1481        1147 :                                                                    NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
    1482             :                     {
    1483           0 :                         return error;
    1484             :                     }
    1485             :                 }
    1486             :             }
    1487             :             else
    1488             :             {
    1489           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong ISM_MODE in VoIP renderer flushing!" );
    1490             :             }
    1491             :         }
    1492        3411 :         else if ( st_ivas->ivas_format == MC_FORMAT )
    1493             :         {
    1494        1151 :             if ( mc_mode_old == MC_MODE_MCT )
    1495             :             {
    1496        1151 :                 int16_t crendInPlaceRotation = FALSE;
    1497             : 
    1498        1151 :                 if ( st_ivas->transport_config != intern_config_old && ( intern_config_old == IVAS_AUDIO_CONFIG_FOA || intern_config_old == IVAS_AUDIO_CONFIG_HOA2 || intern_config_old == IVAS_AUDIO_CONFIG_HOA3 ) )
    1499             :                 {
    1500         526 :                     if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( hIntSetupOld->nchan_out_woLFE + hIntSetupOld->num_lfe ) )
    1501             :                     {
    1502         526 :                         crendInPlaceRotation = TRUE;
    1503         526 :                         ivas_mc2sba( st_ivas->hTransSetup, hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, hIntSetupOld->ambisonics_order, GAIN_LFE );
    1504             :                     }
    1505             :                 }
    1506        1151 :                 if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV || renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1507             :                 {
    1508         526 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, intern_config_old, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    1509         526 :                                                                    hIntSetupOld, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : st_ivas->hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
    1510             :                     {
    1511           0 :                         return error;
    1512             :                     }
    1513             : 
    1514         526 :                     ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
    1515             :                 }
    1516         625 :                 else if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
    1517             :                 {
    1518         625 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
    1519             :                     {
    1520           0 :                         return error;
    1521             :                     }
    1522             : 
    1523         625 :                     ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
    1524             :                 }
    1525             :                 else
    1526             :                 {
    1527           0 :                     return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong renderer in MCT VoIP renderer flushing!" );
    1528             :                 }
    1529             :             }
    1530             :             else
    1531             :             {
    1532           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong MC_MODE in VoIP renderer flushing!" );
    1533             :             }
    1534             :         }
    1535        2260 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1536             :         {
    1537        1889 :             if ( ism_mode_old == ISM_MASA_MODE_DISC )
    1538             :             {
    1539             :                 float *tc_local[MAX_NUM_OBJECTS];
    1540             :                 int16_t last_dirac_md_idx;
    1541             :                 uint16_t nSamplesAvailableNext;
    1542             :                 ISM_MODE ism_mode_orig;
    1543             :                 RENDERER_TYPE renderer_type_orig;
    1544             :                 int32_t ivas_total_brate;
    1545             : 
    1546             :                 /* copy from ISM delay buffer to the correct place in TCs */
    1547        9445 :                 for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    1548             :                 {
    1549        7556 :                     tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx + 2][hTcBuffer->n_samples_rendered];
    1550        7556 :                     mvr2r( st_ivas->hMasaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hMasaIsmData->delayBuffer_size );
    1551             :                 }
    1552             : 
    1553             :                 /* to render flushed samples, use configuration from the last received frame */
    1554        1889 :                 ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1555        1889 :                 renderer_type_orig = st_ivas->renderer_type;
    1556        1889 :                 ism_mode_orig = st_ivas->ism_mode;
    1557        1889 :                 st_ivas->ism_mode = ism_mode_old;
    1558        1889 :                 st_ivas->renderer_type = renderer_type_old;
    1559        1889 :                 st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
    1560        1889 :                 last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
    1561             : 
    1562             :                 /* transfer adapted sf info from hTcBuffer to DirAC */
    1563        1889 :                 st_ivas->hSpatParamRendCom->nb_subframes = 1;
    1564        1889 :                 st_ivas->hSpatParamRendCom->subframes_rendered = 0;
    1565        1889 :                 st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1566        1889 :                 st_ivas->hSpatParamRendCom->slots_rendered = 0;
    1567        1889 :                 st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1568        1889 :                 set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
    1569             : 
    1570        1889 :                 if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, CPE_CHANNELS, p_output ) ) != IVAS_ERR_OK )
    1571             :                 {
    1572           0 :                     return error;
    1573             :                 }
    1574             : 
    1575             :                 /* restore original configuration */
    1576        1889 :                 st_ivas->ism_mode = ism_mode_orig;
    1577        1889 :                 st_ivas->renderer_type = renderer_type_orig;
    1578        1889 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
    1579             :             }
    1580             :         }
    1581         371 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1582             :         {
    1583         371 :             if ( ism_mode_old == ISM_SBA_MODE_DISC )
    1584             :             {
    1585             :                 float *tc_local[MAX_TRANSPORT_CHANNELS];
    1586             :                 int16_t last_spar_md_idx;
    1587             :                 int16_t last_dirac_md_idx;
    1588             :                 uint16_t nSamplesAvailableNext;
    1589             :                 ISM_MODE ism_mode_orig;
    1590             :                 RENDERER_TYPE renderer_type_orig;
    1591             :                 int32_t ivas_total_brate;
    1592             : 
    1593             :                 /* to render flushed samples, use configuration from the last received frame */
    1594         371 :                 ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1595         371 :                 renderer_type_orig = st_ivas->renderer_type;
    1596         371 :                 ism_mode_orig = st_ivas->ism_mode;
    1597         371 :                 st_ivas->ism_mode = ism_mode_old;
    1598         371 :                 st_ivas->renderer_type = renderer_type_old;
    1599         371 :                 st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
    1600         371 :                 last_spar_md_idx = st_ivas->hSpar->render_to_md_map[st_ivas->hSpar->slots_rendered - 1];
    1601         371 :                 last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
    1602             : #ifdef DEBUGGING
    1603             :                 assert( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV );
    1604             : #endif
    1605             : 
    1606             :                 /* copy from ISM delay buffer to the correct place in TCs */
    1607        1113 :                 for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    1608             :                 {
    1609         742 :                     tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx][hTcBuffer->n_samples_rendered];
    1610         742 :                     mvr2r( st_ivas->hSbaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hSbaIsmData->delayBuffer_size );
    1611             :                 }
    1612             : 
    1613             :                 /* transfer adapted sf info from hTcBuffer to SPAR and DirAC */
    1614         371 :                 st_ivas->hSpar->nb_subframes = 1;
    1615         371 :                 st_ivas->hSpar->subframes_rendered = 0;
    1616         371 :                 st_ivas->hSpar->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1617         371 :                 st_ivas->hSpar->slots_rendered = 0;
    1618         371 :                 st_ivas->hSpar->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1619         371 :                 st_ivas->hSpatParamRendCom->nb_subframes = 1;
    1620         371 :                 st_ivas->hSpatParamRendCom->subframes_rendered = 0;
    1621         371 :                 st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1622         371 :                 st_ivas->hSpatParamRendCom->slots_rendered = 0;
    1623         371 :                 st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1624             : 
    1625             :                 /* also adapt md maps, just use the last index */
    1626         371 :                 set_s( st_ivas->hSpar->render_to_md_map, last_spar_md_idx, n_slots_still_available );
    1627         371 :                 set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
    1628             : 
    1629             :                 /* render the last subframe */
    1630         371 :                 if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1631             :                 {
    1632           0 :                     return error;
    1633             :                 }
    1634             : 
    1635             :                 /* restore original configuration */
    1636         371 :                 st_ivas->ism_mode = ism_mode_orig;
    1637         371 :                 st_ivas->renderer_type = renderer_type_orig;
    1638         371 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
    1639             :             }
    1640             :         }
    1641             :         else
    1642             :         {
    1643           0 :             return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong IVAS format in VoIP renderer flushing!" );
    1644             :         }
    1645             : 
    1646        5932 :         hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
    1647             :     }
    1648             : 
    1649             :     /* update global combined orientation start index */
    1650        8728 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    1651             : 
    1652        8728 :     *nSamplesRendered = n_samples_to_render;
    1653             : 
    1654             :     /* Only write out the valid data*/
    1655        8728 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    1656             :     {
    1657        8728 :         if ( st_ivas->ivas_format != MONO_FORMAT )
    1658             :         {
    1659             : #ifndef DISABLE_LIMITER
    1660        8728 :             ivas_limiter_dec( st_ivas->hLimiter, p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    1661             : #endif
    1662             :         }
    1663             :     }
    1664             : 
    1665        8728 :     switch ( pcm_resolution )
    1666             :     {
    1667        8728 :         case PCM_INT16:
    1668             : #ifdef DEBUGGING
    1669             :             st_ivas->noClipping +=
    1670             : #endif
    1671        8728 :                 ivas_syn_output( p_output, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) data );
    1672        8728 :             break;
    1673           0 :         case PCM_FLOAT32:
    1674           0 :             ivas_buffer_deinterleaved_to_interleaved( p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, (float *) data );
    1675           0 :             break;
    1676           0 :         default:
    1677           0 :             error = IVAS_ERR_UNKNOWN;
    1678           0 :             break;
    1679             :     }
    1680             : 
    1681        8728 :     return IVAS_ERR_OK;
    1682             : }
    1683             : 
    1684             : 
    1685             : /*--------------------------------------------------------------------------*
    1686             :  * ivas_jbm_dec_set_discard_samples()
    1687             :  *
    1688             :  * Set number of samples to discard in the first subframe if the renderer granularity changes on a bitrate change
    1689             :  *--------------------------------------------------------------------------*/
    1690             : 
    1691       29973 : ivas_error ivas_jbm_dec_set_discard_samples(
    1692             :     Decoder_Struct *st_ivas /* i/o: main IVAS decoder structre */
    1693             : )
    1694             : {
    1695             :     int16_t nMaxSlotsPerSubframe, nSlotsInFirstSubframe;
    1696             : 
    1697             :     /* render first frame with front zero padding and discarding those samples */
    1698       29973 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    1699       29973 :     nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
    1700       29973 :     if ( nSlotsInFirstSubframe > 0 )
    1701             :     {
    1702        5942 :         st_ivas->hTcBuffer->n_samples_discard = ( nMaxSlotsPerSubframe - nSlotsInFirstSubframe ) * st_ivas->hTcBuffer->n_samples_granularity;
    1703             :         /* set last subframes number to max to ensure correct continuation */
    1704        5942 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nMaxSlotsPerSubframe;
    1705             :     }
    1706             : 
    1707       29973 :     return IVAS_ERR_OK;
    1708             : }
    1709             : 
    1710             : 
    1711             : /*--------------------------------------------------------------------------*
    1712             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    1713             :  *
    1714             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    1715             :  *--------------------------------------------------------------------------*/
    1716             : 
    1717      995708 : void ivas_jbm_dec_get_adapted_linear_interpolator(
    1718             :     const int16_t default_interp_length, /* i  : default length of the (full-frame) interpolator */
    1719             :     const int16_t interp_length,         /* i  : length of the interpolator to be created        */
    1720             :     float *interpolator                  /* o  : the interpolator                                */
    1721             : )
    1722             : {
    1723             :     int16_t jbm_segment_len, idx;
    1724             :     float dec;
    1725             : #ifdef DEBUGGING
    1726             :     assert( default_interp_length % 2 == 0 );
    1727             : #endif
    1728             : 
    1729      995708 :     jbm_segment_len = ( default_interp_length >> 1 );
    1730      995708 :     dec = 1.0f / default_interp_length;
    1731             : 
    1732      995708 :     interpolator[interp_length - 1] = 1.0f;
    1733    34450483 :     for ( idx = interp_length - 2; idx >= jbm_segment_len; idx-- )
    1734             :     {
    1735    33454775 :         interpolator[idx] = max( 0.0f, interpolator[idx + 1] - dec );
    1736             :     }
    1737             : 
    1738      995708 :     if ( interpolator[idx + 1] > 0.0f )
    1739             :     {
    1740      995490 :         dec = interpolator[idx + 1] / ( jbm_segment_len + 1 );
    1741    35192867 :         for ( ; idx >= 0; idx-- )
    1742             :         {
    1743    34197377 :             interpolator[idx] = interpolator[idx + 1] - dec;
    1744             :         }
    1745             :     }
    1746             :     else
    1747             :     {
    1748         218 :         set_f( interpolator, 0.0f, idx + 1 );
    1749             :     }
    1750             : 
    1751      995708 :     return;
    1752             : }
    1753             : 
    1754             : 
    1755             : /*--------------------------------------------------------------------------*
    1756             :  * ivas_jbm_dec_get_adapted_subframes()
    1757             :  *
    1758             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    1759             :  *--------------------------------------------------------------------------*/
    1760             : 
    1761    18288541 : void ivas_jbm_dec_get_adapted_subframes(
    1762             :     const int16_t nCldfbTs,    /* i  : number of time slots in the current frame */
    1763             :     int16_t *subframe_nbslots, /* i/o: subframe grid                             */
    1764             :     int16_t *nb_subframes      /* i/o: number of subframes in the frame          */
    1765             : )
    1766             : {
    1767             :     uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
    1768    18288541 :     uint16_t nCldfbSlotsLocal = nCldfbTs;
    1769             : 
    1770             :     /* get last subframe size from previous frame, determine how many slots have to be processed
    1771             :            in the first subframe (i.e. potential leftover of a 5ms subframe) */
    1772    18288541 :     nSlotsInFirstSubframe = ( PARAM_MC_MAX_NSLOTS_IN_SUBFRAME - subframe_nbslots[*nb_subframes - 1] );
    1773    18288541 :     *nb_subframes = 0;
    1774    18288541 :     if ( nSlotsInFirstSubframe > 0 )
    1775             :     {
    1776      461524 :         *nb_subframes = 1;
    1777      461524 :         nCldfbSlotsLocal -= nSlotsInFirstSubframe;
    1778             :     }
    1779             : 
    1780    18288541 :     *nb_subframes += (int16_t) ceilf( (float) nCldfbSlotsLocal / (float) PARAM_MC_MAX_NSLOTS_IN_SUBFRAME );
    1781    18288541 :     nSlotsInLastSubframe = nCldfbSlotsLocal % PARAM_MC_MAX_NSLOTS_IN_SUBFRAME;
    1782             : 
    1783    18288541 :     set_s( subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    1784    18288541 :     set_s( subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, *nb_subframes );
    1785             : 
    1786    18288541 :     if ( nSlotsInFirstSubframe > 0 )
    1787             :     {
    1788      461524 :         subframe_nbslots[0] = nSlotsInFirstSubframe;
    1789             :     }
    1790             : 
    1791    18288541 :     if ( nSlotsInLastSubframe > 0 )
    1792             :     {
    1793      462100 :         subframe_nbslots[*nb_subframes - 1] = nSlotsInLastSubframe;
    1794             :     }
    1795             : 
    1796    18288541 :     return;
    1797             : }
    1798             : 
    1799             : 
    1800             : /*--------------------------------------------------------------------------*
    1801             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    1802             :  *
    1803             :  * Get an meta data map adapted to a time scale modified IVAS frame
    1804             :  *--------------------------------------------------------------------------*/
    1805             : 
    1806    15819225 : void ivas_jbm_dec_get_md_map(
    1807             :     const int16_t default_len,  /* i  : default frame length in metadata slots         */
    1808             :     const int16_t len,          /* i  : length of the modfied frames in metadata slots */
    1809             :     const int16_t subframe_len, /* i  : default length of a subframe                   */
    1810             :     const int16_t offset,       /* i  : current read offset into the md buffer         */
    1811             :     const int16_t buf_len,      /* i  : length of the metadata buffer                  */
    1812             :     int16_t *map                /* o  : metadata index map                             */
    1813             : )
    1814             : {
    1815             :     int16_t jbm_segment_len, map_idx, src_idx, src_idx_map;
    1816             :     float dec, src_idx_f;
    1817             : 
    1818             : #ifdef DEBUGGING
    1819             :     assert( default_len % 2 == 0 );
    1820             : #endif
    1821    15819225 :     jbm_segment_len = ( default_len >> 1 );
    1822    15819225 :     dec = 1.0f / default_len;
    1823             : 
    1824   142393815 :     for ( map_idx = len - 1, src_idx = default_len - 1; map_idx >= jbm_segment_len; map_idx--, src_idx-- )
    1825             :     {
    1826   126574590 :         src_idx_map = max( 0, src_idx / subframe_len );
    1827   126574590 :         map[map_idx] = ( offset + src_idx_map ) % buf_len;
    1828             :     }
    1829             : 
    1830             :     /* changed part (first segment), interpolate index to parameters
    1831             :            (we do not want to interpolate and smooth acutal direction/diffuseness values even more) */
    1832    15819225 :     if ( src_idx >= 0 )
    1833             :     {
    1834    15817688 :         dec = ( (float) ( src_idx + 1 ) ) / ( (float) jbm_segment_len );
    1835    15817688 :         src_idx_f = (float) ( src_idx + 1 ) - dec;
    1836   142359192 :         for ( ; map_idx >= 0; map_idx-- )
    1837             :         {
    1838   126541504 :             src_idx = max( 0, ( (int16_t) round_f( src_idx_f ) ) / subframe_len );
    1839   126541504 :             map[map_idx] = ( offset + src_idx ) % buf_len;
    1840   126541504 :             src_idx_f -= dec;
    1841             :         }
    1842             :     }
    1843             :     else
    1844             :     {
    1845        1537 :         set_s( map, offset, map_idx + 1 );
    1846             :     }
    1847             : 
    1848    15819225 :     return;
    1849             : }
    1850             : 
    1851             : 
    1852             : /*--------------------------------------------------------------------------*
    1853             :  * ivas_jbm_dec_get_md_map_even_spacing()
    1854             :  *
    1855             :  * Get an meta data map adapted to a time scale modified IVAS frame. Distribute slots evenly across the modified frame.
    1856             :  *--------------------------------------------------------------------------*/
    1857             : 
    1858     2118729 : void ivas_jbm_dec_get_md_map_even_spacing(
    1859             :     const int16_t len,          /* i  : length of the modfied frames in metadata slots */
    1860             :     const int16_t subframe_len, /* i  : default length of a subframe                   */
    1861             :     const int16_t offset,       /* i  : current read offset into the md buffer         */
    1862             :     const int16_t buf_len,      /* i  : length of the metadata buffer                  */
    1863             :     int16_t *map                /* o  : metadata index map                             */
    1864             : )
    1865             : {
    1866             :     int16_t map_idx, sf_idx, sf_length, increment, subframes_written;
    1867             :     float decimal, decimal_sum, eps;
    1868             :     int16_t subframe_map_length[MAX_PARAM_SPATIAL_SUBFRAMES];
    1869             : 
    1870             :     /* subframe map length */
    1871     2118729 :     sf_length = len / subframe_len;
    1872     2118729 :     if ( len % subframe_len == 0 )
    1873             :     {
    1874             :         /* even subframes */
    1875    10590565 :         for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    1876             :         {
    1877     8472452 :             subframe_map_length[sf_idx] = sf_length;
    1878             :         }
    1879             :     }
    1880             :     else
    1881             :     {
    1882             :         /* uneven subframes */
    1883         616 :         decimal = ( (float) len / (float) subframe_len ) - (float) sf_length;
    1884         616 :         decimal_sum = decimal;
    1885         616 :         eps = 0.001f;
    1886        3080 :         for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    1887             :         {
    1888        2464 :             increment = (int16_t) floorf( decimal_sum + eps );
    1889        2464 :             subframe_map_length[sf_idx] = sf_length + increment;
    1890        2464 :             if ( increment > 0 )
    1891             :             {
    1892        1242 :                 decimal_sum -= 1.0f;
    1893             :             }
    1894        2464 :             decimal_sum += decimal;
    1895             :         }
    1896             :     }
    1897             : 
    1898             :     /* map slots to subframes */
    1899     2118729 :     sf_idx = 0;
    1900     2118729 :     subframes_written = 0;
    1901    36020479 :     for ( map_idx = 0; map_idx < len; map_idx++ )
    1902             :     {
    1903    33901750 :         map[map_idx] = ( offset + sf_idx ) % buf_len;
    1904    33901750 :         if ( map_idx - subframes_written >= subframe_map_length[sf_idx] - 1 )
    1905             :         {
    1906     8474916 :             subframes_written += subframe_map_length[sf_idx];
    1907     8474916 :             ++sf_idx;
    1908             :         }
    1909             :     }
    1910             : 
    1911     2118729 :     return;
    1912             : }
    1913             : 
    1914             : 
    1915             : /*--------------------------------------------------------------------------*
    1916             :  * ivas_jbm_dec_get_num_tc_channels()
    1917             :  *
    1918             :  * Get the number of transport channels provided by the JBM transport channel decode function
    1919             :  *--------------------------------------------------------------------------*/
    1920             : 
    1921      459128 : int16_t ivas_jbm_dec_get_num_tc_channels(
    1922             :     Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    1923             : )
    1924             : {
    1925             :     int16_t num_tc;
    1926             :     int32_t ivas_total_brate;
    1927             :     AUDIO_CONFIG output_config;
    1928             : 
    1929             : 
    1930      459128 :     if ( st_ivas->renderer_type == RENDERER_DISABLE )
    1931             :     {
    1932       42983 :         num_tc = st_ivas->hDecoderConfig->nchan_out;
    1933             :     }
    1934             :     else
    1935             :     {
    1936      416145 :         num_tc = st_ivas->nchan_transport;
    1937             :     }
    1938      459128 :     output_config = st_ivas->hDecoderConfig->output_config;
    1939             : 
    1940      459128 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1941             : 
    1942             : #ifdef FIX_1435_MOVE_STEREO_PANNING
    1943      459128 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1944             :     {
    1945         564 :         num_tc = 1;
    1946             :     }
    1947      458564 :     else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
    1948             : #else
    1949             :     if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
    1950             : #endif
    1951             :     {
    1952         322 :         num_tc = 1;
    1953             :     }
    1954      458242 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    1955             :     {
    1956       44872 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
    1957             :         {
    1958        1205 :             num_tc = 1;
    1959             :         }
    1960             :     }
    1961      413370 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1962             :     {
    1963      148958 :         if ( st_ivas->sba_dirac_stereo_flag )
    1964             :         {
    1965        3514 :             num_tc = CPE_CHANNELS;
    1966             :         }
    1967      145444 :         else if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE && ( ivas_total_brate > IVAS_SID_5k2 || ( ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE > 0 && st_ivas->hCPE[0] != NULL && st_ivas->hCPE[0]->nchan_out == 1 ) ) )
    1968             :         {
    1969       45088 :             num_tc = 1; /* Only one channel transported */
    1970             :         }
    1971             : 
    1972      148958 :         if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->nchan_transport == 2 && st_ivas->hDecoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE && st_ivas->hDecoderConfig->ivas_total_brate > IVAS_SID_5k2 )
    1973             :         {
    1974        2532 :             num_tc = CPE_CHANNELS;
    1975             :         }
    1976      148958 :         if ( st_ivas->ivas_format == SBA_FORMAT )
    1977             :         {
    1978             : 
    1979       50938 :             if ( num_tc == 3 )
    1980             :             {
    1981       11146 :                 num_tc++;
    1982             :             }
    1983             :         }
    1984             :     }
    1985      264412 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1986             :     {
    1987      132836 :         if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
    1988             :         {
    1989      123975 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1990             :             {
    1991       72690 :                 num_tc++;
    1992             :             }
    1993       51285 :             else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1994             :             {
    1995       51285 :                 num_tc += st_ivas->nchan_ism;
    1996             :             }
    1997             :         }
    1998             :     }
    1999      131576 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2000             :     {
    2001       57656 :         if ( st_ivas->sba_dirac_stereo_flag )
    2002             :         {
    2003        5074 :             num_tc = CPE_CHANNELS;
    2004             :         }
    2005       57656 :         if ( num_tc == 3 )
    2006             :         {
    2007       11469 :             num_tc++;
    2008             :         }
    2009       57656 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2010             :         {
    2011       25114 :             num_tc += st_ivas->nchan_ism;
    2012             :         }
    2013             :     }
    2014       73920 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    2015             :     {
    2016       72403 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
    2017             :         {
    2018         775 :             num_tc = 1;
    2019             :         }
    2020       71628 :         else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO )
    2021             :         {
    2022        3811 :             num_tc = 2;
    2023             :         }
    2024       67817 :         else if ( st_ivas->mc_mode == MC_MODE_MCT )
    2025             :         {
    2026             :             /* do all static dmx already in the TC decoder if less channels than transported... */
    2027       29575 :             if ( st_ivas->transport_config != st_ivas->intern_config && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
    2028             :             {
    2029         790 :                 if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
    2030             :                 {
    2031           0 :                     num_tc = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    2032             :                 }
    2033             :             }
    2034       28785 :             else if ( ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
    2035             :             {
    2036        1234 :                 num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
    2037             :             }
    2038             :         }
    2039       38242 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2040             :         {
    2041        1933 :             num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    2042             :         }
    2043       36309 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    2044             :         {
    2045       21938 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
    2046             :             {
    2047        4270 :                 num_tc++;
    2048             :             }
    2049       21938 :             if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
    2050        4171 :                                                                 output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    2051        4108 :                                                                 output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
    2052             :             {
    2053             :                 /* LFE is synthesized in TD with the TCs*/
    2054         162 :                 num_tc++;
    2055             :             }
    2056             :         }
    2057             :     }
    2058             : #ifndef FIX_1435_MOVE_STEREO_PANNING
    2059             :     else if ( st_ivas->ivas_format == MONO_FORMAT && st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
    2060             :     {
    2061             :         num_tc = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
    2062             :     }
    2063             : #endif
    2064             : 
    2065      459128 :     return num_tc;
    2066             : }
    2067             : 
    2068             : 
    2069             : /*--------------------------------------------------------------------------*
    2070             :  * ivas_jbm_dec_get_render_granularity()
    2071             :  *
    2072             :  *
    2073             :  *--------------------------------------------------------------------------*/
    2074             : 
    2075             : /*! r: render granularity */
    2076      443971 : int16_t ivas_jbm_dec_get_render_granularity(
    2077             :     const RENDERER_TYPE renderer_type,     /* i  : renderer type            */
    2078             :     const RENDERER_TYPE renderer_type_sec, /* i  : secondary renderer type  */
    2079             :     const int32_t output_Fs                /* i  : sampling rate            */
    2080             : )
    2081             : {
    2082             :     int16_t render_granularity;
    2083             : 
    2084      443971 :     if ( renderer_type == RENDERER_BINAURAL_OBJECTS_TD ||                                                       /* TD renderer */
    2085      425039 :          renderer_type == RENDERER_BINAURAL_MIXER_CONV || renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM || /* Crend */
    2086             :          renderer_type_sec == RENDERER_BINAURAL_OBJECTS_TD                                                      /* TD rend as a secondary renderer -> set the common granularity for both renderers */
    2087             :     )
    2088             :     {
    2089             :         /* 5 ms granularity */
    2090       59377 :         render_granularity = NS2SA( output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
    2091             :     }
    2092             :     else
    2093             :     {
    2094             :         /* 1.25 ms granularity */
    2095      384594 :         render_granularity = NS2SA( output_Fs, CLDFB_SLOT_NS );
    2096             :     }
    2097             : 
    2098      443971 :     return render_granularity;
    2099             : }
    2100             : 
    2101             : 
    2102             : /*--------------------------------------------------------------------------*
    2103             :  * ivas_jbm_dec_tc_audio_allocate()
    2104             :  *
    2105             :  * allocate and initialize TC audio buffer
    2106             :  *--------------------------------------------------------------------------*/
    2107             : 
    2108      352356 : static ivas_error ivas_jbm_dec_tc_audio_allocate(
    2109             :     DECODER_TC_BUFFER_HANDLE hTcBuffer, /* i/o: JBM TSM buffer handle   */
    2110             :     const int32_t output_Fs,            /* i  : output sampling rate    */
    2111             :     const int16_t Opt_tsm               /* i  : TSM option flag         */
    2112             : )
    2113             : {
    2114             :     int16_t nsamp_to_allocate;
    2115             :     int16_t ch_idx, n_samp_full, n_samp_residual, offset;
    2116             : 
    2117      352356 :     if ( Opt_tsm )
    2118             :     {
    2119      123178 :         n_samp_full = ( NS2SA( output_Fs, MAX_JBM_L_FRAME_NS ) );
    2120      123178 :         n_samp_full = max( n_samp_full, L_FRAME48k ); /* buffers are shared between 'hTcBuffer->tc[]' and 'p_output_f[]': ensure minimal length */
    2121      123178 :         n_samp_residual = hTcBuffer->n_samples_granularity - 1;
    2122             :     }
    2123             :     else
    2124             :     {
    2125      229178 :         n_samp_full = (int16_t) ( output_Fs / FRAMES_PER_SEC );
    2126      229178 :         n_samp_residual = 0;
    2127             :     }
    2128             : 
    2129      352356 :     nsamp_to_allocate = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ) * n_samp_full;
    2130             : 
    2131      352356 :     if ( Opt_tsm )
    2132             :     {
    2133             :         /* note: this is stack memory buffer for time-scale modified audio signals */
    2134      123178 :         if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
    2135             :         {
    2136           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2137             :         }
    2138      123178 :         set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
    2139             : 
    2140      123178 :         offset = 0;
    2141      598759 :         for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
    2142             :         {
    2143      475581 :             hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
    2144      475581 :             offset += n_samp_full;
    2145             :         }
    2146     1618445 :         for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2147             :         {
    2148     1495267 :             hTcBuffer->tc[ch_idx] = NULL;
    2149             :         }
    2150             : 
    2151             :         /* memory buffer for TC audio samples not rendered in the previous frame */
    2152      598759 :         for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
    2153             :         {
    2154      475581 :             if ( ( hTcBuffer->tc_buffer_old[ch_idx] = (float *) malloc( n_samp_residual * sizeof( float ) ) ) == NULL )
    2155             :             {
    2156           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2157             :             }
    2158      475581 :             set_zero( hTcBuffer->tc_buffer_old[ch_idx], n_samp_residual );
    2159             :         }
    2160     1618445 :         for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2161             :         {
    2162     1495267 :             hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2163             :         }
    2164             :     }
    2165             :     else
    2166             :     {
    2167      229178 :         hTcBuffer->tc_buffer = NULL;
    2168             : 
    2169     3896026 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2170             :         {
    2171     3666848 :             hTcBuffer->tc[ch_idx] = NULL;
    2172             :         }
    2173             : 
    2174     3896026 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2175             :         {
    2176     3666848 :             hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2177             :         }
    2178             :     }
    2179             : 
    2180      352356 :     hTcBuffer->tc_buffer2 = NULL;
    2181             : 
    2182      352356 :     return IVAS_ERR_OK;
    2183             : }
    2184             : 
    2185             : 
    2186             : /*--------------------------------------------------------------------------*
    2187             :  * ivas_jbm_dec_tc_audio_deallocate()
    2188             :  *
    2189             :  * deallocate TC audio buffer
    2190             :  *--------------------------------------------------------------------------*/
    2191             : 
    2192      352356 : static void ivas_jbm_dec_tc_audio_deallocate(
    2193             :     DECODER_TC_BUFFER_HANDLE hTcBuffer /* i/o: JBM TSM buffer handle   */
    2194             : )
    2195             : {
    2196             :     int16_t ch_idx;
    2197             : 
    2198      352356 :     if ( hTcBuffer != NULL )
    2199             :     {
    2200      352356 :         if ( hTcBuffer->tc_buffer != NULL )
    2201             :         {
    2202     2094026 :             for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2203             :             {
    2204     1970848 :                 hTcBuffer->tc[ch_idx] = NULL;
    2205             :             }
    2206             : 
    2207      123178 :             free( hTcBuffer->tc_buffer );
    2208      123178 :             hTcBuffer->tc_buffer = NULL;
    2209             :         }
    2210             : 
    2211     5990052 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2212             :         {
    2213     5637696 :             if ( hTcBuffer->tc_buffer_old[ch_idx] != NULL )
    2214             :             {
    2215      475581 :                 free( hTcBuffer->tc_buffer_old[ch_idx] );
    2216      475581 :                 hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2217             :             }
    2218             :         }
    2219             : 
    2220      352356 :         if ( hTcBuffer->tc_buffer2 != NULL )
    2221             :         {
    2222      294495 :             free( hTcBuffer->tc_buffer2 );
    2223      294495 :             hTcBuffer->tc_buffer2 = NULL;
    2224             :         }
    2225             :     }
    2226             : 
    2227      352356 :     return;
    2228             : }
    2229             : 
    2230             : 
    2231             : /*--------------------------------------------------------------------------*
    2232             :  * ivas_jbm_dec_tc_buffer_open()
    2233             :  *
    2234             :  * open and initialize JBM transport channel buffer
    2235             :  *--------------------------------------------------------------------------*/
    2236             : 
    2237       89643 : ivas_error ivas_jbm_dec_tc_buffer_open(
    2238             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure              */
    2239             :     const TC_BUFFER_MODE tc_buffer_mode,    /* i  : buffer mode                         */
    2240             :     const int16_t nchan_transport_jbm,      /* i  : number of real transport channels   */
    2241             :     const int16_t nchan_transport_internal, /* i  : number of totally buffered channels */
    2242             :     const int16_t nchan_full,               /* i  : number of channels to fully store   */
    2243             :     const int16_t n_samples_granularity     /* i  : granularity of the renderer/buffer  */
    2244             : )
    2245             : {
    2246             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    2247             :     int16_t nMaxSlotsPerSubframe;
    2248             :     ivas_error error;
    2249             : 
    2250             :     /*-----------------------------------------------------------------*
    2251             :      * prepare library opening
    2252             :      *-----------------------------------------------------------------*/
    2253             : 
    2254       89643 :     if ( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
    2255             :     {
    2256           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2257             :     }
    2258             : 
    2259       89643 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    2260       89643 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    2261       89643 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    2262       89643 :     hTcBuffer->nchan_buffer_full = nchan_full;
    2263       89643 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    2264       89643 :     hTcBuffer->n_samples_available = 0;
    2265       89643 :     hTcBuffer->n_samples_buffered = 0;
    2266       89643 :     hTcBuffer->n_samples_rendered = 0;
    2267       89643 :     hTcBuffer->slots_rendered = 0;
    2268       89643 :     hTcBuffer->subframes_rendered = 0;
    2269       89643 :     hTcBuffer->n_samples_discard = 0;
    2270       89643 :     hTcBuffer->n_samples_flushed = 0;
    2271       89643 :     hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    2272             : 
    2273       89643 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
    2274       89643 :     hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
    2275       89643 :     set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    2276       89643 :     set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
    2277             : 
    2278       89643 :     if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
    2279             :     {
    2280           0 :         return error;
    2281             :     }
    2282             : 
    2283       89643 :     st_ivas->hTcBuffer = hTcBuffer;
    2284             : 
    2285       89643 :     return IVAS_ERR_OK;
    2286             : }
    2287             : 
    2288             : 
    2289             : /*--------------------------------------------------------------------------*
    2290             :  * ivas_jbm_dec_tc_buffer_reconfigure()
    2291             :  *
    2292             :  * open and initialize JBM transport channel buffer
    2293             :  *--------------------------------------------------------------------------*/
    2294             : 
    2295      262713 : ivas_error ivas_jbm_dec_tc_buffer_reconfigure(
    2296             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure                  */
    2297             :     const TC_BUFFER_MODE tc_buffer_mode,    /* i  : new buffer mode                         */
    2298             :     const int16_t nchan_transport_jbm,      /* i  : new number of real transport channels   */
    2299             :     const int16_t nchan_transport_internal, /* i  : new number of totally buffered channels */
    2300             :     const int16_t nchan_full,               /* i  : new number of channels to fully store   */
    2301             :     const int16_t n_samples_granularity     /* i  : new granularity of the renderer/buffer  */
    2302             : )
    2303             : {
    2304             :     int16_t ch_idx, num_tc_buffer_mem, n_samples_still_available;
    2305             :     float tc_buffer_mem[MAX_INTERN_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES - 1];
    2306             :     ivas_error error;
    2307             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    2308             : 
    2309      262713 :     hTcBuffer = st_ivas->hTcBuffer;
    2310             : 
    2311      262713 :     num_tc_buffer_mem = 0;
    2312      262713 :     n_samples_still_available = 0;
    2313             : 
    2314      262713 :     if ( st_ivas->hDecoderConfig->Opt_tsm )
    2315             :     {
    2316             :         /* save samples of the TC buffer from the previous frame */
    2317       84293 :         num_tc_buffer_mem = min( hTcBuffer->nchan_transport_internal, nchan_transport_internal );
    2318       84293 :         n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
    2319             : 
    2320             :         /* what is remaining from last frame needs always be smaller than the new granularity */
    2321       84293 :         assert( n_samples_still_available < n_samples_granularity );
    2322             : 
    2323      321182 :         for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
    2324             :         {
    2325      236889 :             mvr2r( hTcBuffer->tc_buffer_old[ch_idx] + hTcBuffer->n_samples_flushed, tc_buffer_mem[ch_idx], n_samples_still_available );
    2326             :         }
    2327             :     }
    2328             : 
    2329             :     /* if granularity changes, adapt subframe_nb_slots */
    2330      262713 :     if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
    2331             :     {
    2332             : #ifdef DEBUGGING
    2333             :         int16_t nMaxSlotsPerSubframeOld;
    2334             : #endif
    2335             :         int16_t nMaxSlotsPerSubframeNew;
    2336             : 
    2337       59919 :         nMaxSlotsPerSubframeNew = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / n_samples_granularity;
    2338             : #ifdef DEBUGGING
    2339             :         nMaxSlotsPerSubframeOld = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    2340             :         assert( hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] == nMaxSlotsPerSubframeOld );
    2341             :         if ( n_samples_granularity < hTcBuffer->n_samples_granularity )
    2342             :         {
    2343             :             assert( ( hTcBuffer->n_samples_granularity % n_samples_granularity ) == 0 );
    2344             :         }
    2345             :         else
    2346             :         {
    2347             :             assert( ( n_samples_granularity % hTcBuffer->n_samples_granularity ) == 0 );
    2348             :         }
    2349             : #endif
    2350             :         /* if samples were flushed, take that into account here */
    2351       59919 :         if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
    2352             :         {
    2353        5932 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
    2354        5932 :             hTcBuffer->n_samples_flushed = 0;
    2355             :         }
    2356             :         else
    2357             :         {
    2358       53987 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
    2359             :         }
    2360             :     }
    2361             : 
    2362      262713 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    2363      262713 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    2364      262713 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    2365      262713 :     hTcBuffer->nchan_buffer_full = nchan_full;
    2366      262713 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    2367             : 
    2368             :     /* reallocate TC audio buffers */
    2369             : 
    2370      262713 :     ivas_jbm_dec_tc_audio_deallocate( hTcBuffer );
    2371             : 
    2372      262713 :     if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
    2373             :     {
    2374           0 :         return error;
    2375             :     }
    2376             : 
    2377             :     /* propagate samples of the TC buffer from the previous frame */
    2378      499602 :     for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
    2379             :     {
    2380      236889 :         mvr2r( tc_buffer_mem[ch_idx], hTcBuffer->tc_buffer_old[ch_idx], n_samples_still_available );
    2381             :     }
    2382             : 
    2383      262713 :     return IVAS_ERR_OK;
    2384             : }
    2385             : 
    2386             : 
    2387             : /*--------------------------------------------------------------------------*
    2388             :  * ivas_jbm_dec_tc_buffer_playout()
    2389             :  *
    2390             :  *
    2391             :  *--------------------------------------------------------------------------*/
    2392             : 
    2393     9766199 : static void ivas_jbm_dec_tc_buffer_playout(
    2394             :     Decoder_Struct *st_ivas,
    2395             :     const uint16_t nSamplesAsked,
    2396             :     uint16_t *nSamplesRendered,
    2397             :     float *output[] )
    2398             : {
    2399             :     int16_t ch_idx, slot_size, slots_to_render, first_sf, last_sf;
    2400             : 
    2401     9766199 :     slot_size = st_ivas->hTcBuffer->n_samples_granularity;
    2402             : 
    2403             :     /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    2404     9766199 :     slots_to_render = min( st_ivas->hTcBuffer->num_slots - st_ivas->hTcBuffer->slots_rendered, nSamplesAsked / slot_size );
    2405     9766199 :     st_ivas->hTcBuffer->slots_rendered += slots_to_render;
    2406     9766199 :     *nSamplesRendered = (uint16_t) slots_to_render * slot_size;
    2407     9766199 :     first_sf = st_ivas->hTcBuffer->subframes_rendered;
    2408     9766199 :     last_sf = first_sf;
    2409             : 
    2410    30672318 :     while ( slots_to_render > 0 )
    2411             :     {
    2412    20906119 :         slots_to_render -= st_ivas->hTcBuffer->subframe_nbslots[last_sf];
    2413    20906119 :         last_sf++;
    2414             :     }
    2415             : #ifdef DEBUGGING
    2416             :     assert( slots_to_render == 0 );
    2417             : #endif
    2418             : 
    2419    29465585 :     for ( ch_idx = 0; ch_idx < st_ivas->hTcBuffer->nchan_transport_jbm; ch_idx++ )
    2420             :     {
    2421    19699386 :         output[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx] + st_ivas->hTcBuffer->n_samples_rendered;
    2422             :     }
    2423             : 
    2424     9766199 :     st_ivas->hTcBuffer->subframes_rendered = last_sf;
    2425             : 
    2426     9766199 :     return;
    2427             : }
    2428             : 
    2429             : 
    2430             : /*--------------------------------------------------------------------------*
    2431             :  * ivas_jbm_dec_tc_buffer_close()
    2432             :  *
    2433             :  * Close JBM transport channel buffer
    2434             :  *--------------------------------------------------------------------------*/
    2435             : 
    2436       89671 : void ivas_jbm_dec_tc_buffer_close(
    2437             :     DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
    2438             : )
    2439             : {
    2440       89671 :     if ( *phTcBuffer != NULL )
    2441             :     {
    2442       89643 :         ivas_jbm_dec_tc_audio_deallocate( *phTcBuffer );
    2443             : 
    2444       89643 :         free( *phTcBuffer );
    2445       89643 :         *phTcBuffer = NULL;
    2446             :     }
    2447             : 
    2448       89671 :     return;
    2449             : }
    2450             : 
    2451             : 
    2452             : /*--------------------------------------------------------------------------*
    2453             :  * ivas_jbm_dec_td_renderers_adapt_subframes()
    2454             :  *
    2455             :  * Close JBM transport channel buffer
    2456             :  *--------------------------------------------------------------------------*/
    2457             : 
    2458    11395523 : void ivas_jbm_dec_td_renderers_adapt_subframes(
    2459             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    2460             : )
    2461             : {
    2462             :     int16_t nMaxSlotsPerSubframe, nSlotsAvailable;
    2463             :     uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
    2464             : 
    2465    11395523 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    2466    11395523 :     nSlotsAvailable = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
    2467    11395523 :     st_ivas->hTcBuffer->num_slots = nSlotsAvailable;
    2468    11395523 :     st_ivas->hTcBuffer->n_samples_available = nSlotsAvailable * st_ivas->hTcBuffer->n_samples_granularity;
    2469    11395523 :     nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
    2470    11395523 :     st_ivas->hTcBuffer->nb_subframes = 0;
    2471             : 
    2472    11395523 :     if ( nSlotsInFirstSubframe > 0 )
    2473             :     {
    2474      221595 :         st_ivas->hTcBuffer->nb_subframes = 1;
    2475      221595 :         nSlotsAvailable -= nSlotsInFirstSubframe;
    2476             :     }
    2477    11395523 :     st_ivas->hTcBuffer->nb_subframes += (int16_t) ceilf( (float) nSlotsAvailable / (float) nMaxSlotsPerSubframe );
    2478    11395523 :     nSlotsInLastSubframe = nSlotsAvailable % nMaxSlotsPerSubframe;
    2479    11395523 :     set_s( st_ivas->hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    2480    11395523 :     set_s( st_ivas->hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, st_ivas->hTcBuffer->nb_subframes );
    2481             : 
    2482    11395523 :     if ( nSlotsInFirstSubframe > 0 )
    2483             :     {
    2484      221595 :         st_ivas->hTcBuffer->subframe_nbslots[0] = nSlotsInFirstSubframe;
    2485             :     }
    2486             : 
    2487    11395523 :     if ( nSlotsInLastSubframe > 0 )
    2488             :     {
    2489      221684 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nSlotsInLastSubframe;
    2490             :     }
    2491             : 
    2492    11395523 :     st_ivas->hTcBuffer->slots_rendered = 0;
    2493    11395523 :     st_ivas->hTcBuffer->subframes_rendered = 0;
    2494             : 
    2495    11395523 :     return;
    2496             : }
    2497             : 
    2498             : 
    2499             : /*--------------------------------------------------------------------------*
    2500             :  * ivas_jbm_dec_get_tc_buffer_mode()
    2501             :  *
    2502             :  *
    2503             :  *--------------------------------------------------------------------------*/
    2504             : 
    2505      327171 : TC_BUFFER_MODE ivas_jbm_dec_get_tc_buffer_mode(
    2506             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
    2507             : )
    2508             : {
    2509             :     TC_BUFFER_MODE buffer_mode;
    2510      327171 :     buffer_mode = TC_BUFFER_MODE_BUFFER;
    2511             : 
    2512      327171 :     switch ( st_ivas->renderer_type )
    2513             :     {
    2514             :             /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
    2515       49614 :         case RENDERER_DISABLE:
    2516             :         case RENDERER_MCMASA_MONO_STEREO:
    2517             :         case RENDERER_OSBA_STEREO:
    2518             :         case RENDERER_MONO_DOWNMIX:
    2519       49614 :             buffer_mode = TC_BUFFER_MODE_BUFFER;
    2520       49614 :             break;
    2521             : #ifdef FIX_1435_MOVE_STEREO_PANNING
    2522      250249 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    2523             : #endif
    2524             :         case RENDERER_TD_PANNING:
    2525             :         case RENDERER_BINAURAL_OBJECTS_TD:
    2526             :         case RENDERER_BINAURAL_FASTCONV:
    2527             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    2528             :         case RENDERER_BINAURAL_PARAMETRIC:
    2529             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    2530             :         case RENDERER_STEREO_PARAMETRIC:
    2531             :         case RENDERER_DIRAC:
    2532             :         case RENDERER_PARAM_ISM:
    2533             :         case RENDERER_BINAURAL_MIXER_CONV:
    2534             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    2535             :         case RENDERER_OMASA_OBJECT_EXT:
    2536             :         case RENDERER_OMASA_MIX_EXT:
    2537             :         case RENDERER_OSBA_AMBI:
    2538             :         case RENDERER_OSBA_LS:
    2539      250249 :             buffer_mode = TC_BUFFER_MODE_RENDERER;
    2540      250249 :             break;
    2541             : #ifndef FIX_1435_MOVE_STEREO_PANNING
    2542             :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    2543             :             if ( st_ivas->ivas_format == MONO_FORMAT )
    2544             :             {
    2545             :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2546             :             }
    2547             :             else
    2548             :             {
    2549             :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2550             :             }
    2551             : #endif
    2552             :             break;
    2553        4916 :         case RENDERER_MC_PARAMMC:
    2554        4916 :             if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
    2555             :             {
    2556         422 :                 buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
    2557             :             }
    2558             :             else
    2559             :             {
    2560        4494 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2561             :             }
    2562        4916 :             break;
    2563        4781 :         case RENDERER_MC:
    2564        4781 :             if ( ivas_jbm_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
    2565             :             {
    2566        3113 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2567             :             }
    2568        4781 :             break;
    2569       17611 :         case RENDERER_SBA_LINEAR_ENC:
    2570       17611 :             if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT && ( st_ivas->renderer_type == RENDERER_MC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe ) )
    2571             :             {
    2572         306 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2573             :             }
    2574             :             else
    2575             :             {
    2576       17305 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2577             :             }
    2578       17611 :             break;
    2579           0 :         case RENDERER_SBA_LINEAR_DEC:
    2580           0 :             if ( st_ivas->ivas_format == SBA_FORMAT && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) )
    2581             :             {
    2582           0 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2583             :             }
    2584             :             else
    2585             :             {
    2586           0 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2587             :             }
    2588           0 :             break;
    2589             : #ifdef DEBUGGING
    2590             :         default:
    2591             :             assert( 0 );
    2592             : #endif
    2593             :     }
    2594             : 
    2595      327171 :     return buffer_mode;
    2596             : }
    2597             : 
    2598             : 
    2599             : /*--------------------------------------------------------------------------*
    2600             :  * ivas_jbm_dec_metadata_open()
    2601             :  *
    2602             :  * Open structure for metadata buffering in JBM
    2603             :  *--------------------------------------------------------------------------*/
    2604             : 
    2605         510 : ivas_error ivas_jbm_dec_metadata_open(
    2606             :     Decoder_Struct *st_ivas )
    2607             : {
    2608             :     JBM_METADATA_HANDLE hJbmMetadata;
    2609         510 :     if ( ( hJbmMetadata = (JBM_METADATA_HANDLE) malloc( sizeof( JBM_METADATA ) ) ) == NULL )
    2610             :     {
    2611           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM metadata handle\n" ) );
    2612             :     }
    2613         510 :     hJbmMetadata->sf_write_idx = 0;
    2614         510 :     hJbmMetadata->sf_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_PARAM_SPATIAL_SUBFRAMES;
    2615             : 
    2616         510 :     hJbmMetadata->slot_write_idx = 0;
    2617         510 :     hJbmMetadata->slot_read_idx = 0;
    2618         510 :     hJbmMetadata->slot_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2619             : 
    2620         510 :     st_ivas->hJbmMetadata = hJbmMetadata;
    2621             : 
    2622         510 :     return IVAS_ERR_OK;
    2623             : }
    2624             : 
    2625             : 
    2626             : /*--------------------------------------------------------------------------*
    2627             :  * ivas_jbm_dec_copy_masa_meta_to_buffer()
    2628             :  *
    2629             :  * Copy decoded MASA metadata to a ring buffer
    2630             :  *--------------------------------------------------------------------------*/
    2631             : 
    2632       97962 : static void ivas_jbm_dec_copy_masa_meta_to_buffer(
    2633             :     Decoder_Struct *st_ivas )
    2634             : {
    2635             :     int16_t sf, dir, band;
    2636             :     JBM_METADATA_HANDLE hJbmMetadata;
    2637             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    2638             :     int16_t write_idx;
    2639             : 
    2640       97962 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2641       97962 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    2642             : 
    2643      489810 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    2644             :     {
    2645      391848 :         write_idx = ( hJbmMetadata->sf_write_idx + sf ) % hJbmMetadata->sf_md_buffer_length;
    2646             : 
    2647     1175544 :         for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    2648             :         {
    2649    19592400 :             for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2650             :             {
    2651    18808704 :                 hJbmMetadata->directionIndexBuffer[dir][write_idx][band] = extOutMeta->directionIndex[dir][sf][band];
    2652    18808704 :                 hJbmMetadata->directToTotalRatioBuffer[dir][write_idx][band] = extOutMeta->directToTotalRatio[dir][sf][band];
    2653    18808704 :                 hJbmMetadata->spreadCoherenceBuffer[dir][write_idx][band] = extOutMeta->spreadCoherence[dir][sf][band];
    2654             :             }
    2655             :         }
    2656             : 
    2657     9796200 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2658             :         {
    2659     9404352 :             hJbmMetadata->diffuseToTotalRatioBuffer[write_idx][band] = extOutMeta->diffuseToTotalRatio[sf][band];
    2660     9404352 :             hJbmMetadata->surroundCoherenceBuffer[write_idx][band] = extOutMeta->surroundCoherence[sf][band];
    2661             :         }
    2662             : 
    2663      391848 :         hJbmMetadata->numberOfDirections[write_idx] = extOutMeta->descriptiveMeta.numberOfDirections;
    2664             :     }
    2665             : 
    2666       97962 :     return;
    2667             : }
    2668             : 
    2669             : 
    2670             : /*--------------------------------------------------------------------------*
    2671             :  * ivas_jbm_masa_sf_to_slot_map()
    2672             :  *
    2673             :  * Map input MASA metadata subframes to slots in JBM processing
    2674             :  *--------------------------------------------------------------------------*/
    2675             : 
    2676       97962 : static void ivas_jbm_masa_sf_to_slot_map(
    2677             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure     */
    2678             :     const int16_t nCldfbTs   /* i  : number of CLDFB time slots */
    2679             : )
    2680             : {
    2681             :     int16_t sf_to_slot_map[MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME];
    2682             :     int16_t num_slots_in_subfr;
    2683             :     JBM_METADATA_HANDLE hJbmMetadata;
    2684             :     int16_t slot_idx;
    2685             :     int16_t write_idx, sf_index;
    2686             : 
    2687       97962 :     ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
    2688             : 
    2689             :     /* Set values */
    2690       97962 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2691       97962 :     num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
    2692             : 
    2693             :     /* Map input subframes to slots */
    2694       97962 :     ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, MAX_PARAM_SPATIAL_SUBFRAMES, sf_to_slot_map );
    2695             : 
    2696     1665179 :     for ( slot_idx = 0; slot_idx < nCldfbTs; slot_idx++ )
    2697             :     {
    2698     1567217 :         write_idx = ( hJbmMetadata->slot_write_idx + slot_idx ) % hJbmMetadata->slot_md_buffer_length;
    2699     1567217 :         sf_index = ( hJbmMetadata->sf_write_idx + sf_to_slot_map[slot_idx] ) % hJbmMetadata->sf_md_buffer_length;
    2700             : 
    2701     1567217 :         hJbmMetadata->sf_to_slot_map[write_idx] = sf_index;
    2702             :     }
    2703             : 
    2704       97962 :     hJbmMetadata->sf_write_idx = ( hJbmMetadata->sf_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hJbmMetadata->sf_md_buffer_length;
    2705       97962 :     hJbmMetadata->slot_write_idx = ( hJbmMetadata->slot_write_idx + nCldfbTs ) % hJbmMetadata->slot_md_buffer_length;
    2706             : 
    2707       97962 :     return;
    2708             : }
    2709             : 
    2710             : 
    2711             : /*--------------------------------------------------------------------------*
    2712             :  * ivas_jbm_masa_sf_to_sf_map()
    2713             :  *
    2714             :  * Map input MASA metadata subframes to output subframes in JBM processing
    2715             :  *--------------------------------------------------------------------------*/
    2716             : 
    2717       97944 : void ivas_jbm_masa_sf_to_sf_map(
    2718             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
    2719             : )
    2720             : {
    2721             :     int16_t sf_to_sf_map[MAX_PARAM_SPATIAL_SUBFRAMES];
    2722             :     JBM_METADATA_HANDLE hJbmMetadata;
    2723             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    2724             :     int16_t slot_read_idx, sf_read_idx;
    2725             :     int16_t sf_idx;
    2726             :     int16_t dir, band;
    2727             :     uint8_t numberOfDirections;
    2728             : 
    2729             :     /* Set values */
    2730       97944 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2731       97944 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    2732             : 
    2733             :     /* Map slots to subframes */
    2734      489720 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2735             :     {
    2736      391776 :         slot_read_idx = ( hJbmMetadata->slot_read_idx + 2 ) % hJbmMetadata->slot_md_buffer_length; /* Take the latter one of the middle slots of the output subframe */
    2737      391776 :         sf_to_sf_map[sf_idx] = hJbmMetadata->sf_to_slot_map[slot_read_idx];
    2738      391776 :         hJbmMetadata->slot_read_idx = ( hJbmMetadata->slot_read_idx + CLDFB_SLOTS_PER_SUBFRAME ) % hJbmMetadata->slot_md_buffer_length;
    2739             :     }
    2740             : 
    2741             :     /* Copy mapped metadata to the EXT meta buffer for writing */
    2742      489720 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2743             :     {
    2744      391776 :         sf_read_idx = sf_to_sf_map[sf_idx];
    2745             : 
    2746     1175328 :         for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    2747             :         {
    2748    19588800 :             for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2749             :             {
    2750    18805248 :                 extOutMeta->directionIndex[dir][sf_idx][band] = hJbmMetadata->directionIndexBuffer[dir][sf_read_idx][band];
    2751    18805248 :                 extOutMeta->directToTotalRatio[dir][sf_idx][band] = hJbmMetadata->directToTotalRatioBuffer[dir][sf_read_idx][band];
    2752    18805248 :                 extOutMeta->spreadCoherence[dir][sf_idx][band] = hJbmMetadata->spreadCoherenceBuffer[dir][sf_read_idx][band];
    2753             :             }
    2754             :         }
    2755             : 
    2756     9794400 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2757             :         {
    2758     9402624 :             extOutMeta->diffuseToTotalRatio[sf_idx][band] = hJbmMetadata->diffuseToTotalRatioBuffer[sf_read_idx][band];
    2759     9402624 :             extOutMeta->surroundCoherence[sf_idx][band] = hJbmMetadata->surroundCoherenceBuffer[sf_read_idx][band];
    2760             :         }
    2761             :     }
    2762             : 
    2763             :     /* Determine the number of directions for the frame to be written */
    2764       97944 :     numberOfDirections = 0;
    2765      422966 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2766             :     {
    2767      342075 :         sf_read_idx = sf_to_sf_map[sf_idx];
    2768             : 
    2769      342075 :         if ( hJbmMetadata->numberOfDirections[sf_read_idx] == 1 )
    2770             :         {
    2771       17053 :             numberOfDirections = 1;
    2772       17053 :             break;
    2773             :         }
    2774             :     }
    2775             : 
    2776       97944 :     extOutMeta->descriptiveMeta.numberOfDirections = numberOfDirections;
    2777             : 
    2778       97944 :     return;
    2779             : }
    2780             : 
    2781             : 
    2782             : /*--------------------------------------------------------------------------*
    2783             :  * ivas_dec_prepare_renderer()
    2784             :  *
    2785             :  * prepare IVAS renderer routine
    2786             :  *--------------------------------------------------------------------------*/
    2787             : 
    2788    21828523 : void ivas_dec_prepare_renderer(
    2789             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                                      */
    2790             : )
    2791             : {
    2792             :     int16_t n, n_render_timeslots;
    2793             : 
    2794    21828523 :     push_wmops( "ivas_dec_prepare_renderer" );
    2795             : 
    2796    21828523 :     n_render_timeslots = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
    2797             : 
    2798    21828523 :     if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
    2799             :     {
    2800     5187051 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2801             : 
    2802     5187051 :         if ( ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hDecoderConfig->Opt_tsm )
    2803             :         {
    2804       94872 :             ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
    2805             :         }
    2806             : 
    2807             :         /* MASA transport gaining for edited disc OMASA EXT. For ISMs, only metadata is modified */
    2808     5187051 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
    2809             :         {
    2810           0 :             ivas_omasa_gain_masa_tc( st_ivas->hTcBuffer->tc, st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->nchan_ism, st_ivas->hTcBuffer->n_samples_available );
    2811             :         }
    2812             :     }
    2813    16641472 :     else if ( st_ivas->ivas_format == MONO_FORMAT || st_ivas->ivas_format == STEREO_FORMAT )
    2814             :     {
    2815       35008 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2816             :     }
    2817    16606464 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    2818             :     {
    2819     2444514 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    2820             :         {
    2821      461421 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    2822             :             {
    2823      267595 :                 ivas_dirac_dec_set_md_map( st_ivas, n_render_timeslots );
    2824      267595 :                 ivas_param_ism_params_to_masa_param_mapping( st_ivas );
    2825             :             }
    2826      193826 :             else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    2827             :             {
    2828      193826 :                 ivas_param_ism_dec_prepare_renderer( st_ivas, n_render_timeslots );
    2829             :             }
    2830             :         }
    2831             :         else /* ISM_MODE_DISC */
    2832             :         {
    2833     1983093 :             ivas_ism_dec_digest_tc( st_ivas );
    2834             :         }
    2835             :     }
    2836    14161950 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    2837             :     {
    2838     8581092 :         ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2839             :     }
    2840     5580858 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2841             :     {
    2842     2409748 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2843             :         {
    2844     1287488 :             ivas_ism_dec_digest_tc( st_ivas );
    2845             : 
    2846             :             /* delay the objects here for all renderers where it is needed */
    2847     1287488 :             if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    2848      815242 :                    st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    2849      640017 :                    st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
    2850      459531 :                    st_ivas->renderer_type == RENDERER_OSBA_LS ||
    2851      194809 :                    st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) &&
    2852     1204623 :                  ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    2853             :             {
    2854     4256789 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    2855             :                 {
    2856     3130666 :                     delay_signal( st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hSbaIsmData->delayBuffer[n], st_ivas->hSbaIsmData->delayBuffer_size );
    2857             :                 }
    2858             :             }
    2859             : 
    2860     1287488 :             if ( !st_ivas->sba_dirac_stereo_flag )
    2861             :             {
    2862     1204623 :                 if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
    2863             :                 {
    2864      472246 :                     n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
    2865             :                 }
    2866             : 
    2867     1204623 :                 ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2868             :             }
    2869             :         }
    2870             :         else
    2871             :         {
    2872     1122260 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2873             : 
    2874     1122260 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2875             :         }
    2876             :     }
    2877     3171110 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2878             :     {
    2879     1008228 :         if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
    2880             :         {
    2881       17248 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2882             : 
    2883       17248 :             if ( st_ivas->hDecoderConfig->Opt_tsm )
    2884             :             {
    2885        3090 :                 ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
    2886             :             }
    2887             : 
    2888             :             /* MASA transport gaining for edited param_one OMASA EXT. For ISMs, only metadata is modified.  */
    2889       17248 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
    2890             :             {
    2891           0 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2892             :                 {
    2893           0 :                     v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available );
    2894             :                 }
    2895             :             }
    2896             :         }
    2897             :         else
    2898             :         {
    2899      990980 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2900             :             {
    2901      127463 :                 n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
    2902             :             }
    2903             : 
    2904      990980 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2905             : 
    2906      990980 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2907             :             {
    2908      526130 :                 ivas_ism_dec_digest_tc( st_ivas );
    2909             :             }
    2910             : 
    2911      990980 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_DIRAC )
    2912             :             {
    2913             :                 int16_t nchan_transport_ism;
    2914             : 
    2915             :                 /* Delay the signal to match CLDFB delay. Delay the whole buffer. */
    2916      672825 :                 nchan_transport_ism = 0;
    2917      672825 :                 if ( ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ) && st_ivas->renderer_type != RENDERER_BINAURAL_PARAMETRIC )
    2918             :                 {
    2919      181406 :                     nchan_transport_ism = 1;
    2920             :                 }
    2921      491419 :                 else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2922             :                 {
    2923      374880 :                     nchan_transport_ism = st_ivas->nchan_ism;
    2924             :                 }
    2925             : 
    2926      672825 :                 if ( st_ivas->hDecoderConfig->Opt_tsm && st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_DIRAC )
    2927             :                 {
    2928             :                     /* Gain MASA part, if edited */
    2929      118568 :                     if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
    2930             :                     {
    2931       62598 :                         for ( n = 0; n < CPE_CHANNELS; n++ )
    2932             :                         {
    2933       41732 :                             v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available );
    2934             :                         }
    2935             :                     }
    2936             :                 }
    2937             : 
    2938     1737693 :                 for ( n = 0; n < nchan_transport_ism; n++ )
    2939             :                 {
    2940     1064868 :                     if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    2941             :                     {
    2942             :                         float gainIsm;
    2943      327513 :                         gainIsm = OMASA_TDREND_MATCHING_GAIN;
    2944             : 
    2945      327513 :                         if ( st_ivas->hMasaIsmData->ism_gain_is_edited[n] )
    2946             :                         {
    2947       97472 :                             gainIsm *= st_ivas->hMasaIsmData->gain_ism_edited[n];
    2948             :                         }
    2949             : 
    2950      327513 :                         v_multc( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], gainIsm, st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available );
    2951             :                     }
    2952             : 
    2953     1064868 :                     if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2954             :                     {
    2955      974957 :                         delay_signal( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hMasaIsmData->delayBuffer[n], st_ivas->hMasaIsmData->delayBuffer_size );
    2956             :                     }
    2957             :                 }
    2958             : 
    2959      672825 :                 if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    2960             :                 {
    2961             :                     int16_t tcBufferSize;
    2962      127463 :                     tcBufferSize = st_ivas->hSpatParamRendCom->num_slots * st_ivas->hSpatParamRendCom->slot_size;
    2963             : 
    2964      127463 :                     if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
    2965             :                     {
    2966       83283 :                         for ( n = 0; n < BINAURAL_CHANNELS; n++ )
    2967             :                         {
    2968       55522 :                             v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], tcBufferSize );
    2969             :                         }
    2970             :                     }
    2971             :                 }
    2972             :             }
    2973             :         }
    2974             :     }
    2975     2162882 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    2976             :     {
    2977     2162882 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    2978             :         {
    2979     1109992 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2980             :         }
    2981     1052890 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2982             :         {
    2983      127253 :             ivas_mc_paramupmix_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2984             :         }
    2985      925637 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    2986             :         {
    2987      448549 :             ivas_param_mc_dec_prepare_renderer( st_ivas, (uint8_t) n_render_timeslots );
    2988             :         }
    2989      477088 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    2990             :         {
    2991      477088 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2992             :         }
    2993             :     }
    2994             : 
    2995    21828523 :     pop_wmops();
    2996    21828523 :     return;
    2997             : }

Generated by: LCOV version 1.14