LCOV - code coverage report
Current view: top level - lib_dec - ivas_jbm_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 990 1075 92.1 %
Date: 2025-10-27 07:01:45 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     5712495 : 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     5712495 :     push_wmops( "ivas_jbm_dec_tc" );
      82             : 
      83             :     /*----------------------------------------------------------------*
      84             :      * Initialization of local vars after struct has been set
      85             :      *----------------------------------------------------------------*/
      86             : 
      87     5712495 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
      88     5712495 :     nchan_out = st_ivas->hTcBuffer->nchan_transport_jbm;
      89     5712495 :     output_config = st_ivas->hDecoderConfig->output_config;
      90     5712495 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
      91             : 
      92     5712495 :     output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
      93             : 
      94             :     /* set pointers to transport channels audio */
      95    74262435 :     for ( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
      96             :     {
      97    68549940 :         p_output[n] = st_ivas->p_output_f[n];
      98    68549940 :         if ( p_output[n] != NULL )
      99             :         {
     100    39005925 :             set_zero( p_output[n], L_FRAME48k );
     101             :         }
     102             :     }
     103             : 
     104             :     /*----------------------------------------------------------------*
     105             :      * Decoding + pre-rendering
     106             :      *----------------------------------------------------------------*/
     107             : 
     108     5712495 :     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     5712495 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
     124             :     {
     125      304290 :         st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     126      304290 :         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      828687 :         for ( n = 0; n < min( nchan_out, st_ivas->nchan_transport ); n++ )
     133             :         {
     134      524397 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     135             :         }
     136             :     }
     137     5408205 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
     138             :     {
     139             :         /* Metadata decoding and configuration */
     140     1163743 :         if ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA )
     141             :         {
     142        7353 :             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        7353 :             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        7353 :             ivas_ism_dtx_limit_noise_energy_for_near_silence( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport );
     151             : 
     152        7353 :             ivas_param_ism_dec_dequant_md( st_ivas );
     153             :         }
     154     1156390 :         else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     155             :         {
     156      148488 :             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      148488 :             ivas_param_ism_dec_dequant_md( st_ivas );
     162             :         }
     163             :         else /* ISM_MODE_DISC */
     164             :         {
     165     1007902 :             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     4125602 :         for ( n = 0; n < st_ivas->nchan_transport; n++ )
     172             :         {
     173             :             /* for DTX frames, dominant object has already been decoded before */
     174     2961859 :             if ( !( ( ivas_total_brate == IVAS_SID_5k2 || ivas_total_brate == FRAME_NO_DATA ) && n == st_ivas->hISMDTX.sce_id_dtx ) )
     175             :             {
     176     2954506 :                 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     2961859 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     184             :         }
     185             : 
     186     1163743 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     187             :         {
     188       68605 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     189             :         }
     190     1095138 :         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       57383 :             ivas_dirac_dec_binaural_sba_gain( p_output, st_ivas->nchan_transport, output_frame );
     194             :         }
     195             :     }
     196     4244462 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
     197             :     {
     198     1498810 :         set_s( nb_bits_metadata, 0, MAX_SCE );
     199             : 
     200             : 
     201             :         /* read parameters from the bitstream */
     202     1498810 :         if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hQMetaData != NULL )
     203             :         {
     204      493613 :             st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     205             : 
     206      493613 :             if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     207             :             {
     208           0 :                 return error;
     209             :             }
     210             :         }
     211     1005197 :         else if ( st_ivas->ivas_format == SBA_FORMAT )
     212             :         {
     213     1005197 :             if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
     214             :             {
     215           0 :                 return error;
     216             :             }
     217             :         }
     218             : 
     219     1498810 :         if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
     220             :         {
     221      615652 :             st_ivas->hCPE[0]->brate_surplus = 0;
     222      615652 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     223             :         }
     224             : 
     225             :         /* core-decoding of transport channels */
     226     1498810 :         if ( st_ivas->nSCE == 1 )
     227             :         {
     228      466011 :             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     1032799 :         else if ( st_ivas->nCPE == 1 )
     234             :         {
     235      615652 :             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      417147 :         else if ( st_ivas->nCPE > 1 )
     241             :         {
     242      417147 :             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     1498810 :         nchan_remapped = st_ivas->nchan_transport;
     254     1498810 :         if ( st_ivas->sba_dirac_stereo_flag )
     255             :         {
     256       67856 :             nchan_remapped = nchan_out;
     257             : 
     258       67856 :             if ( st_ivas->ivas_format == SBA_FORMAT )
     259             :             {
     260       67856 :                 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       67856 :                 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       67856 :                 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       67856 :             ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, st_ivas->ivas_format == MC_FORMAT );
     271             :         }
     272     1430954 :         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      155889 :             nchan_remapped = 1; /* Only one channel transported */
     275             :         }
     276             : 
     277             :         /* HP filtering */
     278             : #ifndef DEBUG_SPAR_BYPASS_EVS_CODEC
     279     4523184 :         for ( n = 0; n < nchan_remapped; n++ )
     280             :         {
     281     3024374 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     282             :         }
     283             : #endif
     284             : 
     285     1498810 :         if ( st_ivas->ivas_format == SBA_FORMAT )
     286             :         {
     287     1005197 :             nchan_remapped = ivas_sba_remapTCs( p_output, st_ivas, output_frame );
     288             : 
     289     1005197 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     290             :             {
     291      211366 :                 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      211366 :                 ivas_sba_mix_matrix_determiner( st_ivas->hSpar, p_output, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames );
     293             :             }
     294      793831 :             else if ( st_ivas->renderer_type != RENDERER_DISABLE )
     295             :             {
     296      696794 :                 ivas_spar_dec_agc_pca( st_ivas, p_output, output_frame );
     297             :             }
     298             :         }
     299             : 
     300     1498810 :         if ( st_ivas->ivas_format == MASA_FORMAT )
     301             :         {
     302      493613 :             ivas_masa_prerender( st_ivas, p_output, output_frame, nchan_remapped );
     303             : 
     304             :             /* external output */
     305      493613 :             if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
     306             :             {
     307       27522 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
     308             :                 {
     309       19962 :                     set_zero( p_output[st_ivas->nchan_transport + n], output_frame );
     310             :                 }
     311             : 
     312        7560 :                 ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, output_frame );
     313             :             }
     314             :         }
     315     1005197 :         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      211366 :             ivas_dirac_dec_binaural_sba_gain( p_output, nchan_remapped, output_frame );
     319             :         }
     320             :     }
     321     2745652 :     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      467186 :         set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
     327             : 
     328             :         /* Set the number of objects for the parametric rendering */
     329      467186 :         dirac_bs_md_write_idx = 0;
     330      467186 :         if ( st_ivas->hSpatParamRendCom != NULL )
     331             :         {
     332      414164 :             st_ivas->hSpatParamRendCom->numIsmDirections = 0;
     333      414164 :             if ( st_ivas->ism_mode != ISM_MASA_MODE_DISC && st_ivas->ism_mode != ISM_MASA_MODE_MASA_ONE_OBJ )
     334             :             {
     335       64407 :                 st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
     336             :             }
     337             : 
     338      414164 :             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      467186 :         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      467186 :         ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
     349             : 
     350      467186 :         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      467186 :         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     1339525 :         for ( n = 0; n < nchan_transport_ism; n++ )
     360             :         {
     361      872339 :             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      467186 :         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      467186 :         if ( st_ivas->hCPE[0]->nchan_out == 1 )
     374             :         {
     375       64974 :             mvr2r( p_output[0], p_output[1], output_frame ); /* Copy mono signal to stereo output channels */
     376             :         }
     377             : 
     378             :         /* HP filtering */
     379     2273897 :         for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     380             :         {
     381     1806711 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     382             :         }
     383             : 
     384      467186 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
     385             :         {
     386       28743 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     387             :         }
     388      438443 :         else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
     389             :         {
     390       28263 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     391             :             {
     392       21795 :                 ivas_omasa_rearrange_channels( p_output, nchan_transport_ism, output_frame );
     393             :             }
     394        6468 :             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        2484 :                 ivas_omasa_combine_separate_ism_with_masa( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
     398             :             }
     399        3984 :             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        3984 :                 ivas_omasa_render_objects_from_mix( st_ivas, p_output, st_ivas->nchan_ism, output_frame );
     403             :             }
     404             :         }
     405             :     }
     406     2278466 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
     407             :     {
     408             :         int16_t nchan_ism, sba_ch_idx;
     409             : 
     410     1716673 :         set_s( nb_bits_metadata, 0, MAX_SCE + 1 );
     411     1716673 :         nchan_ism = st_ivas->nchan_ism;
     412     1716673 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     413             :         {
     414             :             /* set ISM parameters and decode ISM metadata in OSBA format */
     415      764386 :             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      764386 :             sba_ch_idx = st_ivas->nchan_ism;
     420             :         }
     421             :         else
     422             :         {
     423      952287 :             nb_bits_metadata[1] += NO_BITS_MASA_ISM_NO_OBJ;
     424      952287 :             sba_ch_idx = 0;
     425             :         }
     426             : 
     427             :         /* SBA metadata decoding */
     428     1716673 :         if ( ( error = ivas_spar_dec( st_ivas, nb_bits_metadata ) ) != IVAS_ERR_OK )
     429             :         {
     430           0 :             return error;
     431             :         }
     432             : 
     433     1716673 :         if ( st_ivas->nchan_transport == CPE_CHANNELS && st_ivas->nCPE >= 1 )
     434             :         {
     435      353357 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     436             :         }
     437             : 
     438             :         /* core-decoding of transport channels */
     439     1716673 :         if ( st_ivas->nSCE == 1 )
     440             :         {
     441      509009 :             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     1207664 :         else if ( st_ivas->nCPE == 1 )
     447             :         {
     448      353357 :             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      854307 :         else if ( st_ivas->nCPE > 1 )
     454             :         {
     455      854307 :             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     1716673 :         if ( st_ivas->sba_dirac_stereo_flag )
     462             :         {
     463      108792 :             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      108792 :             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      108792 :             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      108792 :             ivas_sba_dirac_stereo_dec( st_ivas, &p_output[sba_ch_idx], output_frame, 0 );
     473             :         }
     474             : 
     475             :         /* HP filtering */
     476     7738890 :         for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     477             :         {
     478     6022217 :             hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     479             :         }
     480             : 
     481     1716673 :         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     1716673 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     498             :         {
     499      266231 :             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      266231 :             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     1450442 :         else if ( st_ivas->renderer_type != RENDERER_DISABLE && !st_ivas->sba_dirac_stereo_flag )
     504             :         {
     505     1278672 :             ivas_spar_dec_agc_pca( st_ivas, &p_output[sba_ch_idx], output_frame );
     506             :         }
     507             : 
     508     1716673 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
     509             :         {
     510             :             /* loudness correction */
     511      266231 :             ivas_dirac_dec_binaural_sba_gain( &p_output[sba_ch_idx], nchan_remapped, output_frame );
     512             :         }
     513     1450442 :         else if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
     514             :         {
     515       44814 :             ivas_ism_mono_dmx( st_ivas, p_output, output_frame );
     516             : 
     517             :             /* add W */
     518       89628 :             for ( n = 0; n < nchan_out; n++ )
     519             :             {
     520       44814 :                 v_add( p_output[n], p_output[n + max( nchan_out, nchan_ism )], p_output[n], output_frame );
     521             :             }
     522             :         }
     523             :     }
     524      561793 :     else if ( st_ivas->ivas_format == MC_FORMAT )
     525             :     {
     526      561793 :         st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     527             : 
     528      561793 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
     529             :         {
     530             :             /* LFE channel decoder */
     531      290261 :             ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
     532             : 
     533      290261 :             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     2479471 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     540             :             {
     541     2189210 :                 if ( n != LFE_CHANNEL )
     542             :                 {
     543     1898949 :                     hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     544             :                 }
     545             :             }
     546             : 
     547      290261 :             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        8250 :                 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      290261 :             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       58467 :                 if ( st_ivas->renderer_type == RENDERER_MC )
     558             :                 {
     559       46197 :                     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      271532 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
     568             :         {
     569             :             /* LFE channel decoder */
     570       14569 :             ivas_lfe_dec( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output[LFE_CHANNEL] );
     571             : 
     572       14569 :             ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
     573             : 
     574       14569 :             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      131121 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     581             :             {
     582      116552 :                 if ( n != LFE_CHANNEL )
     583             :                 {
     584      101983 :                     hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     585             :                 }
     586             :             }
     587             : 
     588             :             /* Rendering */
     589       14569 :             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        4620 :                 for ( n = 4; n < 8; n++ )
     593             :                 {
     594        3696 :                     v_multc( p_output[n], 2.0f, p_output[n], output_frame );
     595             :                 }
     596             : 
     597         924 :                 if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
     598             :                 {
     599         924 :                     ivas_ls_setup_conversion( st_ivas, audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 ), output_frame, p_output, p_output );
     600             :                 }
     601             :             }
     602             :         }
     603      256963 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
     604             :         {
     605             :             /* read Parametric MC parameters from the bitstream */
     606      105782 :             ivas_param_mc_dec_read_BS( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
     607             : 
     608      105782 :             if ( st_ivas->nCPE == 1 )
     609             :             {
     610       74421 :                 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       31361 :             else if ( st_ivas->nCPE > 1 )
     616             :             {
     617       31361 :                 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      348707 :             for ( n = 0; n < st_ivas->nchan_transport; n++ )
     625             :             {
     626      242925 :                 hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     627             :             }
     628             : 
     629             :             /* Rendering */
     630      105782 :             if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
     631             :             {
     632        8376 :                 ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, output_frame, p_output, p_output );
     633             :             }
     634             :         }
     635      151181 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
     636             :         {
     637      151181 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
     638             :             {
     639       25709 :                 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      151181 :             if ( ( error = ivas_masa_decode( st_ivas, st, &nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     644             :             {
     645           0 :                 return error;
     646             :             }
     647             : 
     648      151181 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
     649             :             {
     650             :                 /* Decode the transport audio signals */
     651       25709 :                 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       25709 :                 n = LFE_CHANNEL - 1;
     658             : 
     659             :                 /* Decode the separated channel to output[n] to be combined with the synthesized channels */
     660       25709 :                 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       25709 :                 if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
     667       20707 :                      output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
     668       17587 :                      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        9992 :                     ivas_lfe_synth_with_filters( st_ivas->hMasa->hMasaLfeSynth, p_output, output_frame, n, LFE_CHANNEL );
     671             :                 }
     672       15717 :                 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      125472 :                 if ( st_ivas->nSCE == 1 )
     681             :                 {
     682      114796 :                     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       10676 :                 else if ( st_ivas->nCPE == 1 )
     688             :                 {
     689       10676 :                     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      151181 :             if ( st_ivas->sba_dirac_stereo_flag ) /* use the flag to trigger the DFT upmix */
     697             :             {
     698        8559 :                 ivas_sba_dirac_stereo_dec( st_ivas, p_output, output_frame, 1 );
     699             :             }
     700             : 
     701             :             /* HP filtering */
     702      373015 :             for ( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     703             :             {
     704      221834 :                 hp20( p_output[n], output_frame, st_ivas->mem_hp20_out[n], output_Fs );
     705             :             }
     706             : 
     707      151181 :             if ( st_ivas->renderer_type == RENDERER_MCMASA_MONO_STEREO )
     708             :             {
     709        5361 :                 ivas_mono_stereo_downmix_mcmasa( st_ivas, p_output, output_frame );
     710             :             }
     711             :         }
     712             :     }
     713             : 
     714             :     /*----------------------------------------------------------------*
     715             :      * Common updates
     716             :      *----------------------------------------------------------------*/
     717             : 
     718     5712495 :     if ( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
     719             :     {
     720     5267191 :         st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
     721     5267191 :         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     5712495 :     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     4677089 :         st_ivas->ini_frame++;
     727             :     }
     728             : 
     729     5712495 :     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     4674150 :         st_ivas->ini_active_frame++;
     732             :     }
     733             : 
     734     5712495 :     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     5712495 :     pop_wmops();
     746     5712495 :     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     5751705 : 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     5751705 :     hTcBuffer = st_ivas->hTcBuffer;
     770     5751705 :     n_ch_cldfb = hTcBuffer->nchan_transport_jbm - hTcBuffer->nchan_buffer_full;
     771             : 
     772     5751705 :     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     2104738 :         n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
     778     2104738 :         hTcBuffer->n_samples_buffered = n_samples_still_available + nSamplesForRendering + hTcBuffer->n_samples_discard;
     779     2104738 :         hTcBuffer->n_samples_available = hTcBuffer->n_samples_granularity * ( hTcBuffer->n_samples_buffered / hTcBuffer->n_samples_granularity );
     780     2104738 :         *nSamplesResidual = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_available;
     781     2104738 :         n_ch_full_copy = min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
     782     2104738 :         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     2104738 :         len_offset = NS2SA( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS );
     788     2104738 :         if ( len_offset < L_FRAME48k )
     789             :         {
     790      633493 :             offset = 0;
     791     3085911 :             for ( ch = 0; ch < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch++ )
     792             :             {
     793     2452418 :                 hTcBuffer->tc[ch] = &hTcBuffer->tc_buffer[offset];
     794     2452418 :                 offset += len_offset;
     795             :             }
     796             :         }
     797             : 
     798     8330318 :         for ( ch = 0; ch < n_ch_full_copy; ch++ )
     799             :         {
     800     6225580 :             mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
     801     6225580 :             set_zero( hTcBuffer->tc[ch], hTcBuffer->n_samples_discard );
     802     6225580 :             mvr2r( hTcBuffer->tc_buffer_old[ch], hTcBuffer->tc[ch] + hTcBuffer->n_samples_discard, n_samples_still_available );
     803     6225580 :             mvr2r( tmp_buf, hTcBuffer->tc[ch] + n_samples_still_available + hTcBuffer->n_samples_discard, nSamplesForRendering - *nSamplesResidual );
     804     6225580 :             mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
     805             :         }
     806             : 
     807     2104738 :         if ( n_ch_res_copy > 0 )
     808             :         {
     809      202143 :             for ( ; ch < hTcBuffer->nchan_transport_jbm; ch++ )
     810             :             {
     811      138969 :                 p_data_f[ch] = hTcBuffer->tc[ch];
     812      138969 :                 mvr2r( hTcBuffer->tc[ch], tmp_buf, nSamplesForRendering );
     813      138969 :                 mvr2r( hTcBuffer->tc_buffer_old[ch], p_data_f[ch], n_samples_still_available );
     814      138969 :                 mvr2r( tmp_buf, p_data_f[ch] + n_samples_still_available, nSamplesForRendering - *nSamplesResidual );
     815      138969 :                 mvr2r( tmp_buf + nSamplesForRendering - *nSamplesResidual, hTcBuffer->tc_buffer_old[ch], *nSamplesResidual );
     816             :             }
     817             :         }
     818             : 
     819     2104738 :         n_render_timeslots = hTcBuffer->n_samples_available / hTcBuffer->n_samples_granularity;
     820             :     }
     821             :     else
     822             :     {
     823     3869777 :         for ( n = 0; n < n_ch_cldfb; n++ )
     824             :         {
     825      222810 :             p_data_f[n] = &st_ivas->p_output_f[n][0];
     826             :         }
     827             : 
     828     3646967 :         ch = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
     829    17895900 :         for ( n = 0; n < ch; n++ )
     830             :         {
     831    14248933 :             hTcBuffer->tc[n] = st_ivas->p_output_f[n]; /* note: buffers needed in the TD decorellator */
     832             :         }
     833             : 
     834     3646967 :         hTcBuffer->n_samples_buffered = nSamplesForRendering;
     835     3646967 :         hTcBuffer->n_samples_available = hTcBuffer->n_samples_buffered;
     836     3646967 :         *nSamplesResidual = 0;
     837             : 
     838     3646967 :         n_render_timeslots = DEFAULT_JBM_CLDFB_TIMESLOTS;
     839             :     }
     840             : 
     841             :     /* CLDFB analysis for ParamMC/ParamISM */
     842     5751705 :     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 ) )
     843             :     {
     844       69318 :         ivas_param_ism_dec_digest_tc( st_ivas, n_render_timeslots, p_data_f );
     845             :     }
     846     5682387 :     else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC && hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_RENDERER )
     847             :     {
     848       97406 :         ivas_param_mc_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, p_data_f );
     849             :     }
     850             : 
     851     5751705 :     hTcBuffer->n_samples_rendered = 0;
     852     5751705 :     hTcBuffer->subframes_rendered = 0;
     853             : 
     854     5751705 :     return;
     855             : }
     856             : 
     857             : 
     858             : /*--------------------------------------------------------------------------*
     859             :  * ivas_dec_render()
     860             :  *
     861             :  * Principal IVAS JBM rendering routine
     862             :  *--------------------------------------------------------------------------*/
     863             : 
     864     7451957 : ivas_error ivas_jbm_dec_render(
     865             :     Decoder_Struct *st_ivas,             /* i/o: IVAS decoder structure                                      */
     866             :     const uint16_t nSamplesAsked,        /* i  : number of samples wanted                                    */
     867             :     uint16_t *nSamplesRendered,          /* o  : number of samples rendered                                  */
     868             :     uint16_t *nSamplesAvailableNext,     /* o  : number of samples still available in the rendering pipeline */
     869             :     const PCM_RESOLUTION pcm_resolution, /* i  : type for the decoded PCM resolution                         */
     870             :     void *data                           /* o  : output synthesis signal                                     */
     871             : )
     872             : {
     873             :     int16_t n, nchan_out;
     874             :     int16_t nchan_transport;
     875             :     int16_t nchan_remapped;
     876             :     int32_t output_Fs;
     877             :     AUDIO_CONFIG output_config;
     878             :     int16_t nSamplesAskedLocal;
     879             :     ivas_error error;
     880             :     float *p_output[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
     881             :     float *p_tc[MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS];
     882             :     int16_t nchan_out_syn_output;
     883             : 
     884     7451957 :     push_wmops( "ivas_dec_render" );
     885             :     /*----------------------------------------------------------------*
     886             :      * Initialization of local vars after struct has been set
     887             :      *----------------------------------------------------------------*/
     888             : 
     889     7451957 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     890     7451957 :     nchan_out = st_ivas->hDecoderConfig->nchan_out;
     891     7451957 :     nchan_transport = st_ivas->hTcBuffer->nchan_transport_jbm;
     892     7451957 :     output_config = st_ivas->hDecoderConfig->output_config;
     893     7451957 :     nSamplesAskedLocal = nSamplesAsked + st_ivas->hTcBuffer->n_samples_discard;
     894             : 
     895   156491097 :     for ( n = 0; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     896             :     {
     897   149039140 :         p_output[n] = st_ivas->p_output_f[n];
     898             :     }
     899             : 
     900     7451957 :     if ( !st_ivas->hDecoderConfig->Opt_tsm )
     901             :     {
     902    87149463 :         for ( n = 0; n < MAX_INTERN_CHANNELS; n++ )
     903             :         {
     904    82023024 :             st_ivas->hTcBuffer->tc[n] = p_output[n];
     905             :         }
     906             :     }
     907             : 
     908    35661887 :     for ( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
     909             :     {
     910    28209930 :         p_tc[n] = &st_ivas->hTcBuffer->tc[n][st_ivas->hTcBuffer->n_samples_rendered];
     911             :     }
     912             : 
     913             :     /*----------------------------------------------------------------*
     914             :      * Update combined orientation access index
     915             :      *----------------------------------------------------------------*/
     916             : 
     917     7451957 :     if ( st_ivas->hCombinedOrientationData != NULL )
     918             :     {
     919             :         /* take the discard samples into account here to make sure head rotation stays on the correct 5ms grid */
     920      842222 :         st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start -= st_ivas->hTcBuffer->n_samples_discard;
     921             : 
     922      842222 :         ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
     923             :     }
     924             : 
     925             :     /*----------------------------------------------------------------*
     926             :      * Rendering
     927             :      *----------------------------------------------------------------*/
     928             : 
     929     7451957 :     if ( st_ivas->ivas_format == UNDEFINED_FORMAT )
     930             :     {
     931           0 :         assert( 0 );
     932             :     }
     933     7451957 :     else if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
     934             :     {
     935     1395084 :         ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
     936             :     }
     937     6056873 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
     938             :     {
     939             :         /* Rendering */
     940       49410 :         if ( st_ivas->renderer_type == RENDERER_MC )
     941             :         {
     942       49410 :             *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
     943       49410 :             ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
     944             :         }
     945             :     }
     946     6007463 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
     947             :     {
     948             :         /* Rendering */
     949     1309185 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
     950             :         {
     951      208473 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
     952             :             {
     953      106068 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, st_ivas->nchan_transport, p_output );
     954             :             }
     955      102405 :             else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     956             :             {
     957      102405 :                 ivas_param_ism_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
     958             : 
     959      102405 :                 if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     960             :                 {
     961             :                     /* Convert CICP19 -> Ambisonics */
     962       60250 :                     ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
     963             :                 }
     964             :             }
     965             :         }
     966             :         else /* ISM_MODE_DISC */
     967             :         {
     968     1100712 :             *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
     969             : 
     970             :             /* Loudspeaker or Ambisonics rendering */
     971     1100712 :             if ( st_ivas->renderer_type == RENDERER_TD_PANNING || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
     972             :             {
     973             :                 /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
     974      501478 :                 ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
     975             :             }
     976      599234 :             else if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
     977             :             {
     978       10500 :                 ivas_apply_non_diegetic_panning( p_tc[0], p_output, st_ivas->hDecoderConfig->non_diegetic_pan_gain, *nSamplesRendered );
     979             :             }
     980             : #ifdef DEBUGGING
     981             :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
     982             : #else
     983      588734 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
     984             : #endif
     985             :             {
     986             :                 /* Convert to Ambisonics */
     987      192195 :                 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 );
     988             :             }
     989             : 
     990             :             /* Binaural rendering */
     991     1100712 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
     992             :             {
     993      396539 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     994             :                 {
     995       17814 :                     if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
     996             :                     {
     997           0 :                         return error;
     998             :                     }
     999             :                 }
    1000             :                 else
    1001             :                 {
    1002      378725 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1003             :                     {
    1004           0 :                         return error;
    1005             :                     }
    1006             :                 }
    1007             :             }
    1008      704173 :             else if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1009             :             {
    1010      146656 :                 if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
    1011      146656 :                                                                NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
    1012             :                 {
    1013           0 :                     return error;
    1014             :                 }
    1015             :             }
    1016             :         }
    1017             :     }
    1018     4698278 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1019             :     {
    1020     1939025 :         nchan_remapped = nchan_transport;
    1021             : 
    1022             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    1023     1939025 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1024             :         {
    1025      608705 :             ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1026             :         }
    1027     1330320 :         else if ( st_ivas->ivas_format == MASA_FORMAT )
    1028             :         {
    1029      287810 :             if ( st_ivas->renderer_type == RENDERER_DIRAC )
    1030             :             {
    1031      287810 :                 ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1032             :             }
    1033             :         }
    1034             :         else
    1035             :         {
    1036     1042510 :             if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1037             :             {
    1038           0 :                 return error;
    1039             :             }
    1040             :         }
    1041             :     }
    1042     2759253 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1043             :     {
    1044      451115 :         nchan_remapped = st_ivas->nchan_transport;
    1045             : 
    1046      451115 :         if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1047             :         {
    1048      218658 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    1049             :             {
    1050       69742 :                 if ( ( error = ivas_omasa_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output ) ) != IVAS_ERR_OK )
    1051             :                 {
    1052           0 :                     return error;
    1053             :                 }
    1054             :             }
    1055             :             else
    1056             :             {
    1057      148916 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1058             :             }
    1059             :         }
    1060      232457 :         else if ( st_ivas->renderer_type == RENDERER_DIRAC )
    1061             :         {
    1062      225989 :             ivas_omasa_dirac_rend_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1063             :         }
    1064        6468 :         else if ( st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT || st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT )
    1065             :         {
    1066        6468 :             ivas_jbm_dec_tc_buffer_playout( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output );
    1067        6468 :             ivas_omasa_rearrange_channels( p_output, st_ivas->nchan_ism, *nSamplesRendered );
    1068             :         }
    1069             :     }
    1070     2308138 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1071             :     {
    1072     1681655 :         nchan_remapped = nchan_transport;
    1073             : 
    1074             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    1075     1681655 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1076             :         {
    1077      804132 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
    1078             :             {
    1079      245260 :                 if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1080             :                 {
    1081           0 :                     return error;
    1082             :                 }
    1083             :             }
    1084      558872 :             else if ( st_ivas->renderer_type == RENDERER_OSBA_STEREO )
    1085             :             {
    1086       51614 :                 *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1087             : 
    1088             :                 /* shift SBA channels to avoid overwrite by ISM upmix in 1 object case and non-TSM unified channel memory*/
    1089       51614 :                 if ( st_ivas->nchan_ism == 1 && st_ivas->hDecoderConfig->Opt_tsm == 0 )
    1090             :                 {
    1091        5940 :                     mvr2r( p_tc[2], p_output[3], *nSamplesRendered );
    1092        5940 :                     mvr2r( p_tc[1], p_output[2], *nSamplesRendered );
    1093        5940 :                     p_tc[1] = p_output[2];
    1094        5940 :                     p_tc[2] = p_output[3];
    1095             :                 }
    1096             : 
    1097             :                 /* render objects */
    1098       51614 :                 ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
    1099             : 
    1100             :                 /* add already rendered SBA part */
    1101       51614 :                 ivas_osba_stereo_add_channels( p_tc, p_output, st_ivas->hSbaIsmData->gain_bed, nchan_out, st_ivas->nchan_ism, *nSamplesRendered );
    1102             :             }
    1103      507258 :             else if ( st_ivas->renderer_type == RENDERER_OSBA_AMBI || st_ivas->renderer_type == RENDERER_OSBA_LS || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
    1104             :             {
    1105      456244 :                 if ( ( error = ivas_osba_render_sf( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1106             :                 {
    1107           0 :                     return error;
    1108             :                 }
    1109             :             }
    1110       51014 :             else if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) /*EXT output = individual objects + HOA3*/
    1111             :             {
    1112       51014 :                 if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, &p_output[st_ivas->nchan_ism] ) ) != IVAS_ERR_OK )
    1113             :                 {
    1114           0 :                     return error;
    1115             :                 }
    1116             : 
    1117      178676 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    1118             :                 {
    1119      127662 :                     mvr2r( p_tc[n], p_output[n], *nSamplesRendered );
    1120             :                 }
    1121             :             }
    1122             :             else
    1123             :             {
    1124           0 :                 if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1125             :                 {
    1126           0 :                     return error;
    1127             :                 }
    1128             :             }
    1129             :         }
    1130      877523 :         else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    1131             :         {
    1132      302783 :             ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1133      908349 :             for ( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
    1134             :             {
    1135      605566 :                 v_multc( p_output[n], 2.0f, p_output[n], *nSamplesRendered );
    1136             :             }
    1137             :         }
    1138             :         else
    1139             :         {
    1140      574740 :             if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1141             :             {
    1142           0 :                 return error;
    1143             :             }
    1144             : 
    1145      574740 :             if ( output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    1146             :             {
    1147             : #ifdef DEBUGGING
    1148             :                 assert( st_ivas->ism_mode == ISM_MODE_NONE );
    1149             : #endif
    1150      649032 :                 for ( n = st_ivas->hIntSetup.nchan_out_woLFE - 1; n >= 0; n-- )
    1151             :                 {
    1152      589254 :                     mvr2r( p_output[n], p_output[n + st_ivas->nchan_ism], *nSamplesRendered );
    1153             :                 }
    1154      214596 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    1155             :                 {
    1156      154818 :                     set_zero( p_output[n], *nSamplesRendered );
    1157             :                 }
    1158             :             }
    1159     5801482 :             for ( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
    1160             :             {
    1161     5226742 :                 v_multc( p_output[n], 2.0f, p_output[n], *nSamplesRendered );
    1162             :             }
    1163             :         }
    1164             :     }
    1165      626483 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    1166             :     {
    1167      626483 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    1168             :         {
    1169      303018 :             int16_t crendInPlaceRotation = FALSE;
    1170      303018 :             *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1171      303018 :             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 ) )
    1172             :             {
    1173        9457 :                 if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
    1174             :                 {
    1175        9457 :                     crendInPlaceRotation = TRUE;
    1176        9457 :                     ivas_mc2sba( st_ivas->hTransSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE );
    1177             :                 }
    1178             :             }
    1179             : 
    1180             :             /* Rendering */
    1181      303018 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV || st_ivas->renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1182             :             {
    1183      147762 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1184             :                 {
    1185       15946 :                     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,
    1186       15946 :                                                                             &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs ) ) != IVAS_ERR_OK )
    1187             :                     {
    1188           0 :                         return error;
    1189             :                     }
    1190             :                 }
    1191             :                 else
    1192             :                 {
    1193      279578 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    1194      279578 :                                                                    &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output : p_tc, p_output, *nSamplesRendered, output_Fs, 0 ) ) != IVAS_ERR_OK )
    1195             :                     {
    1196           0 :                         return error;
    1197             :                     }
    1198             : 
    1199      139789 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
    1200             :                 }
    1201             :             }
    1202      155256 :             else if ( st_ivas->renderer_type == RENDERER_MC )
    1203             :             {
    1204       45641 :                 *nSamplesRendered = min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1205       45641 :                 ivas_ls_setup_conversion( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc, p_output );
    1206             :             }
    1207      109615 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1208             :             {
    1209       18853 :                 ivas_mc2sba( st_ivas->hIntSetup, p_tc, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1210             :             }
    1211       90762 :             else if ( st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD )
    1212             :             {
    1213       90762 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1214             :                 {
    1215       66872 :                     if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1216             :                     {
    1217           0 :                         return error;
    1218             :                     }
    1219             :                 }
    1220             :                 else
    1221             :                 {
    1222       23890 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, *nSamplesRendered ) ) != IVAS_ERR_OK )
    1223             :                     {
    1224           0 :                         return error;
    1225             :                     }
    1226             : 
    1227       23890 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_tc, p_output );
    1228             :                 }
    1229             :             }
    1230             :         }
    1231      323465 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    1232             :         {
    1233       19122 :             ivas_mc_paramupmix_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_tc, p_output );
    1234             : 
    1235             : 
    1236             :             /* Rendering */
    1237       19122 :             if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && !st_ivas->hDecoderConfig->Opt_Headrotation )
    1238             :             {
    1239             :                 /* handled in CLDFB domain already */
    1240        2461 :                 if ( output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1241             :                 {
    1242        2461 :                     ivas_binaural_add_LFE( st_ivas, *nSamplesRendered, p_output, p_output );
    1243             :                 }
    1244             :             }
    1245       16661 :             else if ( st_ivas->renderer_type == RENDERER_MC )
    1246             :             {
    1247        1188 :                 ivas_ls_setup_conversion( st_ivas, MC_PARAMUPMIX_MAX_INPUT_CHANS, *nSamplesRendered, p_output, p_output );
    1248             :             }
    1249       15473 :             else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1250             :             {
    1251         961 :                 ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1252             :             }
    1253             :         }
    1254      304343 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    1255             :         {
    1256      139656 :             ivas_param_mc_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1257             :         }
    1258      164687 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    1259             :         {
    1260      164687 :             nchan_remapped = st_ivas->nchan_transport;
    1261      164687 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    1262             :             {
    1263       85558 :                 ivas_dirac_dec_binaural_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output );
    1264             :             }
    1265       79129 :             else if ( st_ivas->renderer_type == RENDERER_DIRAC || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) /* rendering to CICPxx and Ambisonics */
    1266             :             {
    1267       79129 :                 ivas_dirac_dec_render( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output );
    1268             : 
    1269       79129 :                 if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    1270             :                 {
    1271             :                     /* we still need to copy the separate channel if available  */
    1272       23342 :                     if ( st_ivas->hOutSetup.separateChannelEnabled )
    1273             :                     {
    1274        4630 :                         mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1275             :                     }
    1276             : 
    1277       23342 :                     ivas_mc2sba( st_ivas->hIntSetup, p_output, p_output, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0.f );
    1278             :                 }
    1279       55787 :                 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 ) )
    1280             :                 {
    1281        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++ )
    1282             :                     {
    1283        6030 :                         set_zero( p_output[n], *nSamplesRendered );
    1284             :                     }
    1285             :                 }
    1286             :             }
    1287             : 
    1288             :             /* copy discrete C and TD LFE from internal TC to output  */
    1289      164687 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
    1290             :             {
    1291       22398 :                 if ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
    1292       16800 :                      output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    1293       13680 :                      output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) )
    1294             :                 {
    1295        8718 :                     mvr2r( p_tc[LFE_CHANNEL], p_output[LFE_CHANNEL], *nSamplesRendered );
    1296        8718 :                     mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1297             :                 }
    1298       13680 :                 else if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe == 0 )
    1299             :                 {
    1300           0 :                     mvr2r( p_tc[LFE_CHANNEL - 1], p_output[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    1301             :                 }
    1302             :             }
    1303             :         }
    1304             :     }
    1305             : 
    1306             :     /*----------------------------------------------------------------*
    1307             :      * Write IVAS output channels
    1308             :      *   - compensation for saturation
    1309             :      *   - float to integer conversion
    1310             :      *----------------------------------------------------------------*/
    1311             : 
    1312     7451957 :     st_ivas->hTcBuffer->n_samples_available -= *nSamplesRendered;
    1313     7451957 :     st_ivas->hTcBuffer->n_samples_rendered += *nSamplesRendered;
    1314             : 
    1315             :     /* update global combined orientation start index */
    1316     7451957 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    1317             : 
    1318     7451957 :     if ( st_ivas->hTcBuffer->n_samples_discard > 0 )
    1319             :     {
    1320        2040 :         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++ )
    1321             :         {
    1322        1722 :             p_output[n] += st_ivas->hTcBuffer->n_samples_discard;
    1323             :         }
    1324         318 :         *nSamplesRendered -= st_ivas->hTcBuffer->n_samples_discard;
    1325         318 :         st_ivas->hTcBuffer->n_samples_discard = 0;
    1326             :     }
    1327             : 
    1328     7451957 :     if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1329             :     {
    1330      348093 :         nchan_out_syn_output = BINAURAL_CHANNELS * st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    1331             : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
    1332      348093 :         if ( st_ivas->flushing )
    1333             :         {
    1334         503 :             nchan_out_syn_output = BINAURAL_CHANNELS;
    1335             :         }
    1336             : #endif
    1337             :     }
    1338             :     else
    1339             :     {
    1340     7103864 :         nchan_out_syn_output = nchan_out;
    1341             :     }
    1342             : 
    1343     7451957 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    1344             :     {
    1345     7103864 :         if ( st_ivas->ivas_format != MONO_FORMAT )
    1346             :         {
    1347             : #ifndef DISABLE_LIMITER
    1348     7044054 :             ivas_limiter_dec( st_ivas->hLimiter, p_output, nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    1349             : #endif
    1350             :         }
    1351             :     }
    1352             : 
    1353     7451957 :     switch ( pcm_resolution )
    1354             :     {
    1355     7104367 :         case PCM_INT16:
    1356             : #ifdef DEBUGGING
    1357             :             st_ivas->noClipping +=
    1358             : #endif
    1359     7104367 :                 ivas_syn_output( p_output, *nSamplesRendered, nchan_out_syn_output, (int16_t *) data );
    1360             : 
    1361     7104367 :             break;
    1362      347590 :         case PCM_FLOAT32:
    1363      347590 :             ivas_buffer_deinterleaved_to_interleaved( p_output, nchan_out_syn_output, *nSamplesRendered, (float *) data );
    1364      347590 :             break;
    1365           0 :         default:
    1366           0 :             error = IVAS_ERR_UNKNOWN;
    1367           0 :             break;
    1368             :     }
    1369             : 
    1370     7451957 :     *nSamplesAvailableNext = st_ivas->hTcBuffer->n_samples_available;
    1371             : 
    1372     7451957 :     pop_wmops();
    1373     7451957 :     return IVAS_ERR_OK;
    1374             : }
    1375             : 
    1376             : 
    1377             : /*--------------------------------------------------------------------------*
    1378             :  * ivas_jbm_dec_flush_renderer()
    1379             :  *
    1380             :  * Flush samples if renderer granularity changes on a bitrate change
    1381             :  *--------------------------------------------------------------------------*/
    1382             : 
    1383        1230 : ivas_error ivas_jbm_dec_flush_renderer(
    1384             :     Decoder_Struct *st_ivas,                     /* i/o: IVAS decoder structure    */
    1385             :     const int16_t tc_granularity_new,            /* i  : new renderer granularity  */
    1386             :     const RENDERER_TYPE renderer_type_old,       /* i  : old renderer type         */
    1387             :     const AUDIO_CONFIG intern_config_old,        /* i  : old internal config       */
    1388             :     const IVAS_OUTPUT_SETUP_HANDLE hIntSetupOld, /* i  : old internal output setup */
    1389             :     const MC_MODE mc_mode_old,                   /* i  : old MC mode               */
    1390             :     const ISM_MODE ism_mode_old,                 /* i  : old ISM mode              */
    1391             :     uint16_t *nSamplesRendered,                  /* o  : number of samples flushed */
    1392             :     const PCM_RESOLUTION pcm_resolution,         /* i  : type for the decoded PCM resolution */
    1393             :     void *data                                   /* o  : output synthesis signal   */
    1394             : )
    1395             : {
    1396             :     ivas_error error;
    1397             :     int16_t n_samples_still_available;
    1398             :     int16_t n_slots_still_available;
    1399             :     int16_t n_samples_to_render;
    1400             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    1401             :     float *p_output[MAX_LS_CHANNELS + MAX_NUM_OBJECTS];
    1402             : 
    1403        1230 :     if ( !st_ivas->hDecoderConfig->Opt_tsm )
    1404             :     {
    1405           0 :         return IVAS_ERR_OK;
    1406             :     }
    1407             : 
    1408        1230 :     *nSamplesRendered = 0;
    1409        1230 :     hTcBuffer = st_ivas->hTcBuffer;
    1410             : 
    1411             :     /* get number of possible slots in new granularity */
    1412        1230 :     n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
    1413        1230 :     n_slots_still_available = n_samples_still_available / tc_granularity_new;
    1414        1230 :     *nSamplesRendered = n_slots_still_available * tc_granularity_new;
    1415        1230 :     n_samples_to_render = *nSamplesRendered;
    1416        1230 :     n_samples_still_available -= n_samples_to_render;
    1417        1230 :     assert( n_samples_still_available < tc_granularity_new );
    1418             : 
    1419             :     /* update combined orientation access index */
    1420        1230 :     ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
    1421             : 
    1422        1230 :     if ( n_slots_still_available )
    1423             :     {
    1424             :         int16_t ch_idx;
    1425             : 
    1426             :         /* render available full slots (with new lower granularity) */
    1427        1770 :         for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
    1428             :         {
    1429             :             /* move it at the beginning of the TC buffer with zero padding */
    1430        1455 :             mvr2r( hTcBuffer->tc_buffer_old[ch_idx], hTcBuffer->tc[ch_idx], n_samples_to_render );
    1431        1455 :             set_zero( hTcBuffer->tc[ch_idx] + n_samples_to_render, hTcBuffer->n_samples_granularity - n_samples_to_render );
    1432             :         }
    1433             : 
    1434             :         /* simple change of the slot info */
    1435         315 :         hTcBuffer->num_slots = 1;
    1436         315 :         hTcBuffer->nb_subframes = 1;
    1437         315 :         hTcBuffer->subframes_rendered = 0;
    1438         315 :         hTcBuffer->slots_rendered = 0;
    1439         315 :         hTcBuffer->subframe_nbslots[0] = 1;
    1440         315 :         hTcBuffer->n_samples_buffered = hTcBuffer->n_samples_granularity + n_samples_still_available;
    1441         315 :         hTcBuffer->n_samples_available = 0;
    1442         315 :         hTcBuffer->n_samples_flushed = n_samples_to_render;
    1443         315 :         hTcBuffer->n_samples_rendered = 0;
    1444             : 
    1445        6615 :         for ( ch_idx = 0; ch_idx < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    1446             :         {
    1447        6300 :             p_output[ch_idx] = st_ivas->p_output_f[ch_idx];
    1448             :         }
    1449             : 
    1450         315 :         if ( st_ivas->ivas_format == ISM_FORMAT )
    1451             :         {
    1452         237 :             if ( ism_mode_old == ISM_MODE_DISC )
    1453             :             {
    1454             :                 /* Binaural rendering */
    1455         237 :                 if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
    1456             :                 {
    1457         216 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
    1458             :                     {
    1459           0 :                         return error;
    1460             :                     }
    1461             :                 }
    1462          21 :                 else if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1463             :                 {
    1464             :                     /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
    1465          21 :                     set_f( st_ivas->hIsmRendererData->interpolator, 1.0f, hTcBuffer->n_samples_granularity );
    1466             : 
    1467          21 :                     ivas_ism_render_sf( st_ivas, renderer_type_old, p_output, hTcBuffer->n_samples_granularity );
    1468             : 
    1469          21 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
    1470          21 :                                                                    NULL, NULL, st_ivas->hTcBuffer, p_output, p_output, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs, 0 ) ) != IVAS_ERR_OK )
    1471             :                     {
    1472           0 :                         return error;
    1473             :                     }
    1474             :                 }
    1475             :             }
    1476             :             else
    1477             :             {
    1478           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong ISM_MODE in VoIP renderer flushing!" );
    1479             :             }
    1480             :         }
    1481          78 :         else if ( st_ivas->ivas_format == MC_FORMAT )
    1482             :         {
    1483           6 :             if ( mc_mode_old == MC_MODE_MCT )
    1484             :             {
    1485           6 :                 int16_t crendInPlaceRotation = FALSE;
    1486             : 
    1487           6 :                 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 ) )
    1488             :                 {
    1489           3 :                     if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) < ( hIntSetupOld->nchan_out_woLFE + hIntSetupOld->num_lfe ) )
    1490             :                     {
    1491           3 :                         crendInPlaceRotation = TRUE;
    1492           3 :                         ivas_mc2sba( st_ivas->hTransSetup, hTcBuffer->tc, p_output, hTcBuffer->n_samples_granularity, hIntSetupOld->ambisonics_order, GAIN_LFE );
    1493             :                     }
    1494             :                 }
    1495           6 :                 if ( renderer_type_old == RENDERER_BINAURAL_MIXER_CONV || renderer_type_old == RENDERER_BINAURAL_MIXER_CONV_ROOM )
    1496             :                 {
    1497           3 :                     if ( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, intern_config_old, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    1498           3 :                                                                    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 )
    1499             :                     {
    1500           0 :                         return error;
    1501             :                     }
    1502             : 
    1503           3 :                     ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
    1504             :                 }
    1505           3 :                 else if ( renderer_type_old == RENDERER_BINAURAL_OBJECTS_TD )
    1506             :                 {
    1507           3 :                     if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_output, hTcBuffer->n_samples_granularity ) ) != IVAS_ERR_OK )
    1508             :                     {
    1509           0 :                         return error;
    1510             :                     }
    1511             : 
    1512           3 :                     ivas_binaural_add_LFE( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc, p_output );
    1513             :                 }
    1514             :                 else
    1515             :                 {
    1516           0 :                     return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong renderer in MCT VoIP renderer flushing!" );
    1517             :                 }
    1518             :             }
    1519             :             else
    1520             :             {
    1521           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong MC_MODE in VoIP renderer flushing!" );
    1522             :             }
    1523             :         }
    1524          72 :         else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1525             :         {
    1526           3 :             if ( ism_mode_old == ISM_MASA_MODE_DISC )
    1527             :             {
    1528             :                 float *tc_local[MAX_NUM_OBJECTS];
    1529             :                 int16_t last_dirac_md_idx;
    1530             :                 uint16_t nSamplesAvailableNext;
    1531             :                 ISM_MODE ism_mode_orig;
    1532             :                 RENDERER_TYPE renderer_type_orig;
    1533             :                 int32_t ivas_total_brate;
    1534             : 
    1535             :                 /* copy from ISM delay buffer to the correct place in TCs */
    1536          15 :                 for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    1537             :                 {
    1538          12 :                     tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx + 2][hTcBuffer->n_samples_rendered];
    1539          12 :                     mvr2r( st_ivas->hMasaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hMasaIsmData->delayBuffer_size );
    1540             :                 }
    1541             : 
    1542             :                 /* to render flushed samples, use configuration from the last received frame */
    1543           3 :                 ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1544           3 :                 renderer_type_orig = st_ivas->renderer_type;
    1545           3 :                 ism_mode_orig = st_ivas->ism_mode;
    1546           3 :                 st_ivas->ism_mode = ism_mode_old;
    1547           3 :                 st_ivas->renderer_type = renderer_type_old;
    1548           3 :                 st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
    1549           3 :                 last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
    1550             : 
    1551             :                 /* transfer adapted sf info from hTcBuffer to DirAC */
    1552           3 :                 st_ivas->hSpatParamRendCom->nb_subframes = 1;
    1553           3 :                 st_ivas->hSpatParamRendCom->subframes_rendered = 0;
    1554           3 :                 st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1555           3 :                 st_ivas->hSpatParamRendCom->slots_rendered = 0;
    1556           3 :                 st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1557           3 :                 set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
    1558             : 
    1559           3 :                 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 )
    1560             :                 {
    1561           0 :                     return error;
    1562             :                 }
    1563             : 
    1564             :                 /* restore original configuration */
    1565           3 :                 st_ivas->ism_mode = ism_mode_orig;
    1566           3 :                 st_ivas->renderer_type = renderer_type_orig;
    1567           3 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
    1568             :             }
    1569             :         }
    1570          69 :         else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1571             :         {
    1572          69 :             if ( ism_mode_old == ISM_SBA_MODE_DISC )
    1573             :             {
    1574             :                 float *tc_local[MAX_TRANSPORT_CHANNELS];
    1575             :                 int16_t last_spar_md_idx;
    1576             :                 int16_t last_dirac_md_idx;
    1577             :                 uint16_t nSamplesAvailableNext;
    1578             :                 ISM_MODE ism_mode_orig;
    1579             :                 RENDERER_TYPE renderer_type_orig;
    1580             :                 int32_t ivas_total_brate;
    1581             : 
    1582             :                 /* to render flushed samples, use configuration from the last received frame */
    1583          69 :                 ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1584          69 :                 renderer_type_orig = st_ivas->renderer_type;
    1585          69 :                 ism_mode_orig = st_ivas->ism_mode;
    1586          69 :                 st_ivas->ism_mode = ism_mode_old;
    1587          69 :                 st_ivas->renderer_type = renderer_type_old;
    1588          69 :                 st_ivas->hDecoderConfig->ivas_total_brate = st_ivas->hDecoderConfig->last_ivas_total_brate;
    1589          69 :                 last_spar_md_idx = st_ivas->hSpar->render_to_md_map[st_ivas->hSpar->slots_rendered - 1];
    1590          69 :                 last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
    1591             : #ifdef DEBUGGING
    1592             :                 assert( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV );
    1593             : #endif
    1594             : 
    1595             :                 /* copy from ISM delay buffer to the correct place in TCs */
    1596         207 :                 for ( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    1597             :                 {
    1598         138 :                     tc_local[ch_idx] = &st_ivas->hTcBuffer->tc[ch_idx][hTcBuffer->n_samples_rendered];
    1599         138 :                     mvr2r( st_ivas->hSbaIsmData->delayBuffer[ch_idx], tc_local[ch_idx], st_ivas->hSbaIsmData->delayBuffer_size );
    1600             :                 }
    1601             : 
    1602             :                 /* transfer adapted sf info from hTcBuffer to SPAR and DirAC */
    1603          69 :                 st_ivas->hSpar->nb_subframes = 1;
    1604          69 :                 st_ivas->hSpar->subframes_rendered = 0;
    1605          69 :                 st_ivas->hSpar->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1606          69 :                 st_ivas->hSpar->slots_rendered = 0;
    1607          69 :                 st_ivas->hSpar->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1608          69 :                 st_ivas->hSpatParamRendCom->nb_subframes = 1;
    1609          69 :                 st_ivas->hSpatParamRendCom->subframes_rendered = 0;
    1610          69 :                 st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1611          69 :                 st_ivas->hSpatParamRendCom->slots_rendered = 0;
    1612          69 :                 st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    1613             : 
    1614             :                 /* also adapt md maps, just use the last index */
    1615          69 :                 set_s( st_ivas->hSpar->render_to_md_map, last_spar_md_idx, n_slots_still_available );
    1616          69 :                 set_s( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
    1617             : 
    1618             :                 /* render the last subframe */
    1619          69 :                 if ( ( error = ivas_osba_dirac_td_binaural_jbm( st_ivas, (uint16_t) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, p_output ) ) != IVAS_ERR_OK )
    1620             :                 {
    1621           0 :                     return error;
    1622             :                 }
    1623             : 
    1624             :                 /* restore original configuration */
    1625          69 :                 st_ivas->ism_mode = ism_mode_orig;
    1626          69 :                 st_ivas->renderer_type = renderer_type_orig;
    1627          69 :                 st_ivas->hDecoderConfig->ivas_total_brate = ivas_total_brate;
    1628             :             }
    1629             :         }
    1630             :         else
    1631             :         {
    1632           0 :             return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong IVAS format in VoIP renderer flushing!" );
    1633             :         }
    1634             : 
    1635         315 :         hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
    1636             :     }
    1637             : 
    1638             :     /* update global combined orientation start index */
    1639        1230 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    1640             : 
    1641        1230 :     *nSamplesRendered = n_samples_to_render;
    1642             : 
    1643             :     /* Only write out the valid data*/
    1644        1230 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    1645             :     {
    1646        1230 :         if ( st_ivas->ivas_format != MONO_FORMAT )
    1647             :         {
    1648             : #ifndef DISABLE_LIMITER
    1649        1230 :             ivas_limiter_dec( st_ivas->hLimiter, p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    1650             : #endif
    1651             :         }
    1652             :     }
    1653             : 
    1654        1230 :     switch ( pcm_resolution )
    1655             :     {
    1656        1230 :         case PCM_INT16:
    1657             : #ifdef DEBUGGING
    1658             :             st_ivas->noClipping +=
    1659             : #endif
    1660        1230 :                 ivas_syn_output( p_output, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) data );
    1661        1230 :             break;
    1662           0 :         case PCM_FLOAT32:
    1663           0 :             ivas_buffer_deinterleaved_to_interleaved( p_output, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, (float *) data );
    1664           0 :             break;
    1665           0 :         default:
    1666           0 :             error = IVAS_ERR_UNKNOWN;
    1667           0 :             break;
    1668             :     }
    1669             : 
    1670        1230 :     return IVAS_ERR_OK;
    1671             : }
    1672             : 
    1673             : 
    1674             : /*--------------------------------------------------------------------------*
    1675             :  * ivas_jbm_dec_set_discard_samples()
    1676             :  *
    1677             :  * Set number of samples to discard in the first subframe if the renderer granularity changes on a bitrate change
    1678             :  *--------------------------------------------------------------------------*/
    1679             : 
    1680        2964 : ivas_error ivas_jbm_dec_set_discard_samples(
    1681             :     Decoder_Struct *st_ivas /* i/o: main IVAS decoder structre */
    1682             : )
    1683             : {
    1684             :     int16_t nMaxSlotsPerSubframe, nSlotsInFirstSubframe;
    1685             : 
    1686             :     /* render first frame with front zero padding and discarding those samples */
    1687        2964 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    1688        2964 :     nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
    1689        2964 :     if ( nSlotsInFirstSubframe > 0 )
    1690             :     {
    1691         318 :         st_ivas->hTcBuffer->n_samples_discard = ( nMaxSlotsPerSubframe - nSlotsInFirstSubframe ) * st_ivas->hTcBuffer->n_samples_granularity;
    1692             :         /* set last subframes number to max to ensure correct continuation */
    1693         318 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nMaxSlotsPerSubframe;
    1694             :     }
    1695             : 
    1696        2964 :     return IVAS_ERR_OK;
    1697             : }
    1698             : 
    1699             : 
    1700             : /*--------------------------------------------------------------------------*
    1701             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    1702             :  *
    1703             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    1704             :  *--------------------------------------------------------------------------*/
    1705             : 
    1706      211578 : void ivas_jbm_dec_get_adapted_linear_interpolator(
    1707             :     const int16_t default_interp_length, /* i  : default length of the (full-frame) interpolator */
    1708             :     const int16_t interp_length,         /* i  : length of the interpolator to be created        */
    1709             :     float *interpolator                  /* o  : the interpolator                                */
    1710             : )
    1711             : {
    1712             :     int16_t jbm_segment_len, idx;
    1713             :     float dec;
    1714             : #ifdef DEBUGGING
    1715             :     assert( default_interp_length % 2 == 0 );
    1716             : #endif
    1717             : 
    1718      211578 :     jbm_segment_len = ( default_interp_length >> 1 );
    1719      211578 :     dec = 1.0f / default_interp_length;
    1720             : 
    1721      211578 :     interpolator[interp_length - 1] = 1.0f;
    1722     5968100 :     for ( idx = interp_length - 2; idx >= jbm_segment_len; idx-- )
    1723             :     {
    1724     5756522 :         interpolator[idx] = max( 0.0f, interpolator[idx + 1] - dec );
    1725             :     }
    1726             : 
    1727      211578 :     if ( interpolator[idx + 1] > 0.0f )
    1728             :     {
    1729      211419 :         dec = interpolator[idx + 1] / ( jbm_segment_len + 1 );
    1730     6004291 :         for ( ; idx >= 0; idx-- )
    1731             :         {
    1732     5792872 :             interpolator[idx] = interpolator[idx + 1] - dec;
    1733             :         }
    1734             :     }
    1735             :     else
    1736             :     {
    1737         159 :         set_f( interpolator, 0.0f, idx + 1 );
    1738             :     }
    1739             : 
    1740      211578 :     return;
    1741             : }
    1742             : 
    1743             : 
    1744             : /*--------------------------------------------------------------------------*
    1745             :  * ivas_jbm_dec_get_adapted_subframes()
    1746             :  *
    1747             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    1748             :  *--------------------------------------------------------------------------*/
    1749             : 
    1750     5738005 : void ivas_jbm_dec_get_adapted_subframes(
    1751             :     const int16_t nCldfbTs,    /* i  : number of time slots in the current frame */
    1752             :     int16_t *subframe_nbslots, /* i/o: subframe grid                             */
    1753             :     int16_t *nb_subframes      /* i/o: number of subframes in the frame          */
    1754             : )
    1755             : {
    1756             :     uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
    1757     5738005 :     uint16_t nCldfbSlotsLocal = nCldfbTs;
    1758             : 
    1759             :     /* get last subframe size from previous frame, determine how many slots have to be processed
    1760             :            in the first subframe (i.e. potential leftover of a 5ms subframe) */
    1761     5738005 :     nSlotsInFirstSubframe = ( PARAM_MC_MAX_NSLOTS_IN_SUBFRAME - subframe_nbslots[*nb_subframes - 1] );
    1762     5738005 :     *nb_subframes = 0;
    1763     5738005 :     if ( nSlotsInFirstSubframe > 0 )
    1764             :     {
    1765       61065 :         *nb_subframes = 1;
    1766       61065 :         nCldfbSlotsLocal -= nSlotsInFirstSubframe;
    1767             :     }
    1768             : 
    1769     5738005 :     *nb_subframes += (int16_t) ceilf( (float) nCldfbSlotsLocal / (float) PARAM_MC_MAX_NSLOTS_IN_SUBFRAME );
    1770     5738005 :     nSlotsInLastSubframe = nCldfbSlotsLocal % PARAM_MC_MAX_NSLOTS_IN_SUBFRAME;
    1771             : 
    1772     5738005 :     set_s( subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    1773     5738005 :     set_s( subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, *nb_subframes );
    1774             : 
    1775     5738005 :     if ( nSlotsInFirstSubframe > 0 )
    1776             :     {
    1777       61065 :         subframe_nbslots[0] = nSlotsInFirstSubframe;
    1778             :     }
    1779             : 
    1780     5738005 :     if ( nSlotsInLastSubframe > 0 )
    1781             :     {
    1782       61407 :         subframe_nbslots[*nb_subframes - 1] = nSlotsInLastSubframe;
    1783             :     }
    1784             : 
    1785     5738005 :     return;
    1786             : }
    1787             : 
    1788             : 
    1789             : /*--------------------------------------------------------------------------*
    1790             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    1791             :  *
    1792             :  * Get an meta data map adapted to a time scale modified IVAS frame
    1793             :  *--------------------------------------------------------------------------*/
    1794             : 
    1795     5201622 : void ivas_jbm_dec_get_md_map(
    1796             :     const int16_t default_len,  /* i  : default frame length in metadata slots         */
    1797             :     const int16_t len,          /* i  : length of the modfied frames in metadata slots */
    1798             :     const int16_t subframe_len, /* i  : default length of a subframe                   */
    1799             :     const int16_t offset,       /* i  : current read offset into the md buffer         */
    1800             :     const int16_t buf_len,      /* i  : length of the metadata buffer                  */
    1801             :     int16_t *map                /* o  : metadata index map                             */
    1802             : )
    1803             : {
    1804             :     int16_t jbm_segment_len, map_idx, src_idx, src_idx_map;
    1805             :     float dec, src_idx_f;
    1806             : 
    1807             : #ifdef DEBUGGING
    1808             :     assert( default_len % 2 == 0 );
    1809             : #endif
    1810     5201622 :     jbm_segment_len = ( default_len >> 1 );
    1811     5201622 :     dec = 1.0f / default_len;
    1812             : 
    1813    46829044 :     for ( map_idx = len - 1, src_idx = default_len - 1; map_idx >= jbm_segment_len; map_idx--, src_idx-- )
    1814             :     {
    1815    41627422 :         src_idx_map = max( 0, src_idx / subframe_len );
    1816    41627422 :         map[map_idx] = ( offset + src_idx_map ) % buf_len;
    1817             :     }
    1818             : 
    1819             :     /* changed part (first segment), interpolate index to parameters
    1820             :            (we do not want to interpolate and smooth acutal direction/diffuseness values even more) */
    1821     5201622 :     if ( src_idx >= 0 )
    1822             :     {
    1823     5200590 :         dec = ( (float) ( src_idx + 1 ) ) / ( (float) jbm_segment_len );
    1824     5200590 :         src_idx_f = (float) ( src_idx + 1 ) - dec;
    1825    46805310 :         for ( ; map_idx >= 0; map_idx-- )
    1826             :         {
    1827    41604720 :             src_idx = max( 0, ( (int16_t) round_f( src_idx_f ) ) / subframe_len );
    1828    41604720 :             map[map_idx] = ( offset + src_idx ) % buf_len;
    1829    41604720 :             src_idx_f -= dec;
    1830             :         }
    1831             :     }
    1832             :     else
    1833             :     {
    1834        1032 :         set_s( map, offset, map_idx + 1 );
    1835             :     }
    1836             : 
    1837     5201622 :     return;
    1838             : }
    1839             : 
    1840             : 
    1841             : /*--------------------------------------------------------------------------*
    1842             :  * ivas_jbm_dec_get_md_map_even_spacing()
    1843             :  *
    1844             :  * Get an meta data map adapted to a time scale modified IVAS frame. Distribute slots evenly across the modified frame.
    1845             :  *--------------------------------------------------------------------------*/
    1846             : 
    1847      467471 : void ivas_jbm_dec_get_md_map_even_spacing(
    1848             :     const int16_t len,          /* i  : length of the modfied frames in metadata slots */
    1849             :     const int16_t subframe_len, /* i  : default length of a subframe                   */
    1850             :     const int16_t offset,       /* i  : current read offset into the md buffer         */
    1851             :     const int16_t buf_len,      /* i  : length of the metadata buffer                  */
    1852             :     int16_t *map                /* o  : metadata index map                             */
    1853             : )
    1854             : {
    1855             :     int16_t map_idx, sf_idx, sf_length, increment, subframes_written;
    1856             :     float decimal, decimal_sum, eps;
    1857             :     int16_t subframe_map_length[MAX_PARAM_SPATIAL_SUBFRAMES];
    1858             : 
    1859             :     /* subframe map length */
    1860      467471 :     sf_length = len / subframe_len;
    1861      467471 :     if ( len % subframe_len == 0 )
    1862             :     {
    1863             :         /* even subframes */
    1864     2336500 :         for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    1865             :         {
    1866     1869200 :             subframe_map_length[sf_idx] = sf_length;
    1867             :         }
    1868             :     }
    1869             :     else
    1870             :     {
    1871             :         /* uneven subframes */
    1872         171 :         decimal = ( (float) len / (float) subframe_len ) - (float) sf_length;
    1873         171 :         decimal_sum = decimal;
    1874         171 :         eps = 0.001f;
    1875         855 :         for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    1876             :         {
    1877         684 :             increment = (int16_t) floorf( decimal_sum + eps );
    1878         684 :             subframe_map_length[sf_idx] = sf_length + increment;
    1879         684 :             if ( increment > 0 )
    1880             :             {
    1881         375 :                 decimal_sum -= 1.0f;
    1882             :             }
    1883         684 :             decimal_sum += decimal;
    1884             :         }
    1885             :     }
    1886             : 
    1887             :     /* map slots to subframes */
    1888      467471 :     sf_idx = 0;
    1889      467471 :     subframes_written = 0;
    1890     7948866 :     for ( map_idx = 0; map_idx < len; map_idx++ )
    1891             :     {
    1892     7481395 :         map[map_idx] = ( offset + sf_idx ) % buf_len;
    1893     7481395 :         if ( map_idx - subframes_written >= subframe_map_length[sf_idx] - 1 )
    1894             :         {
    1895     1869884 :             subframes_written += subframe_map_length[sf_idx];
    1896     1869884 :             ++sf_idx;
    1897             :         }
    1898             :     }
    1899             : 
    1900      467471 :     return;
    1901             : }
    1902             : 
    1903             : 
    1904             : /*--------------------------------------------------------------------------*
    1905             :  * ivas_jbm_dec_get_num_tc_channels()
    1906             :  *
    1907             :  * Get the number of transport channels provided by the JBM transport channel decode function
    1908             :  *--------------------------------------------------------------------------*/
    1909             : 
    1910      124254 : int16_t ivas_jbm_dec_get_num_tc_channels(
    1911             :     Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    1912             : )
    1913             : {
    1914             :     int16_t num_tc;
    1915             :     int32_t ivas_total_brate;
    1916             :     AUDIO_CONFIG output_config;
    1917             : 
    1918             : 
    1919      124254 :     if ( st_ivas->renderer_type == RENDERER_DISABLE )
    1920             :     {
    1921       11670 :         num_tc = st_ivas->hDecoderConfig->nchan_out;
    1922             :     }
    1923             :     else
    1924             :     {
    1925      112584 :         num_tc = st_ivas->nchan_transport;
    1926             :     }
    1927      124254 :     output_config = st_ivas->hDecoderConfig->output_config;
    1928             : 
    1929      124254 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    1930             : 
    1931      124254 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1932             :     {
    1933         495 :         num_tc = st_ivas->hDecoderConfig->nchan_out;
    1934             :     }
    1935      123759 :     else if ( st_ivas->ivas_format == STEREO_FORMAT && st_ivas->hDecoderConfig->nchan_out == 1 )
    1936             :     {
    1937         234 :         num_tc = 1;
    1938             :     }
    1939      123525 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    1940             :     {
    1941       22214 :         if ( st_ivas->renderer_type == RENDERER_MONO_DOWNMIX )
    1942             :         {
    1943        1122 :             num_tc = 1;
    1944             :         }
    1945             :     }
    1946      101311 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    1947             :     {
    1948       32186 :         if ( st_ivas->sba_dirac_stereo_flag )
    1949             :         {
    1950         516 :             num_tc = CPE_CHANNELS;
    1951             :         }
    1952       31670 :         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 ) ) )
    1953             :         {
    1954        6003 :             num_tc = 1; /* Only one channel transported */
    1955             :         }
    1956             : 
    1957       32186 :         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 )
    1958             :         {
    1959         274 :             num_tc = CPE_CHANNELS;
    1960             :         }
    1961       32186 :         if ( st_ivas->ivas_format == SBA_FORMAT )
    1962             :         {
    1963             : 
    1964       17393 :             if ( num_tc == 3 )
    1965             :             {
    1966        2859 :                 num_tc++;
    1967             :             }
    1968             :         }
    1969             :     }
    1970       69125 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1971             :     {
    1972       13319 :         if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
    1973             :         {
    1974       12552 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1975             :             {
    1976        4142 :                 num_tc++;
    1977             :             }
    1978        8410 :             else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1979             :             {
    1980        8410 :                 num_tc += st_ivas->nchan_ism;
    1981             :             }
    1982             :         }
    1983             :     }
    1984       55806 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    1985             :     {
    1986       40574 :         if ( st_ivas->sba_dirac_stereo_flag )
    1987             :         {
    1988        2730 :             num_tc = CPE_CHANNELS;
    1989             :         }
    1990       40574 :         if ( num_tc == 3 )
    1991             :         {
    1992        6720 :             num_tc++;
    1993             :         }
    1994       40574 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1995             :         {
    1996       17390 :             num_tc += st_ivas->nchan_ism;
    1997             :         }
    1998             :     }
    1999       15232 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    2000             :     {
    2001       13079 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
    2002             :         {
    2003         755 :             num_tc = 1;
    2004             :         }
    2005       12324 :         else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO )
    2006             :         {
    2007         818 :             num_tc = 2;
    2008             :         }
    2009       11506 :         else if ( st_ivas->mc_mode == MC_MODE_MCT )
    2010             :         {
    2011             :             /* do all static dmx already in the TC decoder if less channels than transported... */
    2012        6506 :             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 ) )
    2013             :             {
    2014          93 :                 if ( ( st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe ) >= ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ) )
    2015             :                 {
    2016           0 :                     num_tc = st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe;
    2017             :                 }
    2018             :             }
    2019        6413 :             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 ) )
    2020             :             {
    2021        1234 :                 num_tc = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
    2022             :             }
    2023             :         }
    2024        5000 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2025             :         {
    2026         184 :             num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    2027             :         }
    2028        4816 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    2029             :         {
    2030        3616 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
    2031             :             {
    2032         478 :                 num_tc++;
    2033             :             }
    2034        3616 :             if ( st_ivas->hOutSetup.separateChannelEnabled && ( output_config == IVAS_AUDIO_CONFIG_5_1 || output_config == IVAS_AUDIO_CONFIG_7_1 ||
    2035         383 :                                                                 output_config == IVAS_AUDIO_CONFIG_5_1_4 || output_config == IVAS_AUDIO_CONFIG_7_1_4 ||
    2036         320 :                                                                 output_config == IVAS_AUDIO_CONFIG_5_1_2 || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && st_ivas->hOutSetup.num_lfe > 0 ) ) )
    2037             :             {
    2038             :                 /* LFE is synthesized in TD with the TCs*/
    2039         158 :                 num_tc++;
    2040             :             }
    2041             :         }
    2042             :     }
    2043        2153 :     else if ( st_ivas->ivas_format == MONO_FORMAT && st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
    2044             :     {
    2045           0 :         num_tc = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
    2046             :     }
    2047             : 
    2048      124254 :     return num_tc;
    2049             : }
    2050             : 
    2051             : 
    2052             : /*--------------------------------------------------------------------------*
    2053             :  * ivas_jbm_dec_get_render_granularity()
    2054             :  *
    2055             :  *
    2056             :  *--------------------------------------------------------------------------*/
    2057             : 
    2058             : /*! r: render granularity */
    2059      120613 : int16_t ivas_jbm_dec_get_render_granularity(
    2060             :     const RENDERER_TYPE renderer_type,     /* i  : renderer type            */
    2061             :     const RENDERER_TYPE renderer_type_sec, /* i  : secondary renderer type  */
    2062             :     const int32_t output_Fs                /* i  : sampling rate            */
    2063             : )
    2064             : {
    2065             :     int16_t render_granularity;
    2066             : 
    2067      120613 :     if ( renderer_type == RENDERER_BINAURAL_OBJECTS_TD ||                                                       /* TD renderer */
    2068      114165 :          renderer_type == RENDERER_BINAURAL_MIXER_CONV || renderer_type == RENDERER_BINAURAL_MIXER_CONV_ROOM || /* Crend */
    2069             :          renderer_type_sec == RENDERER_BINAURAL_OBJECTS_TD                                                      /* TD rend as a secondary renderer -> set the common granularity for both renderers */
    2070             :     )
    2071             :     {
    2072             :         /* 5 ms granularity */
    2073       15781 :         render_granularity = NS2SA( output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
    2074             :     }
    2075             :     else
    2076             :     {
    2077             :         /* 1.25 ms granularity */
    2078      104832 :         render_granularity = NS2SA( output_Fs, CLDFB_SLOT_NS );
    2079             :     }
    2080             : 
    2081      120613 :     return render_granularity;
    2082             : }
    2083             : 
    2084             : 
    2085             : /*--------------------------------------------------------------------------*
    2086             :  * ivas_jbm_dec_tc_audio_allocate()
    2087             :  *
    2088             :  * allocate and initialize TC audio buffer
    2089             :  *--------------------------------------------------------------------------*/
    2090             : 
    2091      110289 : static ivas_error ivas_jbm_dec_tc_audio_allocate(
    2092             :     DECODER_TC_BUFFER_HANDLE hTcBuffer, /* i/o: JBM TSM buffer handle   */
    2093             :     const int32_t output_Fs,            /* i  : output sampling rate    */
    2094             :     const int16_t Opt_tsm               /* i  : TSM option flag         */
    2095             : )
    2096             : {
    2097             :     int16_t nsamp_to_allocate;
    2098             :     int16_t ch_idx, n_samp_full, n_samp_residual, offset;
    2099             : 
    2100      110289 :     if ( Opt_tsm )
    2101             :     {
    2102       47332 :         n_samp_full = ( NS2SA( output_Fs, MAX_JBM_L_FRAME_NS ) );
    2103       47332 :         n_samp_full = max( n_samp_full, L_FRAME48k ); /* buffers are shared between 'hTcBuffer->tc[]' and 'p_output_f[]': ensure minimal length */
    2104       47332 :         n_samp_residual = hTcBuffer->n_samples_granularity - 1;
    2105             :     }
    2106             :     else
    2107             :     {
    2108       62957 :         n_samp_full = (int16_t) ( output_Fs / FRAMES_PER_SEC );
    2109       62957 :         n_samp_residual = 0;
    2110             :     }
    2111             : 
    2112      110289 :     nsamp_to_allocate = max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ) * n_samp_full;
    2113             : 
    2114      110289 :     if ( Opt_tsm )
    2115             :     {
    2116             :         /* note: this is stack memory buffer for time-scale modified audio signals */
    2117       47332 :         if ( ( hTcBuffer->tc_buffer = (float *) malloc( nsamp_to_allocate * sizeof( float ) ) ) == NULL )
    2118             :         {
    2119           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2120             :         }
    2121       47332 :         set_zero( hTcBuffer->tc_buffer, nsamp_to_allocate );
    2122             : 
    2123       47332 :         offset = 0;
    2124      239122 :         for ( ch_idx = 0; ch_idx < max( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full ); ch_idx++ )
    2125             :         {
    2126      191790 :             hTcBuffer->tc[ch_idx] = &hTcBuffer->tc_buffer[offset];
    2127      191790 :             offset += n_samp_full;
    2128             :         }
    2129      612854 :         for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2130             :         {
    2131      565522 :             hTcBuffer->tc[ch_idx] = NULL;
    2132             :         }
    2133             : 
    2134             :         /* memory buffer for TC audio samples not rendered in the previous frame */
    2135      239122 :         for ( ch_idx = 0; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
    2136             :         {
    2137      191790 :             if ( ( hTcBuffer->tc_buffer_old[ch_idx] = (float *) malloc( n_samp_residual * sizeof( float ) ) ) == NULL )
    2138             :             {
    2139           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2140             :             }
    2141      191790 :             set_zero( hTcBuffer->tc_buffer_old[ch_idx], n_samp_residual );
    2142             :         }
    2143      612854 :         for ( ; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2144             :         {
    2145      565522 :             hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2146             :         }
    2147             :     }
    2148             :     else
    2149             :     {
    2150       62957 :         hTcBuffer->tc_buffer = NULL;
    2151             : 
    2152     1070269 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2153             :         {
    2154     1007312 :             hTcBuffer->tc[ch_idx] = NULL;
    2155             :         }
    2156             : 
    2157     1070269 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2158             :         {
    2159     1007312 :             hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2160             :         }
    2161             :     }
    2162             : 
    2163      110289 :     hTcBuffer->tc_buffer2 = NULL;
    2164             : 
    2165      110289 :     return IVAS_ERR_OK;
    2166             : }
    2167             : 
    2168             : 
    2169             : /*--------------------------------------------------------------------------*
    2170             :  * ivas_jbm_dec_tc_audio_deallocate()
    2171             :  *
    2172             :  * deallocate TC audio buffer
    2173             :  *--------------------------------------------------------------------------*/
    2174             : 
    2175      110289 : static void ivas_jbm_dec_tc_audio_deallocate(
    2176             :     DECODER_TC_BUFFER_HANDLE hTcBuffer /* i/o: JBM TSM buffer handle   */
    2177             : )
    2178             : {
    2179             :     int16_t ch_idx;
    2180             : 
    2181      110289 :     if ( hTcBuffer != NULL )
    2182             :     {
    2183      110289 :         if ( hTcBuffer->tc_buffer != NULL )
    2184             :         {
    2185      804644 :             for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2186             :             {
    2187      757312 :                 hTcBuffer->tc[ch_idx] = NULL;
    2188             :             }
    2189             : 
    2190       47332 :             free( hTcBuffer->tc_buffer );
    2191       47332 :             hTcBuffer->tc_buffer = NULL;
    2192             :         }
    2193             : 
    2194     1874913 :         for ( ch_idx = 0; ch_idx < MAX_INTERN_CHANNELS; ch_idx++ )
    2195             :         {
    2196     1764624 :             if ( hTcBuffer->tc_buffer_old[ch_idx] != NULL )
    2197             :             {
    2198      191790 :                 free( hTcBuffer->tc_buffer_old[ch_idx] );
    2199      191790 :                 hTcBuffer->tc_buffer_old[ch_idx] = NULL;
    2200             :             }
    2201             :         }
    2202             : 
    2203      110289 :         if ( hTcBuffer->tc_buffer2 != NULL )
    2204             :         {
    2205       94458 :             free( hTcBuffer->tc_buffer2 );
    2206       94458 :             hTcBuffer->tc_buffer2 = NULL;
    2207             :         }
    2208             :     }
    2209             : 
    2210      110289 :     return;
    2211             : }
    2212             : 
    2213             : 
    2214             : /*--------------------------------------------------------------------------*
    2215             :  * ivas_jbm_dec_tc_buffer_open()
    2216             :  *
    2217             :  * open and initialize JBM transport channel buffer
    2218             :  *--------------------------------------------------------------------------*/
    2219             : 
    2220       86027 : ivas_error ivas_jbm_dec_tc_buffer_open(
    2221             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure              */
    2222             :     const TC_BUFFER_MODE tc_buffer_mode,    /* i  : buffer mode                         */
    2223             :     const int16_t nchan_transport_jbm,      /* i  : number of real transport channels   */
    2224             :     const int16_t nchan_transport_internal, /* i  : number of totally buffered channels */
    2225             :     const int16_t nchan_full,               /* i  : number of channels to fully store   */
    2226             :     const int16_t n_samples_granularity     /* i  : granularity of the renderer/buffer  */
    2227             : )
    2228             : {
    2229             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    2230             :     int16_t nMaxSlotsPerSubframe;
    2231             :     ivas_error error;
    2232             : 
    2233             :     /*-----------------------------------------------------------------*
    2234             :      * prepare library opening
    2235             :      *-----------------------------------------------------------------*/
    2236             : 
    2237       86027 :     if ( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
    2238             :     {
    2239           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    2240             :     }
    2241             : 
    2242       86027 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    2243       86027 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    2244       86027 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    2245       86027 :     hTcBuffer->nchan_buffer_full = nchan_full;
    2246       86027 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    2247       86027 :     hTcBuffer->n_samples_available = 0;
    2248       86027 :     hTcBuffer->n_samples_buffered = 0;
    2249       86027 :     hTcBuffer->n_samples_rendered = 0;
    2250       86027 :     hTcBuffer->slots_rendered = 0;
    2251       86027 :     hTcBuffer->subframes_rendered = 0;
    2252       86027 :     hTcBuffer->n_samples_discard = 0;
    2253       86027 :     hTcBuffer->n_samples_flushed = 0;
    2254       86027 :     hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    2255             : 
    2256       86027 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / hTcBuffer->n_samples_granularity;
    2257       86027 :     hTcBuffer->num_slots = nMaxSlotsPerSubframe * MAX_PARAM_SPATIAL_SUBFRAMES;
    2258       86027 :     set_s( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    2259       86027 :     set_s( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
    2260             : 
    2261       86027 :     if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
    2262             :     {
    2263           0 :         return error;
    2264             :     }
    2265             : 
    2266       86027 :     st_ivas->hTcBuffer = hTcBuffer;
    2267             : 
    2268       86027 :     return IVAS_ERR_OK;
    2269             : }
    2270             : 
    2271             : 
    2272             : /*--------------------------------------------------------------------------*
    2273             :  * ivas_jbm_dec_tc_buffer_reconfigure()
    2274             :  *
    2275             :  * open and initialize JBM transport channel buffer
    2276             :  *--------------------------------------------------------------------------*/
    2277             : 
    2278       24262 : ivas_error ivas_jbm_dec_tc_buffer_reconfigure(
    2279             :     Decoder_Struct *st_ivas,                /* i/o: IVAS decoder structure                  */
    2280             :     const TC_BUFFER_MODE tc_buffer_mode,    /* i  : new buffer mode                         */
    2281             :     const int16_t nchan_transport_jbm,      /* i  : new number of real transport channels   */
    2282             :     const int16_t nchan_transport_internal, /* i  : new number of totally buffered channels */
    2283             :     const int16_t nchan_full,               /* i  : new number of channels to fully store   */
    2284             :     const int16_t n_samples_granularity     /* i  : new granularity of the renderer/buffer  */
    2285             : )
    2286             : {
    2287             :     int16_t ch_idx, num_tc_buffer_mem, n_samples_still_available;
    2288             :     float tc_buffer_mem[MAX_INTERN_CHANNELS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES - 1];
    2289             :     ivas_error error;
    2290             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    2291             : 
    2292       24262 :     hTcBuffer = st_ivas->hTcBuffer;
    2293             : 
    2294       24262 :     num_tc_buffer_mem = 0;
    2295       24262 :     n_samples_still_available = 0;
    2296             : 
    2297       24262 :     if ( st_ivas->hDecoderConfig->Opt_tsm )
    2298             :     {
    2299             :         /* save samples of the TC buffer from the previous frame */
    2300        9122 :         num_tc_buffer_mem = min( hTcBuffer->nchan_transport_internal, nchan_transport_internal );
    2301        9122 :         n_samples_still_available = hTcBuffer->n_samples_buffered - hTcBuffer->n_samples_rendered;
    2302             : 
    2303             :         /* what is remaining from last frame needs always be smaller than the new granularity */
    2304        9122 :         assert( n_samples_still_available < n_samples_granularity );
    2305             : 
    2306       39861 :         for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
    2307             :         {
    2308       30739 :             mvr2r( hTcBuffer->tc_buffer_old[ch_idx] + hTcBuffer->n_samples_flushed, tc_buffer_mem[ch_idx], n_samples_still_available );
    2309             :         }
    2310             :     }
    2311             : 
    2312             :     /* if granularity changes, adapt subframe_nb_slots */
    2313       24262 :     if ( n_samples_granularity != hTcBuffer->n_samples_granularity )
    2314             :     {
    2315             : #ifdef DEBUGGING
    2316             :         int16_t nMaxSlotsPerSubframeOld;
    2317             : #endif
    2318             :         int16_t nMaxSlotsPerSubframeNew;
    2319             : 
    2320        5931 :         nMaxSlotsPerSubframeNew = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / n_samples_granularity;
    2321             : #ifdef DEBUGGING
    2322             :         nMaxSlotsPerSubframeOld = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    2323             :         assert( hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] == nMaxSlotsPerSubframeOld );
    2324             :         if ( n_samples_granularity < hTcBuffer->n_samples_granularity )
    2325             :         {
    2326             :             assert( ( hTcBuffer->n_samples_granularity % n_samples_granularity ) == 0 );
    2327             :         }
    2328             :         else
    2329             :         {
    2330             :             assert( ( n_samples_granularity % hTcBuffer->n_samples_granularity ) == 0 );
    2331             :         }
    2332             : #endif
    2333             :         /* if samples were flushed, take that into account here */
    2334        5931 :         if ( n_samples_granularity < hTcBuffer->n_samples_granularity && hTcBuffer->n_samples_flushed > 0 )
    2335             :         {
    2336         315 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = hTcBuffer->n_samples_flushed / n_samples_granularity;
    2337         315 :             hTcBuffer->n_samples_flushed = 0;
    2338             :         }
    2339             :         else
    2340             :         {
    2341        5616 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
    2342             :         }
    2343             :     }
    2344             : 
    2345       24262 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    2346       24262 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    2347       24262 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    2348       24262 :     hTcBuffer->nchan_buffer_full = nchan_full;
    2349       24262 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    2350             : 
    2351             :     /* reallocate TC audio buffers */
    2352             : 
    2353       24262 :     ivas_jbm_dec_tc_audio_deallocate( hTcBuffer );
    2354             : 
    2355       24262 :     if ( ( error = ivas_jbm_dec_tc_audio_allocate( hTcBuffer, st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->Opt_tsm ) ) != IVAS_ERR_OK )
    2356             :     {
    2357           0 :         return error;
    2358             :     }
    2359             : 
    2360             :     /* propagate samples of the TC buffer from the previous frame */
    2361       55001 :     for ( ch_idx = 0; ch_idx < num_tc_buffer_mem; ch_idx++ )
    2362             :     {
    2363       30739 :         mvr2r( tc_buffer_mem[ch_idx], hTcBuffer->tc_buffer_old[ch_idx], n_samples_still_available );
    2364             :     }
    2365             : 
    2366       24262 :     return IVAS_ERR_OK;
    2367             : }
    2368             : 
    2369             : 
    2370             : /*--------------------------------------------------------------------------*
    2371             :  * ivas_jbm_dec_tc_buffer_playout()
    2372             :  *
    2373             :  *
    2374             :  *--------------------------------------------------------------------------*/
    2375             : 
    2376     1401552 : static void ivas_jbm_dec_tc_buffer_playout(
    2377             :     Decoder_Struct *st_ivas,
    2378             :     const uint16_t nSamplesAsked,
    2379             :     uint16_t *nSamplesRendered,
    2380             :     float *output[] )
    2381             : {
    2382             :     int16_t ch_idx, slot_size, slots_to_render, first_sf, last_sf;
    2383             : 
    2384     1401552 :     slot_size = st_ivas->hTcBuffer->n_samples_granularity;
    2385             : 
    2386             :     /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    2387     1401552 :     slots_to_render = min( st_ivas->hTcBuffer->num_slots - st_ivas->hTcBuffer->slots_rendered, nSamplesAsked / slot_size );
    2388     1401552 :     st_ivas->hTcBuffer->slots_rendered += slots_to_render;
    2389     1401552 :     *nSamplesRendered = (uint16_t) slots_to_render * slot_size;
    2390     1401552 :     first_sf = st_ivas->hTcBuffer->subframes_rendered;
    2391     1401552 :     last_sf = first_sf;
    2392             : 
    2393     5337289 :     while ( slots_to_render > 0 )
    2394             :     {
    2395     3935737 :         slots_to_render -= st_ivas->hTcBuffer->subframe_nbslots[last_sf];
    2396     3935737 :         last_sf++;
    2397             :     }
    2398             : #ifdef DEBUGGING
    2399             :     assert( slots_to_render == 0 );
    2400             : #endif
    2401             : 
    2402     4132892 :     for ( ch_idx = 0; ch_idx < st_ivas->hTcBuffer->nchan_transport_jbm; ch_idx++ )
    2403             :     {
    2404     2731340 :         output[ch_idx] = st_ivas->hTcBuffer->tc[ch_idx] + st_ivas->hTcBuffer->n_samples_rendered;
    2405             :     }
    2406             : 
    2407     1401552 :     st_ivas->hTcBuffer->subframes_rendered = last_sf;
    2408             : 
    2409     1401552 :     return;
    2410             : }
    2411             : 
    2412             : 
    2413             : /*--------------------------------------------------------------------------*
    2414             :  * ivas_jbm_dec_tc_buffer_close()
    2415             :  *
    2416             :  * Close JBM transport channel buffer
    2417             :  *--------------------------------------------------------------------------*/
    2418             : 
    2419       86027 : void ivas_jbm_dec_tc_buffer_close(
    2420             :     DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
    2421             : )
    2422             : {
    2423       86027 :     if ( *phTcBuffer != NULL )
    2424             :     {
    2425       86027 :         ivas_jbm_dec_tc_audio_deallocate( *phTcBuffer );
    2426             : 
    2427       86027 :         free( *phTcBuffer );
    2428       86027 :         *phTcBuffer = NULL;
    2429             :     }
    2430             : 
    2431       86027 :     return;
    2432             : }
    2433             : 
    2434             : 
    2435             : /*--------------------------------------------------------------------------*
    2436             :  * ivas_jbm_dec_td_renderers_adapt_subframes()
    2437             :  *
    2438             :  * Close JBM transport channel buffer
    2439             :  *--------------------------------------------------------------------------*/
    2440             : 
    2441     3946579 : void ivas_jbm_dec_td_renderers_adapt_subframes(
    2442             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    2443             : )
    2444             : {
    2445             :     int16_t nMaxSlotsPerSubframe, nSlotsAvailable;
    2446             :     uint16_t nSlotsInLastSubframe, nSlotsInFirstSubframe;
    2447             : 
    2448     3946579 :     nMaxSlotsPerSubframe = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity;
    2449     3946579 :     nSlotsAvailable = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
    2450     3946579 :     st_ivas->hTcBuffer->num_slots = nSlotsAvailable;
    2451     3946579 :     st_ivas->hTcBuffer->n_samples_available = nSlotsAvailable * st_ivas->hTcBuffer->n_samples_granularity;
    2452     3946579 :     nSlotsInFirstSubframe = nMaxSlotsPerSubframe - st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1];
    2453     3946579 :     st_ivas->hTcBuffer->nb_subframes = 0;
    2454             : 
    2455     3946579 :     if ( nSlotsInFirstSubframe > 0 )
    2456             :     {
    2457       18115 :         st_ivas->hTcBuffer->nb_subframes = 1;
    2458       18115 :         nSlotsAvailable -= nSlotsInFirstSubframe;
    2459             :     }
    2460     3946579 :     st_ivas->hTcBuffer->nb_subframes += (int16_t) ceilf( (float) nSlotsAvailable / (float) nMaxSlotsPerSubframe );
    2461     3946579 :     nSlotsInLastSubframe = nSlotsAvailable % nMaxSlotsPerSubframe;
    2462     3946579 :     set_s( st_ivas->hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    2463     3946579 :     set_s( st_ivas->hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, st_ivas->hTcBuffer->nb_subframes );
    2464             : 
    2465     3946579 :     if ( nSlotsInFirstSubframe > 0 )
    2466             :     {
    2467       18115 :         st_ivas->hTcBuffer->subframe_nbslots[0] = nSlotsInFirstSubframe;
    2468             :     }
    2469             : 
    2470     3946579 :     if ( nSlotsInLastSubframe > 0 )
    2471             :     {
    2472       18159 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nSlotsInLastSubframe;
    2473             :     }
    2474             : 
    2475     3946579 :     st_ivas->hTcBuffer->slots_rendered = 0;
    2476     3946579 :     st_ivas->hTcBuffer->subframes_rendered = 0;
    2477             : 
    2478     3946579 :     return;
    2479             : }
    2480             : 
    2481             : 
    2482             : /*--------------------------------------------------------------------------*
    2483             :  * ivas_jbm_dec_get_tc_buffer_mode()
    2484             :  *
    2485             :  *
    2486             :  *--------------------------------------------------------------------------*/
    2487             : 
    2488       46155 : TC_BUFFER_MODE ivas_jbm_dec_get_tc_buffer_mode(
    2489             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
    2490             : )
    2491             : {
    2492             :     TC_BUFFER_MODE buffer_mode;
    2493       46155 :     buffer_mode = TC_BUFFER_MODE_BUFFER;
    2494             : 
    2495       46155 :     switch ( st_ivas->renderer_type )
    2496             :     {
    2497             :             /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
    2498        7996 :         case RENDERER_DISABLE:
    2499             :         case RENDERER_MCMASA_MONO_STEREO:
    2500             :         case RENDERER_OSBA_STEREO:
    2501             :         case RENDERER_MONO_DOWNMIX:
    2502        7996 :             buffer_mode = TC_BUFFER_MODE_BUFFER;
    2503        7996 :             break;
    2504       30101 :         case RENDERER_TD_PANNING:
    2505             :         case RENDERER_BINAURAL_OBJECTS_TD:
    2506             :         case RENDERER_BINAURAL_FASTCONV:
    2507             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    2508             :         case RENDERER_BINAURAL_PARAMETRIC:
    2509             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    2510             :         case RENDERER_STEREO_PARAMETRIC:
    2511             :         case RENDERER_DIRAC:
    2512             :         case RENDERER_PARAM_ISM:
    2513             :         case RENDERER_BINAURAL_MIXER_CONV:
    2514             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    2515             :         case RENDERER_OMASA_OBJECT_EXT:
    2516             :         case RENDERER_OMASA_MIX_EXT:
    2517             :         case RENDERER_OSBA_AMBI:
    2518             :         case RENDERER_OSBA_LS:
    2519       30101 :             buffer_mode = TC_BUFFER_MODE_RENDERER;
    2520       30101 :             break;
    2521           6 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    2522           6 :             if ( st_ivas->ivas_format == MONO_FORMAT )
    2523             :             {
    2524           3 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2525             :             }
    2526             :             else
    2527             :             {
    2528           3 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2529             :             }
    2530           6 :             break;
    2531         426 :         case RENDERER_MC_PARAMMC:
    2532         426 :             if ( st_ivas->hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
    2533             :             {
    2534         174 :                 buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
    2535             :             }
    2536             :             else
    2537             :             {
    2538         252 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2539             :             }
    2540         426 :             break;
    2541        2504 :         case RENDERER_MC:
    2542        2504 :             if ( ivas_jbm_dec_get_num_tc_channels( st_ivas ) != st_ivas->hDecoderConfig->nchan_out )
    2543             :             {
    2544        1563 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2545             :             }
    2546        2504 :             break;
    2547        5122 :         case RENDERER_SBA_LINEAR_ENC:
    2548        5122 :             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 ) )
    2549             :             {
    2550         306 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2551             :             }
    2552             :             else
    2553             :             {
    2554        4816 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2555             :             }
    2556        5122 :             break;
    2557           0 :         case RENDERER_SBA_LINEAR_DEC:
    2558           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 ) )
    2559             :             {
    2560           0 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    2561             :             }
    2562             :             else
    2563             :             {
    2564           0 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    2565             :             }
    2566           0 :             break;
    2567             : #ifdef DEBUGGING
    2568             :         default:
    2569             :             assert( 0 );
    2570             : #endif
    2571             :     }
    2572             : 
    2573       46155 :     return buffer_mode;
    2574             : }
    2575             : 
    2576             : 
    2577             : /*--------------------------------------------------------------------------*
    2578             :  * ivas_jbm_dec_metadata_open()
    2579             :  *
    2580             :  * Open structure for metadata buffering in JBM
    2581             :  *--------------------------------------------------------------------------*/
    2582             : 
    2583         498 : ivas_error ivas_jbm_dec_metadata_open(
    2584             :     Decoder_Struct *st_ivas )
    2585             : {
    2586             :     JBM_METADATA_HANDLE hJbmMetadata;
    2587         498 :     if ( ( hJbmMetadata = (JBM_METADATA_HANDLE) malloc( sizeof( JBM_METADATA ) ) ) == NULL )
    2588             :     {
    2589           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM metadata handle\n" ) );
    2590             :     }
    2591         498 :     hJbmMetadata->sf_write_idx = 0;
    2592         498 :     hJbmMetadata->sf_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_PARAM_SPATIAL_SUBFRAMES;
    2593             : 
    2594         498 :     hJbmMetadata->slot_write_idx = 0;
    2595         498 :     hJbmMetadata->slot_read_idx = 0;
    2596         498 :     hJbmMetadata->slot_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2597             : 
    2598         498 :     st_ivas->hJbmMetadata = hJbmMetadata;
    2599             : 
    2600         498 :     return IVAS_ERR_OK;
    2601             : }
    2602             : 
    2603             : 
    2604             : /*--------------------------------------------------------------------------*
    2605             :  * ivas_jbm_dec_copy_masa_meta_to_buffer()
    2606             :  *
    2607             :  * Copy decoded MASA metadata to a ring buffer
    2608             :  *--------------------------------------------------------------------------*/
    2609             : 
    2610       28494 : static void ivas_jbm_dec_copy_masa_meta_to_buffer(
    2611             :     Decoder_Struct *st_ivas )
    2612             : {
    2613             :     int16_t sf, dir, band;
    2614             :     JBM_METADATA_HANDLE hJbmMetadata;
    2615             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    2616             :     int16_t write_idx;
    2617             : 
    2618       28494 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2619       28494 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    2620             : 
    2621      142470 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    2622             :     {
    2623      113976 :         write_idx = ( hJbmMetadata->sf_write_idx + sf ) % hJbmMetadata->sf_md_buffer_length;
    2624             : 
    2625      341928 :         for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    2626             :         {
    2627     5698800 :             for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2628             :             {
    2629     5470848 :                 hJbmMetadata->directionIndexBuffer[dir][write_idx][band] = extOutMeta->directionIndex[dir][sf][band];
    2630     5470848 :                 hJbmMetadata->directToTotalRatioBuffer[dir][write_idx][band] = extOutMeta->directToTotalRatio[dir][sf][band];
    2631     5470848 :                 hJbmMetadata->spreadCoherenceBuffer[dir][write_idx][band] = extOutMeta->spreadCoherence[dir][sf][band];
    2632             :             }
    2633             :         }
    2634             : 
    2635     2849400 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2636             :         {
    2637     2735424 :             hJbmMetadata->diffuseToTotalRatioBuffer[write_idx][band] = extOutMeta->diffuseToTotalRatio[sf][band];
    2638     2735424 :             hJbmMetadata->surroundCoherenceBuffer[write_idx][band] = extOutMeta->surroundCoherence[sf][band];
    2639             :         }
    2640             : 
    2641      113976 :         hJbmMetadata->numberOfDirections[write_idx] = extOutMeta->descriptiveMeta.numberOfDirections;
    2642             :     }
    2643             : 
    2644       28494 :     return;
    2645             : }
    2646             : 
    2647             : 
    2648             : /*--------------------------------------------------------------------------*
    2649             :  * ivas_jbm_masa_sf_to_slot_map()
    2650             :  *
    2651             :  * Map input MASA metadata subframes to slots in JBM processing
    2652             :  *--------------------------------------------------------------------------*/
    2653             : 
    2654       28494 : static void ivas_jbm_masa_sf_to_slot_map(
    2655             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure     */
    2656             :     const int16_t nCldfbTs   /* i  : number of CLDFB time slots */
    2657             : )
    2658             : {
    2659             :     int16_t sf_to_slot_map[MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME];
    2660             :     int16_t num_slots_in_subfr;
    2661             :     JBM_METADATA_HANDLE hJbmMetadata;
    2662             :     int16_t slot_idx;
    2663             :     int16_t write_idx, sf_index;
    2664             : 
    2665       28494 :     ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
    2666             : 
    2667             :     /* Set values */
    2668       28494 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2669       28494 :     num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
    2670             : 
    2671             :     /* Map input subframes to slots */
    2672       28494 :     ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, MAX_PARAM_SPATIAL_SUBFRAMES, sf_to_slot_map );
    2673             : 
    2674      484458 :     for ( slot_idx = 0; slot_idx < nCldfbTs; slot_idx++ )
    2675             :     {
    2676      455964 :         write_idx = ( hJbmMetadata->slot_write_idx + slot_idx ) % hJbmMetadata->slot_md_buffer_length;
    2677      455964 :         sf_index = ( hJbmMetadata->sf_write_idx + sf_to_slot_map[slot_idx] ) % hJbmMetadata->sf_md_buffer_length;
    2678             : 
    2679      455964 :         hJbmMetadata->sf_to_slot_map[write_idx] = sf_index;
    2680             :     }
    2681             : 
    2682       28494 :     hJbmMetadata->sf_write_idx = ( hJbmMetadata->sf_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hJbmMetadata->sf_md_buffer_length;
    2683       28494 :     hJbmMetadata->slot_write_idx = ( hJbmMetadata->slot_write_idx + nCldfbTs ) % hJbmMetadata->slot_md_buffer_length;
    2684             : 
    2685       28494 :     return;
    2686             : }
    2687             : 
    2688             : 
    2689             : /*--------------------------------------------------------------------------*
    2690             :  * ivas_jbm_masa_sf_to_sf_map()
    2691             :  *
    2692             :  * Map input MASA metadata subframes to output subframes in JBM processing
    2693             :  *--------------------------------------------------------------------------*/
    2694             : 
    2695       28494 : void ivas_jbm_masa_sf_to_sf_map(
    2696             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
    2697             : )
    2698             : {
    2699             :     int16_t sf_to_sf_map[MAX_PARAM_SPATIAL_SUBFRAMES];
    2700             :     JBM_METADATA_HANDLE hJbmMetadata;
    2701             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    2702             :     int16_t slot_read_idx, sf_read_idx;
    2703             :     int16_t sf_idx;
    2704             :     int16_t dir, band;
    2705             :     uint8_t numberOfDirections;
    2706             : 
    2707             :     /* Set values */
    2708       28494 :     hJbmMetadata = st_ivas->hJbmMetadata;
    2709       28494 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    2710             : 
    2711             :     /* Map slots to subframes */
    2712      142470 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2713             :     {
    2714      113976 :         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 */
    2715      113976 :         sf_to_sf_map[sf_idx] = hJbmMetadata->sf_to_slot_map[slot_read_idx];
    2716      113976 :         hJbmMetadata->slot_read_idx = ( hJbmMetadata->slot_read_idx + CLDFB_SLOTS_PER_SUBFRAME ) % hJbmMetadata->slot_md_buffer_length;
    2717             :     }
    2718             : 
    2719             :     /* Copy mapped metadata to the EXT meta buffer for writing */
    2720      142470 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2721             :     {
    2722      113976 :         sf_read_idx = sf_to_sf_map[sf_idx];
    2723             : 
    2724      341928 :         for ( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    2725             :         {
    2726     5698800 :             for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2727             :             {
    2728     5470848 :                 extOutMeta->directionIndex[dir][sf_idx][band] = hJbmMetadata->directionIndexBuffer[dir][sf_read_idx][band];
    2729     5470848 :                 extOutMeta->directToTotalRatio[dir][sf_idx][band] = hJbmMetadata->directToTotalRatioBuffer[dir][sf_read_idx][band];
    2730     5470848 :                 extOutMeta->spreadCoherence[dir][sf_idx][band] = hJbmMetadata->spreadCoherenceBuffer[dir][sf_read_idx][band];
    2731             :             }
    2732             :         }
    2733             : 
    2734     2849400 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    2735             :         {
    2736     2735424 :             extOutMeta->diffuseToTotalRatio[sf_idx][band] = hJbmMetadata->diffuseToTotalRatioBuffer[sf_read_idx][band];
    2737     2735424 :             extOutMeta->surroundCoherence[sf_idx][band] = hJbmMetadata->surroundCoherenceBuffer[sf_read_idx][band];
    2738             :         }
    2739             :     }
    2740             : 
    2741             :     /* Determine the number of directions for the frame to be written */
    2742       28494 :     numberOfDirections = 0;
    2743      137232 :     for ( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    2744             :     {
    2745      110052 :         sf_read_idx = sf_to_sf_map[sf_idx];
    2746             : 
    2747      110052 :         if ( hJbmMetadata->numberOfDirections[sf_read_idx] == 1 )
    2748             :         {
    2749        1314 :             numberOfDirections = 1;
    2750        1314 :             break;
    2751             :         }
    2752             :     }
    2753             : 
    2754       28494 :     extOutMeta->descriptiveMeta.numberOfDirections = numberOfDirections;
    2755             : 
    2756       28494 :     return;
    2757             : }
    2758             : 
    2759             : 
    2760             : /*--------------------------------------------------------------------------*
    2761             :  * ivas_dec_prepare_renderer()
    2762             :  *
    2763             :  * prepare IVAS renderer routine
    2764             :  *--------------------------------------------------------------------------*/
    2765             : 
    2766     5751705 : void ivas_dec_prepare_renderer(
    2767             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure                                      */
    2768             : )
    2769             : {
    2770             :     int16_t n, n_render_timeslots;
    2771             : 
    2772     5751705 :     push_wmops( "ivas_dec_prepare_renderer" );
    2773             : 
    2774     5751705 :     n_render_timeslots = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity;
    2775             : 
    2776     5751705 :     if ( st_ivas->hTcBuffer->tc_buffer_mode == TC_BUFFER_MODE_BUFFER )
    2777             :     {
    2778      976534 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2779             : 
    2780      976534 :         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 )
    2781             :         {
    2782       25404 :             ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
    2783             :         }
    2784             : 
    2785             :         /* MASA transport gaining for edited disc OMASA EXT. For ISMs, only metadata is modified */
    2786      976534 :         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 )
    2787             :         {
    2788           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 );
    2789             :         }
    2790             :     }
    2791     4775171 :     else if ( st_ivas->ivas_format == STEREO_FORMAT )
    2792             :     {
    2793       49410 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2794             :     }
    2795     4725761 :     else if ( st_ivas->ivas_format == ISM_FORMAT )
    2796             :     {
    2797      973514 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    2798             :         {
    2799      126701 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
    2800             :             {
    2801       57383 :                 ivas_dirac_dec_set_md_map( st_ivas, n_render_timeslots );
    2802       57383 :                 ivas_param_ism_params_to_masa_param_mapping( st_ivas );
    2803             :             }
    2804       69318 :             else if ( st_ivas->renderer_type == RENDERER_PARAM_ISM || st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
    2805             :             {
    2806       69318 :                 ivas_param_ism_dec_prepare_renderer( st_ivas, n_render_timeslots );
    2807             :             }
    2808             :         }
    2809             :         else /* ISM_MODE_DISC */
    2810             :         {
    2811      846813 :             ivas_ism_dec_digest_tc( st_ivas );
    2812             :         }
    2813             :     }
    2814     3752247 :     else if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT )
    2815             :     {
    2816     1347137 :         ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2817             :     }
    2818     2405110 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    2819             :     {
    2820     1546103 :         if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    2821             :         {
    2822      719572 :             ivas_ism_dec_digest_tc( st_ivas );
    2823             : 
    2824             :             /* delay the objects here for all renderers where it is needed */
    2825      719572 :             if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    2826      518426 :                    st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    2827      443552 :                    st_ivas->renderer_type == RENDERER_OSBA_AMBI ||
    2828      312098 :                    st_ivas->renderer_type == RENDERER_OSBA_LS ||
    2829       97028 :                    st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL ) &&
    2830      673558 :                  ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    2831             :             {
    2832     2205330 :                 for ( n = 0; n < st_ivas->nchan_ism; n++ )
    2833             :                 {
    2834     1576136 :                     delay_signal( st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hSbaIsmData->delayBuffer[n], st_ivas->hSbaIsmData->delayBuffer_size );
    2835             :                 }
    2836             :             }
    2837             : 
    2838      719572 :             if ( !st_ivas->sba_dirac_stereo_flag )
    2839             :             {
    2840      673558 :                 if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
    2841             :                 {
    2842      201146 :                     n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
    2843             :                 }
    2844             : 
    2845      673558 :                 ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2846             :             }
    2847             :         }
    2848             :         else
    2849             :         {
    2850      826531 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2851             : 
    2852      826531 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2853             :         }
    2854             :     }
    2855      859007 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2856             :     {
    2857      416648 :         if ( st_ivas->renderer_type == RENDERER_OMASA_MIX_EXT || st_ivas->renderer_type == RENDERER_OMASA_OBJECT_EXT )
    2858             :         {
    2859        6468 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2860             : 
    2861        6468 :             if ( st_ivas->hDecoderConfig->Opt_tsm )
    2862             :             {
    2863        3090 :                 ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
    2864             :             }
    2865             : 
    2866             :             /* MASA transport gaining for edited param_one OMASA EXT. For ISMs, only metadata is modified.  */
    2867        6468 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && st_ivas->hMasaIsmData->masa_gain_is_edited == 1 )
    2868             :             {
    2869           0 :                 for ( n = 0; n < CPE_CHANNELS; n++ )
    2870             :                 {
    2871           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 );
    2872             :                 }
    2873             :             }
    2874             :         }
    2875             :         else
    2876             :         {
    2877      410180 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2878             :             {
    2879       60758 :                 n_render_timeslots *= ( st_ivas->hTcBuffer->n_samples_granularity / st_ivas->hSpatParamRendCom->slot_size );
    2880             :             }
    2881             : 
    2882      410180 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2883             : 
    2884      410180 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2885             :             {
    2886      304910 :                 ivas_ism_dec_digest_tc( st_ivas );
    2887             :             }
    2888             : 
    2889      410180 :             if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_DIRAC )
    2890             :             {
    2891             :                 int16_t nchan_transport_ism;
    2892             : 
    2893             :                 /* Delay the signal to match CLDFB delay. Delay the whole buffer. */
    2894      300704 :                 nchan_transport_ism = 0;
    2895      300704 :                 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 )
    2896             :                 {
    2897       52347 :                     nchan_transport_ism = 1;
    2898             :                 }
    2899      248357 :                 else if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2900             :                 {
    2901      223100 :                     nchan_transport_ism = st_ivas->nchan_ism;
    2902             :                 }
    2903             : 
    2904             : #ifdef NONBE_1399_1400_FIX_OBJ_EDIT_ISSUES
    2905      300704 :                 if ( st_ivas->hDecoderConfig->Opt_tsm && st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_DIRAC )
    2906             :                 {
    2907             :                     /* Gain MASA part, if edited */
    2908       79935 :                     if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
    2909             :                     {
    2910        1305 :                         for ( n = 0; n < CPE_CHANNELS; n++ )
    2911             :                         {
    2912         870 :                             v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], st_ivas->hTcBuffer->n_samples_available );
    2913             :                         }
    2914             :                     }
    2915             :                 }
    2916             : #endif
    2917             : 
    2918      830857 :                 for ( n = 0; n < nchan_transport_ism; n++ )
    2919             :                 {
    2920      530153 :                     if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    2921             :                     {
    2922             :                         float gainIsm;
    2923      122159 :                         gainIsm = OMASA_TDREND_MATCHING_GAIN;
    2924             : 
    2925      122159 :                         if ( st_ivas->hMasaIsmData->ism_gain_is_edited[n] )
    2926             :                         {
    2927        2022 :                             gainIsm *= st_ivas->hMasaIsmData->gain_ism_edited[n];
    2928             :                         }
    2929             : 
    2930      122159 :                         v_multc( st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], gainIsm, st_ivas->hTcBuffer->tc[CPE_CHANNELS + n], st_ivas->hTcBuffer->n_samples_available );
    2931             :                     }
    2932             : 
    2933      530153 :                     if ( st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && st_ivas->hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2934             :                     {
    2935      477456 :                         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 );
    2936             :                     }
    2937             :                 }
    2938             : 
    2939      300704 :                 if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC )
    2940             :                 {
    2941             :                     int16_t tcBufferSize;
    2942       60758 :                     tcBufferSize = st_ivas->hSpatParamRendCom->num_slots * st_ivas->hSpatParamRendCom->slot_size;
    2943             : 
    2944       60758 :                     if ( st_ivas->hMasaIsmData->masa_gain_is_edited )
    2945             :                     {
    2946        1728 :                         for ( n = 0; n < BINAURAL_CHANNELS; n++ )
    2947             :                         {
    2948        1152 :                             v_multc( st_ivas->hTcBuffer->tc[n], st_ivas->hMasaIsmData->gain_masa_edited, st_ivas->hTcBuffer->tc[n], tcBufferSize );
    2949             :                         }
    2950             :                     }
    2951             :                 }
    2952             :             }
    2953             :         }
    2954             :     }
    2955      442359 :     else if ( st_ivas->ivas_format == MC_FORMAT )
    2956             :     {
    2957      442359 :         if ( st_ivas->mc_mode == MC_MODE_MCT )
    2958             :         {
    2959      202696 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    2960             :         }
    2961      239663 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
    2962             :         {
    2963       13645 :             ivas_mc_paramupmix_dec_digest_tc( st_ivas, (uint8_t) n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2964             :         }
    2965      226018 :         else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    2966             :         {
    2967       97406 :             ivas_param_mc_dec_prepare_renderer( st_ivas, (uint8_t) n_render_timeslots );
    2968             :         }
    2969      128612 :         else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    2970             :         {
    2971      128612 :             ivas_sba_dec_digest_tc( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    2972             :         }
    2973             :     }
    2974             : 
    2975     5751705 :     pop_wmops();
    2976     5751705 :     return;
    2977             : }

Generated by: LCOV version 1.14