LCOV - code coverage report
Current view: top level - lib_dec - ivas_jbm_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 1868 2042 91.5 %
Date: 2025-05-03 01:55:50 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_fx.h"
      39             : #include "ivas_prot_fx.h"
      40             : #include "ivas_prot_rend_fx.h"
      41             : #include "ivas_rom_com.h"
      42             : #include <math.h>
      43             : #include "wmc_auto.h"
      44             : #include "ivas_prot_fx.h"
      45             : #ifdef DEBUGGING
      46             : #include "debug.h"
      47             : #endif
      48             : 
      49             : 
      50             : /*-----------------------------------------------------------------------*
      51             :  * Local function prototypes
      52             :  *-----------------------------------------------------------------------*/
      53             : 
      54             : static void ivas_jbm_dec_copy_tc( Decoder_Struct *st_ivas, const Word16 nSamplesForRendering, Word16 *nSamplesResidual, Word32 *data, Word32 *tc_digest_f[], Word16 data_q );
      55             : 
      56             : static void ivas_jbm_dec_tc_buffer_playout_fx( Decoder_Struct *st_ivas, const UWord16 nSamplesAsked, UWord16 *nSamplesRendered, Word32 *output_fx[] );
      57             : 
      58             : static void ivas_jbm_dec_copy_masa_meta_to_buffer( Decoder_Struct *st_ivas );
      59             : 
      60             : static void ivas_jbm_masa_sf_to_slot_map( Decoder_Struct *st_ivas, const Word16 nCldfbTs );
      61             : 
      62             : 
      63             : static Word16 ceil_fx16( Word16 inp, Word16 Q );
      64      230248 : static Word16 ceil_fx16( Word16 inp, Word16 Q )
      65             : {
      66      230248 :     Word16 ret = shr( inp, Q );
      67      230248 :     IF( ( s_and( inp, sub( shl( 1, Q ), 1 ) ) ) > 0 )
      68             :     {
      69        2955 :         ret = add( ret, 1 );
      70             :     }
      71      230248 :     return ret;
      72             : }
      73             : 
      74             : /*--------------------------------------------------------------------------*
      75             :  * ivas_jbm_dec_tc()
      76             :  *
      77             :  * Principal IVAS JBM decoder routine, decoding of metadata and transport channels
      78             :  *--------------------------------------------------------------------------*/
      79             : 
      80      396879 : ivas_error ivas_jbm_dec_tc_fx(
      81             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure         */
      82             :     Word32 *data_fx          /*Q11*/
      83             : )
      84             : {
      85             :     Word16 n, output_frame, nchan_out, i, ii;
      86             :     Decoder_State *st;                           /* used for bitstream handling */
      87             :     Word32 *p_output_fx[MAX_TRANSPORT_CHANNELS]; /* buffer for output synthesis */
      88             :     Word16 nchan_remapped;
      89             :     Word16 nb_bits_metadata[MAX_SCE + 1];
      90             :     Word32 output_Fs, ivas_total_brate;
      91             :     AUDIO_CONFIG output_config;
      92             :     ivas_error error;
      93             :     Word16 num_md_sub_frames;
      94             :     Word32 ism_total_brate;
      95             :     Word16 s;
      96             : 
      97             :     CPE_DEC_HANDLE hCPE;
      98             :     SCE_DEC_HANDLE hSCE;
      99      396879 :     push_wmops( "ivas_jbm_dec_tc" );
     100             : 
     101             :     /*----------------------------------------------------------------*
     102             :      * Initialization of local vars after struct has been set
     103             :      *----------------------------------------------------------------*/
     104             : 
     105      396879 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     106      396879 :     move32();
     107      396879 :     nchan_out = st_ivas->hTcBuffer->nchan_transport_jbm;
     108      396879 :     move16();
     109      396879 :     output_config = st_ivas->hDecoderConfig->output_config;
     110      396879 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     111      396879 :     move32();
     112             : 
     113      396879 :     output_frame = extract_l( Mult_32_16( output_Fs, INV_FRAME_PER_SEC_Q15 ) ) /* output_Fs / FRAMES_PER_SEC */;
     114             : 
     115     5159427 :     FOR( n = 0; n < MAX_TRANSPORT_CHANNELS; n++ )
     116             :     {
     117     4762548 :         p_output_fx[n] = st_ivas->p_output_fx[n];
     118             : 
     119     4762548 :         IF( p_output_fx[n] != NULL )
     120             :         {
     121     2230243 :             set32_fx( p_output_fx[n], 0, L_FRAME48k );
     122             :         }
     123             :     }
     124             : 
     125      396879 :     IF( st_ivas->hDecoderConfig->Opt_tsm == 0 )
     126             :     {
     127     2735838 :         FOR( n = 0; n < ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); n++ )
     128             :         {
     129     2358364 :             set_zero_fx( st_ivas->p_output_fx[n], L_FRAME48k );
     130     2358364 :             st_ivas->hTcBuffer->tc_fx[n] = st_ivas->p_output_fx[n];
     131             :         }
     132      377474 :         st_ivas->hTcBuffer->no_channels = ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
     133      377474 :         move16();
     134             :     }
     135             : 
     136             :     Word16 ch;
     137             : 
     138             : 
     139             :     /*----------------------------------------------------------------*
     140             :      * Decoding + pre-rendering
     141             :      *----------------------------------------------------------------*/
     142             : 
     143      396879 :     test();
     144      396879 :     test();
     145      396879 :     IF( st_ivas->bfi != 0 && st_ivas->ini_frame == 0 )
     146             :     {
     147             :         /* zero output when first frame(s) is lost */
     148           0 :         FOR( n = 0; n < nchan_out; n++ )
     149             :         {
     150           0 :             set32_fx( p_output_fx[n], 0, output_frame );
     151             :         }
     152             :     }
     153      396879 :     ELSE IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
     154             :     {
     155       71432 :         st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     156       71432 :         move32();
     157       71432 :         Word16 q_output = 11;
     158       71432 :         move16();
     159       71432 :         set32_fx( &p_output_fx[0][0], 0, L_FRAME48k );
     160       71432 :         set32_fx( &p_output_fx[1][0], 0, L_FRAME48k );
     161             : 
     162       71432 :         IF( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, 0 ) ) != IVAS_ERR_OK )
     163             :         {
     164           0 :             return error;
     165             :         }
     166             : 
     167       71432 :         IF( NE_16( q_output, Q11 ) )
     168             :         {
     169           0 :             Scale_sig32( p_output_fx[0], L_FRAME48k, sub( Q11, q_output ) ); // Q11
     170           0 :             Scale_sig32( p_output_fx[1], L_FRAME48k, sub( Q11, q_output ) ); // Q11
     171             :         }
     172             : 
     173             :         /* HP filtering */
     174      190396 :         FOR( n = 0; n < s_min( nchan_out, st_ivas->nchan_transport ); n++ )
     175             :         {
     176      118964 :             hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
     177             :         }
     178             : 
     179       71432 :         test();
     180       71432 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) && EQ_16( st_ivas->hDecoderConfig->nchan_out, 1 ) )
     181             :         {
     182           0 :             s = Q16 - Q11;
     183           0 :             move16();
     184           0 :             s = sub( s, find_guarded_bits_fx( st_ivas->nchan_transport ) );
     185           0 :             FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, st_ivas->nchan_transport ); ++i )
     186             :             {
     187           0 :                 Scale_sig32( p_output_fx[i], output_frame, s ); // Q(11+s)
     188             :             }
     189           0 :             ivas_ls_setup_conversion_fx( st_ivas, st_ivas->nchan_transport, output_frame, p_output_fx, p_output_fx );
     190           0 :             FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, st_ivas->nchan_transport ); ++i )
     191             :             {
     192           0 :                 Scale_sig32( p_output_fx[i], output_frame, negate( s ) ); // Q11
     193             :             }
     194             :         }
     195             :     }
     196      325447 :     ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
     197             :     {
     198             :         /* Metadata decoding and configuration */
     199       93837 :         test();
     200       93837 :         IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) || EQ_32( ivas_total_brate, FRAME_NO_DATA ) )
     201        2151 :         {
     202        2151 :             ivas_ism_dtx_dec_fx( st_ivas, nb_bits_metadata );
     203             : 
     204        2151 :             set32_fx( p_output_fx[st_ivas->hISMDTX.sce_id_dtx], 0, L_FRAME48k );
     205             : 
     206        2151 :             IF( ( error = ivas_sce_dec_fx( st_ivas, st_ivas->hISMDTX.sce_id_dtx, &p_output_fx[st_ivas->hISMDTX.sce_id_dtx], output_frame, nb_bits_metadata[st_ivas->hISMDTX.sce_id_dtx] ) ) != IVAS_ERR_OK )
     207             :             {
     208           0 :                 return error;
     209             :             }
     210             :             Word16 Q_cngNoiseLevel[MAX_SCE];
     211       10755 :             FOR( ch = 0; ch < 4; ch++ )
     212             :             {
     213        8604 :                 IF( st_ivas->hSCE[ch] != NULL )
     214             :                 {
     215        5310 :                     Word16 shift = getScaleFactor32( st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
     216        5310 :                     if ( LT_16( sub( st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
     217             :                     {
     218        1433 :                         shift = sub( st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
     219             :                     }
     220        5310 :                     scale_sig32( st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31-(cngNoiseLevelExp -shift) )
     221        5310 :                     st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
     222        5310 :                     Q_cngNoiseLevel[ch] = sub( 31, st_ivas->hSCE[ch]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp );
     223        5310 :                     move16();
     224        5310 :                     move16();
     225             :                 }
     226             :             }
     227        2151 :             ivas_ism_dtx_limit_noise_energy_for_near_silence_fx( st_ivas->hSCE, st_ivas->hISMDTX.sce_id_dtx, st_ivas->nchan_transport, Q_cngNoiseLevel );
     228             :         }
     229       91686 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     230             :         {
     231       15884 :             IF( NE_32( ( error = ivas_ism_metadata_dec_fx( 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 ) )
     232             :             {
     233           0 :                 return error;
     234             :             }
     235             :         }
     236             :         ELSE /* ISM_MODE_DISC */
     237             :         {
     238       75802 :             IF( NE_32( ( error = ivas_ism_metadata_dec_fx( 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 ) )
     239             :             {
     240           0 :                 return error;
     241             :             }
     242             :         }
     243             : 
     244      333337 :         FOR( n = 0; n < st_ivas->nchan_transport; n++ )
     245             :         {
     246             :             /* for DTX frames, dominant object has already been decoded before */
     247      239500 :             test();
     248      239500 :             test();
     249      239500 :             IF( !( ( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) || EQ_32( ivas_total_brate, FRAME_NO_DATA ) ) && EQ_16( n, st_ivas->hISMDTX.sce_id_dtx ) ) )
     250             :             {
     251      237349 :                 set32_fx( p_output_fx[n], 0, L_FRAME48k );
     252             : 
     253      237349 :                 IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, n, &p_output_fx[n], output_frame, nb_bits_metadata[n] ) ), IVAS_ERR_OK ) )
     254             :                 {
     255           0 :                     return error;
     256             :                 }
     257             :             }
     258             : 
     259             :             /* HP filtering */
     260      239500 :             hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
     261             :         }
     262             : 
     263       93837 :         test();
     264       93837 :         test();
     265       93837 :         test();
     266       93837 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_MONO_DOWNMIX ) )
     267             :         {
     268        3970 :             ivas_mono_downmix_render_passive_fx( st_ivas, p_output_fx, output_frame );
     269             : 
     270        3970 :             Scale_sig32( p_output_fx[0], L_FRAME48k, 3 );
     271             :         }
     272       89867 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) && ( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) ) )
     273             :         {
     274             :             /* loudness correction */
     275        7955 :             ivas_dirac_dec_binaural_sba_gain_fx( p_output_fx, st_ivas->nchan_transport, output_frame ); /*returns Q-1*/
     276             : 
     277       23865 :             FOR( i = 0; i < st_ivas->nchan_transport; i++ )
     278             :             {
     279       15910 :                 Scale_sig32( p_output_fx[i], output_frame, 1 ); // Q-1 -> Q
     280             :             }
     281             :         }
     282             :     }
     283      231610 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
     284      151232 :     {
     285      151232 :         set16_fx( nb_bits_metadata, 0, MAX_SCE );
     286             : 
     287             : 
     288             :         /* read parameters from the bitstream */
     289      151232 :         test();
     290      151232 :         IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && st_ivas->hQMetaData != NULL )
     291             :         {
     292             :             // st = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
     293             : 
     294       32208 :             IF( st_ivas->nSCE > 0 )
     295             :             {
     296       13800 :                 st = st_ivas->hSCE[0]->hCoreCoder[0];
     297             :             }
     298             :             ELSE
     299             :             {
     300       18408 :                 st = st_ivas->hCPE[0]->hCoreCoder[0];
     301             :             }
     302             : 
     303       32208 :             IF( NE_32( ( error = ivas_masa_decode_fx( st_ivas, st, &nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     304             :             {
     305           0 :                 return error;
     306             :             }
     307             : 
     308       32208 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
     309             :             {
     310        4859 :                 ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
     311             :             }
     312             :         }
     313      119024 :         ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
     314             :         {
     315      119024 :             IF( NE_32( ( error = ivas_spar_dec_fx( st_ivas, nb_bits_metadata ) ), IVAS_ERR_OK ) )
     316             :             {
     317           0 :                 return error;
     318             :             }
     319             :         }
     320             : 
     321      151232 :         test();
     322      151232 :         IF( EQ_16( st_ivas->nchan_transport, CPE_CHANNELS ) && GE_16( st_ivas->nCPE, 1 ) )
     323             :         {
     324       52895 :             st_ivas->hCPE[0]->brate_surplus = 0;
     325       52895 :             move32();
     326       52895 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     327       52895 :             move32();
     328             :         }
     329             : 
     330             :         /* core-decoding of transport channels */
     331      151232 :         Word16 q_output = Q11;
     332      151232 :         move16();
     333      151232 :         IF( EQ_16( st_ivas->nSCE, 1 ) )
     334             :         {
     335       49099 :             set32_fx( p_output_fx[0], 0, L_FRAME48k );
     336             : 
     337       49099 :             IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     338             :             {
     339           0 :                 return error;
     340             :             }
     341             :         }
     342      102133 :         ELSE IF( EQ_16( st_ivas->nCPE, 1 ) )
     343             :         {
     344       52895 :             set32_fx( &p_output_fx[0][0], 0, L_FRAME48k );
     345       52895 :             set32_fx( &p_output_fx[1][0], 0, L_FRAME48k );
     346             : 
     347       52895 :             IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     348             :             {
     349           0 :                 return error;
     350             :             }
     351             : 
     352       52895 :             IF( NE_16( q_output, Q11 ) )
     353             :             {
     354           0 :                 Scale_sig32( p_output_fx[0], L_FRAME48k, sub( Q11, q_output ) ); // Q11
     355           0 :                 Scale_sig32( p_output_fx[1], L_FRAME48k, sub( Q11, q_output ) ); // Q11
     356             :             }
     357             :         }
     358       49238 :         ELSE IF( GT_16( st_ivas->nCPE, 1 ) )
     359             :         {
     360       49238 :             IF( NE_32( ( error = ivas_mct_dec_fx( st_ivas, p_output_fx, output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     361             :             {
     362           0 :                 return error;
     363             :             }
     364             :         }
     365             : 
     366             :         /* TCs remapping */
     367      151232 :         nchan_remapped = st_ivas->nchan_transport;
     368      151232 :         move16();
     369             : 
     370      151232 :         test();
     371      151232 :         test();
     372      151232 :         test();
     373      151232 :         test();
     374      151232 :         test();
     375      151232 :         IF( st_ivas->sba_dirac_stereo_flag )
     376             :         {
     377        6610 :             nchan_remapped = nchan_out;
     378        6610 :             move16();
     379             : 
     380        6610 :             IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) )
     381             :             {
     382       20540 :                 FOR( i = 0; i < st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport; i++ )
     383             :                 {
     384       13930 :                     Scale_sig32( p_output_fx[i], output_frame, Q14 - Q11 ); // Q14
     385             :                 }
     386        6610 :                 SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
     387             :                 Word16 nchan_transport;
     388        6610 :                 nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
     389        6610 :                 move16();
     390        6610 :                 nchan_out = nchan_transport;
     391        6610 :                 move16();
     392        6610 :                 ivas_agc_dec_process_fx( st_ivas->hSpar->hAgcDec, ( p_output_fx ), ( p_output_fx ), st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
     393             : 
     394        6610 :                 IF( st_ivas->hSpar->hPCA != NULL )
     395             :                 {
     396           0 :                     ivas_pca_dec_fx( 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_fx );
     397             :                 }
     398             : 
     399        6610 :                 ivas_spar_dec_gen_umx_mat_fx( 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 ) );
     400             :             }
     401             :             {
     402             :                 Word16 q;
     403        6610 :                 hCPE = st_ivas->hCPE[0];
     404        6610 :                 hSCE = st_ivas->hSCE[0];
     405             : 
     406        6610 :                 s = 0;
     407        6610 :                 move16();
     408       19830 :                 FOR( i = 0; i < 2; i++ )
     409             :                 {
     410       13220 :                     s = s_min( s, L_norm_arr( p_output_fx[i], L_FRAME48k ) - 11 ) /* Guard bits */; // L_frame should be used instead of L_FRAME48k */
     411             :                 }
     412       19830 :                 FOR( i = 0; i < 2; i++ )
     413             :                 {
     414       13220 :                     Scale_sig32( p_output_fx[i], L_FRAME48k, s ); // Q(11+s)
     415             :                 }
     416        6610 :                 hCPE->hStereoDft->q_dft = add( Q11, s );
     417        6610 :                 move16();
     418        6610 :                 IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
     419             :                 {
     420          61 :                     hCPE->hStereoDft->q_smoothed_nrg = Q6; // hCPE->hStereoDft->q_dft;
     421          61 :                     move16();
     422          61 :                     hCPE->hStereoDft->q_ap_delay_mem_fx = hCPE->hStereoDft->q_dft;
     423          61 :                     move16();
     424             :                 }
     425        6610 :                 q = hCPE->hStereoDft->q_dft;
     426        6610 :                 move16();
     427        6610 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     428       19830 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
     429             :                 {
     430       13220 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) );                                          // q_dft
     431       13220 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     432             :                 }
     433        6610 :                 IF( hCPE->hCoreCoder[0] != NULL )
     434             :                 {
     435        6610 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k, q - hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB ); // q
     436        6610 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, L_FRAME48k, q - hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda );          // q
     437        6610 :                     hCPE->hCoreCoder[0]->hHQ_core->q_old_outLB_fx = q;
     438        6610 :                     move16();
     439             :                 }
     440        6610 :                 IF( hCPE->hStereoDft != NULL )
     441             :                 {
     442        6610 :                     IF( LE_16( st_ivas->nchan_transport, 1 ) )
     443             :                     {
     444        2970 :                         st = hCPE->hCoreCoder[0];
     445        2970 :                         test();
     446        2970 :                         test();
     447        2970 :                         test();
     448        2970 :                         test();
     449        2970 :                         test();
     450        2970 :                         IF( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) || ( EQ_16( st->bfi, 1 ) && EQ_16( st->core, ACELP_CORE ) && EQ_16( st->con_tcx, 1 ) ) )
     451             :                         {
     452        1349 :                             test();
     453        1349 :                             test();
     454        1349 :                             test();
     455        1349 :                             test();
     456        1349 :                             test();
     457        1349 :                             test();
     458        1349 :                             test();
     459        1349 :                             test();
     460        1349 :                             IF( ( ( ( st->last_core != ACELP_CORE ) || ( EQ_16( st->prev_bfi, 1 ) && ( st->last_core == ACELP_CORE ) && EQ_16( st->last_con_tcx, 1 ) ) ) && NE_16( st->last_core, AMR_WB_CORE ) ) || ( st_ivas->sba_dirac_stereo_flag && EQ_16( st->cng_type, FD_CNG ) ) ) /* TCX / HQ-CORE -> TCX / HQ-CORE */
     461             :                             {
     462        1251 :                                 scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     463             :                             }
     464          98 :                             ELSE IF( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) ) /* ACELP -> TCX/HQ */
     465             :                             {
     466          98 :                                 IF( !st->tcxonly )
     467             :                                 {
     468          98 :                                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     469             :                                 }
     470             :                             }
     471             :                         }
     472             :                         ELSE /* ACELP core */
     473             :                         {
     474        1621 :                             test();
     475        1621 :                             test();
     476        1621 :                             IF( EQ_16( st->last_core, TCX_20_CORE ) || EQ_16( st->last_core, TCX_10_CORE ) || EQ_16( st->last_core, HQ_CORE ) ) /* TCX/HQ -> ACELP */
     477             :                             {
     478          82 :                                 test();
     479          82 :                                 test();
     480          82 :                                 test();
     481          82 :                                 test();
     482          82 :                                 IF( ( LE_16( st->last_L_frame, L_FRAME16k ) && LE_16( st->L_frame, L_FRAME16k ) ) || ( st_ivas->sba_dirac_stereo_flag && EQ_32( st->core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) ) )
     483             :                                 {
     484          82 :                                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     485             :                                 }
     486             :                             }
     487             :                         }
     488             :                     }
     489        6610 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA_FX2( 16000, DELAY_BWE_TOTAL_NS ), sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // q_dft
     490        6610 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = hCPE->hStereoDft->q_dft;
     491        6610 :                     move16();
     492             :                 }
     493             : 
     494        6610 :                 IF( hSCE != NULL )
     495             :                 {
     496        2970 :                     Scale_sig32( &hSCE->save_hb_synth_fx[0], extract_l( Mult_32_16( hCPE->hCoreCoder[0]->output_Fs, INV_FRAME_PER_SEC_Q15 ) ), sub( hCPE->hStereoDft->q_dft, hSCE->q_save_hb_synth_fx ) ); // q_dft
     497        2970 :                     hSCE->q_save_hb_synth_fx = hCPE->hStereoDft->q_dft;
     498        2970 :                     move16();
     499        2970 :                     Scale_sig32( &hSCE->save_synth_fx[0], extract_l( Mult_32_16( hCPE->hCoreCoder[0]->output_Fs, INV_FRAME_PER_SEC_Q15 ) ), sub( hCPE->hStereoDft->q_dft, hSCE->q_save_synth_fx ) ); // q_dft
     500        2970 :                     hSCE->q_save_synth_fx = hCPE->hStereoDft->q_dft;
     501        2970 :                     move16();
     502             :                 }
     503       19830 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
     504             :                 {
     505       13220 :                     scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     506       13220 :                     hCPE->q_output_mem_fx[ii] = hCPE->hStereoDft->q_dft;
     507       13220 :                     move16();
     508             :                 }
     509       19830 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
     510             :                 {
     511       13220 :                     Scale_sig32( &hCPE->prev_synth_fx[ii][0], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( hCPE->q_prev_synth_fx, Q11 ) ); // q_prev_synth_fx
     512             :                 }
     513             : 
     514        6610 :                 ivas_sba_dirac_stereo_dec_fx( st_ivas, p_output_fx, output_frame, st_ivas->ivas_format == MC_FORMAT );
     515             : 
     516       19830 :                 FOR( i = 0; i < 2; i++ )
     517             :                 {
     518       13220 :                     Scale_sig32( p_output_fx[i], L_FRAME48k, negate( s ) );
     519             :                 }
     520       19830 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
     521             :                 {
     522       13220 :                     Scale_sig32( &hCPE->prev_synth_fx[ii][0], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->q_prev_synth_fx ) ); // Q11
     523             :                 }
     524             : 
     525        6610 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) );
     526       19830 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
     527             :                 {
     528       13220 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) );                                          // Q11
     529       13220 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     530             :                 }
     531             : 
     532        6610 :                 IF( hCPE->hCoreCoder[0] != NULL )
     533             :                 {
     534        6610 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, L_FRAME32k, hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB - q ); // Q_old_wtda_LB
     535        6610 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, L_FRAME48k, hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda - q );          // Q_old_wtda_LB
     536             :                 }
     537        6610 :                 IF( hCPE->hStereoDft != NULL )
     538             :                 {
     539        6610 :                     IF( LE_16( st_ivas->nchan_transport, 1 ) )
     540             :                     {
     541        2970 :                         st = hCPE->hCoreCoder[0];
     542        2970 :                         test();
     543        2970 :                         test();
     544        2970 :                         test();
     545        2970 :                         test();
     546        2970 :                         test();
     547        2970 :                         IF( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) || ( EQ_16( st->bfi, 1 ) && st->core == ACELP_CORE && EQ_16( st->con_tcx, 1 ) ) )
     548             :                         {
     549        1349 :                             test();
     550        1349 :                             test();
     551        1349 :                             test();
     552        1349 :                             test();
     553        1349 :                             test();
     554        1349 :                             test();
     555        1349 :                             test();
     556        1349 :                             test();
     557        1349 :                             IF( ( ( st->last_core != ACELP_CORE || ( EQ_16( st->prev_bfi, 1 ) && st->last_core == ACELP_CORE && EQ_16( st->last_con_tcx, 1 ) ) ) && NE_16( st->last_core, AMR_WB_CORE ) ) || ( st_ivas->sba_dirac_stereo_flag && EQ_16( st->cng_type, FD_CNG ) ) ) /* TCX / HQ-CORE -> TCX / HQ-CORE */
     558             :                             {
     559        1349 :                                 scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     560             :                             }
     561           0 :                             ELSE IF( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) ) /* ACELP -> TCX/HQ */
     562             :                             {
     563           0 :                                 IF( !st->tcxonly )
     564             :                                 {
     565           0 :                                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     566             :                                 }
     567             :                             }
     568             :                         }
     569             :                         ELSE /* ACELP core */
     570             :                         {
     571        1621 :                             test();
     572        1621 :                             test();
     573        1621 :                             IF( EQ_16( st->last_core, TCX_20_CORE ) || EQ_16( st->last_core, TCX_10_CORE ) || EQ_16( st->last_core, HQ_CORE ) ) /* TCX/HQ -> ACELP */
     574             :                             {
     575           0 :                                 test();
     576           0 :                                 test();
     577           0 :                                 test();
     578           0 :                                 test();
     579           0 :                                 IF( ( LE_16( st->last_L_frame, L_FRAME16k ) && LE_16( st->L_frame, L_FRAME16k ) ) || ( st_ivas->sba_dirac_stereo_flag && EQ_32( st->core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) ) )
     580             :                                 {
     581           0 :                                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( L_mult0( s_max( hCPE->hCoreCoder[0]->L_frame, hCPE->hCoreCoder[0]->last_L_frame ), FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     582             :                                 }
     583             :                             }
     584             :                         }
     585             :                     }
     586        6610 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA_FX2( 16000, DELAY_BWE_TOTAL_NS ), sub( Q11, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // Q11
     587        6610 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = Q11;
     588        6610 :                     test();
     589             :                 }
     590             : 
     591        6610 :                 st_ivas->hSpar->hMdDec->Q_mixer_mat = 30;
     592        6610 :                 move16();
     593       19830 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
     594             :                 {
     595       13220 :                     scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     596       13220 :                     hCPE->q_output_mem_fx[ii] = Q11;
     597       13220 :                     move16();
     598             :                 }
     599             :             }
     600             :         }
     601      144622 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || ( LE_32( ivas_total_brate, IVAS_SID_5k2 ) && st_ivas->nCPE > 0 && EQ_16( st_ivas->hCPE[0]->nchan_out, 1 ) ) ) )
     602             :         {
     603        7960 :             nchan_remapped = 1; /* Only one channel transported */
     604        7960 :             move16();
     605             :         }
     606             : 
     607             :         /* HP filtering */
     608      472685 :         FOR( n = 0; n < nchan_remapped; n++ )
     609             :         {
     610      321453 :             hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
     611             :         }
     612             : 
     613      151232 :         IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) )
     614             :         {
     615      119024 :             nchan_remapped = ivas_sba_remapTCs_fx( p_output_fx, st_ivas, output_frame );
     616             : 
     617      119024 :             test();
     618      119024 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
     619       29841 :             {
     620       29841 :                 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 );
     621             : 
     622       29841 :                 SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
     623       29841 :                 Word16 Q_p_output = MAX_16, temp_min;
     624       29841 :                 move16();
     625             :                 Word16 nchan_transport;
     626             : 
     627       29841 :                 nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
     628       29841 :                 move16();
     629       29841 :                 nchan_out = nchan_transport;
     630       29841 :                 move16();
     631       77252 :                 FOR( ch = 0; ch < nchan_transport; ch++ )
     632             :                 {
     633       47411 :                     temp_min = getScaleFactor32( p_output_fx[ch], output_frame );
     634       47411 :                     Q_p_output = s_min( Q_p_output, temp_min );
     635             :                 }
     636       29841 :                 Q_p_output = sub( Q_p_output, 2 );
     637       29841 :                 if ( GT_16( Q_p_output, 20 ) )
     638             :                 {
     639           0 :                     Q_p_output = 20;
     640           0 :                     move16();
     641             :                 }
     642       77252 :                 FOR( ch = 0; ch < nchan_transport; ch++ )
     643             :                 {
     644       47411 :                     Scale_sig32( p_output_fx[ch], output_frame, Q_p_output ); // Q(11+Q_p_output)
     645             :                 }
     646             : 
     647       29841 :                 ivas_sba_mix_matrix_determiner_fx( st_ivas->hSpar, p_output_fx, st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames, Q_p_output );
     648       77252 :                 FOR( Word16 c = 0; c < nchan_transport; c++ )
     649             :                 {
     650       47411 :                     Scale_sig32( p_output_fx[c], output_frame, 11 ); // Q11
     651             :                 }
     652             :             }
     653       89183 :             ELSE IF( NE_32( st_ivas->renderer_type, RENDERER_DISABLE ) )
     654             :             {
     655       80933 :                 Word16 size = st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport;
     656       80933 :                 move16();
     657       80933 :                 if ( EQ_16( st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, 3 ) )
     658       23069 :                     size = add( size, 1 );
     659             : 
     660      315337 :                 FOR( i = 0; i < size; i++ )
     661             :                 {
     662      234404 :                     Scale_sig32( p_output_fx[i], output_frame, Q14 - Q11 ); // Q14
     663             :                 }
     664             : 
     665       80933 :                 ivas_spar_dec_agc_pca_fx( st_ivas, p_output_fx, output_frame );
     666             :             }
     667             :         }
     668             : 
     669      151232 :         test();
     670      151232 :         test();
     671      151232 :         IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
     672             :         {
     673       32208 :             Word16 q_shift = 0;
     674       32208 :             move16();
     675       32208 :             ivas_masa_prerender_fx( st_ivas, p_output_fx, &q_shift, output_frame, nchan_remapped );
     676       96624 :             FOR( i = 0; i < 2; i++ )
     677             :             {
     678       64416 :                 Scale_sig32( p_output_fx[i], output_frame, negate( q_shift ) );
     679             :             }
     680             :         }
     681      119024 :         ELSE IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) && ( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) )
     682             :         {
     683             :             /* loudness correction */
     684       29841 :             ivas_dirac_dec_binaural_sba_gain_fx( p_output_fx, nchan_remapped, output_frame );
     685             : 
     686       77252 :             FOR( i = 0; i < st_ivas->nchan_transport; i++ )
     687             :             {
     688       47411 :                 Scale_sig32( p_output_fx[i], output_frame, 1 ); // Q-1 -> Q
     689             :             }
     690             :         }
     691             :     }
     692       80378 :     ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     693             :     {
     694             :         Word16 nchan_ism, nchan_transport_ism;
     695             :         Word16 dirac_bs_md_write_idx;
     696             : 
     697        6418 :         set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
     698             : 
     699             :         /* Set the number of objects for the parametric rendering */
     700        6418 :         dirac_bs_md_write_idx = 0;
     701        6418 :         move16();
     702        6418 :         IF( st_ivas->hSpatParamRendCom != NULL )
     703             :         {
     704        4840 :             st_ivas->hSpatParamRendCom->numIsmDirections = 0;
     705        4840 :             move16();
     706        4840 :             test();
     707        4840 :             if ( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) && NE_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
     708             :             {
     709        1248 :                 st_ivas->hSpatParamRendCom->numIsmDirections = st_ivas->nchan_ism;
     710        1248 :                 move16();
     711             :             }
     712             : 
     713        4840 :             dirac_bs_md_write_idx = st_ivas->hSpatParamRendCom->dirac_bs_md_write_idx; /* Store the write-index for this frame */
     714        4840 :             move16();
     715             :         }
     716             : 
     717             : 
     718             :         /* set ISM parameters and decode ISM metadata in OMASA format */
     719        6418 :         Word16 q_output = 11;
     720        6418 :         move16();
     721             :         // NOTE: the following is done to calculate the number of hIsmMetaData indices
     722             :         Word16 tmp_nchan_ism;
     723        6418 :         IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
     724             :         {
     725        2029 :             tmp_nchan_ism = 1;
     726        2029 :             move16();
     727             :         }
     728        4389 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
     729             :         {
     730        1623 :             tmp_nchan_ism = 0;
     731        1623 :             move16();
     732             :         }
     733             :         ELSE
     734             :         {
     735        2766 :             tmp_nchan_ism = st_ivas->nchan_ism;
     736        2766 :             move16();
     737             :         }
     738             : 
     739       15778 :         FOR( n = 0; n < tmp_nchan_ism; n++ )
     740             :         {
     741        9360 :             set32_fx( p_output_fx[n], 0, L_FRAME48k );
     742             :         }
     743             : 
     744             :         /* MASA metadata decoding */
     745        6418 :         IF( NE_32( ( error = ivas_masa_decode_fx( st_ivas, st_ivas->hCPE[0]->hCoreCoder[0], &nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     746             :         {
     747           0 :             return error;
     748             :         }
     749             : 
     750             :         /* Configuration of combined-format bit-budget distribution */
     751        6418 :         ivas_set_surplus_brate_dec( st_ivas, &ism_total_brate );
     752             : 
     753        6418 :         st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream = &( st_ivas->bit_stream[( ism_total_brate / FRAMES_PER_SEC )] );
     754             : 
     755        6418 :         IF( NE_32( ( error = ivas_omasa_ism_metadata_dec_fx( st_ivas, ism_total_brate, &nchan_ism, &nchan_transport_ism, dirac_bs_md_write_idx, &nb_bits_metadata[1] ) ), IVAS_ERR_OK ) )
     756             :         {
     757           0 :             return error;
     758             :         }
     759             : 
     760       17401 :         FOR( n = 0; n < nchan_transport_ism; n++ )
     761             :         {
     762       10983 :             IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, n, &p_output_fx[st_ivas->nchan_transport + n], output_frame, nb_bits_metadata[1] ) ), IVAS_ERR_OK ) )
     763             :             {
     764           0 :                 return error;
     765             :             }
     766             :         }
     767             : 
     768             :         /* decode MASA channels */
     769        6418 :         IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, p_output_fx, output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
     770             :         {
     771           0 :             return error;
     772             :         }
     773             : 
     774             :         /* shifting both the channels from variable q_output to constant Q-factor (Q11) */
     775     5424338 :         FOR( Word16 k = 0; k < output_frame; k++ )
     776             :         {
     777     5417920 :             p_output_fx[0][k] = L_shr( p_output_fx[0][k], sub( Q11, q_output ) );
     778     5417920 :             move32();
     779     5417920 :             p_output_fx[1][k] = L_shr( p_output_fx[1][k], sub( Q11, q_output ) );
     780     5417920 :             move32();
     781             :         }
     782             : 
     783        6418 :         IF( EQ_16( st_ivas->hCPE[0]->nchan_out, 1 ) )
     784             :         {
     785        1314 :             Copy32( p_output_fx[0], p_output_fx[1], output_frame ); /* Copy mono signal to stereo output channels */
     786             :         }
     787             : 
     788       30237 :         FOR( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
     789             :         {
     790       23819 :             hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
     791             :         }
     792             : 
     793        6418 :         Word16 output_q = 11;
     794        6418 :         move16();
     795        6418 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_MONO_DOWNMIX ) )
     796             :         {
     797         975 :             ivas_mono_downmix_render_passive_fx( st_ivas, p_output_fx, output_frame );
     798             : 
     799         975 :             output_q = 8;
     800         975 :             move16();
     801             :         }
     802        5443 :         ELSE IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
     803             :         {
     804             :             /* sanity check in case of bitrate switching */
     805         603 :             IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     806             :             {
     807           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_OUTPUT_FORMAT, "Incorrect output configuration specified for combined MASA and ISM format" );
     808             :             }
     809             : 
     810         603 :             ivas_omasa_rearrange_channels_fx( p_output_fx, nchan_transport_ism, output_frame );
     811             : 
     812         603 :             output_q = 11;
     813         603 :             move16();
     814             : 
     815         603 :             ivas_jbm_dec_copy_masa_meta_to_buffer( st_ivas );
     816             :         }
     817        6418 :         IF( NE_16( output_q, Q11 ) )
     818             :         {
     819        4660 :             FOR( n = 0; n < s_max( getNumChanSynthesis( st_ivas ), nchan_transport_ism + st_ivas->nchan_transport ); n++ )
     820             :             {
     821        3685 :                 Scale_sig32( p_output_fx[n], output_frame, sub( Q11, output_q ) ); // Q11
     822             :             }
     823             :         }
     824             :     }
     825       73960 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
     826             :     {
     827             :         Word16 nchan_ism, sba_ch_idx;
     828             : 
     829       30003 :         set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
     830       30003 :         nchan_ism = st_ivas->nchan_ism;
     831       30003 :         move16();
     832       30003 :         IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
     833             :         {
     834             :             /* set ISM parameters and decode ISM metadata in OSBA format */
     835       13100 :             IF( NE_32( ( error = ivas_osba_ism_metadata_dec_fx( st_ivas, ivas_total_brate, &nchan_ism, &nb_bits_metadata[1] ) ), IVAS_ERR_OK ) )
     836             :             {
     837           0 :                 return error;
     838             :             }
     839       13100 :             sba_ch_idx = st_ivas->nchan_ism;
     840       13100 :             move16();
     841             :         }
     842             :         ELSE
     843             :         {
     844       16903 :             nb_bits_metadata[1] = add( nb_bits_metadata[1], NO_BITS_MASA_ISM_NO_OBJ );
     845       16903 :             sba_ch_idx = 0;
     846       16903 :             move16();
     847       16903 :             move16();
     848             :         }
     849             : 
     850             :         /* SBA metadata decoding */
     851       30003 :         IF( NE_32( ( error = ivas_spar_dec_fx( st_ivas, nb_bits_metadata ) ), IVAS_ERR_OK ) )
     852             :         {
     853           0 :             return error;
     854             :         }
     855             : 
     856       30003 :         test();
     857       30003 :         IF( EQ_16( st_ivas->nchan_transport, CPE_CHANNELS ) && GE_16( st_ivas->nCPE, 1 ) )
     858             :         {
     859        5017 :             st_ivas->hCPE[0]->element_brate = ivas_total_brate;
     860        5017 :             move32();
     861             :         }
     862             : 
     863             :         /* core-decoding of transport channels */
     864       30003 :         Word16 q_output = 11;
     865       30003 :         move16();
     866             : 
     867       30003 :         IF( EQ_16( st_ivas->nSCE, 1 ) )
     868             :         {
     869        6255 :             set32_fx( p_output_fx[0], 0, L_FRAME48k );
     870             : 
     871        6255 :             IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ), IVAS_ERR_OK ) )
     872             :             {
     873           0 :                 return error;
     874             :             }
     875             :         }
     876       23748 :         ELSE IF( EQ_16( st_ivas->nCPE, 1 ) )
     877             :         {
     878             : 
     879        5017 :             IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, add( nb_bits_metadata[0], nb_bits_metadata[1] ) ) ), IVAS_ERR_OK ) )
     880             :             {
     881           0 :                 return error;
     882             :             }
     883             : 
     884        5017 :             IF( NE_16( q_output, Q11 ) )
     885             :             {
     886           0 :                 Scale_sig32( p_output_fx[0], output_frame, sub( Q11, q_output ) ); // Q11
     887           0 :                 Scale_sig32( p_output_fx[1], output_frame, sub( Q11, q_output ) ); // Q11
     888             :             }
     889             :         }
     890       18731 :         ELSE IF( GT_16( st_ivas->nCPE, 1 ) )
     891             :         {
     892       18731 :             IF( NE_32( ( error = ivas_mct_dec_fx( st_ivas, p_output_fx, output_frame, nb_bits_metadata[0] + nb_bits_metadata[1] ) ), IVAS_ERR_OK ) )
     893             :             {
     894           0 :                 return error;
     895             :             }
     896             :         }
     897             : 
     898       30003 :         IF( st_ivas->sba_dirac_stereo_flag )
     899             :         {
     900       12320 :             FOR( i = 0; i < st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport; i++ )
     901             :             {
     902        9320 :                 Scale_sig32( p_output_fx[i + sba_ch_idx], output_frame, Q14 - Q11 ); // Q14
     903             :             }
     904        3000 :             SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
     905             :             Word16 nchan_transport;
     906        3000 :             nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
     907        3000 :             move16();
     908        3000 :             nchan_out = nchan_transport;
     909        3000 :             move16();
     910        3000 :             ivas_agc_dec_process_fx( st_ivas->hSpar->hAgcDec, ( p_output_fx + sba_ch_idx ), ( p_output_fx + sba_ch_idx ), st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, output_frame );
     911             : 
     912        3000 :             IF( st_ivas->hSpar->hPCA != NULL )
     913             :             {
     914           0 :                 ivas_pca_dec_fx( 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_fx[sba_ch_idx] );
     915             :             }
     916        3000 :             ivas_spar_dec_gen_umx_mat_fx( 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 ) );
     917             :             {
     918             :                 Word16 q;
     919        3000 :                 hCPE = st_ivas->hCPE[0];
     920        3000 :                 hSCE = st_ivas->hSCE[0];
     921        3000 :                 s = 0;
     922        3000 :                 move16();
     923        9000 :                 FOR( i = 0; i < 2; i++ )
     924             :                 {
     925        6000 :                     s = s_min( s, L_norm_arr( p_output_fx[sba_ch_idx + i], L_FRAME48k ) - 11 /* Guard bits */ );
     926             :                 }
     927        9000 :                 FOR( i = 0; i < 2; i++ )
     928             :                 {
     929        6000 :                     Scale_sig32( p_output_fx[sba_ch_idx + i], L_FRAME48k, s ); // Q(11+s)
     930             :                 }
     931        3000 :                 hCPE->hStereoDft->q_dft = add( Q11, s );
     932        3000 :                 move16();
     933        3000 :                 IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
     934             :                 {
     935          30 :                     hCPE->hStereoDft->q_smoothed_nrg = Q6; // hCPE->hStereoDft->q_dft;
     936          30 :                     hCPE->hStereoDft->q_ap_delay_mem_fx = hCPE->hStereoDft->q_dft;
     937          30 :                     move16();
     938          30 :                     move16();
     939             :                 }
     940             : 
     941        3000 :                 q = hCPE->hStereoDft->q_dft;
     942        3000 :                 move16();
     943        3000 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) );
     944        9000 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
     945             :                 {
     946        6000 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) );                                          // q_dft
     947        6000 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     948             :                 }
     949        3000 :                 IF( hCPE->hCoreCoder[0] != NULL )
     950             :                 {
     951        3000 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k, sub( q, hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB ) ); // q
     952        3000 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, L_FRAME48k, sub( q, hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda ) );          // q
     953        3000 :                     hCPE->hCoreCoder[0]->hHQ_core->q_old_outLB_fx = q;
     954        3000 :                     move16();
     955             :                 }
     956        3000 :                 IF( hCPE->hStereoDft != NULL )
     957             :                 {
     958        3000 :                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA( 16000, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) );                           // q_dft
     959        3000 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA( 16000, DELAY_BWE_TOTAL_NS ), sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // q_dft
     960        3000 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = hCPE->hStereoDft->q_dft;
     961        3000 :                     move16();
     962             :                 }
     963             : 
     964        3000 :                 IF( hSCE != NULL )
     965             :                 {
     966         361 :                     Scale_sig32( &hSCE->save_hb_synth_fx[0], extract_l( Mpy_32_32( hCPE->hCoreCoder[0]->output_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) ), hCPE->hStereoDft->q_dft - hSCE->q_save_hb_synth_fx ); // q_dft
     967         361 :                     hSCE->q_save_hb_synth_fx = hCPE->hStereoDft->q_dft;
     968         361 :                     move16();
     969         361 :                     Scale_sig32( &hSCE->save_synth_fx[0], extract_l( Mpy_32_32( hCPE->hCoreCoder[0]->output_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) ), hCPE->hStereoDft->q_dft - hSCE->q_save_synth_fx ); // q_dft
     970         361 :                     hSCE->q_save_synth_fx = hCPE->hStereoDft->q_dft;
     971         361 :                     move16();
     972             :                 }
     973        9000 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
     974             :                 {
     975        6000 :                     scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     976        6000 :                     hCPE->q_output_mem_fx[ii] = hCPE->hStereoDft->q_dft;
     977        6000 :                     move16();
     978             :                 }
     979        9000 :                 FOR( i = 0; i < CPE_CHANNELS; i++ )
     980        6000 :                 Scale_sig32( hCPE->prev_synth_fx[i], NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( hCPE->q_prev_synth_fx, Q11 ) ); // q_prev_synth_fx
     981        3000 :                 ivas_sba_dirac_stereo_dec_fx( st_ivas, &p_output_fx[sba_ch_idx], output_frame, 0 );
     982        9000 :                 FOR( i = 0; i < 2; i++ )
     983             :                 {
     984        6000 :                     Scale_sig32( p_output_fx[sba_ch_idx + i], L_FRAME48k, negate( s ) );
     985             :                 }
     986             : 
     987        9000 :                 FOR( i = 0; i < CPE_CHANNELS; i++ )
     988        6000 :                 Scale_sig32( hCPE->prev_synth_fx[i], NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( 11, hCPE->q_prev_synth_fx ) ); // Q11
     989             : 
     990        3000 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     991        9000 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
     992             :                 {
     993        6000 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) );                                          // Q11
     994        6000 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     995             :                 }
     996             : 
     997        3000 :                 IF( hCPE->hCoreCoder[0] != NULL )
     998             :                 {
     999             : 
    1000        3000 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, L_FRAME32k, sub( hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB, q ) ); // Q_old_wtda_LB
    1001        3000 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, L_FRAME48k, sub( hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda, q ) );          // Q_old_wtda
    1002             :                 }
    1003        3000 :                 IF( hCPE->hStereoDft != NULL )
    1004             :                 {
    1005        3000 :                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA( 16000, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) );       // Q11
    1006        3000 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA( 16000, DELAY_BWE_TOTAL_NS ), sub( Q11, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // Q11
    1007        3000 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = Q11;
    1008        3000 :                     move16();
    1009             :                 }
    1010             : 
    1011        9000 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
    1012             :                 {
    1013        6000 :                     scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
    1014        6000 :                     hCPE->q_output_mem_fx[ii] = Q11;
    1015        6000 :                     move16();
    1016             :                 }
    1017             :             }
    1018             :         }
    1019             : 
    1020             :         /* HP filtering */
    1021      151395 :         FOR( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
    1022             :         {
    1023      121392 :             hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
    1024             :         }
    1025             : 
    1026       30003 :         nchan_remapped = ivas_sba_remapTCs_fx( &p_output_fx[sba_ch_idx], st_ivas, output_frame );
    1027             : 
    1028       30003 :         test();
    1029       30003 :         test();
    1030       30003 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
    1031        7213 :         {
    1032        7213 :             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 );
    1033        7213 :             SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
    1034        7213 :             Word16 Q_p_output = 14;
    1035        7213 :             move16();
    1036             :             Word16 nchan_transport;
    1037             :             // num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands;
    1038        7213 :             nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
    1039        7213 :             move16();
    1040        7213 :             nchan_out = nchan_transport;
    1041        7213 :             move16();
    1042       18105 :             FOR( ch = 0; ch < nchan_transport; ch++ )
    1043             :             {
    1044       10892 :                 Scale_sig32( p_output_fx[sba_ch_idx + ch], output_frame, sub( Q_p_output, Q11 ) ); // Q_p_output
    1045             :             }
    1046        7213 :             hSpar->hMdDec->Q_mixer_mat = 31;
    1047        7213 :             move16();
    1048             : 
    1049        7213 :             ivas_sba_mix_matrix_determiner_fx( st_ivas->hSpar, &p_output_fx[sba_ch_idx], st_ivas->bfi, nchan_remapped, output_frame, num_md_sub_frames, sub( Q_p_output, Q11 ) );
    1050       18105 :             FOR( Word16 c = 0; c < nchan_transport; c++ )
    1051             :             {
    1052       10892 :                 Scale_sig32( p_output_fx[sba_ch_idx + c], output_frame, Q11 ); // Q11
    1053             :             }
    1054             :         }
    1055       22790 :         ELSE IF( NE_32( st_ivas->renderer_type, RENDERER_DISABLE ) && st_ivas->sba_dirac_stereo_flag == 0 )
    1056             :         {
    1057       18790 :             Word16 size = add( st_ivas->hSpar->hMdDec->spar_md_cfg.nchan_transport, sba_ch_idx );
    1058       18790 :             if ( EQ_16( size, 3 ) )
    1059             :             {
    1060        4370 :                 size = add( size, 1 );
    1061             :             }
    1062             : 
    1063       86530 :             FOR( i = sba_ch_idx; i < size; i++ )
    1064             :             {
    1065       67740 :                 Scale_sig32( p_output_fx[i], output_frame, ( Q14 - Q11 ) ); // Q14
    1066             :             }
    1067             : 
    1068       18790 :             ivas_spar_dec_agc_pca_fx( st_ivas, &p_output_fx[sba_ch_idx], output_frame );
    1069             :         }
    1070             : 
    1071       30003 :         test();
    1072       30003 :         test();
    1073       30003 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
    1074             :         {
    1075             :             /* loudness correction */
    1076        7213 :             ivas_dirac_dec_binaural_sba_gain_fx( &p_output_fx[sba_ch_idx], nchan_remapped, output_frame ); /*returns Q-1*/
    1077             : 
    1078       18105 :             FOR( i = 0; i < nchan_remapped; i++ )
    1079             :             {
    1080       10892 :                 Scale_sig32( p_output_fx[i], output_frame, 1 ); // Q-1 -> Q
    1081             :             }
    1082             :         }
    1083       22790 :         ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_MONO_DOWNMIX ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    1084             :         {
    1085        1000 :             ivas_mono_downmix_render_passive_fx( st_ivas, p_output_fx, output_frame );
    1086             : 
    1087        1000 :             Scale_sig32( p_output_fx[0], L_FRAME48k, Q11 - Q8 ); // Q11
    1088             : 
    1089             :             /* add W */
    1090        2000 :             FOR( n = 0; n < nchan_out; n++ )
    1091             :             {
    1092        1000 :                 v_add_32( p_output_fx[n], p_output_fx[n + s_max( nchan_out, nchan_ism )], p_output_fx[n], output_frame );
    1093             :             }
    1094             :         }
    1095             :     }
    1096       43957 :     ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
    1097             :     {
    1098             : 
    1099             :         // st = (st_ivas->nSCE > 0) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
    1100       43957 :         IF( st_ivas->nSCE > 0 )
    1101             :         {
    1102        9720 :             st = st_ivas->hSCE[0]->hCoreCoder[0];
    1103             :         }
    1104             :         ELSE
    1105             :         {
    1106       34237 :             st = st_ivas->hCPE[0]->hCoreCoder[0];
    1107             :         }
    1108             : 
    1109       43957 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
    1110             :         {
    1111             :             /* LFE channel decoder */
    1112       24307 :             ivas_lfe_dec_fx( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output_fx[LFE_CHANNEL] );
    1113             : 
    1114       24307 :             Scale_sig32( p_output_fx[LFE_CHANNEL], L_FRAME48k, Q11 - Q9 ); // Q11
    1115       24307 :             IF( NE_32( ( error = ivas_mct_dec_fx( st_ivas, p_output_fx, output_frame, 0 ) ), IVAS_ERR_OK ) )
    1116             :             {
    1117           0 :                 return error;
    1118             :             }
    1119             :             /* HP filtering */
    1120      182047 :             FOR( n = 0; n < st_ivas->nchan_transport; n++ )
    1121             :             {
    1122      157740 :                 IF( NE_16( n, LFE_CHANNEL ) )
    1123             :                 {
    1124      133433 :                     hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
    1125             :                 }
    1126             :             }
    1127             : 
    1128       24307 :             test();
    1129       24307 :             test();
    1130       24307 :             test();
    1131       24307 :             IF( NE_32( st_ivas->transport_config, st_ivas->intern_config ) && ( NE_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_FOA ) || NE_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA2 ) || NE_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
    1132             :             {
    1133         150 :                 IF( GE_16( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ), add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe ) ) )
    1134             :                 {
    1135           0 :                     ivas_mc2sba_fx( st_ivas->hTransSetup, p_output_fx, p_output_fx, output_frame, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE_FX );
    1136             :                 }
    1137             :             }
    1138             : 
    1139       24307 :             test();
    1140       24307 :             test();
    1141       24307 :             IF( ( EQ_32( st_ivas->renderer_type, RENDERER_MC ) || EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) ) && GE_16( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ), add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe ) ) )
    1142             :             {
    1143        3030 :                 IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) )
    1144             :                 {
    1145        3030 :                     s = Q16 - Q11;
    1146        3030 :                     move16();
    1147        3030 :                     s = sub( s, find_guarded_bits_fx( st_ivas->nchan_transport ) );
    1148       21390 :                     FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, st_ivas->nchan_transport ); ++i )
    1149             :                     {
    1150       18360 :                         Scale_sig32( p_output_fx[i], output_frame, s ); // Q(11+s)
    1151             :                     }
    1152        3030 :                     ivas_ls_setup_conversion_fx( st_ivas, st_ivas->nchan_transport, output_frame, p_output_fx, p_output_fx );
    1153       21390 :                     FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, st_ivas->nchan_transport ); ++i )
    1154             :                     {
    1155       18360 :                         Scale_sig32( p_output_fx[i], output_frame, negate( s ) ); // Q11
    1156             :                     }
    1157             :                 }
    1158           0 :                 ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    1159             :                 {
    1160           0 :                     ivas_mc2sba_fx( st_ivas->hIntSetup, p_output_fx, p_output_fx, output_frame, st_ivas->hOutSetup.ambisonics_order, 0 );
    1161             :                 }
    1162             :             }
    1163             :         }
    1164       19650 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    1165             :         {
    1166             :             /* LFE channel decoder */
    1167         780 :             ivas_lfe_dec_fx( st_ivas->hLFE, st, output_frame, st_ivas->bfi, p_output_fx[LFE_CHANNEL] );
    1168             : 
    1169         780 :             Scale_sig32( p_output_fx[LFE_CHANNEL], L_FRAME48k, Q11 - Q9 ); // Q11
    1170             : 
    1171         780 :             ivas_mc_paramupmix_dec_read_BS( st_ivas, st, st_ivas->hMCParamUpmix, &nb_bits_metadata[0] );
    1172             : 
    1173         780 :             IF( NE_32( ( error = ivas_mct_dec_fx( st_ivas, p_output_fx, output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1174             :             {
    1175           0 :                 return error;
    1176             :             }
    1177             :             /* HP filtering */
    1178        7020 :             FOR( n = 0; n < st_ivas->nchan_transport; n++ )
    1179             :             {
    1180        6240 :                 IF( NE_16( n, LFE_CHANNEL ) )
    1181             :                 {
    1182        5460 :                     hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
    1183             :                 }
    1184             :             }
    1185             : 
    1186             :             /* Rendering */
    1187         780 :             test();
    1188         780 :             test();
    1189         780 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) && ( EQ_32( output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_STEREO ) ) )
    1190             :             {
    1191             :                 /* Compensate loudness for not doing full upmix */
    1192          50 :                 FOR( n = 4; n < 8; n++ )
    1193             :                 {
    1194          40 :                     Scale_sig32( p_output_fx[n], output_frame, 1 );
    1195             :                 }
    1196             : 
    1197          10 :                 test();
    1198          10 :                 IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_STEREO ) )
    1199             :                 {
    1200          10 :                     s = Q16 - Q11;
    1201          10 :                     move16();
    1202             :                     Word16 nchan_transport;
    1203          10 :                     nchan_transport = audioCfg2channels( IVAS_AUDIO_CONFIG_5_1_2 );
    1204          10 :                     s = sub( s, find_guarded_bits_fx( nchan_transport ) );
    1205          90 :                     FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, nchan_transport ); ++i )
    1206             :                     {
    1207          80 :                         Scale_sig32( p_output_fx[i], output_frame, s );
    1208             :                     }
    1209          10 :                     ivas_ls_setup_conversion_fx( st_ivas, nchan_transport, output_frame, p_output_fx, p_output_fx );
    1210          90 :                     FOR( i = 0; i < s_max( st_ivas->hDecoderConfig->nchan_out, nchan_transport ); ++i )
    1211             :                     {
    1212          80 :                         Scale_sig32( p_output_fx[i], output_frame, negate( s ) );
    1213             :                     }
    1214             :                 }
    1215             :             }
    1216             :         }
    1217       18870 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    1218             :         {
    1219             :             /* read Parametric MC parameters from the bitstream */
    1220        8780 :             ivas_param_mc_dec_read_BS_fx( ivas_total_brate, st, st_ivas->hParamMC, &nb_bits_metadata[0] );
    1221             : 
    1222        8780 :             IF( EQ_16( st_ivas->nCPE, 1 ) )
    1223             :             {
    1224        8720 :                 Word16 q_output = 11;
    1225        8720 :                 move16();
    1226             : 
    1227        8720 :                 IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1228             :                 {
    1229           0 :                     return error;
    1230             :                 }
    1231        8720 :                 IF( NE_16( q_output, Q11 ) )
    1232             :                 {
    1233           0 :                     Scale_sig32( p_output_fx[0], output_frame, sub( Q11, q_output ) ); // Q11
    1234           0 :                     Scale_sig32( p_output_fx[1], output_frame, sub( Q11, q_output ) ); // Q11
    1235             :                 }
    1236             :             }
    1237          60 :             ELSE IF( GT_16( st_ivas->nCPE, 1 ) )
    1238             :             {
    1239          60 :                 IF( NE_32( ( error = ivas_mct_dec_fx( st_ivas, p_output_fx, output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1240             :                 {
    1241           0 :                     return error;
    1242             :                 }
    1243             :             }
    1244             : 
    1245             :             /* HP filtering */
    1246       26400 :             FOR( n = 0; n < st_ivas->nchan_transport; n++ )
    1247             :             {
    1248       17620 :                 hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
    1249             :             }
    1250             : 
    1251             :             /* Rendering */
    1252        8780 :             test();
    1253        8780 :             IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_STEREO ) )
    1254             :             {
    1255          20 :                 ivas_ls_setup_conversion_fx( st_ivas, st_ivas->nchan_transport, output_frame, p_output_fx, p_output_fx );
    1256             :             }
    1257             :         }
    1258       10090 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1259             :         {
    1260       10090 :             IF( st_ivas->hOutSetup.separateChannelEnabled )
    1261             :             {
    1262         550 :                 st = st_ivas->hCPE[0]->hCoreCoder[0]; /* Metadata is always with CPE in the case of separated channel */
    1263             :             }
    1264             : 
    1265             :             /* read McMASA parameters from the bitstream */
    1266             : 
    1267       10090 :             IF( NE_32( ( error = ivas_masa_decode_fx( st_ivas, st, &nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1268             :             {
    1269           0 :                 return error;
    1270             :             }
    1271             : 
    1272       10090 :             IF( st_ivas->hOutSetup.separateChannelEnabled )
    1273             :             {
    1274             :                 /* Decode the transport audio signals */
    1275         550 :                 Word16 q_output = 11;
    1276         550 :                 move16();
    1277             : 
    1278         550 :                 IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1279             :                 {
    1280           0 :                     return error;
    1281             :                 }
    1282         550 :                 IF( NE_16( q_output, Q11 ) )
    1283             :                 {
    1284           0 :                     Scale_sig32( p_output_fx[0], output_frame, sub( Q11, q_output ) ); // Q11
    1285           0 :                     Scale_sig32( p_output_fx[1], output_frame, sub( Q11, q_output ) ); // Q11
    1286             :                 }
    1287             : 
    1288             :                 /* Identify the index of the separated channel, always LFE_CHANNEL-1 here */
    1289         550 :                 n = LFE_CHANNEL - 1;
    1290         550 :                 move16();
    1291             : 
    1292             :                 /* Decode the separated channel to output[n] to be combined with the synthesized channels */
    1293             : 
    1294         550 :                 set32_fx( p_output_fx[n], 0, L_FRAME48k );
    1295             : 
    1296         550 :                 IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, 0, &p_output_fx[n], output_frame, 0 ) ), IVAS_ERR_OK ) )
    1297             :                 {
    1298           0 :                     return error;
    1299             :                 }
    1300         550 :                 test();
    1301         550 :                 test();
    1302         550 :                 test();
    1303         550 :                 test();
    1304         550 :                 test();
    1305         550 :                 test();
    1306         550 :                 test();
    1307             :                 /* Delay the separated channel to sync with CLDFB delay of the DirAC synthesis, and synthesize the LFE signal. */
    1308         550 :                 IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1 ) ||
    1309             :                     EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1_4 ) ||
    1310             :                     EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) || ( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.num_lfe > 0 ) )
    1311             :                 {
    1312         150 :                     ivas_lfe_synth_with_filters_fx( st_ivas->hMasa->hMasaLfeSynth, p_output_fx, output_frame, n, LFE_CHANNEL );
    1313             :                 }
    1314         400 :                 ELSE IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.num_lfe == 0 )
    1315             :                 {
    1316             :                     /* Delay the separated channel to sync with the DirAC rendering */
    1317           0 :                     delay_signal32_fx( p_output_fx[n], output_frame, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_fx, st_ivas->hMasa->hMasaLfeSynth->delayBuffer_syncDirAC_size );
    1318             :                 }
    1319             :             }
    1320             :             ELSE
    1321             :             {
    1322        9540 :                 Word16 q_output = 11;
    1323        9540 :                 move16();
    1324        9540 :                 IF( EQ_16( st_ivas->nSCE, 1 ) )
    1325             :                 {
    1326        9170 :                     set32_fx( p_output_fx[0], 0, L_FRAME48k );
    1327             : 
    1328        9170 :                     IF( NE_32( ( error = ivas_sce_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1329             :                     {
    1330           0 :                         return error;
    1331             :                     }
    1332             :                 }
    1333         370 :                 ELSE IF( EQ_16( st_ivas->nCPE, 1 ) )
    1334             :                 {
    1335             : 
    1336         370 :                     IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, 0, &p_output_fx[0], output_frame, nb_bits_metadata[0] ) ), IVAS_ERR_OK ) )
    1337             :                     {
    1338           0 :                         return error;
    1339             :                     }
    1340         370 :                     IF( NE_16( q_output, Q11 ) )
    1341             :                     {
    1342           0 :                         Scale_sig32( p_output_fx[0], output_frame, sub( Q11, q_output ) ); // Q11
    1343           0 :                         Scale_sig32( p_output_fx[1], output_frame, sub( Q11, q_output ) ); // Q11
    1344             :                     }
    1345             :                 }
    1346             :             }
    1347             : 
    1348       10090 :             IF( st_ivas->sba_dirac_stereo_flag != 0 ) /* use the flag to trigger the DFT upmix */
    1349             :             {
    1350             :                 Word16 q;
    1351        1180 :                 hCPE = st_ivas->hCPE[0];
    1352        1180 :                 hSCE = st_ivas->hSCE[0];
    1353        1180 :                 s = 0;
    1354        1180 :                 move16();
    1355        3540 :                 FOR( i = 0; i < 2; i++ )
    1356             :                 {
    1357        2360 :                     s = s_min( s, L_norm_arr( p_output_fx[i], L_FRAME48k ) - 11 /* Guard bits */ );
    1358             :                 }
    1359        3540 :                 FOR( i = 0; i < 2; i++ )
    1360             :                 {
    1361        2360 :                     Scale_sig32( p_output_fx[i], L_FRAME48k, s ); // Q(11+s)
    1362             :                 }
    1363             : 
    1364        1180 :                 hCPE->hStereoDft->q_dft = add( Q11, s );
    1365        1180 :                 move16();
    1366        1180 :                 IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
    1367             :                 {
    1368        1180 :                     hCPE->hStereoDft->q_smoothed_nrg = Q6; // hCPE->hStereoDft->q_dft;
    1369        1180 :                     hCPE->hStereoDft->q_ap_delay_mem_fx = hCPE->hStereoDft->q_dft;
    1370             : 
    1371        1180 :                     move16();
    1372        1180 :                     move16();
    1373             :                 }
    1374             : 
    1375        1180 :                 q = hCPE->hStereoDft->q_dft;
    1376        1180 :                 move16();
    1377             : 
    1378        1180 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) );
    1379        3540 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
    1380             :                 {
    1381        2360 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( hCPE->hStereoDft->q_dft, Q11 ) );                                          // q_dft
    1382        2360 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
    1383             :                 }
    1384             : 
    1385        1180 :                 IF( hCPE->hCoreCoder[0] != NULL )
    1386             :                 {
    1387        1180 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k, q - hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB ); // q
    1388        1180 :                     Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, L_FRAME48k, q - hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda );          // q
    1389        1180 :                     hCPE->hCoreCoder[0]->hHQ_core->q_old_outLB_fx = q;
    1390        1180 :                     move16();
    1391             :                 }
    1392        1180 :                 IF( hCPE->hStereoDft != NULL )
    1393             :                 {
    1394        1180 :                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA( 16000, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) );                           // q_dft
    1395        1180 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA( 16000, DELAY_BWE_TOTAL_NS ), sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // q_dft
    1396        1180 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = hCPE->hStereoDft->q_dft;
    1397        1180 :                     move16();
    1398             :                 }
    1399        1180 :                 IF( st_ivas->hSpar != NULL )
    1400             :                 {
    1401           0 :                     st_ivas->hSpar->hMdDec->Q_mixer_mat = 30;
    1402           0 :                     move16();
    1403             :                 }
    1404             : 
    1405        1180 :                 IF( hSCE != NULL )
    1406             :                 {
    1407        1180 :                     Scale_sig32( &hSCE->save_hb_synth_fx[0], extract_l( Mpy_32_32( hCPE->hCoreCoder[0]->output_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) ), hCPE->hStereoDft->q_dft - hSCE->q_save_hb_synth_fx ); // q_dft
    1408        1180 :                     hSCE->q_save_hb_synth_fx = hCPE->hStereoDft->q_dft;
    1409        1180 :                     move16();
    1410        1180 :                     Scale_sig32( &hSCE->save_synth_fx[0], extract_l( Mpy_32_32( hCPE->hCoreCoder[0]->output_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) ), hCPE->hStereoDft->q_dft - hSCE->q_save_synth_fx ); // q_dft
    1411        1180 :                     hSCE->q_save_synth_fx = hCPE->hStereoDft->q_dft;
    1412        1180 :                     move16();
    1413             :                 }
    1414        3540 :                 FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
    1415             :                 {
    1416        2360 :                     scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, hCPE->q_output_mem_fx[ii] ) ); // q_dft
    1417        2360 :                     hCPE->q_output_mem_fx[ii] = hCPE->hStereoDft->q_dft;
    1418        2360 :                     move16();
    1419             :                 }
    1420        3540 :                 FOR( i = 0; i < CPE_CHANNELS; i++ )
    1421        2360 :                 Scale_sig32( hCPE->prev_synth_fx[i], NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( hCPE->q_prev_synth_fx, Q11 ) ); // q_prev_synth_fx
    1422        1180 :                 ivas_sba_dirac_stereo_dec_fx( st_ivas, p_output_fx, output_frame, 1 );
    1423        3540 :                 FOR( i = 0; i < 2; i++ )
    1424             :                 {
    1425        2360 :                     Scale_sig32( p_output_fx[i], L_FRAME48k, negate( s ) );
    1426             :                 }
    1427        3540 :                 FOR( i = 0; i < CPE_CHANNELS; i++ )
    1428        2360 :                 Scale_sig32( hCPE->prev_synth_fx[i], NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->q_prev_synth_fx ) ); // Q11
    1429             : 
    1430        1180 :                 scale_sig32( hCPE->input_mem_BPF_fx[0], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) );
    1431        3540 :                 FOR( i = 0; i < CPE_CHANNELS; ++i )
    1432             :                 {
    1433        2360 :                     scale_sig32( hCPE->input_mem_LB_fx[i], STEREO_DFT32MS_OVL_16k, sub( Q11, hCPE->hStereoDft->q_dft ) );                                          // Q11
    1434        2360 :                     scale_sig32( hCPE->input_mem_fx[i], NS2SA_FX2( hCPE->hCoreCoder[0]->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
    1435             :                 }
    1436             : 
    1437        1180 :                 IF( hCPE->hCoreCoder[0] != NULL )
    1438             :                 {
    1439        1180 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx, L_FRAME32k, sub( hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda_LB, q ) ); // Q_old_wtda_LB
    1440        1180 :                     Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx, L_FRAME48k, sub( hCPE->hCoreCoder[0]->hHQ_core->Q_old_wtda, q ) );          // Q_old_wtda
    1441             :                 }
    1442        1180 :                 IF( hCPE->hStereoDft != NULL )
    1443             :                 {
    1444        1180 :                     scale_sig32( hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( 16000, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) );       // Q11
    1445        1180 :                     scale_sig32( hCPE->hStereoDft->ap_delay_mem_fx, NS2SA_FX2( 16000, DELAY_BWE_TOTAL_NS ), sub( Q11, hCPE->hStereoDft->q_ap_fade_mem_fx ) ); // Q11
    1446        1180 :                     hCPE->hStereoDft->q_ap_fade_mem_fx = Q11;
    1447        1180 :                     move16();
    1448             :                 }
    1449        1180 :                 IF( st_ivas->hSpar != NULL )
    1450             :                 {
    1451           0 :                     FOR( ii = 0; ii < CPE_CHANNELS; ii++ )
    1452             :                     {
    1453           0 :                         scale_sig32( hCPE->output_mem_fx[ii], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
    1454           0 :                         hCPE->q_output_mem_fx[ii] = Q11;
    1455           0 :                         move16();
    1456             :                     }
    1457             :                 }
    1458             :             }
    1459             : 
    1460             :             /* HP filtering */
    1461       22830 :             FOR( n = 0; n < getNumChanSynthesis( st_ivas ); n++ )
    1462             :             {
    1463       12740 :                 hp20_fx_32( p_output_fx[n], output_frame, st_ivas->mem_hp20_out_fx[n], output_Fs );
    1464             :             }
    1465             : 
    1466       10090 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_MCMASA_MONO_STEREO ) )
    1467             :             {
    1468         500 :                 ivas_mono_stereo_downmix_mcmasa_fx( st_ivas, p_output_fx, output_frame );
    1469             :             }
    1470             :         }
    1471             :     }
    1472             : 
    1473             :     /*----------------------------------------------------------------*
    1474             :      * Write IVAS transport channels
    1475             :      *----------------------------------------------------------------*/
    1476             : 
    1477      396879 :     IF( EQ_16( st_ivas->hDecoderConfig->Opt_tsm, 1 ) )
    1478             :     {
    1479       19405 :         ivas_syn_output_f_fx( p_output_fx, output_frame, st_ivas->hTcBuffer->nchan_transport_jbm, data_fx );
    1480             :     }
    1481             :     ELSE
    1482             :     {
    1483             :         /* directly copy to tc buffers */
    1484             :         /*note : the q of cldfb buffers (imag/real) are needed to be Q_p_output - 5 here 6 is taken for that*/
    1485      377474 :         ivas_jbm_dec_copy_tc_no_tsm_fx( st_ivas, p_output_fx, output_frame );
    1486             :     }
    1487             : 
    1488             :     /*----------------------------------------------------------------*
    1489             :      * Common updates
    1490             :      *----------------------------------------------------------------*/
    1491             : 
    1492      396879 :     IF( !st_ivas->bfi ) /* do not update if first frame(s) are lost or NO_DATA */
    1493             :     {
    1494      388314 :         st_ivas->hDecoderConfig->last_ivas_total_brate = ivas_total_brate;
    1495      388314 :         move32();
    1496      388314 :         IF( LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
    1497             :         {
    1498       18751 :             st_ivas->last_active_ivas_total_brate = st_ivas->last_active_ivas_total_brate;
    1499       18751 :             move32();
    1500             :         }
    1501             :         ELSE
    1502             :         {
    1503      369563 :             st_ivas->last_active_ivas_total_brate = ivas_total_brate;
    1504      369563 :             move32();
    1505             :         }
    1506             :     }
    1507             : 
    1508      396879 :     test();
    1509      396879 :     test();
    1510      396879 :     if ( LT_16( 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 */
    1511             :     {
    1512      111784 :         st_ivas->ini_frame = add( st_ivas->ini_frame, 1 );
    1513      111784 :         move16();
    1514             :     }
    1515             : 
    1516      396879 :     test();
    1517      396879 :     test();
    1518      396879 :     if ( LT_16( st_ivas->ini_active_frame, MAX_FRAME_COUNTER ) && !( st_ivas->bfi && st_ivas->ini_frame == 0 ) && GT_32( 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 */
    1519             :     {
    1520      110873 :         st_ivas->ini_active_frame = add( st_ivas->ini_active_frame, 1 );
    1521      110873 :         move16();
    1522             :     }
    1523             : 
    1524      396879 :     st_ivas->last_ivas_format = st_ivas->ivas_format;
    1525      396879 :     move32();
    1526      396879 :     pop_wmops();
    1527             : 
    1528      396879 :     return IVAS_ERR_OK;
    1529             : }
    1530             : 
    1531             : 
    1532             : /*--------------------------------------------------------------------------*
    1533             :  * ivas_jbm_dec_feed_tc_to_renderer()
    1534             :  *
    1535             :  * Feed decoded transport channels and metadata to the IVAS JBM renderer routine
    1536             :  *--------------------------------------------------------------------------*/
    1537      399979 : void ivas_jbm_dec_feed_tc_to_renderer_fx(
    1538             :     Decoder_Struct *st_ivas,           /* i/o: IVAS decoder structure                          */
    1539             :     const Word16 nSamplesForRendering, /* i  : number of TC samples available for rendering    */
    1540             :     Word16 *nSamplesResidual,          /* o  : number of samples not fitting into the renderer grid and buffer for the next call*/
    1541             :     Word32 *data_fx                    /* i  : transport channels                              Q11*/
    1542             : )
    1543             : {
    1544      399979 :     Word32 data_f_fx[MAX_CLDFB_DIGEST_CHANNELS][MAX_JBM_L_FRAME48k] = { 0 }; /* Word32 buffer for transport channels that will be directly converted with the CLDFB */
    1545             :     Word32 *p_data_f_fx[MAX_CLDFB_DIGEST_CHANNELS];
    1546             :     Word16 i;
    1547             : 
    1548     3599811 :     FOR( i = 0; i < MAX_CLDFB_DIGEST_CHANNELS; i++ )
    1549             :     {
    1550     3199832 :         set_zero_fx( data_f_fx[i], MAX_JBM_L_FRAME48k );
    1551             :     }
    1552      399979 :     if ( st_ivas->hTcBuffer )
    1553             :     {
    1554      399979 :         st_ivas->hTcBuffer->q_tc_fx = 11;
    1555      399979 :         move16();
    1556             :     }
    1557             :     Word16 n, n_render_timeslots;
    1558             : 
    1559      399979 :     push_wmops( "ivas_jbm_dec_feed_tc_to_rendererer" );
    1560     3599811 :     FOR( n = 0; n < MAX_CLDFB_DIGEST_CHANNELS; n++ )
    1561             :     {
    1562     3199832 :         p_data_f_fx[n] = &data_f_fx[n][0];
    1563             :     }
    1564             : 
    1565      399979 :     IF( st_ivas->hDecoderConfig->Opt_tsm )
    1566             :     {
    1567       19405 :         ivas_jbm_dec_copy_tc( st_ivas, nSamplesForRendering, nSamplesResidual, data_fx, p_data_f_fx, 11 );
    1568             :     }
    1569             :     ELSE
    1570             :     {
    1571      380574 :         *nSamplesResidual = 0;
    1572      380574 :         move16();
    1573             :     }
    1574      399979 :     n_render_timeslots = idiv1616( st_ivas->hTcBuffer->n_samples_available, st_ivas->hTcBuffer->n_samples_granularity );
    1575             : 
    1576      399979 :     test();
    1577      399979 :     IF( EQ_16( st_ivas->hTcBuffer->tc_buffer_mode, TC_BUFFER_MODE_BUFFER ) )
    1578             :     {
    1579      127492 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    1580             : 
    1581      127492 :         test();
    1582      127492 :         test();
    1583      127492 :         IF( ( EQ_16( st_ivas->ivas_format, MASA_FORMAT ) || EQ_16( st_ivas->ivas_format, MASA_ISM_FORMAT ) ) && EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    1584             :         {
    1585        5462 :             ivas_jbm_masa_sf_to_slot_map( st_ivas, n_render_timeslots );
    1586             :         }
    1587             :     }
    1588      272487 :     ELSE IF( EQ_16( st_ivas->ivas_format, STEREO_FORMAT ) )
    1589             :     {
    1590           0 :         ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    1591             :     }
    1592      272487 :     ELSE IF( EQ_16( st_ivas->ivas_format, ISM_FORMAT ) )
    1593             :     {
    1594             :         /* Rendering */
    1595       68224 :         IF( EQ_16( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1596             :         {
    1597       13208 :             test();
    1598       13208 :             test();
    1599       13208 :             test();
    1600       13208 :             IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_16( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    1601             :             {
    1602        7955 :                 ivas_dirac_dec_set_md_map_fx( st_ivas, n_render_timeslots );
    1603             : 
    1604        7955 :                 ivas_param_ism_params_to_masa_param_mapping_fx( st_ivas );
    1605             :             }
    1606        5253 :             ELSE IF( EQ_16( st_ivas->renderer_type, RENDERER_PARAM_ISM ) || EQ_16( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    1607             :             {
    1608             : 
    1609        5253 :                 ivas_param_ism_dec_digest_tc_fx( st_ivas, n_render_timeslots, p_data_f_fx, Q11 );
    1610             :             }
    1611             :         }
    1612             :         ELSE /* ISM_MODE_DISC */
    1613             :         {
    1614       55016 :             ivas_ism_dec_digest_tc_fx( st_ivas );
    1615             :         }
    1616             :     }
    1617      204263 :     ELSE IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( st_ivas->ivas_format, MASA_FORMAT ) )
    1618             :     {
    1619      137306 :         IF( st_ivas->hSCE[0] )
    1620             :         {
    1621       44077 :             Word16 shift = getScaleFactor32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
    1622       44077 :             IF( LT_16( sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
    1623             :             {
    1624        4043 :                 shift = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
    1625             :             }
    1626       44077 :             scale_sig32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31-cngNoiseLevelExp+shift)
    1627       44077 :             st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
    1628             :         }
    1629      137306 :         ivas_sba_dec_digest_tc_fx( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1630             :     }
    1631       66957 :     ELSE IF( EQ_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    1632             :     {
    1633             : 
    1634       26213 :         IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    1635             :         {
    1636       12100 :             ivas_ism_dec_digest_tc_fx( st_ivas );
    1637             : 
    1638       12100 :             test();
    1639       12100 :             test();
    1640       12100 :             test();
    1641       12100 :             test();
    1642             :             /* delay the objects here for all renderers where it is needed */
    1643       12100 :             IF(
    1644             :                 EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) ||
    1645             :                 EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ||
    1646             :                 EQ_16( st_ivas->renderer_type, RENDERER_OSBA_AMBI ) ||
    1647             :                 EQ_16( st_ivas->renderer_type, RENDERER_OSBA_LS ) ||
    1648             :                 EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    1649             :             {
    1650       47020 :                 FOR( n = 0; n < st_ivas->nchan_ism; n++ )
    1651             :                 {
    1652       36130 :                     delay_signal32_fx( st_ivas->hTcBuffer->tc_fx[n], st_ivas->hTcBuffer->n_samples_available, st_ivas->hSbaIsmData->delayBuffer_fx[n], st_ivas->hSbaIsmData->delayBuffer_size );
    1653             :                 }
    1654             :             }
    1655             : 
    1656       12100 :             IF( !st_ivas->sba_dirac_stereo_flag )
    1657             :             {
    1658       10890 :                 IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) )
    1659             :                 {
    1660             :                     Word16 temp, temp_e;
    1661        4680 :                     temp = BASOP_Util_Divide1616_Scale( st_ivas->hTcBuffer->n_samples_granularity, st_ivas->hSpatParamRendCom->slot_size, &temp_e );
    1662        4680 :                     n_render_timeslots = extract_l( L_shr( L_mult0( n_render_timeslots, temp ), sub( 15, temp_e ) ) );
    1663             :                 }
    1664             : 
    1665       10890 :                 IF( st_ivas->hSCE[0] )
    1666             :                 {
    1667           0 :                     Word16 shift = getScaleFactor32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
    1668           0 :                     if ( LT_16( sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
    1669             :                     {
    1670           0 :                         shift = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
    1671             :                     }
    1672           0 :                     scale_sig32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31-cngNoiseLevelExp+shift)
    1673           0 :                     st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
    1674             :                 }
    1675       10890 :                 ivas_sba_dec_digest_tc_fx( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1676             :             }
    1677             :         }
    1678             :         ELSE
    1679             :         {
    1680       14113 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    1681             : 
    1682       14113 :             IF( st_ivas->hSCE[0] )
    1683             :             {
    1684        5894 :                 Word16 shift = getScaleFactor32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
    1685        5894 :                 if ( LT_16( sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
    1686             :                 {
    1687          85 :                     shift = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
    1688             :                 }
    1689        5894 :                 scale_sig32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31-cngNoiseLevelExp+shift)
    1690        5894 :                 st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
    1691             :             }
    1692       14113 :             ivas_sba_dec_digest_tc_fx( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1693             :         }
    1694             :     }
    1695       40744 :     ELSE IF( EQ_16( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    1696             :     {
    1697        4840 :         test();
    1698        4840 :         IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    1699             :         {
    1700         108 :             n_render_timeslots = i_mult( n_render_timeslots, idiv1616( st_ivas->hTcBuffer->n_samples_granularity, st_ivas->hSpatParamRendCom->slot_size ) );
    1701             :         }
    1702             : 
    1703        4840 :         IF( st_ivas->hSCE[0] )
    1704             :         {
    1705        4840 :             Word16 shift = getScaleFactor32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
    1706        4840 :             if ( LT_16( sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
    1707             :             {
    1708         448 :                 shift = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
    1709             :             }
    1710        4840 :             scale_sig32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31- (st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp - shift)
    1711        4840 :             st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
    1712        4840 :             move16();
    1713             :         }
    1714        4840 :         ivas_sba_dec_digest_tc_fx( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1715             : 
    1716        4840 :         IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    1717             :         {
    1718        1783 :             ivas_ism_dec_digest_tc_fx( st_ivas );
    1719             :         }
    1720             :     }
    1721       35904 :     ELSE IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) )
    1722             :     {
    1723       35904 :         IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
    1724             :         {
    1725       18974 :             ivas_jbm_dec_td_renderers_adapt_subframes( st_ivas );
    1726             :         }
    1727       16930 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    1728             :         {
    1729         770 :             ivas_mc_paramupmix_dec_digest_tc( st_ivas, (UWord8) n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1730             :         }
    1731       16160 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    1732             :         {
    1733             : 
    1734        8760 :             Word16 nchan_transport = st_ivas->nchan_transport;
    1735        8760 :             move16();
    1736        8760 :             Word16 nchan_out_transport = add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe );
    1737             :             Word16 nchan_out_cov;
    1738        8760 :             test();
    1739        8760 :             test();
    1740        8760 :             IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
    1741             :             {
    1742        6910 :                 nchan_out_cov = add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe );
    1743             :             }
    1744        1850 :             ELSE IF( EQ_16( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_LS_CONV_CLDFB ) )
    1745             :             {
    1746         340 :                 nchan_out_cov = nchan_out_transport;
    1747         340 :                 move16();
    1748             :             }
    1749        1510 :             ELSE IF( EQ_16( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_LS_CONV_COV ) || EQ_16( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_MONO_STEREO ) )
    1750             :             {
    1751         150 :                 nchan_out_cov = add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe );
    1752             :             }
    1753             :             ELSE
    1754             :             {
    1755        1360 :                 nchan_out_cov = nchan_out_transport;
    1756        1360 :                 move16();
    1757             :             }
    1758             : 
    1759        8760 :             scale_sig32( st_ivas->hParamMC->proto_matrix_int_fx, st_ivas->hParamMC->proto_matrix_int_len, -1 ); // Q(31-1)
    1760        8760 :             st_ivas->hParamMC->proto_matrix_int_e = 1;
    1761        8760 :             move16();
    1762             : 
    1763        8760 :             ivas_param_mc_dec_digest_tc_fx( st_ivas, (UWord8) n_render_timeslots, (Word32 **) p_data_f_fx, Q11 );
    1764             : 
    1765        8760 :             scale_sig32( st_ivas->hParamMC->proto_matrix_int_fx, st_ivas->hParamMC->proto_matrix_int_len, 1 ); // Q(31-1+1)
    1766             : 
    1767             :             Word16 shift;
    1768      121880 :             FOR( Word16 param_band_idx = 0; param_band_idx < st_ivas->hParamMC->num_param_bands_synth; param_band_idx++ )
    1769             :             {
    1770      113120 :                 shift = getScaleFactor32( st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_fx[param_band_idx], s_min( st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_len, nchan_transport * nchan_transport ) );
    1771      113120 :                 scale_sig32( st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_fx[param_band_idx], s_min( st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_len, i_mult( nchan_transport, nchan_transport ) ), shift ); // Q(31-cx_old_e+shift)
    1772      113120 :                 st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_e[param_band_idx] = sub( st_ivas->hParamMC->h_output_synthesis_cov_state.cx_old_e[param_band_idx], shift );
    1773      113120 :                 move16();
    1774             : 
    1775      113120 :                 shift = getScaleFactor32( st_ivas->hParamMC->h_output_synthesis_cov_state.cy_old_fx[param_band_idx], nchan_out_cov * nchan_out_cov );
    1776      113120 :                 scale_sig32( st_ivas->hParamMC->h_output_synthesis_cov_state.cy_old_fx[param_band_idx], i_mult( nchan_out_cov, nchan_out_cov ), shift ); // Q(31-cy_old_e+shift)
    1777      113120 :                 st_ivas->hParamMC->h_output_synthesis_cov_state.cy_old_e[param_band_idx] = sub( st_ivas->hParamMC->h_output_synthesis_cov_state.cy_old_e[param_band_idx], shift );
    1778      113120 :                 move16();
    1779             :             }
    1780             :         }
    1781        7400 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    1782             :         {
    1783        7400 :             IF( st_ivas->hSCE[0] )
    1784             :             {
    1785        7190 :                 Word16 shift = getScaleFactor32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN );
    1786        7190 :                 if ( LT_16( sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift ), 4 ) )
    1787             :                 {
    1788         975 :                     shift = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, 4 );
    1789             :                 }
    1790        7190 :                 scale_sig32( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevel, FFTCLDFBLEN, shift ); // Q(31-cngNoiseLevelExp+shift)
    1791        7190 :                 st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp = sub( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->cngNoiseLevelExp, shift );
    1792        7190 :                 move16();
    1793             :             }
    1794        7400 :             ivas_sba_dec_digest_tc_fx( st_ivas, n_render_timeslots, st_ivas->hTcBuffer->n_samples_available );
    1795             :         }
    1796             :     }
    1797             : 
    1798      399979 :     pop_wmops();
    1799      399979 :     return;
    1800             : }
    1801             : 
    1802             : /*--------------------------------------------------------------------------*
    1803             :  * ivas_dec_render()
    1804             :  *
    1805             :  * Principal IVAS JBM rendering routine
    1806             :  *--------------------------------------------------------------------------*/
    1807      411969 : ivas_error ivas_jbm_dec_render_fx(
    1808             :     Decoder_Struct *st_ivas,        /* i/o: IVAS decoder structure                                      */
    1809             :     const UWord16 nSamplesAsked,    /* i  : number of samples wanted                                    */
    1810             :     UWord16 *nSamplesRendered,      /* o  : number of samples rendered                                  */
    1811             :     UWord16 *nSamplesAvailableNext, /* o  : number of samples still available in the rendering pipeline */
    1812             :     Word16 *data                    /* o  : output synthesis signal                                      Q0*/
    1813             : )
    1814             : {
    1815             :     Word16 n, nchan_out;
    1816             :     Word16 nchan_transport;
    1817             :     Word16 nchan_remapped;
    1818             :     Word32 output_Fs;
    1819             :     AUDIO_CONFIG output_config;
    1820             :     Word16 nSamplesAskedLocal;
    1821             :     ivas_error error;
    1822             :     Word32 *p_output_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
    1823             :     Word32 *p_tc_fx[MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS];
    1824             :     Word16 subframe_len, gd_bits, exp, nchan_in, i, j, ch;
    1825      411969 :     const Word16 output_q_factor = Q11;
    1826      411969 :     move16();
    1827             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    1828             : 
    1829      411969 :     push_wmops( "ivas_dec_render" );
    1830             :     /*----------------------------------------------------------------*
    1831             :      * Initialization of local vars after struct has been set
    1832             :      *----------------------------------------------------------------*/
    1833             : 
    1834      411969 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    1835      411969 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1836      411969 :     move32();
    1837      411969 :     nchan_out = st_ivas->hDecoderConfig->nchan_out;
    1838      411969 :     move16();
    1839      411969 :     nchan_transport = st_ivas->hTcBuffer->nchan_transport_jbm;
    1840      411969 :     move16();
    1841      411969 :     output_config = st_ivas->hDecoderConfig->output_config;
    1842      411969 :     move32();
    1843      411969 :     nSamplesAskedLocal = add( nSamplesAsked, st_ivas->hTcBuffer->n_samples_discard );
    1844      411969 :     move16();
    1845             : 
    1846     8651349 :     FOR( n = 0; n < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1847             :     {
    1848     8239380 :         p_output_fx[n] = st_ivas->p_output_fx[n];
    1849             :     }
    1850      411969 :     IF( !st_ivas->hDecoderConfig->Opt_tsm )
    1851             :     {
    1852     1666768 :         FOR( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
    1853             :         {
    1854     1286194 :             p_tc_fx[n] = p_output_fx[n];
    1855             :         }
    1856             : 
    1857     6469758 :         FOR( n = 0; n < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; n++ )
    1858             :         {
    1859     6089184 :             st_ivas->hTcBuffer->tc_fx[n] = st_ivas->p_output_fx[n];
    1860             :         }
    1861             :     }
    1862             :     ELSE
    1863             :     {
    1864      127805 :         FOR( n = 0; n < st_ivas->hTcBuffer->nchan_buffer_full; n++ )
    1865             :         {
    1866       96410 :             p_tc_fx[n] = &st_ivas->hTcBuffer->tc_fx[n][st_ivas->hTcBuffer->n_samples_rendered];
    1867             :         }
    1868             :     }
    1869             : 
    1870      411969 :     st_ivas->hTcBuffer->no_channels = st_ivas->hTcBuffer->nchan_buffer_full;
    1871      411969 :     move16();
    1872             :     /*----------------------------------------------------------------*
    1873             :      * Update combined orientation access index
    1874             :      *----------------------------------------------------------------*/
    1875             : 
    1876      411969 :     IF( st_ivas->hCombinedOrientationData != NULL )
    1877             :     {
    1878             :         /* take the discard samples into account here to make sure head rotation stays on the correct 5ms grid */
    1879       69350 :         st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start = sub( st_ivas->hCombinedOrientationData->cur_subframe_samples_rendered_start, st_ivas->hTcBuffer->n_samples_discard );
    1880       69350 :         move16();
    1881             : 
    1882       69350 :         ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
    1883             :     }
    1884             : 
    1885             :     /*----------------------------------------------------------------*
    1886             :      * Rendering
    1887             :      *----------------------------------------------------------------*/
    1888      411969 :     test();
    1889      411969 :     IF( EQ_32( st_ivas->ivas_format, UNDEFINED_FORMAT ) )
    1890             :     {
    1891           0 :         assert( 0 );
    1892             :     }
    1893      411969 :     ELSE IF( EQ_32( st_ivas->hTcBuffer->tc_buffer_mode, TC_BUFFER_MODE_BUFFER ) )
    1894             :     {
    1895             :         Word16 slot_size, tmp, e;
    1896             : 
    1897      128722 :         slot_size = st_ivas->hTcBuffer->n_samples_granularity;
    1898      128722 :         move16();
    1899             : 
    1900             :         /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    1901      128722 :         tmp = BASOP_Util_Divide1616_Scale( nSamplesAsked, slot_size, &e );
    1902      128722 :         tmp = shr( tmp, sub( 15, e ) );
    1903             : 
    1904      128722 :         ivas_jbm_dec_tc_buffer_playout_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, p_output_fx );
    1905             :     }
    1906      283247 :     ELSE IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) )
    1907             :     {
    1908             :         /* Rendering */
    1909           0 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) )
    1910             :         {
    1911           0 :             *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1912           0 :             move16();
    1913             : 
    1914           0 :             ivas_ls_setup_conversion_fx( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc_fx, p_output_fx );
    1915             :         }
    1916             :     }
    1917      283247 :     ELSE IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
    1918             :     {
    1919             :         /* Rendering */
    1920       70909 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
    1921             :         {
    1922       15232 :             test();
    1923       15232 :             test();
    1924       15232 :             test();
    1925       15232 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    1926             :             {
    1927        8673 :                 ivas_dirac_dec_binaural_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, st_ivas->nchan_transport, p_output_fx );
    1928             :             }
    1929        6559 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) )
    1930             :             {
    1931           0 :                 *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1932           0 :                 move16();
    1933           0 :                 ivas_apply_non_diegetic_panning_fx( p_output_fx, st_ivas->hDecoderConfig->non_diegetic_pan_gain_fx, *nSamplesRendered );
    1934             :             }
    1935        6559 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_PARAM_ISM ) || EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    1936             :             {
    1937        6559 :                 ivas_param_ism_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx );
    1938             : 
    1939        6559 :                 IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    1940             :                 {
    1941             :                     /* Convert CICP19 -> Ambisonics */
    1942        4516 :                     ivas_mc2sba_fx( st_ivas->hIntSetup, p_output_fx, p_output_fx, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0 );
    1943             :                 }
    1944             :             }
    1945             :         }
    1946             :         ELSE /* ISM_MODE_DISC */
    1947             :         {
    1948       55677 :             *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    1949       55677 :             move16();
    1950             : 
    1951       55677 :             test();
    1952       55677 :             test();
    1953             :             /* Loudspeaker or Ambisonics rendering */
    1954       55677 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_TD_PANNING ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    1955             :             {
    1956             :                 /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
    1957       20811 :                 ivas_ism_render_sf_fx( st_ivas, p_output_fx, *nSamplesRendered );
    1958             :             }
    1959       34866 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) )
    1960             :             {
    1961        1500 :                 ivas_apply_non_diegetic_panning_fx( p_output_fx, st_ivas->hDecoderConfig->non_diegetic_pan_gain_fx, *nSamplesRendered );
    1962             :             }
    1963       33366 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) )
    1964             :             {
    1965             :                 /* Convert to Ambisonics; used also for ISM->HOA3->binaural rendering */
    1966       20780 :                 FOR( i = 0; i < st_ivas->nchan_transport; i++ )
    1967             :                 {
    1968      270028 :                     FOR( j = 0; j < 16; j++ )
    1969             :                     {
    1970      254144 :                         st_ivas->hIsmRendererData->gains_fx[i][j] = L_shr( st_ivas->hIsmRendererData->gains_fx[i][j], 1 ); // Q30 -> Q29
    1971      254144 :                         move32();
    1972      254144 :                         st_ivas->hIsmRendererData->prev_gains_fx[i][j] = L_shr( st_ivas->hIsmRendererData->prev_gains_fx[i][j], 1 ); // Q30 -> Q29
    1973      254144 :                         move32();
    1974             :                     }
    1975             :                 }
    1976        4896 :                 ivas_ism2sba_sf_fx( st_ivas->hTcBuffer->tc_fx, p_output_fx, st_ivas->hIsmRendererData, st_ivas->nchan_transport, *nSamplesRendered, st_ivas->hTcBuffer->n_samples_rendered, st_ivas->hIntSetup.ambisonics_order );
    1977        4896 :                 Word16 sba_num_chans = imult1616( add( st_ivas->hIntSetup.ambisonics_order, 1 ), add( st_ivas->hIntSetup.ambisonics_order, 1 ) );
    1978       65032 :                 FOR( j = 0; j < sba_num_chans; j++ )
    1979             :                 {
    1980       60136 :                     scale_sig32( p_output_fx[j], *nSamplesRendered, sub( Q11, sub( add( output_q_factor, 29 ), 31 ) ) ); // Q11
    1981             :                 }
    1982             : 
    1983             : 
    1984       20780 :                 FOR( i = 0; i < st_ivas->nchan_transport; i++ )
    1985             :                 {
    1986      270028 :                     FOR( j = 0; j < 16; j++ )
    1987             :                     {
    1988      254144 :                         st_ivas->hIsmRendererData->gains_fx[i][j] = L_shl( st_ivas->hIsmRendererData->gains_fx[i][j], 1 ); // Q29 -> Q30
    1989      254144 :                         move32();
    1990      254144 :                         st_ivas->hIsmRendererData->prev_gains_fx[i][j] = L_shl( st_ivas->hIsmRendererData->prev_gains_fx[i][j], 1 ); // Q29 -> Q30
    1991      254144 :                         move32();
    1992             :                     }
    1993             :                 }
    1994             :             }
    1995             : 
    1996             :             /* Binaural rendering */
    1997       55677 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
    1998             :             {
    1999       28470 :                 IF( NE_32( ( error = ivas_td_binaural_renderer_sf_fx( st_ivas, p_output_fx, *nSamplesRendered ) ), IVAS_ERR_OK ) )
    2000             :                 {
    2001           0 :                     return error;
    2002             :                 }
    2003             :             }
    2004       27207 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2005             :             {
    2006       10711 :                 st_ivas->hCrendWrapper->p_io_qfactor = &st_ivas->hCrendWrapper->io_qfactor;
    2007       10711 :                 subframe_len = imult1616( st_ivas->hTcBuffer->subframe_nbslots[0], st_ivas->hTcBuffer->n_samples_granularity );
    2008       10711 :                 gd_bits = find_guarded_bits_fx( subframe_len );
    2009       10711 :                 exp = 13;
    2010       10711 :                 move16();
    2011       10711 :                 nchan_in = 12;
    2012       10711 :                 move16();
    2013       10711 :                 nchan_out = 2;
    2014       10711 :                 move16();
    2015       10711 :                 exp = sub( exp, gd_bits );
    2016       10711 :                 *st_ivas->hCrendWrapper->p_io_qfactor = exp;
    2017       10711 :                 move16();
    2018      139243 :                 FOR( i = 0; i < nchan_in; i++ )
    2019             :                 {
    2020      128532 :                     scale_sig32( p_output_fx[i], *nSamplesRendered, negate( sub( 11, *st_ivas->hCrendWrapper->p_io_qfactor ) ) ); // Q11
    2021             :                 }
    2022       10711 :                 IF( NE_32( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL, NULL, NULL, st_ivas->hTcBuffer, p_output_fx, p_output_fx, *nSamplesRendered, output_Fs ) ), IVAS_ERR_OK ) )
    2023             :                 {
    2024           0 :                     return error;
    2025             :                 }
    2026       32133 :                 FOR( i = 0; i < nchan_out; i++ )
    2027             :                 {
    2028       21422 :                     scale_sig32( p_output_fx[i], *nSamplesRendered, sub( 11, *st_ivas->hCrendWrapper->p_io_qfactor ) ); // Q11
    2029             :                 }
    2030             :             }
    2031             :         }
    2032             :     }
    2033      212338 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
    2034             :     {
    2035      143059 :         nchan_remapped = nchan_transport;
    2036      143059 :         move16();
    2037      143059 :         test();
    2038      143059 :         test();
    2039             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    2040      143059 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    2041             :         {
    2042       45226 :             ivas_dirac_dec_binaural_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx );
    2043             :         }
    2044       97833 :         ELSE IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) )
    2045             :         {
    2046       15961 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_DEC ) )
    2047             :             {
    2048           0 :                 *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    2049           0 :                 move16();
    2050           0 :                 FOR( n = 0; n < nchan_remapped; n++ )
    2051             :                 {
    2052           0 :                     Copy32( st_ivas->hTcBuffer->tc_fx[n] + st_ivas->hTcBuffer->n_samples_rendered, p_output_fx[n], *nSamplesRendered );
    2053             :                 }
    2054           0 :                 IF( NE_32( ( error = ivas_sba_linear_renderer_fx( p_output_fx, *nSamplesRendered, nchan_remapped, 0, output_config, st_ivas->hOutSetup ) ), IVAS_ERR_OK ) )
    2055             :                 {
    2056           0 :                     return error;
    2057             :                 }
    2058             :             }
    2059       15961 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) )
    2060             :             {
    2061       15961 :                 ivas_dirac_dec_render_fx( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx );
    2062             :             }
    2063             :         }
    2064             :         ELSE
    2065             :         {
    2066       81872 :             IF( NE_32( ( error = ivas_sba_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2067             :             {
    2068           0 :                 return error;
    2069             :             }
    2070             :         }
    2071             :     }
    2072       69279 :     ELSE IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    2073             :     {
    2074        5401 :         nchan_remapped = st_ivas->nchan_transport;
    2075        5401 :         move16();
    2076        5401 :         test();
    2077        5401 :         test();
    2078        5401 :         IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    2079             :         {
    2080        2696 :             test();
    2081        2696 :             IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) && EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) )
    2082             :             {
    2083         122 :                 IF( NE_32( ( error = ivas_omasa_dirac_td_binaural_jbm_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx ) ), IVAS_ERR_OK ) )
    2084             :                 {
    2085           0 :                     return error;
    2086             :                 }
    2087             :             }
    2088             :             ELSE
    2089             :             {
    2090        2574 :                 ivas_dirac_dec_binaural_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx );
    2091             :             }
    2092             :         }
    2093        2705 :         ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) )
    2094             :         {
    2095        2705 :             ivas_omasa_dirac_rend_jbm_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx );
    2096             :         }
    2097             :     }
    2098       63878 :     ELSE IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    2099             :     {
    2100       27035 :         nchan_remapped = nchan_transport;
    2101       27035 :         move16();
    2102             :         /* Loudspeakers, Ambisonics or Binaural rendering */
    2103       27035 :         IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    2104             :         {
    2105       12100 :             test();
    2106       12100 :             test();
    2107       12100 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) )
    2108             :             {
    2109             :                 SPAR_DEC_HANDLE hSpar;
    2110        4680 :                 hSpar = st_ivas->hSpar;
    2111        4680 :                 hSpar->hMdDec->Q_mixer_mat = 30;
    2112        4680 :                 move16();
    2113             : 
    2114        4680 :                 IF( NE_32( ( error = ivas_osba_dirac_td_binaural_jbm_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2115             :                 {
    2116           0 :                     return error;
    2117             :                 }
    2118             :             }
    2119        7420 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_OSBA_STEREO ) )
    2120             :             {
    2121        1210 :                 *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    2122        1210 :                 move16();
    2123             : 
    2124        1210 :                 test();
    2125             :                 /* shift SBA channels to avoid overwrite by ISM upmix in 1 object case and non-TSM unified channel memory*/
    2126        1210 :                 IF( EQ_16( st_ivas->nchan_ism, 1 ) && EQ_16( st_ivas->hDecoderConfig->Opt_tsm, 0 ) )
    2127             :                 {
    2128           0 :                     Copy32( p_tc_fx[2], p_output_fx[3], *nSamplesRendered );
    2129           0 :                     Copy32( p_tc_fx[1], p_output_fx[2], *nSamplesRendered );
    2130           0 :                     p_tc_fx[1] = p_output_fx[2];
    2131           0 :                     p_tc_fx[2] = p_output_fx[3];
    2132             :                 }
    2133             : 
    2134             :                 /* render objects */
    2135        1210 :                 ivas_ism_render_sf_fx( st_ivas, p_output_fx, *nSamplesRendered );
    2136             : 
    2137             :                 /* add already rendered SBA part */
    2138        3630 :                 FOR( n = 0; n < nchan_out; n++ )
    2139             :                 {
    2140             : #ifdef VEC_ARITH_OPT_v1
    2141        2420 :                     v_add_fixed_no_hdrm( p_output_fx[n], p_tc_fx[n + st_ivas->nchan_ism], p_output_fx[n], *nSamplesRendered );
    2142             : #else  /* VEC_ARITH_OPT_v1 */
    2143             :                     v_add_fixed( p_output_fx[n], p_tc_fx[n + st_ivas->nchan_ism], p_output_fx[n], *nSamplesRendered, 0 );
    2144             : #endif /* VEC_ARITH_OPT_v1 */
    2145             :                 }
    2146             :             }
    2147        6210 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_OSBA_AMBI ) || EQ_32( st_ivas->renderer_type, RENDERER_OSBA_LS ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
    2148             :             {
    2149        4000 :                 IF( NE_32( ( error = ivas_osba_render_sf_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2150             :                 {
    2151           0 :                     return error;
    2152             :                 }
    2153             :             }
    2154        2210 :             ELSE IF( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) ) /*EXT output = individual objects + HOA3*/
    2155             :             {
    2156        2210 :                 IF( NE_32( ( error = ivas_sba_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, &p_output_fx[st_ivas->nchan_ism] ) ), IVAS_ERR_OK ) )
    2157             :                 {
    2158           0 :                     return error;
    2159             :                 }
    2160             : 
    2161        8050 :                 FOR( n = 0; n < st_ivas->nchan_ism; n++ )
    2162             :                 {
    2163        5840 :                     Copy32( st_ivas->hTcBuffer->tc_fx[n] + st_ivas->hTcBuffer->n_samples_rendered, p_output_fx[n], *nSamplesRendered );
    2164             :                 }
    2165             :             }
    2166             :             ELSE
    2167             :             {
    2168           0 :                 IF( NE_32( ( error = ivas_sba_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2169             :                 {
    2170           0 :                     return error;
    2171             :                 }
    2172             :             }
    2173             :         }
    2174       14935 :         ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
    2175             :         {
    2176        7655 :             ivas_dirac_dec_binaural_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx );
    2177             :         }
    2178             :         ELSE
    2179             :         {
    2180        7280 :             IF( NE_32( ( error = ivas_sba_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2181             :             {
    2182           0 :                 return error;
    2183             :             }
    2184             : 
    2185        7280 :             IF( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    2186             :             {
    2187       13430 :                 FOR( n = st_ivas->hIntSetup.nchan_out_woLFE - 1; n >= 0; n-- )
    2188             :                 {
    2189       12640 :                     Copy32( p_output_fx[n], p_output_fx[n + st_ivas->nchan_ism], *nSamplesRendered );
    2190             :                 }
    2191        3950 :                 FOR( n = 0; n < st_ivas->nchan_ism; n++ )
    2192             :                 {
    2193        3160 :                     set32_fx( p_output_fx[n], 0, *nSamplesRendered );
    2194             :                 }
    2195             :             }
    2196             :         }
    2197             :     }
    2198       36843 :     ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) )
    2199             :     {
    2200       36843 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
    2201             :         {
    2202       19913 :             Word16 crendInPlaceRotation = FALSE;
    2203       19913 :             move16();
    2204       19913 :             *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    2205       19913 :             move16();
    2206             : 
    2207       19913 :             test();
    2208       19913 :             test();
    2209       19913 :             test();
    2210       19913 :             IF( NE_32( st_ivas->transport_config, st_ivas->intern_config ) && ( EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_FOA ) || EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA2 ) || EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
    2211             :             {
    2212         150 :                 IF( LT_16( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ), add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe ) ) )
    2213             :                 {
    2214         150 :                     crendInPlaceRotation = TRUE;
    2215         150 :                     move16();
    2216             : 
    2217         150 :                     ivas_mc2sba_fx( st_ivas->hTransSetup, p_tc_fx, p_output_fx, *nSamplesRendered, st_ivas->hIntSetup.ambisonics_order, GAIN_LFE_FX );
    2218             :                 }
    2219             :             }
    2220             : 
    2221       19913 :             test();
    2222             :             /* Rendering */
    2223       19913 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2224             :             {
    2225       11960 :                 st_ivas->hCrendWrapper->p_io_qfactor = &st_ivas->hCrendWrapper->io_qfactor;
    2226       11960 :                 subframe_len = imult1616( st_ivas->hTcBuffer->subframe_nbslots[0], st_ivas->hTcBuffer->n_samples_granularity );
    2227       11960 :                 gd_bits = find_guarded_bits_fx( subframe_len );
    2228       11960 :                 exp = 13;
    2229       11960 :                 move16();
    2230       11960 :                 IF( NE_32( ( error = getAudioConfigNumChannels( st_ivas->intern_config, &nchan_in ) ), IVAS_ERR_OK ) )
    2231             :                 {
    2232           0 :                     return error;
    2233             :                 }
    2234       11960 :                 IF( NE_32( ( error = getAudioConfigNumChannels( st_ivas->hOutSetup.output_config, &nchan_out ) ), IVAS_ERR_OK ) )
    2235             :                 {
    2236           0 :                     return error;
    2237             :                 }
    2238       11960 :                 exp = sub( exp, gd_bits );
    2239       11960 :                 *st_ivas->hCrendWrapper->p_io_qfactor = exp;
    2240       11960 :                 move16();
    2241             : 
    2242       94520 :                 FOR( i = 0; i < nchan_in; i++ )
    2243             :                 {
    2244       82560 :                     scale_sig32( p_output_fx[i], *nSamplesRendered, negate( sub( Q11, *st_ivas->hCrendWrapper->p_io_qfactor ) ) ); // Q11
    2245       82560 :                     IF( st_ivas->hDecoderConfig->Opt_tsm )
    2246             :                     {
    2247           0 :                         scale_sig32( p_tc_fx[i], *nSamplesRendered, negate( sub( Q11, *st_ivas->hCrendWrapper->p_io_qfactor ) ) ); // Q11
    2248             :                     }
    2249             :                 }
    2250       11960 :                 IF( NE_32( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, st_ivas->intern_config, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    2251             :                                                                      &st_ivas->hIntSetup, st_ivas->hEFAPdata, st_ivas->hTcBuffer, crendInPlaceRotation ? p_output_fx : p_tc_fx, p_output_fx, *nSamplesRendered, output_Fs ) ),
    2252             :                            IVAS_ERR_OK ) )
    2253             :                 {
    2254           0 :                     return error;
    2255             :                 }
    2256             : 
    2257       11960 :                 ivas_binaural_add_LFE_fx( st_ivas, *nSamplesRendered, p_tc_fx, p_output_fx );
    2258             : 
    2259       94520 :                 FOR( i = 0; i < nchan_in; i++ )
    2260             :                 {
    2261       82560 :                     scale_sig32( p_output_fx[i], *nSamplesRendered, sub( Q11, *st_ivas->hCrendWrapper->p_io_qfactor ) ); // Q11
    2262       82560 :                     IF( st_ivas->hDecoderConfig->Opt_tsm )
    2263             :                     {
    2264           0 :                         scale_sig32( p_tc_fx[i], *nSamplesRendered, sub( Q11, *st_ivas->hCrendWrapper->p_io_qfactor ) ); // Q11
    2265             :                     }
    2266             :                 }
    2267             :             }
    2268        7953 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) )
    2269             :             {
    2270        4403 :                 *nSamplesRendered = s_min( st_ivas->hTcBuffer->n_samples_available, nSamplesAskedLocal );
    2271        4403 :                 move16();
    2272             : 
    2273        4403 :                 ivas_ls_setup_conversion_fx( st_ivas, st_ivas->nchan_transport, *nSamplesRendered, p_tc_fx, p_output_fx );
    2274             :             }
    2275        3550 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    2276             :             {
    2277         490 :                 ivas_mc2sba_fx( st_ivas->hIntSetup, p_tc_fx, p_output_fx, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0 );
    2278             :             }
    2279        3060 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
    2280             :             {
    2281        3060 :                 IF( NE_32( ( error = ivas_td_binaural_renderer_sf_fx( st_ivas, p_output_fx, *nSamplesRendered ) ), IVAS_ERR_OK ) )
    2282             :                 {
    2283           0 :                     return error;
    2284             :                 }
    2285             : 
    2286        3060 :                 ivas_binaural_add_LFE_fx( st_ivas, *nSamplesRendered, p_tc_fx, p_output_fx );
    2287             :             }
    2288             :         }
    2289       16930 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    2290             :         {
    2291         770 :             ivas_mc_paramupmix_dec_render( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_tc_fx, p_output_fx );
    2292             : 
    2293         770 :             test();
    2294         770 :             test();
    2295             :             /* Rendering */
    2296         770 :             IF( ( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ) && !st_ivas->hDecoderConfig->Opt_Headrotation )
    2297             :             {
    2298         160 :                 ivas_binaural_add_LFE_fx( st_ivas, *nSamplesRendered, p_output_fx, p_output_fx );
    2299             :             }
    2300         610 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_MC ) )
    2301             :             {
    2302           0 :                 ivas_ls_setup_conversion_fx( st_ivas, MC_PARAMUPMIX_MAX_INPUT_CHANS, *nSamplesRendered, p_output_fx, p_output_fx );
    2303             :             }
    2304         610 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    2305             :             {
    2306          10 :                 ivas_mc2sba_fx( st_ivas->hIntSetup, p_output_fx, p_output_fx, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0 );
    2307             :             }
    2308         600 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
    2309             :             {
    2310           0 :                 IF( NE_32( ( error = ivas_td_binaural_renderer_fx( st_ivas, p_output_fx, *nSamplesRendered ) ), IVAS_ERR_OK ) )
    2311             :                 {
    2312           0 :                     return error;
    2313             :                 }
    2314             : 
    2315           0 :                 ivas_binaural_add_LFE_fx( st_ivas, *nSamplesRendered, p_output_fx, p_output_fx );
    2316             :             }
    2317             :         }
    2318       16160 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
    2319             :         {
    2320             :             Word16 channel_active_fx[MAX_OUTPUT_CHANNELS];
    2321             :             Word16 nchan_out_cov;
    2322        8760 :             Word16 nchan_out_cldfb = 0;
    2323        8760 :             move16();
    2324             : 
    2325        8760 :             set16_fx( channel_active_fx, 0, MAX_CICP_CHANNELS );
    2326        8760 :             Word16 nchan_transport_tmp = st_ivas->nchan_transport;
    2327        8760 :             move16();
    2328        8760 :             output_Fs = st_ivas->hDecoderConfig->output_Fs;
    2329        8760 :             move32();
    2330        8760 :             Word16 nchan_out_transport = add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe );
    2331        8760 :             test();
    2332        8760 :             test();
    2333        8760 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
    2334             :             {
    2335        6910 :                 nchan_out_cldfb = BINAURAL_CHANNELS;
    2336        6910 :                 move16();
    2337        6910 :                 set16_fx( channel_active_fx, 1, BINAURAL_CHANNELS );
    2338        6910 :                 nchan_out_cov = add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe );
    2339             :             }
    2340        1850 :             ELSE IF( EQ_32( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_LS_CONV_CLDFB ) )
    2341             :             {
    2342         340 :                 nchan_out_cov = nchan_out_transport;
    2343         340 :                 move16();
    2344         340 :                 nchan_out_cldfb = add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe );
    2345             :             }
    2346        1510 :             ELSE IF( EQ_32( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_LS_CONV_COV ) || EQ_32( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_MONO_STEREO ) )
    2347             :             {
    2348         150 :                 nchan_out_cov = add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe );
    2349         150 :                 nchan_out_cldfb = nchan_out_cov;
    2350         150 :                 move16();
    2351         150 :                 set16_fx( channel_active_fx, 1, nchan_out_cov );
    2352             :             }
    2353             :             ELSE
    2354             :             {
    2355        1360 :                 nchan_out_cov = nchan_out_transport;
    2356        1360 :                 move16();
    2357        1360 :                 nchan_out_cldfb = nchan_out_transport;
    2358        1360 :                 move16();
    2359        1360 :                 set16_fx( channel_active_fx, 1, nchan_out_cov );
    2360             :             }
    2361        8760 :             IF( st_ivas->hParamMC->max_band_decorr > 0 )
    2362             :             {
    2363             :                 Word16 tmp;
    2364        8760 :                 tmp = L_norm_arr( st_ivas->hParamMC->h_freq_domain_decorr_ap_state->decorr_buffer_fx, st_ivas->hParamMC->h_freq_domain_decorr_ap_state->decorr_buffer_len );
    2365        8760 :                 scale_sig32( st_ivas->hParamMC->h_freq_domain_decorr_ap_state->decorr_buffer_fx, st_ivas->hParamMC->h_freq_domain_decorr_ap_state->decorr_buffer_len, tmp ); // Q(st_ivas->hParamMC->h_freq_domain_decorr_ap_state->q_decorr_buffer+ tmp)
    2366        8760 :                 st_ivas->hParamMC->h_freq_domain_decorr_ap_state->q_decorr_buffer = add( st_ivas->hParamMC->h_freq_domain_decorr_ap_state->q_decorr_buffer, tmp );
    2367        8760 :                 move16();
    2368             :             }
    2369             :             /* CLDFB synthesis */
    2370       35840 :             FOR( ch = 0; ch < nchan_out_cldfb; ch++ )
    2371             :             {
    2372       27080 :                 IF( st_ivas->cldfbSynDec[ch] )
    2373             :                 {
    2374       27080 :                     scale_sig32( st_ivas->cldfbSynDec[ch]->cldfb_state_fx, st_ivas->cldfbSynDec[ch]->cldfb_size, ( Q5 - Q11 ) ); // Q5
    2375       27080 :                     st_ivas->cldfbSynDec[ch]->Q_cldfb_state = Q5;
    2376       27080 :                     move16();
    2377             :                 }
    2378             :             }
    2379             :             Word16 tempp;
    2380      121880 :             FOR( Word16 param_band_idx = 0; param_band_idx < st_ivas->hParamMC->num_param_bands_synth; param_band_idx++ )
    2381             :             {
    2382      113120 :                 tempp = getScaleFactor32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_len );
    2383      113120 :                 scale_sig32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_len, tempp ); // Q(31-(st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_exp[param_band_idx]- tempp))
    2384      113120 :                 st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_exp[param_band_idx] = sub( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_exp[param_band_idx], tempp );
    2385      113120 :                 move16();
    2386             : 
    2387      113120 :                 IF( LT_16( st_ivas->hParamMC->band_grouping[param_band_idx], st_ivas->hParamMC->h_output_synthesis_params.max_band_decorr ) )
    2388             :                 {
    2389       90900 :                     tempp = getScaleFactor32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_len );
    2390       90900 :                     scale_sig32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_len, tempp ); // Q(31- (st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_exp[param_band_idx] - tempp))
    2391       90900 :                     st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_exp[param_band_idx] = sub( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_exp[param_band_idx], tempp );
    2392       90900 :                     move16();
    2393             :                 }
    2394             :             }
    2395             : 
    2396        8760 :             ivas_param_mc_dec_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx, channel_active_fx );
    2397             : 
    2398       35840 :             FOR( ch = 0; ch < nchan_out_cldfb; ch++ )
    2399             :             {
    2400       27080 :                 IF( st_ivas->cldfbSynDec[ch] )
    2401             :                 {
    2402       27080 :                     scale_sig32( st_ivas->cldfbSynDec[ch]->cldfb_state_fx, st_ivas->cldfbSynDec[ch]->cldfb_size, Q11 - Q5 ); // Q11
    2403       27080 :                     st_ivas->cldfbSynDec[ch]->Q_cldfb_state = Q11;
    2404       27080 :                     move16();
    2405             :                 }
    2406             :             }
    2407        8760 :             IF( EQ_16( st_ivas->hParamMC->slots_rendered, st_ivas->hParamMC->num_slots ) )
    2408             :             {
    2409      123780 :                 FOR( Word16 param_band_idx = 0; param_band_idx < st_ivas->hParamMC->hMetadataPMC->nbands_coded; param_band_idx++ )
    2410             :                 {
    2411      115020 :                     IF( LT_16( st_ivas->hParamMC->band_grouping[param_band_idx], st_ivas->hParamMC->h_output_synthesis_params.max_band_decorr ) )
    2412             :                     {
    2413       90900 :                         Copy32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old_fx[param_band_idx], imult1616( nchan_transport_tmp, nchan_out_cov ) );
    2414       90900 :                         st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old_exp[param_band_idx] = st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_exp[param_band_idx];
    2415       90900 :                         move16();
    2416             :                     }
    2417      115020 :                     Copy32( st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_fx[param_band_idx], st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_old_fx[param_band_idx], imult1616( nchan_transport_tmp, nchan_out_cov ) );
    2418      115020 :                     st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_old_exp[param_band_idx] = st_ivas->hParamMC->h_output_synthesis_cov_state.mixing_matrix_exp[param_band_idx];
    2419      115020 :                     move16();
    2420             :                 }
    2421             :             }
    2422             :         }
    2423        7400 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    2424             :         {
    2425        7400 :             Word16 offset = imult1616( hSpatParamRendCom->slots_rendered, hSpatParamRendCom->slot_size );
    2426        7400 :             nchan_remapped = st_ivas->nchan_transport;
    2427        7400 :             move16();
    2428             : 
    2429        7400 :             test();
    2430        7400 :             test();
    2431        7400 :             test();
    2432        7400 :             IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
    2433             :             {
    2434        5760 :                 ivas_dirac_dec_binaural_render_fx( st_ivas, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, nchan_remapped, p_output_fx );
    2435             :             }
    2436        1640 :             ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_DIRAC ) || EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) ) /* rendering to CICPxx and Ambisonics */
    2437             :             {
    2438        1640 :                 ivas_dirac_dec_render_fx( st_ivas, nchan_remapped, nSamplesAskedLocal, nSamplesRendered, nSamplesAvailableNext, p_output_fx );
    2439             : 
    2440        1640 :                 test();
    2441        1640 :                 test();
    2442        1640 :                 test();
    2443        1640 :                 IF( EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) )
    2444             :                 {
    2445             :                     /* we still need to copy the separate channel if available  */
    2446         290 :                     IF( st_ivas->hOutSetup.separateChannelEnabled )
    2447             :                     {
    2448          25 :                         Copy32( st_ivas->hTcBuffer->tc_fx[LFE_CHANNEL - 1] + offset, p_output_fx[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    2449             :                     }
    2450             : 
    2451         290 :                     ivas_mc2sba_fx( st_ivas->hIntSetup, p_output_fx, p_output_fx, *nSamplesRendered, st_ivas->hOutSetup.ambisonics_order, 0 );
    2452             :                 }
    2453        1350 :                 ELSE IF( EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_5_1 ) && ( EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1 ) ) )
    2454             :                 {
    2455           0 :                     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++ )
    2456             :                     {
    2457           0 :                         set32_fx( p_output_fx[n], 0, *nSamplesRendered );
    2458             :                     }
    2459             :                 }
    2460             :             }
    2461             : 
    2462             :             /* copy discrete C and TD LFE from internal TC to output  */
    2463        7400 :             IF( st_ivas->hOutSetup.separateChannelEnabled )
    2464             :             {
    2465         200 :                 test();
    2466         200 :                 test();
    2467         200 :                 test();
    2468         200 :                 test();
    2469         200 :                 test();
    2470         200 :                 test();
    2471         200 :                 test();
    2472         200 :                 IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1 ) ||
    2473             :                     EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1_4 ) ||
    2474             :                     EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) || ( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && GT_16( st_ivas->hOutSetup.num_lfe, 0 ) ) )
    2475             :                 {
    2476         150 :                     Copy32( st_ivas->hTcBuffer->tc_fx[LFE_CHANNEL] + offset, p_output_fx[LFE_CHANNEL], *nSamplesRendered );
    2477         150 :                     Copy32( st_ivas->hTcBuffer->tc_fx[LFE_CHANNEL - 1] + offset, p_output_fx[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    2478             :                 }
    2479          50 :                 ELSE IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.num_lfe == 0 )
    2480             :                 {
    2481             :                     /* Delay the separated channel to sync with the DirAC rendering */
    2482           0 :                     Copy32( st_ivas->hTcBuffer->tc_fx[LFE_CHANNEL - 1] + offset, p_output_fx[st_ivas->hOutSetup.separateChannelIndex], *nSamplesRendered );
    2483             :                 }
    2484             :             }
    2485             :         }
    2486             :     }
    2487             : 
    2488             :     /*----------------------------------------------------------------*
    2489             :      * Write IVAS output channels
    2490             :      *   - compensation for saturation
    2491             :      *   - float to integer conversion
    2492             :      *----------------------------------------------------------------*/
    2493             : 
    2494      411969 :     st_ivas->hTcBuffer->n_samples_available = sub( st_ivas->hTcBuffer->n_samples_available, *nSamplesRendered );
    2495      411969 :     st_ivas->hTcBuffer->n_samples_rendered = add( st_ivas->hTcBuffer->n_samples_rendered, *nSamplesRendered );
    2496      411969 :     move16();
    2497      411969 :     move16();
    2498             : 
    2499             :     /* update global combined orientation start index */
    2500      411969 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    2501             : 
    2502      411969 :     IF( st_ivas->hTcBuffer->n_samples_discard > 0 )
    2503             :     {
    2504         286 :         FOR( n = 0; n < s_min( MAX_OUTPUT_CHANNELS, ivas_get_nchan_buffers_dec( st_ivas, st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ) ); n++ )
    2505             :         {
    2506         264 :             p_output_fx[n] += st_ivas->hTcBuffer->n_samples_discard;
    2507             :         }
    2508          22 :         *nSamplesRendered = sub( (Word16) *nSamplesRendered, st_ivas->hTcBuffer->n_samples_discard );
    2509          22 :         move16();
    2510          22 :         st_ivas->hTcBuffer->n_samples_discard = 0;
    2511          22 :         move16();
    2512             :     }
    2513             : 
    2514             :     {
    2515      411969 :         IF( NE_32( st_ivas->ivas_format, MONO_FORMAT ) )
    2516             :         {
    2517             : #ifndef DISABLE_LIMITER
    2518      408869 :             ivas_limiter_dec_fx( st_ivas->hLimiter, p_output_fx, nchan_out, *nSamplesRendered, st_ivas->BER_detect, output_q_factor );
    2519             : #endif
    2520             :         }
    2521             :     }
    2522             : 
    2523      411969 :     ivas_syn_output_fx( p_output_fx, output_q_factor, *nSamplesRendered, nchan_out, data );
    2524             : 
    2525      411969 :     *nSamplesAvailableNext = st_ivas->hTcBuffer->n_samples_available;
    2526      411969 :     move16();
    2527             : 
    2528      411969 :     pop_wmops();
    2529      411969 :     return IVAS_ERR_OK;
    2530             : }
    2531             : 
    2532             : /*--------------------------------------------------------------------------*
    2533             :  * ivas_jbm_dec_dec_flush_renderer()
    2534             :  *
    2535             :  * Flush samples if renderer granularity changes on a bitrate change
    2536             :  *--------------------------------------------------------------------------*/
    2537             : 
    2538         318 : ivas_error ivas_jbm_dec_flush_renderer_fx(
    2539             :     Decoder_Struct *st_ivas,                     /* i/o: IVAS decoder structure    */
    2540             :     const Word16 tc_granularity_new,             /* i  : new renderer granularity  */
    2541             :     const RENDERER_TYPE renderer_type_old,       /* i  : old renderer type         */
    2542             :     const AUDIO_CONFIG intern_config_old,        /* i  : old internal config       */
    2543             :     const IVAS_OUTPUT_SETUP_HANDLE hIntSetupOld, /* i  : old internal output setup */
    2544             :     const MC_MODE mc_mode_old,                   /* i  : old MC mode               */
    2545             :     const ISM_MODE ism_mode_old,                 /* i  : old ISM mode              */
    2546             :     UWord16 *nSamplesRendered,                   /* o  : number of samples flushed */
    2547             :     Word16 *data                                 /* o  : output synthesis signal  Q0*/
    2548             : )
    2549             : {
    2550             :     ivas_error error;
    2551             :     Word16 n_samples_still_available;
    2552             :     Word16 n_slots_still_available;
    2553             :     Word16 n_samples_to_render;
    2554             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    2555             :     Word32 output_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
    2556             :     Word32 *p_output_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
    2557             :     Word16 nchan_in, nchan_out, gd_bits, n_chan_inp, i, shift;
    2558         318 :     IF( !st_ivas->hDecoderConfig->Opt_tsm )
    2559             :     {
    2560         217 :         return IVAS_ERR_OK;
    2561             :     }
    2562             : 
    2563         101 :     *nSamplesRendered = 0;
    2564         101 :     move16();
    2565         101 :     hTcBuffer = st_ivas->hTcBuffer;
    2566             : 
    2567             :     /* get number of possible slots in new granularity */
    2568         101 :     n_samples_still_available = sub( hTcBuffer->n_samples_buffered, hTcBuffer->n_samples_rendered );
    2569             : 
    2570             :     // n_slots_still_available = n_samples_still_available / tc_granularity_new;
    2571             :     Word16 tmp, tmp_e;
    2572         101 :     tmp = BASOP_Util_Divide1616_Scale( n_samples_still_available, tc_granularity_new, &tmp_e );
    2573         101 :     n_slots_still_available = shr( tmp, sub( 15, tmp_e ) );
    2574         101 :     *nSamplesRendered = imult1616( n_slots_still_available, tc_granularity_new );
    2575         101 :     n_samples_to_render = *nSamplesRendered;
    2576         101 :     move16();
    2577         101 :     move16();
    2578         101 :     n_samples_still_available = sub( n_samples_still_available, n_samples_to_render );
    2579         101 :     assert( n_samples_still_available < tc_granularity_new );
    2580             : 
    2581             :     /* update combined orientation access index */
    2582         101 :     ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
    2583             : 
    2584         101 :     IF( n_slots_still_available )
    2585             :     {
    2586             :         Word16 ch_idx;
    2587             : 
    2588             :         /* render what is still there with zero padding */
    2589         151 :         FOR( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
    2590             :         {
    2591             :             /* move it at the beginning of the TC buffer with zero padding */
    2592         129 :             Copy32( hTcBuffer->tc_fx[ch_idx] + hTcBuffer->n_samples_rendered, hTcBuffer->tc_fx[ch_idx], n_samples_to_render );
    2593         129 :             set_zero_fx( hTcBuffer->tc_fx[ch_idx] + n_samples_to_render, hTcBuffer->n_samples_granularity - n_samples_to_render );
    2594         129 :             Copy32( hTcBuffer->tc_fx[ch_idx] + hTcBuffer->n_samples_rendered + n_samples_to_render, hTcBuffer->tc_fx[ch_idx] + hTcBuffer->n_samples_granularity, n_samples_still_available );
    2595             :         }
    2596             :         /* simple change of the slot info */
    2597          22 :         hTcBuffer->num_slots = 1;
    2598          22 :         hTcBuffer->nb_subframes = 1;
    2599          22 :         hTcBuffer->subframes_rendered = 0;
    2600          22 :         hTcBuffer->slots_rendered = 0;
    2601          22 :         hTcBuffer->subframe_nbslots[0] = 1;
    2602          22 :         hTcBuffer->n_samples_buffered = add( hTcBuffer->n_samples_granularity, n_samples_still_available );
    2603          22 :         hTcBuffer->n_samples_available = 0;
    2604          22 :         hTcBuffer->n_samples_flushed = n_samples_to_render;
    2605             : #ifdef CR_FIX_JBM_FLUSH_OFFSET
    2606             :         hTcBuffer->n_samples_rendered = 0;
    2607             : #else
    2608          22 :         hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
    2609             : #endif
    2610          22 :         move16();
    2611          22 :         move16();
    2612          22 :         move16();
    2613          22 :         move16();
    2614          22 :         move16();
    2615          22 :         move16();
    2616          22 :         move16();
    2617          22 :         move16();
    2618          22 :         move16();
    2619             : 
    2620         374 :         FOR( ch_idx = 0; ch_idx < MAX_CICP_CHANNELS; ch_idx++ )
    2621             :         {
    2622         352 :             p_output_fx[ch_idx] = output_fx[ch_idx];
    2623             :         }
    2624             : 
    2625          22 :         test();
    2626          22 :         IF( EQ_16( st_ivas->ivas_format, ISM_FORMAT ) )
    2627             :         {
    2628           7 :             IF( EQ_16( ism_mode_old, ISM_MODE_DISC ) )
    2629             :             {
    2630             :                 /* Binaural rendering */
    2631           7 :                 IF( EQ_16( renderer_type_old, RENDERER_BINAURAL_OBJECTS_TD ) )
    2632             :                 {
    2633           0 :                     IF( NE_32( ( error = ivas_td_binaural_renderer_sf_fx( st_ivas, p_output_fx, hTcBuffer->n_samples_granularity ) ), IVAS_ERR_OK ) )
    2634             :                     {
    2635           0 :                         return error;
    2636             :                     }
    2637             :                 }
    2638           7 :                 ELSE IF( EQ_16( renderer_type_old, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2639             :                 {
    2640             :                     /*TODO :To be tested : no stream entering---------------------------------------*/
    2641             :                     /* Convert to CICPxx; used also for ISM->CICP19->binaural_room rendering */
    2642           7 :                     set16_fx( st_ivas->hIsmRendererData->interpolator_fx, 32767, hTcBuffer->n_samples_granularity ); // 32767=1.0f in Q15
    2643             : 
    2644           7 :                     ivas_ism_render_sf_fx( st_ivas, p_output_fx, hTcBuffer->n_samples_granularity );
    2645             : 
    2646           7 :                     st_ivas->hCrendWrapper->p_io_qfactor = &st_ivas->hCrendWrapper->io_qfactor;
    2647           7 :                     *st_ivas->hCrendWrapper->p_io_qfactor = 11;
    2648           7 :                     move16();
    2649             : 
    2650           7 :                     shift = MAX_16;
    2651           7 :                     move16();
    2652           7 :                     n_chan_inp = add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe );
    2653             : 
    2654          91 :                     FOR( i = 0; i < n_chan_inp; i++ )
    2655             :                     {
    2656          84 :                         shift = s_min( shift, L_norm_arr( p_output_fx[i], hTcBuffer->n_samples_granularity ) );
    2657             :                     }
    2658             : 
    2659           7 :                     gd_bits = sub( find_guarded_bits_fx( imult1616( hTcBuffer->subframe_nbslots[0], hTcBuffer->n_samples_granularity ) ), shift );
    2660             : 
    2661           7 :                     *st_ivas->hCrendWrapper->p_io_qfactor = sub( *st_ivas->hCrendWrapper->p_io_qfactor, gd_bits );
    2662           7 :                     move16();
    2663             : 
    2664          91 :                     FOR( i = 0; i < n_chan_inp; i++ )
    2665             :                     {
    2666          84 :                         scale_sig32( p_output_fx[i], hTcBuffer->n_samples_granularity, sub( *st_ivas->hCrendWrapper->p_io_qfactor, Q11 ) ); // Q(*st_ivas->hCrendWrapper->p_io_qfactor)
    2667             :                     }
    2668             : 
    2669           7 :                     IF( NE_32( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, st_ivas->hDecoderConfig, NULL,
    2670             :                                                                          NULL, NULL, st_ivas->hTcBuffer, p_output_fx, p_output_fx, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs ) ),
    2671             :                                IVAS_ERR_OK ) )
    2672             :                     {
    2673           0 :                         return error;
    2674             :                     }
    2675             : 
    2676          91 :                     FOR( i = 0; i < n_chan_inp; i++ )
    2677             :                     {
    2678          84 :                         scale_sig32( p_output_fx[i], hTcBuffer->n_samples_granularity, sub( Q11, *st_ivas->hCrendWrapper->p_io_qfactor ) ); // Q(11)
    2679             :                     }
    2680           7 :                     *st_ivas->hCrendWrapper->p_io_qfactor = Q11;
    2681           7 :                     move16();
    2682             :                 }
    2683             :             }
    2684             :             ELSE
    2685             :             {
    2686           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong ISM_MODE in VoIP renderer flushing!" );
    2687             :             }
    2688             :         }
    2689          15 :         ELSE IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) )
    2690             :         {
    2691           0 :             IF( EQ_16( mc_mode_old, MC_MODE_MCT ) )
    2692             :             {
    2693           0 :                 test();
    2694           0 :                 IF( EQ_16( renderer_type_old, RENDERER_BINAURAL_MIXER_CONV ) || EQ_16( renderer_type_old, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    2695             :                 {
    2696             :                     /*TODO :To be tested : no stream entering*/
    2697           0 :                     st_ivas->hCrendWrapper->p_io_qfactor = &st_ivas->hCrendWrapper->io_qfactor;
    2698           0 :                     IF( NE_32( ( error = getAudioConfigNumChannels( intern_config_old, &nchan_in ) ), IVAS_ERR_OK ) )
    2699             :                     {
    2700           0 :                         return error;
    2701             :                     }
    2702           0 :                     IF( NE_32( ( error = getAudioConfigNumChannels( st_ivas->hOutSetup.output_config, &nchan_out ) ), IVAS_ERR_OK ) )
    2703             :                     {
    2704           0 :                         return error;
    2705             :                     }
    2706           0 :                     *st_ivas->hCrendWrapper->p_io_qfactor = 11;
    2707           0 :                     move16();
    2708           0 :                     IF( NE_32( ( error = ivas_rend_crendProcessSubframe( st_ivas->hCrendWrapper, intern_config_old, st_ivas->hOutSetup.output_config, st_ivas->hDecoderConfig, st_ivas->hCombinedOrientationData,
    2709             :                                                                          hIntSetupOld, st_ivas->hEFAPdata, st_ivas->hTcBuffer, hTcBuffer->tc_fx, p_output_fx, hTcBuffer->n_samples_granularity, st_ivas->hDecoderConfig->output_Fs ) ),
    2710             :                                IVAS_ERR_OK ) )
    2711             :                     {
    2712           0 :                         return error;
    2713             :                     }
    2714             : 
    2715           0 :                     ivas_binaural_add_LFE_fx( st_ivas, hTcBuffer->n_samples_granularity, st_ivas->hTcBuffer->tc_fx, p_output_fx );
    2716             :                 }
    2717           0 :                 ELSE IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
    2718             :                 {
    2719           0 :                     IF( NE_32( ( error = ivas_td_binaural_renderer_sf_fx( st_ivas, p_output_fx, hTcBuffer->n_samples_granularity ) ), IVAS_ERR_OK ) )
    2720             :                     {
    2721           0 :                         return error;
    2722             :                     }
    2723           0 :                     ivas_binaural_add_LFE_fx( st_ivas, *nSamplesRendered, st_ivas->hTcBuffer->tc_fx, p_output_fx );
    2724             :                 }
    2725             :             }
    2726             :             ELSE
    2727             :             {
    2728           0 :                 return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong MC_MODE in VoIP renderer flushing!" );
    2729             :             }
    2730             :         }
    2731          15 :         ELSE IF( EQ_16( st_ivas->ivas_format, MASA_ISM_FORMAT ) || EQ_16( st_ivas->ivas_format, MASA_FORMAT ) )
    2732             :         {
    2733           6 :             IF( EQ_16( ism_mode_old, ISM_MASA_MODE_DISC ) )
    2734             :             {
    2735             :                 Word32 *tc_local_fx[MAX_TRANSPORT_CHANNELS];
    2736             : 
    2737           6 :                 move16();
    2738          22 :                 FOR( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    2739             :                 {
    2740          16 :                     tc_local_fx[ch_idx] = &st_ivas->hTcBuffer->tc_fx[ch_idx + 2][hTcBuffer->n_samples_rendered];
    2741          16 :                     Copy32( st_ivas->hMasaIsmData->delayBuffer_fx[ch_idx], tc_local_fx[ch_idx], st_ivas->hMasaIsmData->delayBuffer_size ); /*Q11*/
    2742             :                 }
    2743             : 
    2744           6 :                 IF( st_ivas->nchan_ism > 0 )
    2745             :                 {
    2746           4 :                     IF( NE_32( ( error = ivas_td_binaural_renderer_sf_fx( st_ivas, p_output_fx, hTcBuffer->n_samples_granularity ) ), IVAS_ERR_OK ) )
    2747             :                     {
    2748           0 :                         return error;
    2749             :                     }
    2750             :                 }
    2751             :                 ELSE
    2752             :                 {
    2753           6 :                     FOR( ch_idx = 0; ch_idx < st_ivas->hDecoderConfig->nchan_out; ch_idx++ )
    2754             :                     {
    2755           4 :                         set_zero_fx( p_output_fx[ch_idx], (Word16) ( *nSamplesRendered ) );
    2756             :                     }
    2757           2 :                     st_ivas->hTcBuffer->slots_rendered = add( st_ivas->hTcBuffer->slots_rendered, 1 );
    2758           2 :                     st_ivas->hTcBuffer->subframes_rendered = add( st_ivas->hTcBuffer->subframes_rendered, 1 );
    2759           2 :                     move16();
    2760           2 :                     move16();
    2761             :                 }
    2762             :             }
    2763             :         }
    2764           9 :         ELSE IF( EQ_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    2765             :         {
    2766             :             Word32 *tc_local_fx[MAX_TRANSPORT_CHANNELS];
    2767             :             Word16 last_spar_md_idx;
    2768             :             Word16 last_dirac_md_idx;
    2769             :             UWord16 nSamplesAvailableNext;
    2770             : 
    2771           9 :             last_spar_md_idx = st_ivas->hSpar->render_to_md_map[st_ivas->hSpar->slots_rendered - 1];
    2772           9 :             last_dirac_md_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->slots_rendered - 1];
    2773           9 :             move16();
    2774           9 :             move16();
    2775             :             /* copy from ISM delay buffer to the correct place in tcs */
    2776          27 :             FOR( ch_idx = 0; ch_idx < st_ivas->nchan_ism; ch_idx++ )
    2777             :             {
    2778          18 :                 tc_local_fx[ch_idx] = &st_ivas->hTcBuffer->tc_fx[ch_idx + 2][hTcBuffer->n_samples_rendered];
    2779          18 :                 Copy32( st_ivas->hSbaIsmData->delayBuffer_fx[ch_idx], tc_local_fx[ch_idx], st_ivas->hSbaIsmData->delayBuffer_size );
    2780             :             }
    2781             : 
    2782             :             /* transfer adapted sf info from hTcBuffer to SPAR and DirAC */
    2783           9 :             st_ivas->hSpar->nb_subframes = 1;
    2784           9 :             st_ivas->hSpar->subframes_rendered = 0;
    2785           9 :             st_ivas->hSpar->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2786           9 :             st_ivas->hSpar->slots_rendered = 0;
    2787           9 :             st_ivas->hSpar->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2788           9 :             st_ivas->hSpatParamRendCom->nb_subframes = 1;
    2789           9 :             st_ivas->hSpatParamRendCom->subframes_rendered = 0;
    2790           9 :             st_ivas->hSpatParamRendCom->subframe_nbslots[0] = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2791           9 :             st_ivas->hSpatParamRendCom->slots_rendered = 0;
    2792           9 :             st_ivas->hSpatParamRendCom->num_slots = JBM_CLDFB_SLOTS_IN_SUBFRAME;
    2793           9 :             move16();
    2794           9 :             move16();
    2795           9 :             move16();
    2796           9 :             move16();
    2797           9 :             move16();
    2798           9 :             move16();
    2799           9 :             move16();
    2800           9 :             move16();
    2801           9 :             move16();
    2802           9 :             move16();
    2803             : 
    2804             :             /* also adapt md maps, just use the last index */
    2805           9 :             set16_fx( st_ivas->hSpar->render_to_md_map, last_spar_md_idx, n_slots_still_available );
    2806           9 :             set16_fx( st_ivas->hSpatParamRendCom->render_to_md_map, last_dirac_md_idx, n_slots_still_available );
    2807             : 
    2808             :             /* render the last subframe */
    2809           9 :             IF( NE_32( ( error = ivas_osba_dirac_td_binaural_jbm_fx( st_ivas, (UWord16) hTcBuffer->n_samples_granularity, nSamplesRendered, &nSamplesAvailableNext, p_output_fx ) ), IVAS_ERR_OK ) )
    2810             :             {
    2811           0 :                 return error;
    2812             :             }
    2813             :         }
    2814             :         ELSE
    2815             :         {
    2816           0 :             return IVAS_ERROR( IVAS_ERR_WRONG_MODE, "Wrong IVAS format in VoIP renderer flushing!" );
    2817             :         }
    2818             : #ifdef CR_FIX_JBM_FLUSH_OFFSET
    2819             :         hTcBuffer->n_samples_rendered = hTcBuffer->n_samples_granularity;
    2820             : #endif
    2821             :     }
    2822             : 
    2823             :     /* update global combined orientation start index */
    2824         101 :     ivas_combined_orientation_update_start_index( st_ivas->hCombinedOrientationData, *nSamplesRendered );
    2825             : 
    2826         101 :     *nSamplesRendered = n_samples_to_render;
    2827         101 :     move16();
    2828             :     /* Only write out the valid data*/
    2829             :     {
    2830         101 :         IF( NE_16( st_ivas->ivas_format, MONO_FORMAT ) )
    2831             :         {
    2832             : #ifndef DISABLE_LIMITER
    2833         101 :             Word16 ch_idx, exp = 11;
    2834         101 :             move16();
    2835        1717 :             FOR( ch_idx = 0; ch_idx < MAX_CICP_CHANNELS; ch_idx++ )
    2836             :             {
    2837        1616 :                 p_output_fx[ch_idx] = output_fx[ch_idx];
    2838             :             }
    2839         101 :             ivas_limiter_dec_fx( st_ivas->hLimiter, p_output_fx, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect, exp );
    2840             : #endif
    2841             :         }
    2842             :     }
    2843             : 
    2844         101 :     ivas_syn_output_fx( p_output_fx, Q11, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, data );
    2845         101 :     return IVAS_ERR_OK;
    2846             : }
    2847             : 
    2848             : 
    2849             : /*--------------------------------------------------------------------------*
    2850             :  * ivas_jbm_dec_set_discard_samples()
    2851             :  *
    2852             :  * Set number of samples to discard in the first subframe if the renderer granularity changes on a bitrate change
    2853             :  *--------------------------------------------------------------------------*/
    2854             : 
    2855         324 : ivas_error ivas_jbm_dec_set_discard_samples(
    2856             :     Decoder_Struct *st_ivas /* i/o: main IVAS decoder structre */
    2857             : )
    2858             : {
    2859             :     Word16 nMaxSlotsPerSubframe, nSlotsInFirstSubframe;
    2860             :     Word16 temp, temp_e;
    2861             :     /* render first frame with front zero padding and discarding those samples */
    2862             :     // nMaxSlotsPerSubframe = (Word16)(st_ivas->hDecoderConfig->output_Fs / (FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES)) / st_ivas->hTcBuffer->n_samples_granularity;
    2863         324 :     temp = BASOP_Util_Divide3232_Scale( st_ivas->hDecoderConfig->output_Fs, ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ), &temp_e );
    2864         324 :     temp = shr( temp, sub( 15, temp_e ) );
    2865         324 :     nMaxSlotsPerSubframe = BASOP_Util_Divide1616_Scale( temp, st_ivas->hTcBuffer->n_samples_granularity, &temp_e );
    2866         324 :     nMaxSlotsPerSubframe = shr( nMaxSlotsPerSubframe, sub( 15, temp_e ) ); // Q0
    2867         324 :     nSlotsInFirstSubframe = sub( nMaxSlotsPerSubframe, st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] );
    2868         324 :     IF( nSlotsInFirstSubframe > 0 )
    2869             :     {
    2870          22 :         st_ivas->hTcBuffer->n_samples_discard = imult1616( sub( nMaxSlotsPerSubframe, nSlotsInFirstSubframe ), st_ivas->hTcBuffer->n_samples_granularity );
    2871             :         /* set last subframes number to max to ensure correct continuation */
    2872          22 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nMaxSlotsPerSubframe;
    2873          22 :         move16();
    2874          22 :         move16();
    2875             :     }
    2876             : 
    2877         324 :     return IVAS_ERR_OK;
    2878             : }
    2879             : 
    2880             : 
    2881             : /*--------------------------------------------------------------------------*
    2882             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    2883             :  *
    2884             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    2885             :  *--------------------------------------------------------------------------*/
    2886       17686 : void ivas_jbm_dec_get_adapted_linear_interpolator_fx(
    2887             :     const Word16 default_interp_length, /* i  : default length of the (full-frame) interpolator */
    2888             :     const Word16 interp_length,         /* i  : length of the interpolator to be created        */
    2889             :     Word16 *interpolator_fx             /* o  : the interpolator                               Q15 */
    2890             : )
    2891             : {
    2892             :     Word16 jbm_segment_len, idx;
    2893             :     Word16 dec_fx;
    2894             :     Word16 dec_e;
    2895             : 
    2896       17686 :     jbm_segment_len = shr( default_interp_length, 1 );
    2897       17686 :     dec_fx = divide1616( 1, default_interp_length ); /*32767 / default_interp_length*/
    2898             : 
    2899       17686 :     interpolator_fx[interp_length - 1] = 32767; /* (1.0f in Q15) -1 */
    2900       17686 :     move16();
    2901       17686 :     interpolator_fx[interp_length - 2] = add( sub( 32767, dec_fx ), 1 ); // Use 32768 to maintain precision
    2902       17686 :     move16();
    2903     1540746 :     FOR( idx = interp_length - 3; idx >= jbm_segment_len; idx-- )
    2904             :     {
    2905     1523060 :         interpolator_fx[idx] = s_max( 0, sub( interpolator_fx[idx + 1], dec_fx ) );
    2906     1523060 :         move16();
    2907             :     }
    2908             : 
    2909       17686 :     IF( interpolator_fx[idx + 1] > 0 )
    2910             :     {
    2911       17685 :         dec_fx = BASOP_Util_Divide1616_Scale( interpolator_fx[idx + 1], add( jbm_segment_len, 1 ), &dec_e );
    2912       17685 :         dec_fx = shr( dec_fx, sub( 15, dec_e ) ); // Q0
    2913     1575477 :         FOR( ; idx >= 0; idx-- )
    2914             :         {
    2915     1557792 :             interpolator_fx[idx] = sub( interpolator_fx[idx + 1], dec_fx );
    2916     1557792 :             move16();
    2917             :         }
    2918             :     }
    2919             :     ELSE
    2920             :     {
    2921           1 :         set16_fx( interpolator_fx, 0, idx + 1 );
    2922             :     }
    2923             : 
    2924       17686 :     return;
    2925             : }
    2926             : 
    2927             : /*--------------------------------------------------------------------------*
    2928             :  * ivas_jbm_dec_get_adapted_subframes()
    2929             :  *
    2930             :  * Get an interpolator that is adapted to time scale modified IVAS frame
    2931             :  *--------------------------------------------------------------------------*/
    2932             : 
    2933      290284 : void ivas_jbm_dec_get_adapted_subframes(
    2934             :     const Word16 nCldfbTs,    /* i  : number of time slots in the current frame */
    2935             :     Word16 *subframe_nbslots, /* i/o: subframe grid                             */
    2936             :     Word16 *nb_subframes      /* i/o: number of subframes in the frame          */
    2937             : )
    2938             : {
    2939             :     UWord16 nSlotsInLastSubframe, nSlotsInFirstSubframe;
    2940      290284 :     UWord16 nCldfbSlotsLocal = nCldfbTs;
    2941      290284 :     move16();
    2942             :     Word16 temp, temp_e;
    2943             :     /* get last subframe size from previous frame, determine how many slots have to be processed
    2944             :            in the first subframe (i.e. potential leftover of a 5ms subframe) */
    2945      290284 :     nSlotsInFirstSubframe = ( sub( PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, subframe_nbslots[*nb_subframes - 1] ) );
    2946      290284 :     *nb_subframes = 0;
    2947      290284 :     move16();
    2948      290284 :     IF( nSlotsInFirstSubframe > 0 )
    2949             :     {
    2950        7849 :         *nb_subframes = 1;
    2951        7849 :         move16();
    2952        7849 :         nCldfbSlotsLocal = sub( nCldfbSlotsLocal, nSlotsInFirstSubframe );
    2953             :     }
    2954             : 
    2955      290284 :     temp = BASOP_Util_Divide3232_Scale( L_add( nCldfbSlotsLocal, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME - 1 ), PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, &temp_e );
    2956      290284 :     *nb_subframes = add( *nb_subframes, shr( temp, sub( 15, temp_e ) ) ); // Q0
    2957      290284 :     move16();
    2958      290284 :     nSlotsInLastSubframe = nCldfbSlotsLocal % PARAM_MC_MAX_NSLOTS_IN_SUBFRAME;
    2959      290284 :     move16();
    2960             : 
    2961      290284 :     set16_fx( subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    2962      290284 :     set16_fx( subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, *nb_subframes );
    2963             : 
    2964      290284 :     IF( nSlotsInFirstSubframe > 0 )
    2965             :     {
    2966        7849 :         subframe_nbslots[0] = nSlotsInFirstSubframe;
    2967        7849 :         move16();
    2968             :     }
    2969             : 
    2970      290284 :     IF( nSlotsInLastSubframe > 0 )
    2971             :     {
    2972        7861 :         subframe_nbslots[*nb_subframes - 1] = nSlotsInLastSubframe;
    2973        7861 :         move16();
    2974             :     }
    2975             : 
    2976      290284 :     return;
    2977             : }
    2978             : /*--------------------------------------------------------------------------*
    2979             :  * ivas_jbm_dec_get_adapted_linear_interpolator()
    2980             :  *
    2981             :  * Get an meta data map adapted to a time scale modified IVAS frame
    2982             :  *--------------------------------------------------------------------------*/
    2983             : 
    2984      254992 : void ivas_jbm_dec_get_md_map(
    2985             :     const Word16 default_len,  /* i  : default frame length in metadata slots         */
    2986             :     const Word16 len,          /* i  : length of the modfied frames in metadata slots */
    2987             :     const Word16 subframe_len, /* i  : default length of a subframe                   */
    2988             :     const Word16 offset,       /* i  : current read offset into the md buffer         */
    2989             :     const Word16 buf_len,      /* i  : length of the metadata buffer                  */
    2990             :     Word16 *map                /* o  : metadata index map                             */
    2991             : )
    2992             : {
    2993             :     Word16 jbm_segment_len, map_idx, src_idx, src_idx_map;
    2994             :     Word32 dec_fx, src_idx_fx;
    2995             :     Word16 temp_e;
    2996             :     Word16 temp;
    2997      254992 :     jbm_segment_len = shr( default_len, 1 );
    2998             : 
    2999     2294961 :     FOR( ( map_idx = len - 1, src_idx = default_len - 1 ); map_idx >= jbm_segment_len; ( map_idx--, src_idx-- ) )
    3000             :     {
    3001     2039969 :         temp = BASOP_Util_Divide1616_Scale( src_idx, subframe_len, &temp_e );
    3002     2039969 :         temp = shr( temp, sub( 15, temp_e ) ); // Q0
    3003     2039969 :         src_idx_map = s_max( 0, temp );
    3004     2039969 :         map[map_idx] = add( offset, src_idx_map ) % buf_len;
    3005     2039969 :         move16();
    3006             :     }
    3007             : 
    3008             :     /* changed part (first segment), interpolate index to parameters
    3009             :            (we do not want to interpolate and smooth acutal direction/diffuseness values even more) */
    3010      254992 :     IF( src_idx >= 0 )
    3011             :     {
    3012      254986 :         dec_fx = BASOP_Util_Divide3232_Scale( L_shl( add( src_idx, 1 ), 16 ), jbm_segment_len, &temp_e );
    3013      254986 :         dec_fx = L_shr( dec_fx, sub( 15, temp_e ) );
    3014      254986 :         src_idx_fx = L_sub( L_shl( add( src_idx, 1 ), 16 ), dec_fx );
    3015     2294874 :         FOR( ; map_idx >= 0; map_idx-- )
    3016             :         {
    3017     2039888 :             temp = BASOP_Util_Divide1616_Scale( round_fx( src_idx_fx ), subframe_len, &temp_e );
    3018     2039888 :             temp = shr( temp, sub( 15, temp_e ) ); // Q0
    3019     2039888 :             src_idx = s_max( 0, temp );
    3020     2039888 :             map[map_idx] = add( offset, src_idx ) % buf_len;
    3021     2039888 :             src_idx_fx = L_sub( src_idx_fx, dec_fx );
    3022             :         }
    3023             :     }
    3024             :     ELSE
    3025             :     {
    3026           6 :         set16_fx( map, offset, add( map_idx, 1 ) );
    3027             :     }
    3028             : 
    3029      254992 :     return;
    3030             : }
    3031             : 
    3032             : /*--------------------------------------------------------------------------*
    3033             :  * ivas_jbm_dec_get_md_map_even_spacing()
    3034             :  *
    3035             :  * Get an meta data map adapted to a time scale modified IVAS frame. Distribute slots evenly across the modified frame.
    3036             :  *--------------------------------------------------------------------------*/
    3037             : 
    3038       31994 : void ivas_jbm_dec_get_md_map_even_spacing(
    3039             :     const Word16 len,          /* i  : length of the modfied frames in metadata slots */
    3040             :     const Word16 subframe_len, /* i  : default length of a subframe                   */
    3041             :     const Word16 offset,       /* i  : current read offset into the md buffer         */
    3042             :     const Word16 buf_len,      /* i  : length of the metadata buffer                  */
    3043             :     Word16 *map                /* o  : metadata index map                             */
    3044             : )
    3045             : {
    3046             :     Word16 map_idx, sf_idx, sf_length, increment, subframes_written;
    3047             :     Word32 decimal_fx, decimal_sum_fx, eps_fx; // q = 16
    3048             :     Word16 subframe_map_length[MAX_PARAM_SPATIAL_SUBFRAMES];
    3049             : 
    3050             :     /* subframe map length */
    3051       31994 :     sf_length = idiv1616( len, subframe_len );
    3052       31994 :     IF( len % subframe_len == 0 )
    3053             :     {
    3054             :         /* even subframes */
    3055      159915 :         FOR( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    3056             :         {
    3057      127932 :             subframe_map_length[sf_idx] = sf_length;
    3058      127932 :             move16();
    3059             :         }
    3060             :     }
    3061             :     ELSE
    3062             :     {
    3063             :         /* uneven subframes */
    3064             :         Word32 temp;
    3065             :         Word16 temp_e;
    3066          11 :         temp = BASOP_Util_Divide3232_Scale( L_shl( len, 16 ), subframe_len, &temp_e );
    3067          11 :         temp = L_shr( temp, sub( 15, temp_e ) );            // Q16
    3068          11 :         decimal_fx = L_sub( temp, L_shl( sf_length, 16 ) ); // Q16
    3069          11 :         decimal_sum_fx = decimal_fx;
    3070             : 
    3071          11 :         eps_fx = 65;
    3072          11 :         move32();
    3073          11 :         move32();
    3074          55 :         FOR( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    3075             :         {
    3076          44 :             increment = extract_l( L_shr( L_add( decimal_sum_fx, eps_fx ), 16 ) );
    3077          44 :             subframe_map_length[sf_idx] = add( sf_length, increment );
    3078          44 :             move16();
    3079          44 :             IF( increment > 0 )
    3080             :             {
    3081          22 :                 decimal_sum_fx = L_sub( decimal_sum_fx, ONE_IN_Q16 );
    3082             :             }
    3083          44 :             decimal_sum_fx = L_add( decimal_sum_fx, decimal_fx );
    3084             :         }
    3085             :     }
    3086             : 
    3087             :     /* map slots to subframes */
    3088       31994 :     sf_idx = 0;
    3089       31994 :     subframes_written = 0;
    3090       31994 :     move16();
    3091       31994 :     move16();
    3092      544016 :     FOR( map_idx = 0; map_idx < len; map_idx++ )
    3093             :     {
    3094      512022 :         map[map_idx] = add( offset, sf_idx ) % buf_len;
    3095      512022 :         move16();
    3096      512022 :         IF( GE_16( sub( map_idx, subframes_written ), sub( subframe_map_length[sf_idx], 1 ) ) )
    3097             :         {
    3098      127976 :             subframes_written = add( subframes_written, subframe_map_length[sf_idx] );
    3099      127976 :             sf_idx = add( sf_idx, 1 );
    3100             :         }
    3101             :     }
    3102             : 
    3103       31994 :     return;
    3104             : }
    3105             : 
    3106             : /*--------------------------------------------------------------------------*
    3107             :  * ivas_jbm_dec_get_num_tc_channels()
    3108             :  *
    3109             :  * Get the number of transport channels provided by the JBM transport channel decode function
    3110             :  *--------------------------------------------------------------------------*/
    3111             : 
    3112             : 
    3113        6849 : Word16 ivas_jbm_dec_get_num_tc_channels_fx(
    3114             :     Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    3115             : )
    3116             : {
    3117             :     Word16 num_tc;
    3118             :     Word32 ivas_total_brate;
    3119             :     AUDIO_CONFIG output_config;
    3120             : 
    3121             : 
    3122        6849 :     IF( EQ_16( st_ivas->renderer_type, RENDERER_DISABLE ) )
    3123             :     {
    3124         892 :         num_tc = st_ivas->hDecoderConfig->nchan_out;
    3125         892 :         move16();
    3126             :     }
    3127             :     ELSE
    3128             :     {
    3129        5957 :         num_tc = st_ivas->nchan_transport;
    3130        5957 :         move16();
    3131             :     }
    3132        6849 :     output_config = st_ivas->hDecoderConfig->output_config;
    3133        6849 :     move16();
    3134             : 
    3135        6849 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    3136        6849 :     move32();
    3137             : 
    3138        6849 :     test();
    3139        6849 :     test();
    3140        6849 :     test();
    3141        6849 :     IF( EQ_16( st_ivas->ivas_format, MONO_FORMAT ) )
    3142             :     {
    3143           3 :         num_tc = st_ivas->hDecoderConfig->nchan_out;
    3144           3 :         move16();
    3145             :     }
    3146        6846 :     ELSE IF( EQ_16( st_ivas->ivas_format, STEREO_FORMAT ) && EQ_16( st_ivas->hDecoderConfig->nchan_out, 1 ) )
    3147             :     {
    3148          22 :         num_tc = 1;
    3149          22 :         move16();
    3150             :     }
    3151        6824 :     ELSE IF( EQ_16( st_ivas->ivas_format, ISM_FORMAT ) )
    3152             :     {
    3153        1066 :         if ( EQ_16( st_ivas->renderer_type, RENDERER_MONO_DOWNMIX ) )
    3154             :         {
    3155           2 :             num_tc = 1;
    3156           2 :             move16();
    3157             :         }
    3158             :     }
    3159        5758 :     ELSE IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( st_ivas->ivas_format, MASA_FORMAT ) )
    3160             :     {
    3161        3055 :         test();
    3162        3055 :         test();
    3163        3055 :         test();
    3164        3055 :         test();
    3165        3055 :         test();
    3166        3055 :         test();
    3167        3055 :         IF( st_ivas->sba_dirac_stereo_flag )
    3168             :         {
    3169         124 :             num_tc = CPE_CHANNELS;
    3170         124 :             move16();
    3171             :         }
    3172        2931 :         ELSE IF( EQ_16( st_ivas->ivas_format, MASA_FORMAT ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || ( LE_32( ivas_total_brate, IVAS_SID_5k2 ) && GT_16( st_ivas->nCPE, 0 ) && st_ivas->hCPE[0] != NULL && EQ_16( st_ivas->hCPE[0]->nchan_out, 1 ) ) ) )
    3173             :         {
    3174         626 :             num_tc = 1; /* Only one channel transported */
    3175         626 :             move16();
    3176             :         }
    3177             : 
    3178        3055 :         test();
    3179        3055 :         test();
    3180        3055 :         test();
    3181        3055 :         test();
    3182        3055 :         IF( EQ_16( st_ivas->ivas_format, MASA_FORMAT ) && EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) && EQ_16( st_ivas->nchan_transport, 2 ) && LT_32( st_ivas->hDecoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) && GT_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
    3183             :         {
    3184           5 :             num_tc = CPE_CHANNELS;
    3185           5 :             move16();
    3186             :         }
    3187        3055 :         IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) )
    3188             :         {
    3189        1263 :             if ( EQ_16( num_tc, 3 ) )
    3190             :             {
    3191         267 :                 num_tc = add( num_tc, 1 );
    3192             :             }
    3193             :         }
    3194             :     }
    3195        2703 :     ELSE IF( EQ_16( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    3196             :     {
    3197        1098 :         IF( NE_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) )
    3198             :         {
    3199        1095 :             test();
    3200        1095 :             IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
    3201             :             {
    3202         693 :                 num_tc = add( num_tc, 1 );
    3203             :             }
    3204         402 :             ELSE IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
    3205             :             {
    3206         402 :                 num_tc = add( num_tc, st_ivas->nchan_ism );
    3207             :             }
    3208             :         }
    3209             :     }
    3210        1605 :     ELSE IF( EQ_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
    3211             :     {
    3212             : 
    3213             : 
    3214         501 :         if ( st_ivas->sba_dirac_stereo_flag )
    3215             :         {
    3216          94 :             num_tc = CPE_CHANNELS;
    3217          94 :             move16();
    3218             :         }
    3219             : 
    3220         501 :         test();
    3221         501 :         test();
    3222         501 :         if ( ( st_ivas->sba_planar && GE_16( num_tc, 3 ) ) || EQ_16( num_tc, 3 ) )
    3223             :         {
    3224         115 :             num_tc = add( num_tc, 1 );
    3225             :         }
    3226         501 :         if ( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
    3227             :         {
    3228          96 :             num_tc = add( num_tc, st_ivas->nchan_ism );
    3229             :         }
    3230             :     }
    3231        1104 :     ELSE IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) )
    3232             :     {
    3233        1059 :         IF( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) )
    3234             :         {
    3235           5 :             num_tc = 1;
    3236           5 :             move16();
    3237             :         }
    3238        1054 :         ELSE IF( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) )
    3239             :         {
    3240          26 :             num_tc = 2;
    3241          26 :             move16();
    3242             :         }
    3243        1028 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
    3244             :         {
    3245             :             /* do all static dmx already in the TC decoder if less channels than transported... */
    3246         517 :             test();
    3247         517 :             test();
    3248         517 :             test();
    3249         517 :             test();
    3250         517 :             test();
    3251         517 :             IF( NE_16( st_ivas->transport_config, st_ivas->intern_config ) && ( EQ_16( st_ivas->intern_config, IVAS_AUDIO_CONFIG_FOA ) || EQ_16( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA2 ) || EQ_16( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
    3252             :             {
    3253           1 :                 if ( GE_16( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ), add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe ) ) )
    3254             :                 {
    3255           0 :                     num_tc = add( st_ivas->hIntSetup.nchan_out_woLFE, st_ivas->hIntSetup.num_lfe );
    3256             :                 }
    3257             :             }
    3258         516 :             ELSE IF( ( EQ_16( st_ivas->renderer_type, RENDERER_MC ) || EQ_16( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) ) && GE_16( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ), add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe ) ) )
    3259             :             {
    3260           0 :                 num_tc = add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe );
    3261             :             }
    3262             :         }
    3263         511 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
    3264             :         {
    3265           9 :             num_tc = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
    3266           9 :             move16();
    3267             :         }
    3268         502 :         ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
    3269             :         {
    3270         216 :             if ( st_ivas->hOutSetup.separateChannelEnabled )
    3271             :             {
    3272          11 :                 num_tc = add( num_tc, 1 );
    3273             :             }
    3274         216 :             test();
    3275         216 :             test();
    3276         216 :             test();
    3277         216 :             test();
    3278         216 :             test();
    3279         216 :             test();
    3280         216 :             test();
    3281         226 :             if ( st_ivas->hOutSetup.separateChannelEnabled && ( EQ_16( output_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_16( output_config, IVAS_AUDIO_CONFIG_7_1 ) ||
    3282          30 :                                                                 EQ_16( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) || EQ_16( output_config, IVAS_AUDIO_CONFIG_7_1_4 ) ||
    3283          20 :                                                                 EQ_16( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) || ( EQ_16( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) && st_ivas->hOutSetup.num_lfe > 0 ) ) )
    3284             :             {
    3285             :                 /* LFE is synthesized in TD with the TCs*/
    3286           1 :                 num_tc = add( num_tc, 1 );
    3287             :             }
    3288             :         }
    3289             :     }
    3290          45 :     ELSE IF( EQ_16( st_ivas->ivas_format, MONO_FORMAT ) && EQ_16( st_ivas->renderer_type, RENDERER_NON_DIEGETIC_DOWNMIX ) )
    3291             :     {
    3292           0 :         num_tc = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
    3293           0 :         move16();
    3294             :     }
    3295             : 
    3296        6849 :     return num_tc;
    3297             : }
    3298             : 
    3299             : /*--------------------------------------------------------------------------*
    3300             :  * ivas_jbm_dec_copy_tc()
    3301             :  *
    3302             :  * Copy interleaved transport chnannels to the correct buffers, update the TC
    3303             :  * buffer handle
    3304             :  *--------------------------------------------------------------------------*/
    3305       19405 : static void ivas_jbm_dec_copy_tc(
    3306             :     Decoder_Struct *st_ivas,           /* i/o: IVAS decoder structure                                 */
    3307             :     const Word16 nSamplesForRendering, /* i  : number of samples to digest                            */
    3308             :     Word16 *nSamplesResidual,          /* o  : number of samples that will be left for the next frame */
    3309             :     Word32 *data,                      /* i  : (interleaved) transport channel samples                data_q*/
    3310             :     Word32 *tc_digest_f[],             /* o  : samples that will be directly digested (e.g. by CLDFB) data_q*/
    3311             :     Word16 data_q )
    3312             : {
    3313             :     Word16 ch;
    3314             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    3315             :     Word16 n_samples_still_available, m;
    3316             :     Word16 n_ch_full_copy;
    3317             :     Word16 n_ch_res_copy;
    3318             : 
    3319       19405 :     hTcBuffer = st_ivas->hTcBuffer;
    3320       19405 :     n_samples_still_available = sub( hTcBuffer->n_samples_buffered, hTcBuffer->n_samples_rendered );
    3321       19405 :     hTcBuffer->n_samples_buffered = add( add( n_samples_still_available, nSamplesForRendering ), hTcBuffer->n_samples_discard );
    3322       19405 :     hTcBuffer->n_samples_available = i_mult( hTcBuffer->n_samples_granularity, idiv1616( hTcBuffer->n_samples_buffered, hTcBuffer->n_samples_granularity ) );
    3323       19405 :     *nSamplesResidual = sub( hTcBuffer->n_samples_buffered, hTcBuffer->n_samples_available );
    3324       19405 :     move16();
    3325       19405 :     move16();
    3326       19405 :     move16();
    3327       19405 :     n_ch_full_copy = s_min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
    3328       19405 :     n_ch_res_copy = sub( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
    3329             : 
    3330       60104 :     FOR( ch = 0; ch < n_ch_full_copy; ch++ )
    3331             :     {
    3332       40699 :         set32_fx( hTcBuffer->tc_fx[ch], 0, hTcBuffer->n_samples_discard );
    3333       40699 :         Copy32( hTcBuffer->tc_fx[ch] + hTcBuffer->n_samples_rendered, hTcBuffer->tc_fx[ch] + hTcBuffer->n_samples_discard, n_samples_still_available );
    3334    32047796 :         FOR( m = 0; m < nSamplesForRendering; m++ )
    3335             :         {
    3336    32007097 :             hTcBuffer->tc_fx[ch][n_samples_still_available + hTcBuffer->n_samples_discard + m] = data[m * st_ivas->hTcBuffer->nchan_transport_jbm + ch];
    3337    32007097 :             move32();
    3338             :         }
    3339             :     }
    3340             : 
    3341       19405 :     IF( n_ch_res_copy > 0 )
    3342             :     {
    3343        4518 :         FOR( ; ch < hTcBuffer->nchan_transport_jbm; ch++ )
    3344             :         {
    3345        3012 :             Copy32( hTcBuffer->tc_fx[ch], tc_digest_f[ch], n_samples_still_available );
    3346     2894538 :             FOR( m = 0; m < nSamplesForRendering; m++ )
    3347             :             {
    3348     2891526 :                 tc_digest_f[ch][n_samples_still_available + m] = data[( m * st_ivas->hTcBuffer->nchan_transport_jbm ) + ch]; // data_q
    3349     2891526 :                 move32();
    3350             :             }
    3351        3012 :             Copy32( tc_digest_f[ch] + hTcBuffer->n_samples_available, hTcBuffer->tc_fx[ch], *nSamplesResidual );
    3352             :         }
    3353             :     }
    3354       19405 :     hTcBuffer->q_tc_fx = data_q;
    3355       19405 :     move16();
    3356       19405 :     hTcBuffer->n_samples_rendered = 0;
    3357       19405 :     move16();
    3358       19405 :     hTcBuffer->subframes_rendered = 0;
    3359       19405 :     move16();
    3360             : 
    3361       19405 :     return;
    3362             : }
    3363             : 
    3364             : /*--------------------------------------------------------------------------*
    3365             :  * ivas_jbm_dec_get_render_granularity()
    3366             :  *
    3367             :  *
    3368             :  *--------------------------------------------------------------------------*/
    3369             : 
    3370             : /*! r: render granularity */
    3371             : 
    3372             : 
    3373        1907 : Word16 ivas_jbm_dec_get_render_granularity(
    3374             :     const RENDERER_TYPE rendererType, /* i  : renderer type     */
    3375             :     const IVAS_FORMAT ivas_format,    /* i  : ivas format */
    3376             :     const MC_MODE mc_mode,            /* i  : MC mode */
    3377             :     const Word32 output_Fs            /* i  : sampling rate     Q0*/
    3378             : )
    3379             : {
    3380             :     Word16 render_granularity;
    3381             : 
    3382        1907 :     test();
    3383        1907 :     test();
    3384        1907 :     IF( EQ_32( rendererType, RENDERER_BINAURAL_OBJECTS_TD ) || EQ_32( rendererType, RENDERER_BINAURAL_MIXER_CONV ) || EQ_32( rendererType, RENDERER_BINAURAL_MIXER_CONV_ROOM ) )
    3385             :     {
    3386         467 :         test();
    3387         467 :         IF( ( EQ_32( ivas_format, MC_FORMAT ) ) && ( EQ_32( mc_mode, MC_MODE_PARAMUPMIX ) ) )
    3388             :         {
    3389           0 :             render_granularity = NS2SA_FX2( output_Fs, CLDFB_SLOT_NS );
    3390             :         }
    3391             :         ELSE
    3392             :         {
    3393         467 :             render_granularity = NS2SA_FX2( output_Fs, FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES );
    3394             :         }
    3395             :     }
    3396             :     ELSE
    3397             :     {
    3398        1440 :         render_granularity = NS2SA_FX2( output_Fs, CLDFB_SLOT_NS );
    3399             :     }
    3400             : 
    3401        1907 :     return render_granularity;
    3402             : }
    3403             : 
    3404             : 
    3405             : /*--------------------------------------------------------------------------*
    3406             :  * ivas_jbm_dec_tc_buffer_open()
    3407             :  *
    3408             :  * open and initialize JBM transport channel buffer
    3409             :  *--------------------------------------------------------------------------*/
    3410             : 
    3411             : 
    3412         594 : ivas_error ivas_jbm_dec_tc_buffer_open_fx(
    3413             :     Decoder_Struct *st_ivas,               /* i/o: IVAS decoder structure              */
    3414             :     const TC_BUFFER_MODE tc_buffer_mode,   /* i  : buffer mode                         */
    3415             :     const Word16 nchan_transport_jbm,      /* i  : number of real transport channels   */
    3416             :     const Word16 nchan_transport_internal, /* i  : number of totally buffered channels */
    3417             :     const Word16 nchan_full,               /* i  : number of channels to fully store   */
    3418             :     const Word16 n_samples_granularity     /* i  : granularity of the renderer/buffer  */
    3419             : )
    3420             : {
    3421             :     Word16 nsamp_to_allocate;
    3422             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    3423             :     Word16 nMaxSlotsPerSubframe;
    3424             :     Word16 nchan_residual;
    3425             :     Word16 ch_idx;
    3426             :     Word16 tmp, tmp_e;
    3427             :     Word32 tmp32;
    3428             : 
    3429             :     /*-----------------------------------------------------------------*
    3430             :      * prepare library opening
    3431             :      *-----------------------------------------------------------------*/
    3432             : 
    3433         594 :     IF( ( hTcBuffer = (DECODER_TC_BUFFER_HANDLE) malloc( sizeof( DECODER_TC_BUFFER ) ) ) == NULL )
    3434             :     {
    3435           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    3436             :     }
    3437             : 
    3438         594 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    3439         594 :     move16();
    3440         594 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    3441         594 :     move16();
    3442         594 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    3443         594 :     move16();
    3444         594 :     hTcBuffer->nchan_buffer_full = nchan_full;
    3445         594 :     move16();
    3446         594 :     nchan_residual = sub( nchan_transport_internal, nchan_full );
    3447         594 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    3448         594 :     move16();
    3449         594 :     hTcBuffer->n_samples_available = 0;
    3450         594 :     move16();
    3451         594 :     hTcBuffer->n_samples_buffered = 0;
    3452         594 :     move16();
    3453         594 :     hTcBuffer->n_samples_rendered = 0;
    3454         594 :     move16();
    3455         594 :     hTcBuffer->slots_rendered = 0;
    3456         594 :     move16();
    3457         594 :     hTcBuffer->subframes_rendered = 0;
    3458         594 :     move16();
    3459         594 :     hTcBuffer->n_samples_discard = 0;
    3460         594 :     move16();
    3461         594 :     hTcBuffer->n_samples_flushed = 0;
    3462         594 :     move16();
    3463         594 :     hTcBuffer->nb_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
    3464         594 :     move16();
    3465         594 :     nsamp_to_allocate = 0;
    3466         594 :     move16();
    3467             : 
    3468         594 :     tmp32 = L_mult0( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES, hTcBuffer->n_samples_granularity ); // Q0
    3469         594 :     tmp = BASOP_Util_Divide3232_Scale( st_ivas->hDecoderConfig->output_Fs, tmp32, &tmp_e );
    3470         594 :     nMaxSlotsPerSubframe = shr( tmp, sub( 15, tmp_e ) ); // Q0
    3471             : 
    3472         594 :     hTcBuffer->num_slots = mult0( nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
    3473         594 :     move16();
    3474             : 
    3475         594 :     set16_fx( hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    3476         594 :     set16_fx( hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, MAX_PARAM_SPATIAL_SUBFRAMES );
    3477             : 
    3478         594 :     IF( EQ_16( hTcBuffer->tc_buffer_mode, TC_BUFFER_MODE_NONE ) )
    3479             :     {
    3480           0 :         hTcBuffer->tc_buffer_fx = NULL;
    3481             : 
    3482           0 :         FOR( ch_idx = 0; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    3483             :         {
    3484           0 :             hTcBuffer->tc_fx[ch_idx] = NULL;
    3485             :         }
    3486             :     }
    3487             :     ELSE
    3488             :     {
    3489             :         Word16 n_samp_full, n_samp_residual;
    3490             :         Word32 offset;
    3491         594 :         IF( st_ivas->hDecoderConfig->Opt_tsm )
    3492             :         {
    3493          27 :             n_samp_full = add( NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS ), sub( hTcBuffer->n_samples_granularity, 1 ) );
    3494          27 :             n_samp_residual = sub( hTcBuffer->n_samples_granularity, 1 );
    3495             :         }
    3496             :         ELSE
    3497             :         {
    3498             :             /* n_samp_full = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC ); */
    3499         567 :             n_samp_full = extract_l( Mpy_32_32( st_ivas->hDecoderConfig->output_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
    3500         567 :             n_samp_residual = 0;
    3501         567 :             move16();
    3502             :         }
    3503             : 
    3504         594 :         nsamp_to_allocate = mult0( hTcBuffer->nchan_buffer_full, n_samp_full );
    3505         594 :         nsamp_to_allocate = add( nsamp_to_allocate, mult0( nchan_residual, n_samp_residual ) );
    3506             : 
    3507         594 :         IF( nsamp_to_allocate == 0 )
    3508             :         {
    3509          14 :             hTcBuffer->tc_buffer_fx = NULL;
    3510             : 
    3511         238 :             FOR( ch_idx = 0; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    3512             :             {
    3513         224 :                 hTcBuffer->tc_fx[ch_idx] = NULL;
    3514             :             }
    3515             :         }
    3516             :         ELSE
    3517             :         {
    3518         580 :             IF( st_ivas->hDecoderConfig->Opt_tsm )
    3519             :             {
    3520          27 :                 IF( ( hTcBuffer->tc_buffer_fx = (Word32 *) malloc( nsamp_to_allocate * sizeof( Word32 ) ) ) == NULL )
    3521             :                 {
    3522           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM TC Buffer\n" ) );
    3523             :                 }
    3524          27 :                 set32_fx( hTcBuffer->tc_buffer_fx, 0, nsamp_to_allocate );
    3525             : 
    3526          27 :                 offset = 0;
    3527          27 :                 move16();
    3528         125 :                 FOR( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
    3529             :                 {
    3530          98 :                     hTcBuffer->tc_fx[ch_idx] = &hTcBuffer->tc_buffer_fx[offset];
    3531          98 :                     offset = L_add( offset, n_samp_full );
    3532             :                 }
    3533          29 :                 FOR( ; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
    3534             :                 {
    3535           2 :                     hTcBuffer->tc_fx[ch_idx] = &hTcBuffer->tc_buffer_fx[offset];
    3536           2 :                     offset = L_add( offset, n_samp_residual );
    3537             :                 }
    3538         359 :                 FOR( ; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    3539             :                 {
    3540         332 :                     hTcBuffer->tc_fx[ch_idx] = NULL;
    3541             :                 }
    3542             :             }
    3543             :             ELSE
    3544             :             {
    3545         553 :                 hTcBuffer->tc_buffer_fx = NULL;
    3546             :             }
    3547             :         }
    3548             :     }
    3549             : 
    3550         594 :     st_ivas->hTcBuffer = hTcBuffer;
    3551             : 
    3552         594 :     return IVAS_ERR_OK;
    3553             : }
    3554             : 
    3555             : 
    3556             : /*--------------------------------------------------------------------------*
    3557             :  * ivas_jbm_dec_tc_buffer_reconfigure()
    3558             :  *
    3559             :  * open and initialize JBM transport channel buffer
    3560             :  *--------------------------------------------------------------------------*/
    3561             : 
    3562             : 
    3563        3499 : ivas_error ivas_jbm_dec_tc_buffer_reconfigure_fx(
    3564             :     Decoder_Struct *st_ivas,               /* i/o: IVAS decoder structure                  */
    3565             :     const TC_BUFFER_MODE tc_buffer_mode,   /* i  : new buffer mode                         */
    3566             :     const Word16 nchan_transport_jbm,      /* i  : new number of real transport channels   */
    3567             :     const Word16 nchan_transport_internal, /* i  : new number of totally buffered channels */
    3568             :     const Word16 nchan_full,               /* i  : new number of channels to fully store   */
    3569             :     const Word16 n_samples_granularity     /* i  : new granularity of the renderer/buffer  */
    3570             : )
    3571             : {
    3572             :     Word16 nsamp_to_allocate, n_samp_full, n_samp_residual, offset, nchan_residual;
    3573             :     Word16 ch_idx;
    3574             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    3575             : 
    3576        3499 :     hTcBuffer = st_ivas->hTcBuffer;
    3577        3499 :     move16();
    3578             : 
    3579             : 
    3580             :     /* if granularity changes, adapt subframe_nb_slots */
    3581        3499 :     IF( NE_16( n_samples_granularity, hTcBuffer->n_samples_granularity ) )
    3582             :     {
    3583             :         Word16 nMaxSlotsPerSubframeNew;
    3584             : 
    3585         642 :         nMaxSlotsPerSubframeNew = idiv1616( divide3216( st_ivas->hDecoderConfig->output_Fs, ( ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) >> 1 ) ), n_samples_granularity ); // Q0
    3586             :         /* if samples were flushed, take that into account here */
    3587         642 :         test();
    3588         642 :         IF( LT_16( n_samples_granularity, hTcBuffer->n_samples_granularity ) && hTcBuffer->n_samples_flushed > 0 )
    3589             :         {
    3590          22 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = idiv1616( hTcBuffer->n_samples_flushed, n_samples_granularity );
    3591          22 :             hTcBuffer->n_samples_flushed = 0;
    3592          22 :             move16();
    3593          22 :             move16();
    3594             :         }
    3595             :         ELSE
    3596             :         {
    3597         620 :             hTcBuffer->subframe_nbslots[hTcBuffer->subframes_rendered - 1] = nMaxSlotsPerSubframeNew;
    3598         620 :             move16();
    3599             :         }
    3600             :     }
    3601             : 
    3602        3499 :     hTcBuffer->tc_buffer_mode = tc_buffer_mode;
    3603        3499 :     move16();
    3604        3499 :     hTcBuffer->nchan_transport_jbm = nchan_transport_jbm;
    3605        3499 :     move16();
    3606        3499 :     hTcBuffer->nchan_transport_internal = nchan_transport_internal;
    3607        3499 :     move16();
    3608        3499 :     hTcBuffer->nchan_buffer_full = nchan_full;
    3609        3499 :     move16();
    3610        3499 :     nchan_residual = sub( nchan_transport_internal, nchan_full );
    3611        3499 :     hTcBuffer->n_samples_granularity = n_samples_granularity;
    3612        3499 :     move16();
    3613             : 
    3614             :     /* realloc buffers */
    3615        3499 :     IF( hTcBuffer->tc_buffer_fx != NULL )
    3616             :     {
    3617         927 :         free( hTcBuffer->tc_buffer_fx );
    3618         927 :         hTcBuffer->tc_buffer_fx = NULL;
    3619             :     }
    3620             : 
    3621        3499 :     IF( st_ivas->hDecoderConfig->Opt_tsm )
    3622             :     {
    3623         927 :         n_samp_full = ( add( NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS ), sub( hTcBuffer->n_samples_granularity, 1 ) ) );
    3624         927 :         n_samp_residual = sub( hTcBuffer->n_samples_granularity, 1 );
    3625             :     }
    3626             :     ELSE
    3627             :     {
    3628        2572 :         n_samp_full = extract_l( Mult_32_16( st_ivas->hDecoderConfig->output_Fs, INV_FRAME_PER_SEC_Q15 ) );
    3629        2572 :         n_samp_residual = 0;
    3630        2572 :         move16();
    3631             :     }
    3632        3499 :     nsamp_to_allocate = imult1616( hTcBuffer->nchan_buffer_full, n_samp_full );
    3633        3499 :     nsamp_to_allocate = add( nsamp_to_allocate, imult1616( nchan_residual, n_samp_residual ) );
    3634             : 
    3635        3499 :     IF( nsamp_to_allocate == 0 )
    3636             :     {
    3637         292 :         hTcBuffer->tc_buffer_fx = NULL;
    3638        4964 :         FOR( ch_idx = 0; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    3639             :         {
    3640        4672 :             hTcBuffer->tc_fx[ch_idx] = NULL;
    3641             :         }
    3642             :     }
    3643             :     ELSE
    3644             :     {
    3645        3207 :         IF( st_ivas->hDecoderConfig->Opt_tsm )
    3646             :         {
    3647             : 
    3648         927 :             IF( ( hTcBuffer->tc_buffer_fx = (Word32 *) malloc( nsamp_to_allocate * sizeof( Word32 ) ) ) == NULL )
    3649             :             {
    3650           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory FOR JBM TC Buffer\n" ) );
    3651             :             }
    3652         927 :             set32_fx( hTcBuffer->tc_buffer_fx, 0, nsamp_to_allocate );
    3653             : 
    3654         927 :             offset = 0;
    3655         927 :             move16();
    3656        4492 :             FOR( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
    3657             :             {
    3658        3565 :                 hTcBuffer->tc_fx[ch_idx] = &hTcBuffer->tc_buffer_fx[offset];
    3659        3565 :                 offset = add( offset, n_samp_full );
    3660             :             }
    3661         927 :             FOR( ; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
    3662             :             {
    3663           0 :                 hTcBuffer->tc_fx[ch_idx] = &hTcBuffer->tc_buffer_fx[offset];
    3664           0 :                 offset = add( offset, n_samp_residual );
    3665             :             }
    3666       12194 :             FOR( ; ch_idx < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; ch_idx++ )
    3667             :             {
    3668       11267 :                 hTcBuffer->tc_fx[ch_idx] = NULL;
    3669             :             }
    3670             : 
    3671         927 :             hTcBuffer->tc_buff_len = nsamp_to_allocate;
    3672         927 :             move16();
    3673             : 
    3674         927 :             offset = 0;
    3675         927 :             move16();
    3676        4492 :             FOR( ch_idx = 0; ch_idx < hTcBuffer->nchan_buffer_full; ch_idx++ )
    3677             :             {
    3678        3565 :                 offset = add( offset, n_samp_full );
    3679             :             }
    3680         927 :             FOR( ; ch_idx < hTcBuffer->nchan_transport_internal; ch_idx++ )
    3681             :             {
    3682           0 :                 offset = add( offset, n_samp_residual );
    3683             :             }
    3684             :         }
    3685             :         ELSE
    3686             :         {
    3687        2280 :             hTcBuffer->tc_buffer_fx = NULL;
    3688             :         }
    3689             :     }
    3690             : 
    3691        3499 :     return IVAS_ERR_OK;
    3692             : }
    3693             : 
    3694             : 
    3695             : /*--------------------------------------------------------------------------*
    3696             :  * ivas_jbm_dec_tc_buffer_playout()
    3697             :  *
    3698             :  *
    3699             :  *--------------------------------------------------------------------------*/
    3700             : 
    3701      128722 : static void ivas_jbm_dec_tc_buffer_playout_fx(
    3702             :     Decoder_Struct *st_ivas,
    3703             :     const UWord16 nSamplesAsked,
    3704             :     UWord16 *nSamplesRendered,
    3705             :     Word32 *output_fx[] /*Q11*/
    3706             : )
    3707             : {
    3708             :     Word16 ch_idx, slot_size, slots_to_render, first_sf, last_sf, tmp, e;
    3709             : 
    3710      128722 :     slot_size = st_ivas->hTcBuffer->n_samples_granularity;
    3711      128722 :     move16();
    3712             : 
    3713             :     /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
    3714      128722 :     tmp = BASOP_Util_Divide1616_Scale( nSamplesAsked, slot_size, &e );
    3715      128722 :     tmp = shr( tmp, sub( 15, e ) ); // Q0
    3716      128722 :     slots_to_render = s_min( sub( st_ivas->hTcBuffer->num_slots, st_ivas->hTcBuffer->slots_rendered ), tmp );
    3717      128722 :     st_ivas->hTcBuffer->slots_rendered = add( st_ivas->hTcBuffer->slots_rendered, slots_to_render ); // Q0
    3718      128722 :     *nSamplesRendered = (UWord16) L_mult0( slots_to_render, slot_size );
    3719      128722 :     first_sf = st_ivas->hTcBuffer->subframes_rendered;
    3720      128722 :     last_sf = first_sf;
    3721      128722 :     move16();
    3722      128722 :     move16();
    3723      128722 :     move16();
    3724      128722 :     move16();
    3725             : 
    3726      639843 :     WHILE( slots_to_render > 0 )
    3727             :     {
    3728      511121 :         slots_to_render = sub( slots_to_render, st_ivas->hTcBuffer->subframe_nbslots[last_sf] );
    3729      511121 :         last_sf = add( last_sf, 1 );
    3730             :     }
    3731             : 
    3732      374911 :     FOR( ch_idx = 0; ch_idx < st_ivas->hTcBuffer->nchan_transport_jbm; ch_idx++ )
    3733             :     {
    3734      246189 :         Copy32( st_ivas->hTcBuffer->tc_fx[ch_idx] + st_ivas->hTcBuffer->n_samples_rendered, output_fx[ch_idx], *nSamplesRendered );
    3735             :     }
    3736             : 
    3737      128722 :     st_ivas->hTcBuffer->subframes_rendered = last_sf;
    3738      128722 :     move16();
    3739             : 
    3740      128722 :     return;
    3741             : }
    3742             : 
    3743             : 
    3744             : /*--------------------------------------------------------------------------*
    3745             :  * ivas_jbm_dec_tc_buffer_close()
    3746             :  *
    3747             :  * Close JBM transport channel buffer
    3748             :  *--------------------------------------------------------------------------*/
    3749             : 
    3750         604 : void ivas_jbm_dec_tc_buffer_close(
    3751             :     DECODER_TC_BUFFER_HANDLE *phTcBuffer /* i/o: TC buffer handle */
    3752             : )
    3753             : {
    3754             :     Word16 i;
    3755             : 
    3756         604 :     IF( *phTcBuffer != NULL )
    3757             :     {
    3758       10098 :         FOR( i = 0; i < MAX_TRANSPORT_CHANNELS + MAX_NUM_OBJECTS; i++ )
    3759             :         {
    3760        9504 :             ( *phTcBuffer )->tc_fx[i] = NULL;
    3761             :         }
    3762             : 
    3763         594 :         IF( ( *phTcBuffer )->tc_buffer_fx != NULL )
    3764             :         {
    3765          27 :             free( ( *phTcBuffer )->tc_buffer_fx );
    3766          27 :             ( *phTcBuffer )->tc_buffer_fx = NULL;
    3767             :         }
    3768             : 
    3769         594 :         free( *phTcBuffer );
    3770         594 :         *phTcBuffer = NULL;
    3771             :     }
    3772             : 
    3773         604 :     return;
    3774             : }
    3775             : 
    3776             : /*--------------------------------------------------------------------------*
    3777             :  * ivas_jbm_dec_td_renderers_adapt_subframes()
    3778             :  *
    3779             :  * Close JBM transport channel buffer
    3780             :  *--------------------------------------------------------------------------*/
    3781      230248 : void ivas_jbm_dec_td_renderers_adapt_subframes(
    3782             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure      */
    3783             : )
    3784             : {
    3785             :     Word16 nMaxSlotsPerSubframe, nSlotsAvailable, tmp, exp, tmp1, tmp2, s1, s2;
    3786             :     UWord16 nSlotsInLastSubframe, nSlotsInFirstSubframe;
    3787             : 
    3788             :     /* nMaxSlotsPerSubframe = (Word16) ( st_ivas->hDecoderConfig->output_Fs / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) / st_ivas->hTcBuffer->n_samples_granularity; */
    3789      230248 :     tmp = extract_l( Mpy_32_32_r( st_ivas->hDecoderConfig->output_Fs, 10737418 /* 1 / ( FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) in Q31 */ ) );
    3790      230248 :     tmp = BASOP_Util_Divide1616_Scale( tmp, st_ivas->hTcBuffer->n_samples_granularity, &exp );
    3791      230248 :     nMaxSlotsPerSubframe = shr( tmp, sub( 15, exp ) ); // Q0
    3792             : 
    3793             :     /* nSlotsAvailable = st_ivas->hTcBuffer->n_samples_available / st_ivas->hTcBuffer->n_samples_granularity; */
    3794      230248 :     tmp = BASOP_Util_Divide1616_Scale( st_ivas->hTcBuffer->n_samples_available, st_ivas->hTcBuffer->n_samples_granularity, &exp );
    3795      230248 :     nSlotsAvailable = shr( tmp, sub( 15, exp ) ); // Q0
    3796             : 
    3797      230248 :     st_ivas->hTcBuffer->num_slots = nSlotsAvailable;
    3798      230248 :     move16();
    3799             :     /* st_ivas->hTcBuffer->n_samples_available = nSlotsAvailable * st_ivas->hTcBuffer->n_samples_granularity; */
    3800      230248 :     st_ivas->hTcBuffer->n_samples_available = i_mult( nSlotsAvailable, st_ivas->hTcBuffer->n_samples_granularity );
    3801      230248 :     move16();
    3802      230248 :     nSlotsInFirstSubframe = sub( nMaxSlotsPerSubframe, st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] );
    3803      230248 :     st_ivas->hTcBuffer->nb_subframes = 0;
    3804      230248 :     move16();
    3805             : 
    3806      230248 :     IF( nSlotsInFirstSubframe > 0 )
    3807             :     {
    3808        2950 :         st_ivas->hTcBuffer->nb_subframes = 1;
    3809        2950 :         move16();
    3810        2950 :         nSlotsAvailable = sub( nSlotsAvailable, nSlotsInFirstSubframe );
    3811             :     }
    3812             : 
    3813      230248 :     s1 = sub( norm_s( nSlotsAvailable ), 1 );
    3814      230248 :     s2 = norm_s( nMaxSlotsPerSubframe );
    3815             : 
    3816      230248 :     tmp1 = shl( nSlotsAvailable, s1 );
    3817      230248 :     tmp2 = shl( nMaxSlotsPerSubframe, s2 );
    3818             : 
    3819      230248 :     tmp = div_s( tmp1, tmp2 );
    3820      230248 :     st_ivas->hTcBuffer->nb_subframes = add( st_ivas->hTcBuffer->nb_subframes, ceil_fx16( tmp, sub( 15, sub( s2, s1 ) ) ) );
    3821      230248 :     move16();
    3822             : 
    3823      230248 :     nSlotsInLastSubframe = nSlotsAvailable % nMaxSlotsPerSubframe;
    3824      230248 :     move16();
    3825      230248 :     set16_fx( st_ivas->hTcBuffer->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
    3826      230248 :     set16_fx( st_ivas->hTcBuffer->subframe_nbslots, nMaxSlotsPerSubframe, st_ivas->hTcBuffer->nb_subframes );
    3827             : 
    3828      230248 :     if ( nSlotsInFirstSubframe > 0 )
    3829             :     {
    3830        2950 :         st_ivas->hTcBuffer->subframe_nbslots[0] = nSlotsInFirstSubframe;
    3831        2950 :         move16();
    3832             :     }
    3833             : 
    3834      230248 :     if ( nSlotsInLastSubframe > 0 )
    3835             :     {
    3836        2955 :         st_ivas->hTcBuffer->subframe_nbslots[st_ivas->hTcBuffer->nb_subframes - 1] = nSlotsInLastSubframe;
    3837        2955 :         move16();
    3838             :     }
    3839             : 
    3840      230248 :     st_ivas->hTcBuffer->slots_rendered = 0;
    3841      230248 :     move16();
    3842      230248 :     st_ivas->hTcBuffer->subframes_rendered = 0;
    3843      230248 :     move16();
    3844             : 
    3845      230248 :     return;
    3846             : }
    3847             : /*--------------------------------------------------------------------------*
    3848             :  * ivas_jbm_dec_get_tc_buffer_mode()
    3849             :  *
    3850             :  *
    3851             :  *--------------------------------------------------------------------------*/
    3852             : 
    3853        4819 : TC_BUFFER_MODE ivas_jbm_dec_get_tc_buffer_mode(
    3854             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
    3855             : )
    3856             : {
    3857             :     TC_BUFFER_MODE buffer_mode;
    3858        4819 :     buffer_mode = TC_BUFFER_MODE_BUFFER;
    3859        4819 :     move16();
    3860             : 
    3861        4819 :     SWITCH( st_ivas->renderer_type )
    3862             :     {
    3863             :             /* all renderers where we are done after TC decoding (might include DMX to mono/stereo */
    3864         740 :         case RENDERER_DISABLE:
    3865             :         case RENDERER_MCMASA_MONO_STEREO:
    3866             :         case RENDERER_OSBA_STEREO:
    3867             :         case RENDERER_MONO_DOWNMIX:
    3868         740 :             buffer_mode = TC_BUFFER_MODE_BUFFER;
    3869         740 :             move16();
    3870         740 :             BREAK;
    3871        3576 :         case RENDERER_TD_PANNING:
    3872             :         case RENDERER_BINAURAL_OBJECTS_TD:
    3873             :         case RENDERER_BINAURAL_FASTCONV:
    3874             :         case RENDERER_BINAURAL_FASTCONV_ROOM:
    3875             :         case RENDERER_BINAURAL_PARAMETRIC:
    3876             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    3877             :         case RENDERER_STEREO_PARAMETRIC:
    3878             :         case RENDERER_DIRAC:
    3879             :         case RENDERER_PARAM_ISM:
    3880             :         case RENDERER_BINAURAL_MIXER_CONV:
    3881             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
    3882             :         case RENDERER_OSBA_AMBI:
    3883             :         case RENDERER_OSBA_LS:
    3884        3576 :             buffer_mode = TC_BUFFER_MODE_RENDERER;
    3885        3576 :             move16();
    3886        3576 :             BREAK;
    3887           2 :         case RENDERER_NON_DIEGETIC_DOWNMIX:
    3888           2 :             IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) )
    3889             :             {
    3890           1 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    3891           1 :                 move16();
    3892             :             }
    3893             :             ELSE
    3894             :             {
    3895           1 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    3896           1 :                 move16();
    3897             :             }
    3898           2 :             break;
    3899          70 :         case RENDERER_MC_PARAMMC:
    3900          70 :             IF( EQ_32( st_ivas->hParamMC->synthesis_conf, PARAM_MC_SYNTH_MONO_STEREO ) )
    3901             :             {
    3902           2 :                 buffer_mode = TC_BUFFER_MODE_BUFFER; /* TCs are already the DMX to mono or stereo */
    3903           2 :                 move16();
    3904             :             }
    3905             :             ELSE
    3906             :             {
    3907          68 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    3908          68 :                 move16();
    3909             :             }
    3910          70 :             BREAK;
    3911         102 :         case RENDERER_MC:
    3912         102 :             if ( NE_16( ivas_jbm_dec_get_num_tc_channels_fx( st_ivas ), st_ivas->hDecoderConfig->nchan_out ) )
    3913             :             {
    3914          95 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    3915          95 :                 move16();
    3916             :             }
    3917         102 :             BREAK;
    3918         329 :         case RENDERER_SBA_LINEAR_ENC:
    3919         329 :             test();
    3920         329 :             test();
    3921         329 :             test();
    3922         329 :             test();
    3923         329 :             IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) && ( EQ_32( st_ivas->renderer_type, RENDERER_MC ) || EQ_32( st_ivas->renderer_type, RENDERER_SBA_LINEAR_ENC ) ) && GE_16( ( add( st_ivas->hTransSetup.nchan_out_woLFE, st_ivas->hTransSetup.num_lfe ) ), ( add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe ) ) ) )
    3924             :             {
    3925           0 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    3926           0 :                 move16();
    3927             :             }
    3928             :             ELSE
    3929             :             {
    3930         329 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    3931         329 :                 move16();
    3932             :             }
    3933         329 :             BREAK;
    3934           0 :         case RENDERER_SBA_LINEAR_DEC:
    3935           0 :             test();
    3936           0 :             test();
    3937           0 :             IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) && ( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) ) )
    3938             :             {
    3939           0 :                 buffer_mode = TC_BUFFER_MODE_BUFFER;
    3940           0 :                 move16();
    3941             :             }
    3942             :             ELSE
    3943             :             {
    3944           0 :                 buffer_mode = TC_BUFFER_MODE_RENDERER;
    3945           0 :                 move16();
    3946             :             }
    3947           0 :             BREAK;
    3948             :     }
    3949             : 
    3950        4819 :     return buffer_mode;
    3951             : }
    3952             : 
    3953             : 
    3954             : /*--------------------------------------------------------------------------*
    3955             :  * ivas_jbm_dec_copy_tc_no_tsm()
    3956             :  *
    3957             :  *
    3958             :  *--------------------------------------------------------------------------*/
    3959             : 
    3960      380574 : void ivas_jbm_dec_copy_tc_no_tsm_fx(
    3961             :     Decoder_Struct *st_ivas,  /* i/o: IVAS decoder handle        Q0  */
    3962             :     Word32 *tc_fx[],          /* i  : transport channels         Q11 */
    3963             :     const Word16 output_frame /* i  : output frame size          Q0  */
    3964             : )
    3965             : {
    3966             :     Word16 Q_tc;
    3967             :     Word16 n_ch_full_copy;
    3968             :     Word16 n_ch_cldfb;
    3969             :     Word16 ch_idx;
    3970             :     DECODER_TC_BUFFER_HANDLE hTcBuffer;
    3971             : 
    3972      380574 :     hTcBuffer = st_ivas->hTcBuffer;
    3973      380574 :     hTcBuffer->n_samples_buffered = output_frame;
    3974      380574 :     hTcBuffer->n_samples_available = hTcBuffer->n_samples_buffered;
    3975      380574 :     move16();
    3976      380574 :     move16();
    3977      380574 :     n_ch_full_copy = s_min( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
    3978      380574 :     n_ch_cldfb = sub( hTcBuffer->nchan_transport_jbm, hTcBuffer->nchan_buffer_full );
    3979             : 
    3980      380574 :     Q_tc = 11; /*tc buffer is at Q11*/
    3981      380574 :     move16();
    3982             :     /* copy full tcs*/
    3983      380574 :     IF( st_ivas->hDecoderConfig->Opt_tsm )
    3984             :     {
    3985           0 :         FOR( ch_idx = 0; ch_idx < n_ch_full_copy; ch_idx++ )
    3986             :         {
    3987           0 :             Copy32( tc_fx[ch_idx], st_ivas->hTcBuffer->tc_fx[ch_idx], hTcBuffer->n_samples_buffered ); // keeping tc_fx in Q11
    3988             :         }
    3989             :     }
    3990      380574 :     ch_idx = 0;
    3991      380574 :     move16();
    3992             : 
    3993             :     /* CLDFB ana for ParamMC/ParamISM */
    3994      380574 :     IF( n_ch_cldfb > 0 )
    3995             :     {
    3996             :         Word32 *cldfb_real_buffer_fx;
    3997             :         Word32 *cldfb_imag_buffer_fx;
    3998             :         Word16 cldfb_ch, slot_idx, num_freq_bands;
    3999             : 
    4000       12507 :         cldfb_real_buffer_fx = NULL;
    4001       12507 :         cldfb_imag_buffer_fx = NULL;
    4002       12507 :         num_freq_bands = 0;
    4003       12507 :         move16();
    4004             : 
    4005       12507 :         IF( EQ_16( (Word16) st_ivas->ivas_format, (Word16) ISM_FORMAT ) )
    4006             :         {
    4007        3747 :             cldfb_real_buffer_fx = st_ivas->hParamIsmDec->hParamIsmRendering->Cldfb_RealBuffer_tc_fx;
    4008        3747 :             cldfb_imag_buffer_fx = st_ivas->hParamIsmDec->hParamIsmRendering->Cldfb_ImagBuffer_tc_fx;
    4009        3747 :             num_freq_bands = st_ivas->hSpatParamRendCom->num_freq_bands;
    4010        3747 :             move16();
    4011             : 
    4012        3747 :             ivas_ism_param_dec_tc_gain_ajust_fx( st_ivas, output_frame, output_frame / 2, tc_fx, &Q_tc );
    4013        3747 :             Scale_sig32( tc_fx[0], output_frame, sub( Q11, Q_tc ) ); /*keepeing tc_fx at Q11*/
    4014        3747 :             Scale_sig32( tc_fx[1], output_frame, sub( Q11, Q_tc ) ); /*keepeing tc_fx at Q11*/
    4015             :         }
    4016        8760 :         ELSE IF( EQ_16( (Word16) st_ivas->ivas_format, (Word16) MC_FORMAT ) )
    4017             :         {
    4018        8760 :             cldfb_real_buffer_fx = st_ivas->hParamMC->Cldfb_RealBuffer_tc_fx;
    4019        8760 :             cldfb_imag_buffer_fx = st_ivas->hParamMC->Cldfb_ImagBuffer_tc_fx;
    4020        8760 :             num_freq_bands = st_ivas->hParamMC->num_freq_bands;
    4021        8760 :             move16();
    4022             :         }
    4023             :         /* CLDFB Analysis*/
    4024             : 
    4025       37561 :         FOR( cldfb_ch = 0; cldfb_ch < n_ch_cldfb; ( cldfb_ch++, ch_idx++ ) )
    4026             :         {
    4027      425918 :             FOR( slot_idx = 0; slot_idx < DEFAULT_JBM_CLDFB_TIMESLOTS; slot_idx++ )
    4028             :             {
    4029      400864 :                 Q_tc = 11;
    4030      400864 :                 move16();
    4031      400864 :                 cldfbAnalysis_ts_fx_fixed_q( &( tc_fx[ch_idx][num_freq_bands * slot_idx] ),
    4032      400864 :                                              &cldfb_real_buffer_fx[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
    4033      400864 :                                              &cldfb_imag_buffer_fx[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
    4034             :                                              num_freq_bands, st_ivas->cldfbAnaDec[cldfb_ch], &Q_tc );
    4035             :             }
    4036             :         }
    4037             :     }
    4038      380574 :     hTcBuffer->n_samples_rendered = 0;
    4039      380574 :     move16();
    4040      380574 :     hTcBuffer->subframes_rendered = 0;
    4041      380574 :     move16();
    4042             : 
    4043      380574 :     return;
    4044             : }
    4045             : 
    4046             : /*--------------------------------------------------------------------------*
    4047             :  * ivas_jbm_dec_metadata_open()
    4048             :  *
    4049             :  * Open structure for metadata buffering in JBM
    4050             :  *--------------------------------------------------------------------------*/
    4051             : 
    4052          44 : ivas_error ivas_jbm_dec_metadata_open(
    4053             :     Decoder_Struct *st_ivas )
    4054             : {
    4055             :     JBM_METADATA_HANDLE hJbmMetadata;
    4056          44 :     IF( ( hJbmMetadata = (JBM_METADATA_HANDLE) malloc( sizeof( JBM_METADATA ) ) ) == NULL )
    4057             :     {
    4058           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for JBM metadata handle\n" ) );
    4059             :     }
    4060          44 :     hJbmMetadata->sf_write_idx = 0;
    4061          44 :     move16();
    4062          44 :     hJbmMetadata->sf_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_PARAM_SPATIAL_SUBFRAMES;
    4063          44 :     move16();
    4064             : 
    4065          44 :     hJbmMetadata->slot_write_idx = 0;
    4066          44 :     move16();
    4067          44 :     hJbmMetadata->slot_read_idx = 0;
    4068          44 :     move16();
    4069          44 :     hJbmMetadata->slot_md_buffer_length = MASA_JBM_RINGBUFFER_FRAMES * MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
    4070          44 :     move16();
    4071             : 
    4072          44 :     st_ivas->hJbmMetadata = hJbmMetadata;
    4073             : 
    4074          44 :     return IVAS_ERR_OK;
    4075             : }
    4076             : 
    4077             : 
    4078             : /*--------------------------------------------------------------------------*
    4079             :  * ivas_jbm_dec_copy_masa_meta_to_buffer()
    4080             :  *
    4081             :  * Copy decoded MASA metadata to a ring buffer
    4082             :  *--------------------------------------------------------------------------*/
    4083             : 
    4084        5462 : static void ivas_jbm_dec_copy_masa_meta_to_buffer(
    4085             :     Decoder_Struct *st_ivas )
    4086             : {
    4087             :     Word16 sf, dir, band;
    4088             :     JBM_METADATA_HANDLE hJbmMetadata;
    4089             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    4090             :     Word16 write_idx;
    4091             : 
    4092        5462 :     hJbmMetadata = st_ivas->hJbmMetadata;
    4093        5462 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    4094             : 
    4095       27310 :     FOR( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    4096             :     {
    4097       21848 :         write_idx = ( hJbmMetadata->sf_write_idx + sf ) % hJbmMetadata->sf_md_buffer_length;
    4098       21848 :         move16();
    4099       65544 :         FOR( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    4100             :         {
    4101     1092400 :             FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    4102             :             {
    4103     1048704 :                 hJbmMetadata->directionIndexBuffer[dir][write_idx][band] = extOutMeta->directionIndex[dir][sf][band];
    4104     1048704 :                 move16();
    4105     1048704 :                 hJbmMetadata->directToTotalRatioBuffer[dir][write_idx][band] = extOutMeta->directToTotalRatio[dir][sf][band];
    4106     1048704 :                 move16();
    4107     1048704 :                 hJbmMetadata->spreadCoherenceBuffer[dir][write_idx][band] = extOutMeta->spreadCoherence[dir][sf][band];
    4108     1048704 :                 move16();
    4109             :             }
    4110             :         }
    4111             : 
    4112      546200 :         FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    4113             :         {
    4114      524352 :             hJbmMetadata->diffuseToTotalRatioBuffer[write_idx][band] = extOutMeta->diffuseToTotalRatio[sf][band];
    4115      524352 :             move16();
    4116      524352 :             hJbmMetadata->surroundCoherenceBuffer[write_idx][band] = extOutMeta->surroundCoherence[sf][band];
    4117      524352 :             move16();
    4118             :         }
    4119             : 
    4120       21848 :         hJbmMetadata->numberOfDirections[write_idx] = extOutMeta->descriptiveMeta.numberOfDirections;
    4121       21848 :         move16();
    4122             :     }
    4123             : 
    4124        5462 :     return;
    4125             : }
    4126             : 
    4127             : 
    4128             : /*--------------------------------------------------------------------------*
    4129             :  * ivas_jbm_masa_sf_to_slot_map()
    4130             :  *
    4131             :  * Map input MASA metadata subframes to slots in JBM processing
    4132             :  *--------------------------------------------------------------------------*/
    4133        5462 : static void ivas_jbm_masa_sf_to_slot_map(
    4134             :     Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure     */
    4135             :     const Word16 nCldfbTs    /* i  : number of CLDFB time slots */
    4136             : )
    4137             : {
    4138             :     Word16 sf_to_slot_map[MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME];
    4139             :     Word16 num_slots_in_subfr;
    4140             :     JBM_METADATA_HANDLE hJbmMetadata;
    4141             :     Word16 slot_idx;
    4142             :     Word16 write_idx, sf_index;
    4143             : 
    4144             :     /* Set values */
    4145        5462 :     hJbmMetadata = st_ivas->hJbmMetadata;
    4146        5462 :     num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
    4147        5462 :     move16();
    4148             : 
    4149             :     /* Map input subframes to slots */
    4150        5462 :     ivas_jbm_dec_get_md_map_even_spacing( nCldfbTs, num_slots_in_subfr, 0, MAX_PARAM_SPATIAL_SUBFRAMES, sf_to_slot_map );
    4151             : 
    4152       92874 :     FOR( slot_idx = 0; slot_idx < nCldfbTs; slot_idx++ )
    4153             :     {
    4154       87412 :         write_idx = ( add( hJbmMetadata->slot_write_idx, slot_idx ) ) % hJbmMetadata->slot_md_buffer_length;
    4155       87412 :         move16();
    4156       87412 :         sf_index = ( add( hJbmMetadata->sf_write_idx, sf_to_slot_map[slot_idx] ) ) % hJbmMetadata->sf_md_buffer_length;
    4157       87412 :         move16();
    4158             : 
    4159       87412 :         hJbmMetadata->sf_to_slot_map[write_idx] = sf_index;
    4160       87412 :         move16();
    4161             :     }
    4162             : 
    4163        5462 :     hJbmMetadata->sf_write_idx = ( add( hJbmMetadata->sf_write_idx, MAX_PARAM_SPATIAL_SUBFRAMES ) ) % hJbmMetadata->sf_md_buffer_length;
    4164        5462 :     move16();
    4165        5462 :     hJbmMetadata->slot_write_idx = ( add( hJbmMetadata->slot_write_idx, nCldfbTs ) ) % hJbmMetadata->slot_md_buffer_length;
    4166        5462 :     move16();
    4167             : 
    4168        5462 :     return;
    4169             : }
    4170             : 
    4171             : /*--------------------------------------------------------------------------*
    4172             :  * ivas_jbm_masa_sf_to_sf_map()
    4173             :  *
    4174             :  * Map input MASA metadata subframes to output subframes in JBM processing
    4175             :  *--------------------------------------------------------------------------*/
    4176             : 
    4177        1308 : void ivas_jbm_masa_sf_to_sf_map(
    4178             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
    4179             : )
    4180             : {
    4181             :     Word16 sf_to_sf_map[MAX_PARAM_SPATIAL_SUBFRAMES];
    4182             :     JBM_METADATA_HANDLE hJbmMetadata;
    4183             :     MASA_DECODER_EXT_OUT_META *extOutMeta;
    4184             :     Word16 slot_read_idx, sf_read_idx;
    4185             :     Word16 sf_idx;
    4186             :     Word16 dir, band;
    4187             :     UWord8 numberOfDirections;
    4188             : 
    4189             :     /* Set values */
    4190        1308 :     hJbmMetadata = st_ivas->hJbmMetadata;
    4191        1308 :     extOutMeta = st_ivas->hMasa->data.extOutMeta;
    4192             : 
    4193             :     /* Map slots to subframes */
    4194        6540 :     FOR( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    4195             :     {
    4196        5232 :         slot_read_idx = ( add( hJbmMetadata->slot_read_idx, 2 ) ) % hJbmMetadata->slot_md_buffer_length; /* Take the latter one of the middle slots of the output subframe */
    4197        5232 :         move16();
    4198        5232 :         sf_to_sf_map[sf_idx] = hJbmMetadata->sf_to_slot_map[slot_read_idx];
    4199        5232 :         move16();
    4200        5232 :         hJbmMetadata->slot_read_idx = ( add( hJbmMetadata->slot_read_idx, CLDFB_SLOTS_PER_SUBFRAME ) ) % hJbmMetadata->slot_md_buffer_length;
    4201        5232 :         move16();
    4202             :     }
    4203             : 
    4204             :     /* Copy mapped metadata to the EXT meta buffer for writing */
    4205        6540 :     FOR( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    4206             :     {
    4207        5232 :         sf_read_idx = sf_to_sf_map[sf_idx];
    4208        5232 :         move16();
    4209             : 
    4210       15696 :         FOR( dir = 0; dir < MASA_MAXIMUM_DIRECTIONS; dir++ )
    4211             :         {
    4212      261600 :             FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    4213             :             {
    4214      251136 :                 extOutMeta->directionIndex[dir][sf_idx][band] = hJbmMetadata->directionIndexBuffer[dir][sf_read_idx][band];
    4215      251136 :                 move16();
    4216      251136 :                 extOutMeta->directToTotalRatio[dir][sf_idx][band] = hJbmMetadata->directToTotalRatioBuffer[dir][sf_read_idx][band];
    4217      251136 :                 move16();
    4218      251136 :                 extOutMeta->spreadCoherence[dir][sf_idx][band] = hJbmMetadata->spreadCoherenceBuffer[dir][sf_read_idx][band];
    4219      251136 :                 move16();
    4220             :             }
    4221             :         }
    4222             : 
    4223      130800 :         FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    4224             :         {
    4225      125568 :             extOutMeta->diffuseToTotalRatio[sf_idx][band] = hJbmMetadata->diffuseToTotalRatioBuffer[sf_read_idx][band];
    4226      125568 :             move16();
    4227      125568 :             extOutMeta->surroundCoherence[sf_idx][band] = hJbmMetadata->surroundCoherenceBuffer[sf_read_idx][band];
    4228      125568 :             move16();
    4229             :         }
    4230             :     }
    4231             : 
    4232             :     /* Determine the number of directions for the frame to be written */
    4233        1308 :     numberOfDirections = 0;
    4234        1308 :     move16();
    4235        5594 :     FOR( sf_idx = 0; sf_idx < MAX_PARAM_SPATIAL_SUBFRAMES; sf_idx++ )
    4236             :     {
    4237        4524 :         sf_read_idx = sf_to_sf_map[sf_idx];
    4238        4524 :         move16();
    4239             : 
    4240        4524 :         IF( hJbmMetadata->numberOfDirections[sf_read_idx] == 1 )
    4241             :         {
    4242         238 :             numberOfDirections = 1;
    4243         238 :             move16();
    4244         238 :             BREAK;
    4245             :         }
    4246             :     }
    4247             : 
    4248        1308 :     extOutMeta->descriptiveMeta.numberOfDirections = numberOfDirections;
    4249        1308 :     move16();
    4250             : 
    4251        1308 :     return;
    4252             : }

Generated by: LCOV version 1.14