LCOV - code coverage report
Current view: top level - lib_rend - ivas_dirac_rend_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 2202 2371 92.9 %
Date: 2025-06-27 02:59:36 Functions: 23 23 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "cnst.h"
      38             : #include "prot_fx.h"
      39             : #include "ivas_prot_rend_fx.h"
      40             : #include "ivas_cnst.h"
      41             : #include "ivas_rom_rend.h"
      42             : #include "wmc_auto.h"
      43             : #include "ivas_prot_fx.h"
      44             : #include "ivas_rom_binaural_crend_head.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------------
      48             :  * ivas_dirac_allocate_parameters()
      49             :  *
      50             :  * Allocate and initialize DirAC parameters
      51             :  *-------------------------------------------------------------------------*/
      52             : 
      53         987 : ivas_error ivas_dirac_allocate_parameters_fx(
      54             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering */
      55             :     const Word16 params_flag                              /* i  : set of parameters flag    */
      56             : )
      57             : {
      58             :     Word16 i;
      59             : 
      60         987 :     IF( EQ_16( params_flag, 1 ) )
      61             :     {
      62         596 :         IF( ( hSpatParamRendCom->azimuth = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
      63             :         {
      64           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      65             :         }
      66             : 
      67         596 :         IF( ( hSpatParamRendCom->elevation = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
      68             :         {
      69           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      70             :         }
      71             : 
      72         596 :         IF( ( hSpatParamRendCom->diffuseness_vector_fx = (Word32 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word32 * ) ) ) == NULL )
      73             :         {
      74           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      75             :         }
      76             : 
      77         596 :         IF( ( hSpatParamRendCom->energy_ratio1_fx = (Word32 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word32 * ) ) ) == NULL )
      78             :         {
      79           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      80             :         }
      81             : 
      82         596 :         IF( ( hSpatParamRendCom->spreadCoherence_fx = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
      83             :         {
      84           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      85             :         }
      86             : 
      87         596 :         IF( ( hSpatParamRendCom->surroundingCoherence_fx = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
      88             :         {
      89           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      90             :         }
      91             : 
      92        3496 :         FOR( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
      93             :         {
      94        2900 :             IF( ( hSpatParamRendCom->azimuth[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
      95             :             {
      96           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
      97             :             }
      98        2900 :             set16_fx( hSpatParamRendCom->azimuth[i], 0, hSpatParamRendCom->num_freq_bands );
      99             : 
     100        2900 :             IF( ( hSpatParamRendCom->elevation[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     101             :             {
     102           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     103             :             }
     104        2900 :             set16_fx( hSpatParamRendCom->elevation[i], 0, hSpatParamRendCom->num_freq_bands );
     105             : 
     106        2900 :             IF( ( hSpatParamRendCom->diffuseness_vector_fx[i] = (Word32 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word32 ) ) ) == NULL )
     107             :             {
     108           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     109             :             }
     110        2900 :             set32_fx( hSpatParamRendCom->diffuseness_vector_fx[i], ONE_IN_Q30, hSpatParamRendCom->num_freq_bands ); // q30
     111             : 
     112        2900 :             IF( ( hSpatParamRendCom->energy_ratio1_fx[i] = (Word32 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word32 ) ) ) == NULL )
     113             :             {
     114           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     115             :             }
     116        2900 :             set32_fx( hSpatParamRendCom->energy_ratio1_fx[i], 0, hSpatParamRendCom->num_freq_bands );
     117             : 
     118        2900 :             IF( ( hSpatParamRendCom->spreadCoherence_fx[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     119             :             {
     120           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     121             :             }
     122        2900 :             set16_fx( hSpatParamRendCom->spreadCoherence_fx[i], 0, hSpatParamRendCom->num_freq_bands );
     123             : 
     124        2900 :             IF( ( hSpatParamRendCom->surroundingCoherence_fx[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     125             :             {
     126           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     127             :             }
     128        2900 :             set16_fx( hSpatParamRendCom->surroundingCoherence_fx[i], 0, hSpatParamRendCom->num_freq_bands );
     129             :         }
     130             :     }
     131         391 :     ELSE IF( EQ_16( params_flag, 2 ) )
     132             :     {
     133         391 :         IF( ( hSpatParamRendCom->azimuth2 = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
     134             :         {
     135           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     136             :         }
     137             : 
     138         391 :         IF( ( hSpatParamRendCom->elevation2 = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
     139             :         {
     140           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     141             :         }
     142             : 
     143         391 :         IF( ( hSpatParamRendCom->energy_ratio2_fx = (Word32 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word32 * ) ) ) == NULL )
     144             :         {
     145           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     146             :         }
     147             : 
     148         391 :         IF( ( hSpatParamRendCom->spreadCoherence2_fx = (Word16 **) malloc( hSpatParamRendCom->dirac_md_buffer_length * sizeof( Word16 * ) ) ) == NULL )
     149             :         {
     150           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     151             :         }
     152             : 
     153        2449 :         FOR( i = 0; i < hSpatParamRendCom->dirac_md_buffer_length; i++ )
     154             :         {
     155        2058 :             IF( ( hSpatParamRendCom->azimuth2[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     156             :             {
     157           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     158             :             }
     159        2058 :             set16_fx( hSpatParamRendCom->azimuth2[i], 0, hSpatParamRendCom->num_freq_bands );
     160             : 
     161        2058 :             IF( ( hSpatParamRendCom->elevation2[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     162             :             {
     163           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     164             :             }
     165        2058 :             set16_fx( hSpatParamRendCom->elevation2[i], 0, hSpatParamRendCom->num_freq_bands );
     166             : 
     167        2058 :             IF( ( hSpatParamRendCom->energy_ratio2_fx[i] = (Word32 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word32 ) ) ) == NULL )
     168             :             {
     169           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     170             :             }
     171        2058 :             set32_fx( hSpatParamRendCom->energy_ratio2_fx[i], 0, hSpatParamRendCom->num_freq_bands );
     172             : 
     173        2058 :             IF( ( hSpatParamRendCom->spreadCoherence2_fx[i] = (Word16 *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( Word16 ) ) ) == NULL )
     174             :             {
     175           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
     176             :             }
     177        2058 :             set16_fx( hSpatParamRendCom->spreadCoherence2_fx[i], 0, hSpatParamRendCom->num_freq_bands );
     178             :         }
     179             :     }
     180             : 
     181         987 :     return IVAS_ERR_OK;
     182             : }
     183             : 
     184             : 
     185             : /*-------------------------------------------------------------------------
     186             :  * ivas_spat_hSpatParamRendCom_config()
     187             :  *
     188             :  *
     189             :  *-------------------------------------------------------------------------*/
     190             : 
     191        4289 : ivas_error ivas_spat_hSpatParamRendCom_config_fx(
     192             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out, /* i/o: IVAS decoder structure  */
     193             :     const DIRAC_CONFIG_FLAG flag_config_inp,                   /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
     194             :     const Word16 dec_param_estim_flag,
     195             :     const IVAS_FORMAT ivas_format,
     196             :     const MC_MODE mc_mode,
     197             :     const Word32 output_Fs,
     198             :     const Word16 hodirac_flag,
     199             :     const Word16 masa_ext_rend_flag )
     200             : {
     201             :     ivas_error error;
     202             :     Word16 map_idx;
     203             :     DIRAC_CONFIG_FLAG flag_config;
     204             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
     205             : 
     206        4289 :     IF( EQ_32( flag_config_inp, DIRAC_RECONFIGURE_MODE ) )
     207        1019 :     flag_config = DIRAC_RECONFIGURE;
     208             :     ELSE
     209        3270 :         flag_config = flag_config_inp;
     210        4289 :     move32();
     211        4289 :     error = IVAS_ERR_OK;
     212        4289 :     move32();
     213             : 
     214        4289 :     hSpatParamRendCom = NULL;
     215             : 
     216        4289 :     IF( EQ_32( flag_config, DIRAC_RECONFIGURE ) )
     217             :     {
     218        3791 :         hSpatParamRendCom = *hSpatParamRendCom_out;
     219             :     }
     220         498 :     ELSE IF( flag_config == DIRAC_OPEN )
     221             :     {
     222             :         /*-----------------------------------------------------------------*
     223             :          * prepare library opening
     224             :          *-----------------------------------------------------------------*/
     225             : 
     226         498 :         IF( ( hSpatParamRendCom = (SPAT_PARAM_REND_COMMON_DATA_HANDLE) malloc( sizeof( SPAT_PARAM_REND_COMMON_DATA ) ) ) == NULL )
     227             :         {
     228           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC meta\n" ) );
     229             :         }
     230             : 
     231         498 :         *hSpatParamRendCom_out = hSpatParamRendCom;
     232             :     }
     233             : 
     234        4289 :     IF( EQ_32( flag_config, DIRAC_OPEN ) )
     235             :     {
     236             :         /* hSpatParamRendCom->slot_size = (int16_t) ( ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX ); */
     237         498 :         hSpatParamRendCom->slot_size = extract_l( Mpy_32_32( output_Fs, 2684355 /* 1 / ( FRAMES_PER_SEC * CLDFB_NO_COL_MAX) in Q31 */ ) ); // Q0+Q31-Q31=>Q0
     238         498 :         move16();
     239         498 :         set16_fx( hSpatParamRendCom->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
     240         498 :         set16_fx( hSpatParamRendCom->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
     241         498 :         hSpatParamRendCom->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
     242         498 :         move16();
     243         498 :         hSpatParamRendCom->subframes_rendered = 0;
     244         498 :         move16();
     245         498 :         hSpatParamRendCom->slots_rendered = 0;
     246         498 :         move16();
     247         498 :         hSpatParamRendCom->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
     248         498 :         move16();
     249             :         /* hSpatParamRendCom->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f ); */
     250         498 :         hSpatParamRendCom->num_freq_bands = extract_l( Mpy_32_32( output_Fs, 2684355 /* INV_CLDFB_BANDWIDTH in Q31 */ ) ); // 0+31-31=>0
     251         498 :         move16();
     252         498 :         hSpatParamRendCom->numSimultaneousDirections = 0;
     253         498 :         move16();
     254         498 :         hSpatParamRendCom->numParametricDirections = 0;
     255         498 :         move16();
     256         498 :         hSpatParamRendCom->numIsmDirections = 0;
     257         498 :         move16();
     258             :     }
     259             : 
     260             :     /*-----------------------------------------------------------------*
     261             :      * set input parameters
     262             :      *-----------------------------------------------------------------*/
     263             : 
     264        4289 :     test();
     265        4289 :     test();
     266        4289 :     IF( ( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, SBA_ISM_FORMAT ) ) && EQ_32( flag_config, DIRAC_RECONFIGURE ) )
     267             :     {
     268        1019 :         test();
     269        1019 :         test();
     270        1019 :         IF( hodirac_flag && hSpatParamRendCom->azimuth2 == NULL )
     271             :         {
     272          86 :             IF( NE_32( ( error = ivas_dirac_allocate_parameters_fx( hSpatParamRendCom, 2 ) ), IVAS_ERR_OK ) )
     273             :             {
     274           0 :                 return error;
     275             :             }
     276             :         }
     277         933 :         ELSE IF( !hodirac_flag && hSpatParamRendCom->azimuth2 != NULL )
     278             :         {
     279          79 :             ivas_dirac_deallocate_parameters_fx( hSpatParamRendCom, 2 );
     280             :         }
     281             :     }
     282             : 
     283        4289 :     IF( flag_config == DIRAC_OPEN )
     284             :     {
     285         498 :         hSpatParamRendCom->dirac_md_buffer_length = 0;
     286         498 :         move16();
     287         498 :         hSpatParamRendCom->dirac_bs_md_write_idx = 0;
     288         498 :         move16();
     289         498 :         hSpatParamRendCom->dirac_read_idx = 0;
     290         498 :         move16();
     291         498 :         test();
     292         498 :         test();
     293         498 :         IF( EQ_32( mc_mode, MC_MODE_MCMASA ) || EQ_16( masa_ext_rend_flag, 1 ) )
     294             :         {
     295         240 :             hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES;
     296         240 :             move16();
     297             : 
     298         240 :             set16_fx( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     299        1200 :             FOR( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
     300             :             {
     301         960 :                 hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
     302         960 :                 move16();
     303             :             }
     304             :         }
     305         258 :         ELSE IF( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) )
     306             :         {
     307         137 :             hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_MASA_PARAM_DEC_SFR;
     308         137 :             move16();
     309         137 :             hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_MASA_PARAM_DEC_SFR;
     310         137 :             move16();
     311             : 
     312         137 :             set16_fx( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     313         685 :             FOR( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS; map_idx++ )
     314             :             {
     315         548 :                 hSpatParamRendCom->render_to_md_map[map_idx] = map_idx;
     316         548 :                 move16();
     317             :             }
     318             :         }
     319             :         ELSE
     320             :         {
     321             :             Word16 num_slots_in_subfr, tmp;
     322         121 :             tmp = 1;
     323         121 :             move16();
     324         121 :             IF( dec_param_estim_flag )
     325          50 :             num_slots_in_subfr = CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES;
     326             :             ELSE
     327          71 :                 num_slots_in_subfr = 1;
     328         121 :             move16();
     329         121 :             hSpatParamRendCom->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_DIRAC_PARAM_DEC_SFR;
     330         121 :             move16();
     331         121 :             hSpatParamRendCom->dirac_bs_md_write_idx = DELAY_DIRAC_PARAM_DEC_SFR;
     332         121 :             move16();
     333         121 :             hSpatParamRendCom->dirac_read_idx = 0;
     334         121 :             move16();
     335             : 
     336         121 :             set16_fx( hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
     337             : 
     338         121 :             if ( EQ_16( num_slots_in_subfr, CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES ) )
     339             :             {
     340          50 :                 tmp = 2;
     341          50 :                 move16();
     342             :             }
     343             : 
     344        1205 :             FOR( map_idx = 0; map_idx < DEFAULT_JBM_SUBFRAMES_5MS * num_slots_in_subfr; map_idx++ )
     345             :             {
     346        1084 :                 hSpatParamRendCom->render_to_md_map[map_idx] = add( hSpatParamRendCom->dirac_read_idx, shr( map_idx, tmp ) );
     347        1084 :                 move16();
     348             :             }
     349             :         }
     350             : 
     351         498 :         IF( NE_32( ( error = ivas_dirac_allocate_parameters_fx( hSpatParamRendCom, 1 ) ), IVAS_ERR_OK ) )
     352             :         {
     353           0 :             return error;
     354             :         }
     355             : 
     356         498 :         test();
     357         498 :         test();
     358         498 :         test();
     359         498 :         test();
     360         498 :         IF( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || ( ( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, SBA_ISM_FORMAT ) ) && hodirac_flag ) )
     361             :         {
     362         207 :             IF( NE_32( ( error = ivas_dirac_allocate_parameters_fx( hSpatParamRendCom, 2 ) ), IVAS_ERR_OK ) )
     363             :             {
     364           0 :                 return error;
     365             :             }
     366             :         }
     367             :         ELSE
     368             :         {
     369         291 :             hSpatParamRendCom->azimuth2 = NULL;
     370         291 :             hSpatParamRendCom->elevation2 = NULL;
     371         291 :             hSpatParamRendCom->energy_ratio2_fx = NULL;
     372         291 :             hSpatParamRendCom->spreadCoherence2_fx = NULL;
     373             :         }
     374             :     }
     375             : 
     376        4289 :     return error;
     377             : }
     378             : 
     379             : 
     380             : /*-------------------------------------------------------------------------
     381             :  * ivas_spat_hSpatParamRendCom_close()
     382             :  *
     383             :  *
     384             :  *-------------------------------------------------------------------------*/
     385             : 
     386        1074 : void ivas_spat_hSpatParamRendCom_close_fx(
     387             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE *hSpatParamRendCom_out )
     388             : {
     389        1074 :     test();
     390        1074 :     IF( hSpatParamRendCom_out == NULL || *hSpatParamRendCom_out == NULL )
     391             :     {
     392         576 :         return;
     393             :     }
     394         498 :     ivas_dirac_deallocate_parameters_fx( *hSpatParamRendCom_out, 1 );
     395         498 :     ivas_dirac_deallocate_parameters_fx( *hSpatParamRendCom_out, 2 );
     396             : 
     397         498 :     free( *hSpatParamRendCom_out );
     398         498 :     *hSpatParamRendCom_out = NULL;
     399             : 
     400         498 :     return;
     401             : }
     402             : 
     403             : 
     404             : /*-------------------------------------------------------------------------
     405             :  * ivas_dirac_rend_close()
     406             :  *
     407             :  *
     408             :  *-------------------------------------------------------------------------*/
     409             : 
     410        3168 : void ivas_dirac_rend_close_fx(
     411             :     DIRAC_REND_HANDLE *hDirACRend_out )
     412             : {
     413             :     Word16 i, j;
     414             :     DIRAC_REND_HANDLE hDirACRend;
     415             : 
     416        3168 :     test();
     417        3168 :     IF( hDirACRend_out == NULL || *hDirACRend_out == NULL )
     418             :     {
     419        2781 :         return;
     420             :     }
     421             : 
     422         387 :     hDirACRend = *hDirACRend_out;
     423             : 
     424             :     /* close Output synthesis sub-module */
     425         387 :     ivas_dirac_dec_output_synthesis_close_fx( hDirACRend );
     426             : 
     427             :     /* close Decorrelator sub-module */
     428         387 :     IF( hDirACRend->proto_signal_decorr_on )
     429             :     {
     430         326 :         ivas_dirac_dec_decorr_close_fx( &hDirACRend->h_freq_domain_decorr_ap_params, &hDirACRend->h_freq_domain_decorr_ap_state );
     431             :     }
     432             : 
     433             :     /* Params */
     434             : 
     435             :     /* free frequency axis buffer */
     436         387 :     IF( hDirACRend->frequency_axis_fx != NULL )
     437             :     {
     438         387 :         free( hDirACRend->frequency_axis_fx );
     439         387 :         hDirACRend->frequency_axis_fx = NULL;
     440             :     }
     441             : 
     442         387 :     IF( hDirACRend->diffuse_response_function_fx != NULL )
     443             :     {
     444         387 :         free( hDirACRend->diffuse_response_function_fx );
     445         387 :         hDirACRend->diffuse_response_function_fx = NULL;
     446             :     }
     447             : 
     448         387 :     IF( hDirACRend->hoa_encoder_fx != NULL )
     449             :     {
     450          15 :         free( hDirACRend->hoa_encoder_fx );
     451          15 :         hDirACRend->hoa_encoder_fx = NULL;
     452             :     }
     453             : 
     454         387 :     IF( hDirACRend->hoa_encoder_fx != NULL )
     455             :     {
     456           0 :         free( hDirACRend->hoa_encoder_fx );
     457           0 :         hDirACRend->hoa_encoder_fx = NULL;
     458             :     }
     459             : 
     460             :     /* prototype indexing */
     461         387 :     IF( hDirACRend->proto_index_dir != NULL )
     462             :     {
     463         387 :         free( hDirACRend->proto_index_dir );
     464         387 :         hDirACRend->proto_index_dir = NULL;
     465             :     }
     466             : 
     467         387 :     IF( hDirACRend->proto_index_diff != NULL )
     468             :     {
     469         387 :         free( hDirACRend->proto_index_diff );
     470         387 :         hDirACRend->proto_index_dir = NULL;
     471             :     }
     472             : 
     473             :     /* States */
     474             : 
     475             :     /* free prototype signal buffers */
     476         387 :     IF( hDirACRend->proto_frame_f_fx != NULL )
     477             :     {
     478         208 :         free( hDirACRend->proto_frame_f_fx );
     479         208 :         hDirACRend->proto_frame_f_fx = NULL;
     480             :     }
     481             : 
     482             : 
     483        1548 :     FOR( i = 0; i < DIRAC_NUM_DIMS; i++ )
     484             :     {
     485       38313 :         FOR( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
     486             :         {
     487       37152 :             IF( hDirACRend->buffer_intensity_real_fx[i][j] != NULL )
     488             :             {
     489       12384 :                 free( hDirACRend->buffer_intensity_real_fx[i][j] );
     490       12384 :                 hDirACRend->buffer_intensity_real_fx[i][j] = NULL;
     491             :             }
     492             :         }
     493             :     }
     494         387 :     IF( hDirACRend->buffer_energy_fx != NULL )
     495             :     {
     496         129 :         free( hDirACRend->buffer_energy_fx );
     497         129 :         hDirACRend->buffer_energy_fx = NULL;
     498             :     }
     499             : 
     500         387 :     IF( hDirACRend->masa_stereo_type_detect != NULL )
     501             :     {
     502          66 :         free( hDirACRend->masa_stereo_type_detect );
     503          66 :         hDirACRend->masa_stereo_type_detect = NULL;
     504             :     }
     505             : 
     506         387 :     ivas_dirac_free_mem_fx( &( hDirACRend->stack_mem ) );
     507             : 
     508         387 :     free( *hDirACRend_out );
     509         387 :     *hDirACRend_out = NULL;
     510             : 
     511         387 :     return;
     512             : }
     513             : 
     514             : 
     515             : /*-------------------------------------------------------------------------
     516             :  * ivas_dirac_deallocate_parameters()
     517             :  *
     518             :  * Deallocate DirAC parameters
     519             :  *-------------------------------------------------------------------------*/
     520             : 
     521        1271 : void ivas_dirac_deallocate_parameters_fx(
     522             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, /* i/o: common data for spatial parametric rendering  */
     523             :     const Word16 params_flag                              /* i  : set of parameters flag                        */
     524             : )
     525             : {
     526             :     Word16 i;
     527             :     Word16 md_buffer_length;
     528             : 
     529        1271 :     IF( hSpatParamRendCom == NULL )
     530             :     {
     531           0 :         return;
     532             :     }
     533             : 
     534        1271 :     md_buffer_length = hSpatParamRendCom->dirac_md_buffer_length;
     535        1271 :     move16();
     536             : 
     537        1271 :     IF( EQ_16( params_flag, 1 ) )
     538             :     {
     539         596 :         IF( hSpatParamRendCom->azimuth != NULL )
     540             :         {
     541        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     542             :             {
     543        2900 :                 IF( hSpatParamRendCom->azimuth[i] != NULL )
     544             :                 {
     545        2900 :                     free( hSpatParamRendCom->azimuth[i] );
     546        2900 :                     hSpatParamRendCom->azimuth[i] = NULL;
     547             :                 }
     548             :             }
     549             : 
     550         596 :             free( hSpatParamRendCom->azimuth );
     551         596 :             hSpatParamRendCom->azimuth = NULL;
     552             :         }
     553             : 
     554         596 :         IF( hSpatParamRendCom->elevation != NULL )
     555             :         {
     556        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     557             :             {
     558        2900 :                 IF( hSpatParamRendCom->elevation[i] != NULL )
     559             :                 {
     560        2900 :                     free( hSpatParamRendCom->elevation[i] );
     561        2900 :                     hSpatParamRendCom->elevation[i] = NULL;
     562             :                 }
     563             :             }
     564             : 
     565         596 :             free( hSpatParamRendCom->elevation );
     566         596 :             hSpatParamRendCom->elevation = NULL;
     567             :         }
     568             : 
     569         596 :         IF( hSpatParamRendCom->energy_ratio1_fx != NULL )
     570             :         {
     571        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     572             :             {
     573        2900 :                 IF( hSpatParamRendCom->energy_ratio1_fx[i] != NULL )
     574             :                 {
     575        2900 :                     free( hSpatParamRendCom->energy_ratio1_fx[i] );
     576        2900 :                     hSpatParamRendCom->energy_ratio1_fx[i] = NULL;
     577             :                 }
     578             :             }
     579         596 :             free( hSpatParamRendCom->energy_ratio1_fx );
     580         596 :             hSpatParamRendCom->energy_ratio1_fx = NULL;
     581             :         }
     582             : 
     583         596 :         IF( hSpatParamRendCom->diffuseness_vector_fx != NULL )
     584             :         {
     585        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     586             :             {
     587        2900 :                 IF( hSpatParamRendCom->diffuseness_vector_fx[i] != NULL )
     588             :                 {
     589        2900 :                     free( hSpatParamRendCom->diffuseness_vector_fx[i] );
     590        2900 :                     hSpatParamRendCom->diffuseness_vector_fx[i] = NULL;
     591             :                 }
     592             :             }
     593             : 
     594         596 :             free( hSpatParamRendCom->diffuseness_vector_fx );
     595         596 :             hSpatParamRendCom->diffuseness_vector_fx = NULL;
     596             :         }
     597             : 
     598         596 :         IF( hSpatParamRendCom->spreadCoherence_fx != NULL )
     599             :         {
     600        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     601             :             {
     602        2900 :                 IF( hSpatParamRendCom->spreadCoherence_fx[i] != NULL )
     603             :                 {
     604        2900 :                     free( hSpatParamRendCom->spreadCoherence_fx[i] );
     605        2900 :                     hSpatParamRendCom->spreadCoherence_fx[i] = NULL;
     606             :                 }
     607             :             }
     608         596 :             free( hSpatParamRendCom->spreadCoherence_fx );
     609         596 :             hSpatParamRendCom->spreadCoherence_fx = NULL;
     610             :         }
     611             : 
     612         596 :         IF( hSpatParamRendCom->surroundingCoherence_fx != NULL )
     613             :         {
     614        3496 :             FOR( i = 0; i < md_buffer_length; i++ )
     615             :             {
     616        2900 :                 IF( hSpatParamRendCom->surroundingCoherence_fx[i] != NULL )
     617             :                 {
     618        2900 :                     free( hSpatParamRendCom->surroundingCoherence_fx[i] );
     619        2900 :                     hSpatParamRendCom->surroundingCoherence_fx[i] = NULL;
     620             :                 }
     621             :             }
     622         596 :             free( hSpatParamRendCom->surroundingCoherence_fx );
     623         596 :             hSpatParamRendCom->surroundingCoherence_fx = NULL;
     624             :         }
     625             :     }
     626         675 :     ELSE IF( EQ_16( params_flag, 2 ) )
     627             :     {
     628         675 :         IF( hSpatParamRendCom->azimuth2 != NULL )
     629             :         {
     630        2449 :             FOR( i = 0; i < md_buffer_length; i++ )
     631             :             {
     632        2058 :                 IF( hSpatParamRendCom->azimuth2[i] != NULL )
     633             :                 {
     634        2058 :                     free( hSpatParamRendCom->azimuth2[i] );
     635        2058 :                     hSpatParamRendCom->azimuth2[i] = NULL;
     636             :                 }
     637             :             }
     638         391 :             free( hSpatParamRendCom->azimuth2 );
     639         391 :             hSpatParamRendCom->azimuth2 = NULL;
     640             :         }
     641             : 
     642         675 :         IF( hSpatParamRendCom->elevation2 != NULL )
     643             :         {
     644        2449 :             FOR( i = 0; i < md_buffer_length; i++ )
     645             :             {
     646        2058 :                 IF( hSpatParamRendCom->elevation2[i] != NULL )
     647             :                 {
     648        2058 :                     free( hSpatParamRendCom->elevation2[i] );
     649        2058 :                     hSpatParamRendCom->elevation2[i] = NULL;
     650             :                 }
     651             :             }
     652         391 :             free( hSpatParamRendCom->elevation2 );
     653         391 :             hSpatParamRendCom->elevation2 = NULL;
     654             :         }
     655             : 
     656         675 :         IF( hSpatParamRendCom->energy_ratio2_fx != NULL )
     657             :         {
     658        2449 :             FOR( i = 0; i < md_buffer_length; i++ )
     659             :             {
     660        2058 :                 IF( hSpatParamRendCom->energy_ratio2_fx[i] != NULL )
     661             :                 {
     662        2058 :                     free( hSpatParamRendCom->energy_ratio2_fx[i] );
     663        2058 :                     hSpatParamRendCom->energy_ratio2_fx[i] = NULL;
     664             :                 }
     665             :             }
     666         391 :             free( hSpatParamRendCom->energy_ratio2_fx );
     667         391 :             hSpatParamRendCom->energy_ratio2_fx = NULL;
     668             :         }
     669             : 
     670         675 :         IF( hSpatParamRendCom->spreadCoherence2_fx != NULL )
     671             :         {
     672        2449 :             FOR( i = 0; i < md_buffer_length; i++ )
     673             :             {
     674        2058 :                 IF( hSpatParamRendCom->spreadCoherence2_fx[i] != NULL )
     675             :                 {
     676        2058 :                     free( hSpatParamRendCom->spreadCoherence2_fx[i] );
     677        2058 :                     hSpatParamRendCom->spreadCoherence2_fx[i] = NULL;
     678             :                 }
     679             :             }
     680         391 :             free( hSpatParamRendCom->spreadCoherence2_fx );
     681         391 :             hSpatParamRendCom->spreadCoherence2_fx = NULL;
     682             :         }
     683             :     }
     684             : 
     685        1271 :     return;
     686             : }
     687             : 
     688             : 
     689             : /*-------------------------------------------------------------------------
     690             :  * ivas_dirac_alloc_mem()
     691             :  *
     692             :  * Allocate stack memory for DirAC renderer
     693             :  *------------------------------------------------------------------------*/
     694             : 
     695        2171 : ivas_error ivas_dirac_alloc_mem_fx(
     696             :     DIRAC_REND_HANDLE hDirACRend,
     697             :     const RENDERER_TYPE renderer_type,
     698             :     const Word16 num_freq_bands,
     699             :     DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem,
     700             :     const Word16 hodirac_flag )
     701             : {
     702             :     Word16 num_freq_bands_diff, size;
     703             :     Word16 size_ho;
     704             :     Word16 size_pf;
     705             :     Word16 num_outputs_dir, num_outputs_diff;
     706             :     Word16 num_protos_dir;
     707             : 
     708        2171 :     num_protos_dir = hDirACRend->num_protos_dir;
     709        2171 :     move16();
     710             : 
     711        2171 :     num_freq_bands_diff = hDirACRend->h_output_synthesis_psd_params.max_band_decorr;
     712        2171 :     move16();
     713             : 
     714        2171 :     num_outputs_dir = hDirACRend->num_outputs_dir;
     715        2171 :     move16();
     716        2171 :     num_outputs_diff = hDirACRend->num_outputs_diff;
     717        2171 :     move16();
     718             : 
     719        2171 :     size = imult1616( num_freq_bands, num_outputs_dir );
     720        2171 :     IF( hodirac_flag )
     721             :     {
     722         112 :         size_ho = imult1616( size, DIRAC_HO_NUMSECTORS );
     723         112 :         size_pf = imult1616( num_freq_bands, DIRAC_HO_NUMSECTORS );
     724             :     }
     725             :     ELSE
     726             :     {
     727        2059 :         size_ho = size;
     728        2059 :         move16();
     729        2059 :         size_pf = num_freq_bands;
     730        2059 :         move16();
     731             :     }
     732             : 
     733             :     /* PSD related buffers */
     734        2171 :     hDirAC_mem->cy_auto_dir_smooth_fx = NULL;
     735        2171 :     hDirAC_mem->proto_power_smooth_fx = NULL;
     736        2171 :     hDirAC_mem->proto_power_diff_smooth_fx = NULL;
     737        2171 :     hDirAC_mem->direct_responses_square_fx = NULL;
     738        2171 :     hDirAC_mem->frame_dec_f_fx = NULL;
     739        2171 :     IF( NE_32( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
     740             :     {
     741        1358 :         IF( ( hDirAC_mem->cy_auto_dir_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * size ) ) == NULL )
     742             :         {
     743           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     744             :         }
     745        1358 :         set_zero_fx( hDirAC_mem->cy_auto_dir_smooth_fx, size );
     746        1358 :         hDirACRend->h_output_synthesis_psd_state.cy_auto_dir_smooth_len = size;
     747        1358 :         move16();
     748             : 
     749        1358 :         IF( ( hDirAC_mem->proto_power_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * size ) ) == NULL )
     750             :         {
     751           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     752             :         }
     753        1358 :         set_zero_fx( hDirAC_mem->proto_power_smooth_fx, size );
     754        1358 :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_len = size;
     755        1358 :         move16();
     756             : 
     757        1358 :         IF( ( hDirAC_mem->proto_power_diff_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * size ) ) == NULL )
     758             :         {
     759           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     760             :         }
     761        1358 :         set_zero_fx( hDirAC_mem->proto_power_diff_smooth_fx, size );
     762        1358 :         hDirAC_mem->proto_power_diff_smooth_len = size;
     763        1358 :         move16();
     764        1358 :         hDirAC_mem->proto_power_diff_smooth_q = Q31;
     765        1358 :         move16();
     766             : 
     767        1358 :         IF( ( hDirAC_mem->direct_responses_square_fx = (Word32 *) malloc( sizeof( Word32 ) * size ) ) == NULL )
     768             :         {
     769           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     770             :         }
     771        1358 :         set32_fx( hDirAC_mem->direct_responses_square_fx, 0, size );
     772        1358 :         test();
     773        1358 :         test();
     774        1358 :         test();
     775        1358 :         IF( hDirACRend->proto_signal_decorr_on && ( NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) && NE_32( renderer_type, RENDERER_STEREO_PARAMETRIC ) ) )
     776             :         {
     777        1200 :             IF( ( hDirAC_mem->frame_dec_f_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * num_outputs_diff * num_freq_bands ) ) == NULL )
     778             :             {
     779           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     780             :             }
     781        1200 :             set_zero_fx( hDirAC_mem->frame_dec_f_fx, 2 * num_outputs_diff * num_freq_bands );
     782        1200 :             hDirAC_mem->frame_dec_f_len = imult1616( imult1616( 2, num_outputs_diff ), num_freq_bands );
     783             :         }
     784             :     }
     785        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx = hDirAC_mem->proto_power_smooth_fx;
     786             : #ifdef FIX_867_CLDFB_NRG_SCALE
     787        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0] = Q31;
     788        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] = Q31;
     789        2171 :     move16();
     790        2171 :     move16();
     791        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] = Q31;
     792        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] = Q31;
     793        2171 :     move16();
     794        2171 :     move16();
     795             : #else
     796             :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q = Q31;
     797             :     move16();
     798             :     hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q = Q31;
     799             :     move16();
     800             : #endif
     801        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_fx = hDirAC_mem->proto_power_diff_smooth_fx;
     802        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q = hDirAC_mem->proto_power_diff_smooth_q;
     803        2171 :     move16();
     804        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_len = hDirAC_mem->proto_power_diff_smooth_len;
     805        2171 :     move16();
     806        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q = Q31;
     807        2171 :     move16();
     808        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q = Q31;
     809        2171 :     move16();
     810        2171 :     hDirACRend->h_output_synthesis_psd_state.cy_auto_dir_smooth_fx = hDirAC_mem->cy_auto_dir_smooth_fx;
     811        2171 :     hDirACRend->h_output_synthesis_psd_state.q_cy_auto_dir_smooth = 0;
     812        2171 :     move16();
     813        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_square_fx = hDirAC_mem->direct_responses_square_fx;
     814        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_square_q = Q31;
     815        2171 :     move16();
     816        2171 :     move16();
     817             : 
     818             :     /* Target and smoothed nrg factors/gains */
     819        2171 :     IF( ( hDirAC_mem->cy_cross_dir_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * size_ho ) ) == NULL )
     820             :     {
     821           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     822             :     }
     823        2171 :     hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_len = size_ho;
     824        2171 :     move16();
     825        2171 :     set_zero_fx( hDirAC_mem->cy_cross_dir_smooth_fx, size_ho );
     826             : 
     827        2171 :     IF( NE_32( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
     828             :     {
     829        1358 :         IF( ( hDirAC_mem->cy_auto_diff_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * size ) ) == NULL )
     830             :         {
     831           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     832             :         }
     833        1358 :         set_zero_fx( hDirAC_mem->cy_auto_diff_smooth_fx, size );
     834        1358 :         hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_len = size;
     835        1358 :         move16();
     836             :     }
     837             :     ELSE
     838             :     {
     839         813 :         IF( ( hDirAC_mem->cy_auto_diff_smooth_fx = (Word32 *) malloc( sizeof( Word32 ) * num_outputs_diff * num_freq_bands_diff ) ) == NULL )
     840             :         {
     841           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     842             :         }
     843         813 :         set_zero_fx( hDirAC_mem->cy_auto_diff_smooth_fx, imult1616( num_outputs_diff, num_freq_bands_diff ) );
     844         813 :         hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_len = imult1616( num_outputs_diff, num_freq_bands_diff );
     845             :     }
     846        2171 :     hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_fx = hDirAC_mem->cy_cross_dir_smooth_fx;
     847        2171 :     hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth = 31;
     848        2171 :     move16();
     849        2171 :     hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_fx = hDirAC_mem->cy_auto_diff_smooth_fx;
     850        2171 :     hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth = 0;
     851        2171 :     move16();
     852             : 
     853             :     /*Responses (gains/factors)*/
     854        2171 :     IF( ( hDirAC_mem->direct_responses_fx = (Word32 *) malloc( sizeof( Word32 ) * size_ho ) ) == NULL )
     855             :     {
     856           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     857             :     }
     858        2171 :     set32_fx( hDirAC_mem->direct_responses_fx, 0, size );
     859             : 
     860             : 
     861        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_fx = hDirAC_mem->direct_responses_fx;
     862        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_q = Q31;
     863        2171 :     move16();
     864             : 
     865             :     /* Prototypes */
     866        2171 :     hDirAC_mem->proto_direct_buffer_f_fx = NULL;
     867        2171 :     hDirAC_mem->proto_diffuse_buffer_f_fx = NULL;
     868        2171 :     test();
     869        2171 :     test();
     870        2171 :     IF( NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) && NE_32( renderer_type, RENDERER_STEREO_PARAMETRIC ) )
     871             :     {
     872        2171 :         IF( ( hDirAC_mem->proto_direct_buffer_f_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_protos_dir * num_freq_bands ) ) == NULL )
     873             :         {
     874           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     875             :         }
     876        2171 :         hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len = imult1616( imult1616( 2 * MAX_PARAM_SPATIAL_SUBFRAMES, num_protos_dir ), num_freq_bands );
     877        2171 :         set_zero_fx( hDirAC_mem->proto_direct_buffer_f_fx, hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len );
     878        2171 :         move16();
     879        2171 :         hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q = Q31;
     880        2171 :         move16();
     881             : 
     882        2171 :         IF( hDirACRend->proto_signal_decorr_on )
     883             :         {
     884        2013 :             IF( EQ_32( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_PSD_SHD ) )
     885             :             {
     886         412 :                 IF( ( hDirAC_mem->proto_diffuse_buffer_f_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * size ) ) == NULL )
     887             :                 {
     888           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     889             :                 }
     890         412 :                 hDirAC_mem->proto_diffuse_buffer_f_len = imult1616( 2 * MAX_PARAM_SPATIAL_SUBFRAMES, size );
     891         412 :                 move16();
     892             :             }
     893             :             ELSE
     894             :             {
     895        1601 :                 IF( ( hDirAC_mem->proto_diffuse_buffer_f_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * MAX_PARAM_SPATIAL_SUBFRAMES * num_outputs_diff * num_freq_bands ) ) == NULL )
     896             :                 {
     897           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     898             :                 }
     899        1601 :                 hDirAC_mem->proto_diffuse_buffer_f_len = imult1616( imult1616( 2 * MAX_PARAM_SPATIAL_SUBFRAMES, num_outputs_diff ), num_freq_bands );
     900             :             }
     901        2013 :             set_zero_fx( hDirAC_mem->proto_diffuse_buffer_f_fx, hDirAC_mem->proto_diffuse_buffer_f_len );
     902             :         }
     903             :     }
     904        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx = hDirAC_mem->proto_direct_buffer_f_fx;
     905        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx = hDirAC_mem->proto_diffuse_buffer_f_fx;
     906        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_len = hDirAC_mem->proto_diffuse_buffer_f_len;
     907        2171 :     move16();
     908        2171 :     hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q = Q31;
     909        2171 :     move16();
     910             : 
     911             :     /* Gains/power factors*/
     912        2171 :     hDirAC_mem->direct_power_factor_fx = NULL;
     913        2171 :     hDirAC_mem->diffuse_power_factor_fx = NULL;
     914             : 
     915        2171 :     test();
     916        2171 :     test();
     917        2171 :     IF( NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) && NE_32( renderer_type, RENDERER_STEREO_PARAMETRIC ) )
     918             :     {
     919        2171 :         IF( ( hDirAC_mem->direct_power_factor_fx = (Word32 *) malloc( sizeof( Word32 ) * size_pf ) ) == NULL )
     920             :         {
     921           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     922             :         }
     923        2171 :         IF( ( hDirAC_mem->diffuse_power_factor_fx = (Word32 *) malloc( sizeof( Word32 ) * size_pf ) ) == NULL )
     924             :         {
     925           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     926             :         }
     927             :     }
     928             : 
     929        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx = hDirAC_mem->direct_power_factor_fx;
     930        2171 :     hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx = hDirAC_mem->diffuse_power_factor_fx;
     931        2171 :     hDirACRend->h_output_synthesis_psd_state.diff_dir_power_factor_len = size_pf;
     932        2171 :     set_zero_fx( hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx, size_pf );
     933        2171 :     set_zero_fx( hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx, size_pf );
     934        2171 :     hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q = Q31;
     935        2171 :     move16();
     936        2171 :     hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q = Q31;
     937        2171 :     move16();
     938             : 
     939        2171 :     hDirAC_mem->reference_power_fx = NULL;
     940        2171 :     hDirAC_mem->onset_filter_fx = NULL;
     941        2171 :     IF( NE_32( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
     942             :     {
     943        1358 :         test();
     944        1358 :         test();
     945        1358 :         IF( NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && NE_32( renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) && NE_32( renderer_type, RENDERER_STEREO_PARAMETRIC ) )
     946             :         {
     947        1358 :             IF( ( hDirAC_mem->reference_power_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * num_freq_bands ) ) == NULL )
     948             :             {
     949           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     950             :             }
     951             : 
     952        1358 :             hDirAC_mem->reference_power_len = imult1616( 2, num_freq_bands );
     953        1358 :             move16();
     954             : #ifdef FIX_867_CLDFB_NRG_SCALE
     955        1358 :             hDirAC_mem->reference_power_q[0] = Q31;
     956        1358 :             hDirAC_mem->reference_power_q[1] = Q31;
     957        1358 :             move16();
     958        1358 :             move16();
     959        1358 :             hDirAC_mem->reference_power_smooth_q[0] = Q31;
     960        1358 :             hDirAC_mem->reference_power_smooth_q[1] = Q31;
     961        1358 :             move16();
     962        1358 :             move16();
     963             : #else
     964             :             hDirAC_mem->reference_power_q = Q31;
     965             :             move16();
     966             :             hDirAC_mem->reference_power_smooth_q = Q31;
     967             :             move16();
     968             : #endif
     969        1358 :             IF( hDirACRend->proto_signal_decorr_on )
     970             :             {
     971        1200 :                 IF( ( hDirAC_mem->onset_filter_fx = (Word32 *) malloc( sizeof( Word32 ) * num_outputs_diff * num_freq_bands ) ) == NULL )
     972             :                 {
     973           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     974             :                 }
     975        1200 :                 set_zero_fx( hDirAC_mem->onset_filter_fx, imult1616( num_outputs_diff, num_freq_bands ) );
     976             :             }
     977             :         }
     978             :     }
     979             :     ELSE
     980             :     {
     981         813 :         IF( GT_16( num_protos_dir, 2 ) )
     982             :         {
     983         804 :             IF( ( hDirAC_mem->reference_power_fx = (Word32 *) malloc( sizeof( Word32 ) * 5 * num_freq_bands ) ) == NULL )
     984             :             {
     985           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
     986             :             }
     987             : 
     988         804 :             hDirAC_mem->reference_power_len = imult1616( 5, num_freq_bands );
     989             : #ifdef FIX_867_CLDFB_NRG_SCALE
     990         804 :             set16_fx( hDirAC_mem->reference_power_q, Q31, 2 );
     991             : #else
     992             :             hDirAC_mem->reference_power_q = Q31;
     993             :             move16();
     994             : #endif
     995             :         }
     996             : 
     997         813 :         IF( hDirACRend->proto_signal_decorr_on )
     998             :         {
     999         813 :             IF( ( hDirAC_mem->onset_filter_fx = (Word32 *) malloc( sizeof( Word32 ) * 2 * num_freq_bands ) ) == NULL )
    1000             :             {
    1001           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate stack memory for DirAC\n" ) );
    1002             :             }
    1003         813 :             set_zero_fx( hDirAC_mem->onset_filter_fx, imult1616( 2, num_freq_bands ) );
    1004             :         }
    1005             :     }
    1006             : 
    1007        2171 :     return IVAS_ERR_OK;
    1008             : }
    1009             : 
    1010             : 
    1011             : /*-------------------------------------------------------------------------
    1012             :  * ivas_dirac_free_mem()
    1013             :  *
    1014             :  *
    1015             :  *-------------------------------------------------------------------------*/
    1016             : 
    1017        2171 : void ivas_dirac_free_mem_fx(
    1018             :     DIRAC_DEC_STACK_MEM_HANDLE hDirAC_mem )
    1019             : {
    1020        2171 :     IF( hDirAC_mem->cy_auto_dir_smooth_fx != NULL )
    1021             :     {
    1022        1358 :         free( hDirAC_mem->cy_auto_dir_smooth_fx );
    1023             :     }
    1024        2171 :     IF( hDirAC_mem->proto_power_smooth_fx != NULL )
    1025             :     {
    1026        1358 :         free( hDirAC_mem->proto_power_smooth_fx );
    1027             :     }
    1028        2171 :     IF( hDirAC_mem->proto_power_diff_smooth_fx != NULL )
    1029             :     {
    1030        1358 :         free( hDirAC_mem->proto_power_diff_smooth_fx );
    1031             :     }
    1032        2171 :     IF( hDirAC_mem->direct_responses_square_fx != NULL )
    1033             :     {
    1034        1358 :         free( hDirAC_mem->direct_responses_square_fx );
    1035             :     }
    1036        2171 :     IF( hDirAC_mem->frame_dec_f_fx != NULL )
    1037             :     {
    1038        1200 :         free( hDirAC_mem->frame_dec_f_fx );
    1039             :     }
    1040        2171 :     IF( hDirAC_mem->cy_cross_dir_smooth_fx != NULL )
    1041             :     {
    1042        2171 :         free( hDirAC_mem->cy_cross_dir_smooth_fx );
    1043             :     }
    1044        2171 :     IF( hDirAC_mem->cy_auto_diff_smooth_fx != NULL )
    1045             :     {
    1046        2171 :         free( hDirAC_mem->cy_auto_diff_smooth_fx );
    1047             :     }
    1048        2171 :     IF( hDirAC_mem->direct_responses_fx != NULL )
    1049             :     {
    1050        2171 :         free( hDirAC_mem->direct_responses_fx );
    1051             :     }
    1052        2171 :     IF( hDirAC_mem->proto_direct_buffer_f_fx != NULL )
    1053             :     {
    1054        2171 :         free( hDirAC_mem->proto_direct_buffer_f_fx );
    1055             :     }
    1056        2171 :     IF( hDirAC_mem->proto_diffuse_buffer_f_fx != NULL )
    1057             :     {
    1058        2013 :         free( hDirAC_mem->proto_diffuse_buffer_f_fx );
    1059             :     }
    1060        2171 :     IF( hDirAC_mem->direct_power_factor_fx != NULL )
    1061             :     {
    1062        2171 :         free( hDirAC_mem->direct_power_factor_fx );
    1063             :     }
    1064        2171 :     IF( hDirAC_mem->diffuse_power_factor_fx != NULL )
    1065             :     {
    1066        2171 :         free( hDirAC_mem->diffuse_power_factor_fx );
    1067             :     }
    1068        2171 :     IF( hDirAC_mem->reference_power_fx != NULL )
    1069             :     {
    1070        2162 :         free( hDirAC_mem->reference_power_fx );
    1071             :     }
    1072        2171 :     IF( hDirAC_mem->onset_filter_fx != NULL )
    1073             :     {
    1074        2013 :         free( hDirAC_mem->onset_filter_fx );
    1075             :     }
    1076             : 
    1077        2171 :     return;
    1078             : }
    1079             : 
    1080             : 
    1081             : /*-------------------------------------------------------------------------
    1082             :  * compute_hoa_encoder_mtx()
    1083             :  *
    1084             :  *
    1085             :  *------------------------------------------------------------------------*/
    1086             : 
    1087         424 : void compute_hoa_encoder_mtx_fx(
    1088             :     const Word32 *azimuth,   /*q22*/
    1089             :     const Word32 *elevation, /*q22*/
    1090             :     Word32 *response_fx,     /*q31*/
    1091             :     const Word16 num_responses,
    1092             :     const Word16 ambisonics_order )
    1093             : {
    1094             :     Word16 k, num_sh;
    1095             : 
    1096         424 :     num_sh = ivas_sba_get_nchan_fx( ambisonics_order, 0 );
    1097        3806 :     FOR( k = 0; k < num_responses; k++ )
    1098             :     {
    1099        3382 :         ivas_dirac_dec_get_response_fx( shr( extract_h( azimuth[k] ), Q22 - Q16 ), shr( extract_h( elevation[k] ), Q22 - Q16 ), &response_fx[k * num_sh], ambisonics_order, Q31 ); /* Q31 */
    1100             :     }
    1101             : 
    1102         424 :     return;
    1103             : }
    1104             : 
    1105             : 
    1106             : /*-------------------------------------------------------------------------
    1107             :  * ivas_dirac_dec_get_frequency_axis()
    1108             :  *
    1109             :  * DirAC decoding initialization
    1110             :  *------------------------------------------------------------------------*/
    1111             : 
    1112        1532 : void ivas_dirac_dec_get_frequency_axis_fx(
    1113             :     Word16 *frequency_axis, /* Q0 */
    1114             :     const Word32 output_Fs,
    1115             :     const Word16 num_freq_bands )
    1116             : {
    1117             :     Word16 k, const_part, scale;
    1118             :     /* calc cldfb frequency axis */
    1119             : 
    1120        1532 :     const_part = BASOP_Util_Divide3216_Scale( output_Fs, shl( num_freq_bands, 1 ), &scale ); // 15-(scale+31-15)=>-1-scale
    1121        1532 :     const_part = shr( const_part, sub( -1, scale ) );                                        // q(-1-scale)->q0
    1122       78572 :     FOR( k = 0; k < num_freq_bands; k++ )
    1123             :     {
    1124             :         /* frequency_axis[k] = ((float)k + 0.5f) * const_part; */
    1125       77040 :         frequency_axis[k] = add( i_mult( k, const_part ), shr( const_part, 1 ) ); /*q0*/
    1126       77040 :         move16();
    1127             :     }
    1128             : 
    1129        1532 :     return;
    1130             : }
    1131             : 
    1132             : 
    1133             : /*-------------------------------------------------------------------------
    1134             :  * initDiffuseResponses()
    1135             :  *
    1136             :  *
    1137             :  *-------------------------------------------------------------------------*/
    1138             : 
    1139        2171 : void initDiffuseResponses_fx(
    1140             :     Word16 *diffuse_response_function_fx, /*q15*/
    1141             :     const Word16 num_channels,
    1142             :     const AUDIO_CONFIG output_config,
    1143             :     const IVAS_OUTPUT_SETUP hOutSetup,
    1144             :     const Word16 ambisonics_order,
    1145             :     const IVAS_FORMAT ivas_format,
    1146             :     Word16 *num_ele_spk_no_diffuse_rendering,
    1147             :     const AUDIO_CONFIG transport_config )
    1148             : {
    1149             :     Word16 i, l, k, idx, num_horizontal_speakers;
    1150        2171 :     *num_ele_spk_no_diffuse_rendering = 0;
    1151             : 
    1152             :     Word16 var1, exp_var1, res;
    1153             : 
    1154        2171 :     test();
    1155        2171 :     test();
    1156        2171 :     IF( EQ_32( output_config, IVAS_AUDIO_CONFIG_MONO ) )
    1157             :     {
    1158         111 :         diffuse_response_function_fx[0] = MAX16B; /*1 q15*/
    1159         111 :         move16();
    1160         111 :         diffuse_response_function_fx[1] = 18918 /*inv_sqrt(3.0f) oin Q15*/;
    1161         111 :         move16();
    1162             :     }
    1163        2060 :     ELSE IF( !( EQ_32( output_config, IVAS_AUDIO_CONFIG_FOA ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_HOA2 ) || EQ_32( output_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
    1164             :     {
    1165             :         /* set diffuse response function */
    1166         835 :         test();
    1167         835 :         test();
    1168         835 :         test();
    1169         835 :         test();
    1170         835 :         test();
    1171         835 :         test();
    1172         835 :         test();
    1173         835 :         test();
    1174         835 :         test();
    1175         835 :         test();
    1176         835 :         test();
    1177         835 :         test();
    1178         835 :         test();
    1179         835 :         test();
    1180         835 :         test();
    1181         835 :         test();
    1182         835 :         test();
    1183         835 :         test();
    1184         835 :         test();
    1185         835 :         test();
    1186         835 :         test();
    1187         835 :         IF( EQ_32( ivas_format, MC_FORMAT ) && ( EQ_32( transport_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_32( transport_config, IVAS_AUDIO_CONFIG_7_1 ) ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) )
    1188             :         {
    1189           0 :             num_horizontal_speakers = sub( num_channels, NUM_ELEVATED_SPEAKERS );
    1190           0 :             Copy( diffuse_response_CICP6_fx, diffuse_response_function_fx, num_horizontal_speakers ); // q15
    1191           0 :             set16_fx( &diffuse_response_function_fx[num_horizontal_speakers], 0, NUM_ELEVATED_SPEAKERS );
    1192           0 :             *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
    1193           0 :             move16();
    1194             :         }
    1195         835 :         ELSE IF( EQ_32( ivas_format, MC_FORMAT ) && ( EQ_32( transport_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_32( transport_config, IVAS_AUDIO_CONFIG_7_1 ) ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_7_1_4 ) )
    1196             :         {
    1197          79 :             num_horizontal_speakers = sub( num_channels, NUM_ELEVATED_SPEAKERS );
    1198          79 :             exp_var1 = 15;
    1199          79 :             move16();
    1200          79 :             var1 = ISqrt16( num_horizontal_speakers, &exp_var1 );                    // q=15-exp_var1
    1201          79 :             var1 = shr( var1, negate( exp_var1 ) );                                  // Q15
    1202          79 :             set16_fx( diffuse_response_function_fx, var1, num_horizontal_speakers ); // q15
    1203          79 :             set16_fx( &diffuse_response_function_fx[num_horizontal_speakers], 0, NUM_ELEVATED_SPEAKERS );
    1204          79 :             *num_ele_spk_no_diffuse_rendering = NUM_ELEVATED_SPEAKERS;
    1205          79 :             move16();
    1206             :         }
    1207         756 :         ELSE IF( ( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || EQ_32( ivas_format, MC_FORMAT ) ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1 ) && EQ_16( num_channels, 5 ) )
    1208             :         {
    1209         101 :             Copy( diffuse_response_CICP6_fx, diffuse_response_function_fx, num_channels ); // q15
    1210             :         }
    1211         655 :         ELSE IF( ( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || EQ_32( ivas_format, MC_FORMAT ) ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_2 ) && EQ_16( num_channels, 7 ) )
    1212             :         {
    1213         197 :             Copy( diffuse_response_CICP14_fx, diffuse_response_function_fx, num_channels ); // q15
    1214             :         }
    1215         458 :         ELSE IF( ( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || EQ_32( ivas_format, MC_FORMAT ) ) && ( EQ_32( output_config, IVAS_AUDIO_CONFIG_5_1_4 ) ) && EQ_16( num_channels, 9 ) )
    1216             :         {
    1217         207 :             Copy( diffuse_response_CICP16_fx, diffuse_response_function_fx, num_channels ); // q15
    1218             :         }
    1219         251 :         ELSE IF( ( EQ_32( ivas_format, MASA_FORMAT ) || EQ_32( ivas_format, MASA_ISM_FORMAT ) || EQ_32( ivas_format, MC_FORMAT ) ) && EQ_32( output_config, IVAS_AUDIO_CONFIG_LS_CUSTOM ) )
    1220             :         {
    1221           0 :             IF( EQ_32( transport_config, IVAS_AUDIO_CONFIG_5_1 ) || EQ_32( transport_config, IVAS_AUDIO_CONFIG_7_1 ) )
    1222             :             {
    1223             :                 /* Detect loudspeakers with elevation */
    1224           0 :                 num_horizontal_speakers = 0;
    1225           0 :                 move16();
    1226           0 :                 FOR( i = 0; i < num_channels; i++ )
    1227             :                 {
    1228           0 :                     IF( LE_32( L_abs( hOutSetup.ls_elevation_fx[i] /*q22*/ ), 20971520 /*5 in Q22*/ ) )
    1229             :                     {
    1230           0 :                         num_horizontal_speakers = add( num_horizontal_speakers, 1 );
    1231           0 :                         diffuse_response_function_fx[i] = MAX16B; // q15
    1232           0 :                         move16();
    1233             :                     }
    1234             :                     ELSE
    1235             :                     {
    1236           0 :                         *num_ele_spk_no_diffuse_rendering = add( *num_ele_spk_no_diffuse_rendering, 1 );
    1237           0 :                         move16();
    1238           0 :                         diffuse_response_function_fx[i] = 0;
    1239           0 :                         move16();
    1240             :                     }
    1241             :                 }
    1242             :                 /* Diffuse only to horizontal plane IF enough loudspeakers */
    1243           0 :                 IF( GT_16( num_horizontal_speakers, 2 ) )
    1244             :                 {
    1245           0 :                     exp_var1 = 15;
    1246           0 :                     move16();
    1247           0 :                     Word16 num_horizontal_speakers_isq = ISqrt16( num_horizontal_speakers, &exp_var1 );   // q15-exp_var1
    1248           0 :                     num_horizontal_speakers_isq = shr( num_horizontal_speakers_isq, negate( exp_var1 ) ); // q15
    1249           0 :                     assert( 0 );
    1250             :                     /*No stream hitting*/
    1251             :                     FOR( i = 0; i < num_channels; i++ )
    1252             :                     {
    1253             :                         diffuse_response_function_fx[i] = mult( diffuse_response_function_fx[i], num_horizontal_speakers_isq ); // q15
    1254             :                         move16();
    1255             :                     }
    1256             :                 }
    1257             :                 ELSE
    1258             :                 {
    1259           0 :                     var1 = num_channels, exp_var1 = 15;
    1260           0 :                     move16();
    1261           0 :                     move16();
    1262           0 :                     res = ISqrt16( var1, &exp_var1 );                            // q=15-exp_var1
    1263           0 :                     res = shr( res, negate( exp_var1 ) );                        // q=15
    1264           0 :                     set16_fx( diffuse_response_function_fx, res, num_channels ); // q15
    1265           0 :                     *num_ele_spk_no_diffuse_rendering = 0;
    1266           0 :                     move16();
    1267             :                 }
    1268             :             }
    1269             :             ELSE
    1270             :             {
    1271           0 :                 var1 = num_channels, exp_var1 = 15;
    1272           0 :                 move16();
    1273           0 :                 move16();
    1274           0 :                 res = ISqrt16( var1, &exp_var1 );                            // q=15-exp_var1
    1275           0 :                 res = shr( res, negate( exp_var1 ) );                        // q15
    1276           0 :                 set16_fx( diffuse_response_function_fx, res, num_channels ); // q15
    1277             :             }
    1278             :         }
    1279             :         ELSE
    1280             :         {
    1281         251 :             var1 = num_channels, exp_var1 = 15;
    1282         251 :             move16();
    1283         251 :             move16();
    1284         251 :             res = ISqrt16( var1, &exp_var1 );                            // q(15-exp_var1)
    1285         251 :             res = shr( res, negate( exp_var1 ) );                        // q15
    1286         251 :             set16_fx( diffuse_response_function_fx, res, num_channels ); // q15
    1287             :         }
    1288             :     }
    1289             :     ELSE
    1290             :     {
    1291        1225 :         idx = 0;
    1292        1225 :         move16();
    1293             :         Word16 j;
    1294        5698 :         FOR( l = 0; l <= ambisonics_order; l++ )
    1295             :         {
    1296        4473 :             j = add( imult1616( 2, l ), 1 );
    1297       21502 :             FOR( k = 0; k < j; k++ )
    1298             :             {
    1299       17029 :                 var1 = j, exp_var1 = 15;
    1300       17029 :                 move16();
    1301       17029 :                 move16();
    1302       17029 :                 res = ISqrt16( var1, &exp_var1 );                                   // q(15-exp_var1)
    1303       17029 :                 diffuse_response_function_fx[idx] = shr( res, negate( exp_var1 ) ); // q15
    1304       17029 :                 idx = idx + 1;
    1305             :             }
    1306             :         }
    1307             :     }
    1308             : 
    1309        2171 :     return;
    1310             : }
    1311             : 
    1312             : 
    1313             : /*-------------------------------------------------------------------------
    1314             :  * protoSignalComputation_shd()
    1315             :  *
    1316             :  *
    1317             :  *-------------------------------------------------------------------------*/
    1318             : 
    1319      884463 : void protoSignalComputation_shd_fx(
    1320             :     Word32 RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*Q(q_cldfb)*/
    1321             :     Word32 ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*Q(q_cldfb)*/
    1322             :     Word32 *proto_direct_buffer_f_fx,                                           /*Q(proto_direct_buffer_f_q)*/
    1323             :     Word16 *proto_direct_buffer_f_q,
    1324             :     Word32 *proto_diffuse_buffer_f_fx, /*Q(proto_diffuse_buffer_f_q)*/
    1325             :     Word16 *proto_diffuse_buffer_f_q,
    1326             :     Word32 *reference_power_fx, /*Q(reference_power_q)*/
    1327             :     Word16 *reference_power_q,
    1328             :     const Word16 slot_index,
    1329             :     const Word16 num_inputs,
    1330             :     const Word16 num_outputs_diff,
    1331             :     const Word16 num_freq_bands,
    1332             :     Word32 *p_Rmat_fx, /* Q30 */
    1333             :     Word16 q_cldfb )
    1334             : {
    1335             :     Word16 l, k;
    1336             :     Word16 Rmat_k[4];
    1337             :     Word32 *p_proto_direct_buffer_fx;
    1338             :     Word32 *p_proto_diffuse_buffer_fx;
    1339             :     Word32 re1, im1;
    1340             :     Word32 re2, im2;
    1341             :     Word32 re3, im3;
    1342             :     Word32 *p_k_fx[4];
    1343             :     Word16 min_q_shift, q_shift;
    1344             :     Word16 idx, idx1;
    1345             : 
    1346      884463 :     k = 0; /* to avoid compilation warning */
    1347      884463 :     move16();
    1348             : 
    1349      884463 :     min_q_shift = Q31;
    1350      884463 :     move16();
    1351      884463 :     q_shift = Q31;
    1352      884463 :     move16();
    1353             : 
    1354      884463 :     p_proto_direct_buffer_fx = proto_direct_buffer_f_fx + i_mult( i_mult( slot_index, 2 ), i_mult( num_freq_bands, num_inputs ) );         /*proto_direct_buffer_f_q*/
    1355      884463 :     p_proto_diffuse_buffer_fx = proto_diffuse_buffer_f_fx + i_mult( i_mult( slot_index, 2 ), i_mult( num_freq_bands, num_outputs_diff ) ); /*proto_diffuse_buffer_f_q*/
    1356             : 
    1357      884463 :     IF( EQ_16( num_inputs, 1 ) )
    1358             :     {
    1359     2989244 :         FOR( l = 0; l < num_freq_bands; l++ )
    1360             :         {
    1361     2940240 :             p_proto_direct_buffer_fx[2 * l] = RealBuffer_fx[0][0][l]; /*Q(q_cldfb)*/
    1362     2940240 :             move32();
    1363     2940240 :             p_proto_direct_buffer_fx[2 * l + 1] = ImagBuffer_fx[0][0][l]; /*Q(q_cldfb)*/
    1364     2940240 :             move32();
    1365             :         }
    1366       49004 :         *proto_direct_buffer_f_q = q_cldfb;
    1367       49004 :         move16();
    1368             :     }
    1369      835459 :     ELSE IF( EQ_16( num_inputs, 2 ) )
    1370             :     {
    1371           0 :         IF( p_Rmat_fx != 0 )
    1372             :         {
    1373           0 :             assert( EQ_16( num_inputs, 4 ) && "This code block should never be run with num_inputs != 4!" );
    1374             : 
    1375           0 :             FOR( l = 0; l < num_freq_bands; l++ )
    1376             :             {
    1377           0 :                 re1 = L_add( RealBuffer_fx[0][0][l], RealBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1378           0 :                 im1 = L_add( ImagBuffer_fx[0][0][l], ImagBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1379             : 
    1380           0 :                 re2 = L_sub( RealBuffer_fx[0][0][l], RealBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1381           0 :                 im2 = L_sub( ImagBuffer_fx[0][0][l], ImagBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1382             : 
    1383           0 :                 p_proto_direct_buffer_fx[2 * l] = re1; /*Q(q_cldfb)*/
    1384           0 :                 move32();
    1385           0 :                 p_proto_direct_buffer_fx[2 * l + 1] = im1; /*Q(q_cldfb)*/
    1386           0 :                 move32();
    1387           0 :                 p_proto_direct_buffer_fx[2 * ( num_freq_bands + l )] = L_shl( Mpy_32_32( p_Rmat_fx[0], re2 ), Q1 ); // left shift is done to maintain constant Q factor for p_proto_direct_buffer_fx =>Q(q_cldfb)
    1388           0 :                 move32();
    1389           0 :                 p_proto_direct_buffer_fx[2 * ( num_freq_bands + l ) + 1] = L_shl( Mpy_32_32( p_Rmat_fx[0], im2 ), Q1 ); // left shift is done to maintain constant Q factor for p_proto_direct_buffer_fx => Q(q_cldfb)
    1390           0 :                 move32();
    1391             :             }
    1392           0 :             *proto_direct_buffer_f_q = q_cldfb;
    1393           0 :             move16();
    1394             :         }
    1395             :         ELSE
    1396             :         {
    1397           0 :             FOR( l = 0; l < num_freq_bands; l++ )
    1398             :             {
    1399           0 :                 re1 = L_add( RealBuffer_fx[0][0][l], RealBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1400           0 :                 im1 = L_add( ImagBuffer_fx[0][0][l], ImagBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1401             : 
    1402           0 :                 p_proto_direct_buffer_fx[2 * l] = re1; /*Q(q_cldfb)*/
    1403           0 :                 move32();
    1404           0 :                 p_proto_direct_buffer_fx[2 * l + 1] = im1; /*Q(q_cldfb)*/
    1405           0 :                 move32();
    1406           0 :                 p_proto_direct_buffer_fx[2 * num_freq_bands + 2 * l] = L_sub( RealBuffer_fx[0][0][l], RealBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1407           0 :                 move32();
    1408           0 :                 p_proto_direct_buffer_fx[2 * num_freq_bands + 2 * l + 1] = L_sub( ImagBuffer_fx[0][0][l], ImagBuffer_fx[1][0][l] ); /*Q(q_cldfb)*/
    1409           0 :                 move32();
    1410             :             }
    1411             :         }
    1412           0 :         *proto_direct_buffer_f_q = q_cldfb;
    1413           0 :         move16();
    1414             :     }
    1415      835459 :     ELSE IF( GE_16( num_inputs, 4 ) )
    1416             :     {
    1417      835459 :         p_k_fx[0] = p_proto_direct_buffer_fx;                               /*Q(proto_direct_buffer_f_q)*/
    1418      835459 :         p_k_fx[1] = p_proto_direct_buffer_fx + i_mult( 2, num_freq_bands ); /*Q(proto_direct_buffer_f_q)*/
    1419      835459 :         p_k_fx[2] = p_proto_direct_buffer_fx + i_mult( 4, num_freq_bands ); /*Q(proto_direct_buffer_f_q)*/
    1420      835459 :         p_k_fx[3] = p_proto_direct_buffer_fx + i_mult( 6, num_freq_bands ); /*Q(proto_direct_buffer_f_q)*/
    1421             : 
    1422      835459 :         Rmat_k[0] = 0;
    1423      835459 :         move16();
    1424      835459 :         Rmat_k[1] = 1;
    1425      835459 :         move16();
    1426      835459 :         Rmat_k[2] = 2;
    1427      835459 :         move16();
    1428      835459 :         Rmat_k[3] = 0;
    1429      835459 :         move16();
    1430             : 
    1431             :         /* calculate the minimum possible shift for the buffers RealBuffer_fx and ImagBuffer_fx */
    1432     4177295 :         FOR( k = 0; k < 4; k++ )
    1433             :         {
    1434     3341836 :             q_shift = L_norm_arr( RealBuffer_fx[k][0], num_freq_bands );
    1435     3341836 :             min_q_shift = s_min( min_q_shift, q_shift );
    1436     3341836 :             q_shift = L_norm_arr( ImagBuffer_fx[k][0], num_freq_bands );
    1437     3341836 :             min_q_shift = s_min( min_q_shift, q_shift );
    1438             :         }
    1439             : 
    1440      835459 :         min_q_shift = sub( min_q_shift, 4 ); /* 4 is for guard bits*/
    1441             : 
    1442      835459 :         IF( p_Rmat_fx != 0 )
    1443             :         {
    1444      147200 :             assert( EQ_16( num_inputs, 4 ) && "This code block should never be run with num_inputs != 4!" );
    1445             : 
    1446     5779200 :             FOR( l = 0; l < num_freq_bands; l++ )
    1447             :             {
    1448     5632000 :                 *p_k_fx[0] = L_shl( RealBuffer_fx[0][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1449     5632000 :                 move32();
    1450     5632000 :                 reference_power_fx[l + num_freq_bands] = Mpy_32_32( *p_k_fx[0], *p_k_fx[0] ); /*2*Q(q_cldfb+min_q_shift)-31*/
    1451     5632000 :                 move32();
    1452     5632000 :                 p_k_fx[0]++;
    1453             : 
    1454     5632000 :                 *p_k_fx[0] = L_shl( ImagBuffer_fx[0][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1455     5632000 :                 move32();
    1456     5632000 :                 reference_power_fx[l + num_freq_bands] = Madd_32_32( reference_power_fx[l + num_freq_bands], *p_k_fx[0], *p_k_fx[0] ); /*2*Q(q_cldfb+min_q_shift)-31*/
    1457     5632000 :                 move32();
    1458     5632000 :                 p_k_fx[0]++;
    1459             : 
    1460     5632000 :                 reference_power_fx[l] = L_shr( reference_power_fx[l + num_freq_bands], 1 ); /*2*Q(q_cldfb+min_q_shift)-31-1*/
    1461     5632000 :                 move32();
    1462             : 
    1463     5632000 :                 re1 = L_shl( RealBuffer_fx[1][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1464     5632000 :                 re2 = L_shl( RealBuffer_fx[2][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1465     5632000 :                 re3 = L_shl( RealBuffer_fx[3][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1466     5632000 :                 im1 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1467     5632000 :                 im2 = L_shl( ImagBuffer_fx[2][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1468     5632000 :                 im3 = L_shl( ImagBuffer_fx[3][0][l], min_q_shift ); /*Q(q_cldfb+min_q_shift)*/
    1469             : 
    1470    22528000 :                 FOR( k = 1; k < 4; k++ )
    1471             :                 {
    1472    16896000 :                     idx = i_mult( 3, Rmat_k[k] );
    1473    16896000 :                     idx1 = add( l, i_mult( add( k, 1 ), num_freq_bands ) );
    1474             : 
    1475    16896000 :                     *p_k_fx[k] = Madd_32_32( Madd_32_32( Mpy_32_32( p_Rmat_fx[idx + 1], re1 ), p_Rmat_fx[idx + 2], re2 ), p_Rmat_fx[idx], re3 ); /*Q(30 + q_cldfb+min_q_shift-31)=>Q(q_cldfb+min_q_shift-1)*/
    1476    16896000 :                     move32();
    1477    16896000 :                     *p_k_fx[k] = L_shl( *p_k_fx[k], Q1 ); // left shift is done to maintain constant Q factor for p_k_fx Q(q_cldfb+min_q_shift)
    1478    16896000 :                     move32();
    1479    16896000 :                     reference_power_fx[idx1] = Mpy_32_32( *p_k_fx[k], *p_k_fx[k] ); // Q(2*(q_cldfb + min_q_shift)-31)
    1480    16896000 :                     move32();
    1481    16896000 :                     p_k_fx[k]++;
    1482             : 
    1483    16896000 :                     *p_k_fx[k] = Madd_32_32( Madd_32_32( Mpy_32_32( p_Rmat_fx[idx + 1], im1 ), p_Rmat_fx[idx + 2], im2 ), p_Rmat_fx[idx], im3 ); /*Q(q_cldfb+min_q_shift-1)*/
    1484    16896000 :                     move32();
    1485    16896000 :                     *p_k_fx[k] = L_shl( *p_k_fx[k], Q1 ); // left shift is done to maintain constant Q factor Q(q_cldfb+min_q_shift)
    1486    16896000 :                     move32();
    1487    16896000 :                     reference_power_fx[idx1] = Mpy_32_32( *p_k_fx[k], *p_k_fx[k] ); // Q(2*(q_cldfb + min_q_shift)-31)
    1488    16896000 :                     move32();
    1489    16896000 :                     p_k_fx[k]++;
    1490             : 
    1491    16896000 :                     reference_power_fx[l] = L_add( reference_power_fx[l], L_shr( reference_power_fx[idx1], 1 ) ); /*2*Q(q_cldfb+min_q_shift)-31-1*/
    1492    16896000 :                     move32();
    1493             :                 }
    1494             : 
    1495     5632000 :                 *proto_direct_buffer_f_q = add( q_cldfb, min_q_shift );
    1496     5632000 :                 move16();
    1497             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1498     5632000 :                 Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    1499     5632000 :                 reference_power_q[qidx] = sub( add( *proto_direct_buffer_f_q, *proto_direct_buffer_f_q ), 31 );
    1500     5632000 :                 move16();
    1501             : #else
    1502             :                 *reference_power_q = sub( add( *proto_direct_buffer_f_q, *proto_direct_buffer_f_q ), 31 );
    1503             :                 move16();
    1504             : #endif
    1505             : 
    1506     5632000 :                 Word16 shift = sub( *proto_direct_buffer_f_q, q_cldfb );
    1507    22528000 :                 FOR( k = 1; k < 4; k++ )
    1508             :                 {
    1509    16896000 :                     RealBuffer_fx[k][0][l] = L_shr( p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l )], shift ); // proto_direct_buffer_f_q -> q_cldfb
    1510    16896000 :                     move32();
    1511    16896000 :                     ImagBuffer_fx[k][0][l] = L_shr( p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l ) + 1], shift ); // proto_direct_buffer_f_q -> q_cldfb
    1512    16896000 :                     move32();
    1513             :                 }
    1514             :             }
    1515             :         }
    1516             :         ELSE
    1517             :         {
    1518      688259 :             set_zero_fx( reference_power_fx, num_freq_bands );
    1519     3441295 :             FOR( k = 0; k < 4; k++ )
    1520             :             {
    1521   154847276 :                 FOR( l = 0; l < num_freq_bands; l++ )
    1522             :                 {
    1523   152094240 :                     p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l )] = RealBuffer_fx[k][0][l]; // q_cldfb
    1524   152094240 :                     move32();
    1525   152094240 :                     p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l ) + 1] = ImagBuffer_fx[k][0][l]; // q_cldfb
    1526   152094240 :                     move32();
    1527             : 
    1528   152094240 :                     re1 = L_shl( RealBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1529   152094240 :                     im1 = L_shl( ImagBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1530             : 
    1531   152094240 :                     reference_power_fx[l + ( k + 1 ) * num_freq_bands] = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 ); // 2*(q_cldfb+min_q_shift)-31
    1532   152094240 :                     move32();
    1533   152094240 :                     reference_power_fx[l] = L_add( reference_power_fx[l], L_shr( reference_power_fx[l + ( k + 1 ) * num_freq_bands], 1 ) ); // 2*(q_cldfb+min_q_shift)-31-1
    1534   152094240 :                     move32();
    1535             :                 }
    1536             :             }
    1537      688259 :             *proto_direct_buffer_f_q = q_cldfb;
    1538      688259 :             move16();
    1539             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1540      688259 :             reference_power_q[0] = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    1541      688259 :             reference_power_q[1] = reference_power_q[0];
    1542      688259 :             move16();
    1543      688259 :             move16();
    1544             : #else
    1545             :             *reference_power_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    1546             :             move16();
    1547             : #endif
    1548             :         }
    1549             : 
    1550             :         /* Additional transport channels = planar SBA components of degree higher than 1*/
    1551     2257867 :         FOR( ; k < num_inputs; k++ )
    1552             :         {
    1553    84949288 :             FOR( l = 0; l < num_freq_bands; l++ )
    1554             :             {
    1555    83526880 :                 p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l )] = RealBuffer_fx[k][0][l]; // q_cldfb
    1556    83526880 :                 move32();
    1557    83526880 :                 p_proto_direct_buffer_fx[2 * ( k * num_freq_bands + l ) + 1] = ImagBuffer_fx[k][0][l]; // q_cldfb
    1558    83526880 :                 move32();
    1559             :             }
    1560             :         }
    1561             :     }
    1562             : 
    1563             : 
    1564             :     /*Copy direct to diffuse proto*/
    1565      884463 :     Copy32( p_proto_direct_buffer_fx, p_proto_diffuse_buffer_fx, i_mult( i_mult( 2, num_freq_bands ), s_min( num_outputs_diff, num_inputs ) ) ); /*proto_diffuse_buffer_f_q -> proto_direct_buffer_f_q*/
    1566      884463 :     *proto_diffuse_buffer_f_q = *proto_direct_buffer_f_q;
    1567      884463 :     move16();
    1568             : 
    1569      884463 :     IF( EQ_16( num_inputs, 1 ) )
    1570             :     {
    1571             :         /* Add comfort noise addition (CNA) to diffuse proto only*/
    1572     2989244 :         FOR( l = 0; l < num_freq_bands; l++ )
    1573             :         {
    1574     2940240 :             p_proto_diffuse_buffer_fx[2 * l] = L_add( p_proto_diffuse_buffer_fx[2 * l], RealBuffer_fx[1][0][l] ); // q_cldfb
    1575     2940240 :             move32();
    1576     2940240 :             p_proto_diffuse_buffer_fx[2 * l + 1] = L_add( p_proto_diffuse_buffer_fx[2 * l + 1], ImagBuffer_fx[1][0][l] ); // q_cldfb
    1577     2940240 :             move32();
    1578             :         }
    1579             :     }
    1580             : 
    1581      884463 :     return;
    1582             : }
    1583             : 
    1584             : 
    1585             : /*-------------------------------------------------------------------------
    1586             :  * protoSignalComputation1()
    1587             :  *
    1588             :  *
    1589             :  *-------------------------------------------------------------------------*/
    1590             : 
    1591      133812 : void protoSignalComputation1_fx(
    1592             :     Word32 RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    1593             :     Word32 ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    1594             :     Word32 *proto_frame_f_fx,                                                   /*proto_frame_f_q*/
    1595             :     Word16 *proto_frame_f_q,
    1596             :     Word32 *proto_direct_buffer_f_fx, /*proto_direct_buffer_f_q*/
    1597             :     Word16 *proto_direct_buffer_f_q,
    1598             :     Word32 *reference_power_fx, /*reference_power_q*/
    1599             :     Word16 *reference_power_q,
    1600             :     Word32 *proto_power_smooth_fx, /*proto_power_smooth_q*/
    1601             :     Word16 *proto_power_smooth_q,
    1602             :     const Word16 slot_index,
    1603             :     const Word16 num_outputs_diff,
    1604             :     const Word16 num_freq_bands,
    1605             :     Word16 q_cldfb )
    1606             : {
    1607             :     Word16 l, k, idx;
    1608             :     Word32 *p_proto_buffer_fx;
    1609             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1610             :     Word16 proto_power_smooth_fx_q[2], min_q_shift, q_shift;
    1611             : #else
    1612             :     Word16 proto_power_smooth_fx_q, min_q_shift, q_shift;
    1613             : #endif
    1614             :     Word32 re, im;
    1615             : 
    1616      133812 :     min_q_shift = Q31;
    1617      133812 :     move16();
    1618      133812 :     q_shift = Q31;
    1619      133812 :     move16();
    1620             : 
    1621      133812 :     p_proto_buffer_fx = proto_direct_buffer_f_fx + ( slot_index * ( 2 * num_freq_bands ) ); /*proto_direct_buffer_f_q*/
    1622             : 
    1623             :     /* calculate the maximum shift possible for the bufferS RealBuffer_fx and  ImagBuffer_fx*/
    1624      133812 :     q_shift = L_norm_arr( RealBuffer_fx[0][0], num_freq_bands );
    1625      133812 :     min_q_shift = s_min( q_shift, min_q_shift );
    1626      133812 :     q_shift = L_norm_arr( ImagBuffer_fx[0][0], num_freq_bands );
    1627      133812 :     min_q_shift = s_min( q_shift, min_q_shift );
    1628             : 
    1629      133812 :     min_q_shift = sub( min_q_shift, find_guarded_bits_fx( 2 ) );
    1630             : 
    1631             :     /* calculate the maximum shift possible for the buffer proto_power_smooth_fx */
    1632      133812 :     q_shift = getScaleFactor32( proto_power_smooth_fx, num_freq_bands );
    1633      133812 :     q_shift = sub( q_shift, find_guarded_bits_fx( 2 ) );
    1634             : 
    1635      133812 :     Scale_sig32( proto_power_smooth_fx, num_freq_bands, q_shift ); /*proto_power_smooth_q+q_shift*/
    1636             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1637      133812 :     proto_power_smooth_q[0] = add( proto_power_smooth_q[0], q_shift );
    1638      133812 :     proto_power_smooth_q[1] = add( proto_power_smooth_q[1], q_shift );
    1639      133812 :     proto_power_smooth_fx_q[0] = proto_power_smooth_q[0];
    1640      133812 :     proto_power_smooth_fx_q[1] = proto_power_smooth_q[1];
    1641      133812 :     move16();
    1642      133812 :     move16();
    1643             : #else
    1644             :     *proto_power_smooth_q = add( *proto_power_smooth_q, q_shift );
    1645             :     proto_power_smooth_fx_q = *proto_power_smooth_q;
    1646             :     move16();
    1647             : #endif
    1648             : 
    1649     7839732 :     FOR( l = 0; l < num_freq_bands; l++ )
    1650             :     {
    1651             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1652     7705920 :         Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    1653             : #endif
    1654     7705920 :         re = L_shl( RealBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1655     7705920 :         im = L_shl( ImagBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1656             : 
    1657     7705920 :         reference_power_fx[l] = Madd_32_32( Mpy_32_32( re, re ), im, im ); // 2*(q_cldfb+min_q_shift)-31
    1658     7705920 :         move32();
    1659             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1660     7705920 :         reference_power_q[qidx] = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    1661     7705920 :         move16();
    1662             : 
    1663     7705920 :         IF( LT_16( reference_power_q[qidx], proto_power_smooth_q[qidx] ) )
    1664             :         {
    1665     4560980 :             proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( proto_power_smooth_q[qidx], reference_power_q[qidx] ) ), reference_power_fx[l] ); // reference_power_q
    1666     4560980 :             move32();
    1667     4560980 :             proto_power_smooth_fx_q[qidx] = reference_power_q[qidx];
    1668     4560980 :             move16();
    1669             :         }
    1670             :         ELSE
    1671             :         {
    1672     3144940 :             proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( reference_power_fx[l], sub( reference_power_q[qidx], proto_power_smooth_q[qidx] ) ) ); // proto_power_smooth_q
    1673     3144940 :             move32();
    1674     3144940 :             proto_power_smooth_fx_q[qidx] = proto_power_smooth_q[qidx];
    1675     3144940 :             move16();
    1676             :         }
    1677             : #else
    1678             :         *reference_power_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    1679             :         move16();
    1680             : 
    1681             :         IF( LT_16( *reference_power_q, *proto_power_smooth_q ) )
    1682             :         {
    1683             :             proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *proto_power_smooth_q, *reference_power_q ) ), reference_power_fx[l] ); // reference_power_q
    1684             :             move32();
    1685             :             proto_power_smooth_fx_q = *reference_power_q;
    1686             :             move16();
    1687             :         }
    1688             :         ELSE
    1689             :         {
    1690             :             proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( reference_power_fx[l], sub( *reference_power_q, *proto_power_smooth_q ) ) ); // proto_power_smooth_q
    1691             :             move32();
    1692             :             proto_power_smooth_fx_q = *proto_power_smooth_q;
    1693             :             move16();
    1694             :         }
    1695             : #endif
    1696             : 
    1697     7705920 :         idx = 2 * l;
    1698     7705920 :         p_proto_buffer_fx[idx] = RealBuffer_fx[0][0][l]; // q_cldfb
    1699     7705920 :         move32();
    1700     7705920 :         p_proto_buffer_fx[idx + 1] = ImagBuffer_fx[0][0][l]; // q_cldfb
    1701     7705920 :         move32();
    1702             : 
    1703    15411840 :         FOR( k = 0; k < num_outputs_diff; k++ )
    1704             :         {
    1705     7705920 :             idx = add( i_mult( i_mult( 2, k ), num_freq_bands ), i_mult( 2, l ) );
    1706     7705920 :             proto_frame_f_fx[idx] = RealBuffer_fx[0][0][l]; // q_cldfb
    1707     7705920 :             move32();
    1708     7705920 :             proto_frame_f_fx[idx + 1] = ImagBuffer_fx[0][0][l]; // q_cldfb
    1709     7705920 :             move32();
    1710             :         }
    1711             :     }
    1712             : 
    1713      133812 :     *proto_frame_f_q = q_cldfb;
    1714      133812 :     move16();
    1715      133812 :     *proto_direct_buffer_f_q = q_cldfb;
    1716      133812 :     move16();
    1717             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1718      133812 :     proto_power_smooth_q[0] = proto_power_smooth_fx_q[0];
    1719      133812 :     proto_power_smooth_q[1] = proto_power_smooth_fx_q[1];
    1720      133812 :     move16();
    1721      133812 :     move16();
    1722             : #else
    1723             :     *proto_power_smooth_q = proto_power_smooth_fx_q;
    1724             :     move16();
    1725             : #endif
    1726             : 
    1727      133812 :     return;
    1728             : }
    1729             : 
    1730             : 
    1731             : /*-------------------------------------------------------------------------
    1732             :  * protoSignalComputation2()
    1733             :  *
    1734             :  *
    1735             :  *-------------------------------------------------------------------------*/
    1736             : 
    1737      175074 : void protoSignalComputation2_fx(
    1738             :     Word32 RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    1739             :     Word32 ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    1740             :     Word32 *proto_frame_f_fx,                                                   /*q_proto_frame_f*/
    1741             :     Word16 *q_proto_frame_f,
    1742             :     Word32 *proto_direct_buffer_f_fx, /*q_proto_direct_buffer_f*/
    1743             :     Word16 *q_proto_direct_buffer_f,
    1744             :     Word32 *reference_power_fx, /*q_reference_power*/
    1745             :     Word16 *q_reference_power,
    1746             :     Word32 *proto_power_smooth_fx, /*q_proto_power_smooth*/
    1747             :     Word16 *q_proto_power_smooth,
    1748             :     const Word16 isloudspeaker,
    1749             :     const Word16 slot_index,
    1750             :     const Word16 num_freq_bands,
    1751             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect,
    1752             :     Word16 q_cldfb )
    1753             : {
    1754             :     Word16 l;
    1755             :     Word16 dipole_freq_range[2];
    1756             :     Word32 *p_proto_buffer_fx;
    1757             :     Word32 Real_aux_fx, Imag_aux_fx, re_aux, im_aux;
    1758             :     Word32 re1, im1, re2, im2;
    1759             :     Word32 RealSubtract_fx, ImagSubtract_fx;
    1760             :     Word32 left_bb_power_fx, right_bb_power_fx, total_bb_power_fx, lr_bb_power_fx;
    1761             :     Word32 left_hi_power_fx, right_hi_power_fx, total_hi_power_fx, lr_hi_power_fx;
    1762             :     Word32 sum_power_fx, Left_power_fx, Right_power_fx, Total_power_fx;
    1763             :     Word16 q_lr_bb_power, q_lr_hi_power;
    1764             :     Word32 lr_total_bb_ratio_fx, lr_total_hi_ratio_fx;
    1765             :     Word32 min_sum_total_ratio_fx, min_sum_total_ratio_db_fx;
    1766             :     Word32 sum_total_ratio_fx[MASA_SUM_FREQ_RANGE_BINS];
    1767             :     Word16 exp_sum_total_ratio_fx[MASA_SUM_FREQ_RANGE_BINS];
    1768      175074 :     Word16 q_sum_total_ratio = 0;
    1769      175074 :     move16();
    1770             :     Word32 a_fx, b_fx, a2_fx, b2_fx;
    1771             :     Word16 interpolatorSpaced_fx, interpolatorDmx_fx;
    1772             :     Word32 tempSpaced_fx, tempDmx_fx;
    1773             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1774             :     Word16 q_shift, min_q_shift[2], exp, q_temp[2];
    1775             : #else
    1776             :     Word16 q_shift, min_q_shift, exp, q_temp, temp_q_shift, q_temp2;
    1777             : #endif
    1778             :     Word32 temp;
    1779             :     Word64 W_tmp1, W_tmp2, W_tmp3;
    1780             :     Word64 reference_power_64fx[CLDFB_NO_CHANNELS_MAX];
    1781             :     Word16 q_reference_power_64fx;
    1782             :     Word16 head_room, q_Left_Right_power;
    1783             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1784      175074 :     Word16 num_proto = 3;
    1785             : #endif
    1786             :     /* Calculate maximum possible shift for the buffers RealBuffer_fx and ImagBuffer_fx */
    1787             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1788      175074 :     min_q_shift[0] = Q31;
    1789      175074 :     min_q_shift[1] = Q31;
    1790      175074 :     move16();
    1791      175074 :     move16();
    1792             : #else
    1793             :     min_q_shift = Q31;
    1794             :     move16();
    1795             : #endif
    1796      175074 :     q_sum_total_ratio = 0;
    1797      175074 :     move16();
    1798      175074 :     exp = 0;
    1799      175074 :     move16();
    1800      175074 :     interpolatorSpaced_fx = 0;
    1801      175074 :     move16();
    1802      175074 :     interpolatorDmx_fx = MAX16B;
    1803      175074 :     move16();
    1804             : 
    1805             :     /* Calculate the max shift possible for the buffers RealBuffer_fx and ImagBuffer_fx */
    1806      525222 :     FOR( l = 0; l < 2; l++ )
    1807             :     {
    1808             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1809      350148 :         q_shift = s_min( L_norm_arr( RealBuffer_fx[l][0], s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ) ), L_norm_arr( ImagBuffer_fx[l][0], s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ) ) );
    1810      350148 :         min_q_shift[0] = s_min( min_q_shift[0], q_shift );
    1811      350148 :         q_shift = s_min( L_norm_arr( RealBuffer_fx[l][0] + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) ), L_norm_arr( ImagBuffer_fx[l][0] + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) ) );
    1812      350148 :         min_q_shift[1] = s_min( min_q_shift[1], q_shift );
    1813             : #if ( MASA_SUM_FREQ_RANGE_BINS > CLDFB_NO_CHANNELS_HALF )
    1814             : #error MASA_SUM_FREQ_RANGE_BINS if greater than CLDFB_NO_CHANNELS_HALF, this does not work
    1815             : #endif
    1816             : #else
    1817             :         q_shift = s_min( L_norm_arr( RealBuffer_fx[l][0], num_freq_bands ), L_norm_arr( ImagBuffer_fx[l][0], num_freq_bands ) );
    1818             :         min_q_shift = s_min( min_q_shift, q_shift );
    1819             : #endif
    1820      350148 :         q_shift = s_min( L_norm_arr( RealBuffer_fx[l][0], s_min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ) ), L_norm_arr( ImagBuffer_fx[l][0], s_min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ) ) );
    1821             :     }
    1822             : 
    1823             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1824      175074 :     min_q_shift[0] = sub( min_q_shift[0], 2 ); // guard bits
    1825      175074 :     min_q_shift[1] = sub( min_q_shift[1], 2 ); // guard bits
    1826             : #else
    1827             :     min_q_shift = sub( min_q_shift, 2 ); // guard bits
    1828             : #endif
    1829             : 
    1830             :     /* Upscaling of the buffer proto_power_smooth_fx */
    1831             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1832      175074 :     IF( isloudspeaker )
    1833             :     {
    1834       93875 :         num_proto = 3;
    1835       93875 :         q_shift = L_norm_arr( proto_power_smooth_fx, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) );
    1836       93875 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    1837       93875 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + num_freq_bands + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    1838       93875 :         scale_sig32( proto_power_smooth_fx, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( q_shift, 1 ) );                                   // q_proto_power_smooth+q_shift-1
    1839       93875 :         scale_sig32( proto_power_smooth_fx + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( q_shift, 1 ) );                  // q_proto_power_smooth+q_shift-1
    1840       93875 :         scale_sig32( proto_power_smooth_fx + num_freq_bands + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1841             :     }
    1842             :     ELSE
    1843             :     {
    1844       81199 :         num_proto = 2;
    1845       81199 :         q_shift = L_norm_arr( proto_power_smooth_fx, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) );
    1846       81199 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    1847       81199 :         scale_sig32( proto_power_smooth_fx, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( q_shift, 1 ) );                  // q_proto_power_smooth+q_shift-1
    1848       81199 :         scale_sig32( proto_power_smooth_fx + num_freq_bands, s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1849             :     }
    1850      175074 :     q_proto_power_smooth[0] = add( q_proto_power_smooth[0], sub( q_shift, 1 ) );
    1851      175074 :     move16();
    1852      175074 :     IF( isloudspeaker )
    1853             :     {
    1854       93875 :         q_shift = L_norm_arr( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    1855       93875 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) ) );
    1856       93875 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) ) );
    1857       93875 :         scale_sig32( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( q_shift, 1 ) );                                   // q_proto_power_smooth+q_shift-1
    1858       93875 :         scale_sig32( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( q_shift, 1 ) );                  // q_proto_power_smooth+q_shift-1
    1859       93875 :         scale_sig32( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1860             :     }
    1861             :     ELSE
    1862             :     {
    1863       81199 :         q_shift = L_norm_arr( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    1864       81199 :         q_shift = s_min( q_shift, L_norm_arr( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) ) );
    1865       81199 :         scale_sig32( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( q_shift, 1 ) );                  // q_proto_power_smooth+q_shift-1
    1866       81199 :         scale_sig32( proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + num_freq_bands, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1867             :     }
    1868      175074 :     q_proto_power_smooth[1] = add( q_proto_power_smooth[1], sub( q_shift, 1 ) );
    1869      175074 :     move16();
    1870             : #else
    1871             :     IF( isloudspeaker )
    1872             :     {
    1873             :         q_shift = getScaleFactor32( proto_power_smooth_fx, i_mult( 3, num_freq_bands ) );
    1874             :         scale_sig32( proto_power_smooth_fx, i_mult( 3, num_freq_bands ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1875             :     }
    1876             :     ELSE
    1877             :     {
    1878             :         q_shift = getScaleFactor32( proto_power_smooth_fx, i_mult( 2, num_freq_bands ) );
    1879             :         scale_sig32( proto_power_smooth_fx, i_mult( 2, num_freq_bands ), sub( q_shift, 1 ) ); // q_proto_power_smooth+q_shift-1
    1880             :     }
    1881             :     *q_proto_power_smooth = add( *q_proto_power_smooth, sub( q_shift, 1 ) );
    1882             :     move16();
    1883             : #endif
    1884             : 
    1885      175074 :     IF( isloudspeaker )
    1886             :     {
    1887       93875 :         p_proto_buffer_fx = proto_direct_buffer_f_fx + i_mult( i_mult( i_mult( slot_index, 2 ), num_freq_bands ), 3 ); // q_proto_direct_buffer_f
    1888             : 
    1889             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1890       93875 :         q_temp[0] = sub( add( add( q_cldfb, min_q_shift[0] ), add( q_cldfb, min_q_shift[0] ) ), 31 );
    1891       93875 :         q_temp[1] = sub( add( add( q_cldfb, min_q_shift[1] ), add( q_cldfb, min_q_shift[1] ) ), 31 );
    1892             : #else
    1893             :         q_temp = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    1894             :         move16();
    1895             : #endif
    1896             : 
    1897     5630375 :         FOR( l = 0; l < num_freq_bands; l++ )
    1898             :         {
    1899             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1900     5536500 :             Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    1901             : 
    1902     5536500 :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    1903     5536500 :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    1904     5536500 :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    1905     5536500 :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    1906             : #else
    1907             :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1908             :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1909             :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1910             :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+min_q_shift
    1911             : #endif
    1912             : 
    1913     5536500 :             Real_aux_fx = L_add( re1, re2 ); // q_cldfb+min_q_shift
    1914     5536500 :             Imag_aux_fx = L_add( im1, im2 ); // q_cldfb+min_q_shift
    1915             : 
    1916             :             // Left_power_fx = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 );
    1917     5536500 :             W_tmp1 = W_add( W_mult0_32_32( re1, re1 ), W_mult0_32_32( im1, im1 ) ); // 2*(q_cldfb+min_q_shift)
    1918             :             // Right_power_fx = Madd_32_32( Mpy_32_32( re2, re2 ), im2, im2 );
    1919     5536500 :             W_tmp2 = W_add( W_mult0_32_32( re2, re2 ), W_mult0_32_32( im2, im2 ) ); // 2*(q_cldfb+min_q_shift)
    1920             : 
    1921             : 
    1922             :             // reference_power_fx[l] = L_add( Left_power_fx, Right_power_fx );
    1923     5536500 :             reference_power_64fx[l] = W_add( W_tmp1, W_tmp2 ); // 2*(q_cldfb+min_q_shift)
    1924     5536500 :             move64();
    1925             : 
    1926     5536500 :             temp = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx ); // 2*(q_cldfb+min_q_shift)-31
    1927             : 
    1928             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1929     5536500 :             IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    1930             : #else
    1931             :             IF( LT_16( q_temp, *q_proto_power_smooth ) )
    1932             : #endif
    1933             :             {
    1934             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1935     4328500 :                 proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    1936             : #else
    1937             :                 proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    1938             : #endif
    1939     4328500 :                 move32();
    1940             :             }
    1941             :             ELSE
    1942             :             {
    1943             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1944     1208000 :                 proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    1945             : #else
    1946             :                 proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    1947             : #endif
    1948     1208000 :                 move32();
    1949             :             }
    1950             : 
    1951     5536500 :             p_proto_buffer_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    1952     5536500 :             move32();
    1953     5536500 :             p_proto_buffer_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    1954     5536500 :             move32();
    1955             : 
    1956     5536500 :             temp = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 ); // 2*(q_cldfb+min_q_shift)-31
    1957             : 
    1958             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1959     5536500 :             IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    1960             : #else
    1961             :             IF( LT_16( q_temp, *q_proto_power_smooth ) )
    1962             : #endif
    1963             :             {
    1964             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1965     4328500 :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    1966             : #else
    1967             :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    1968             : #endif
    1969     4328500 :                 move32();
    1970             :             }
    1971             :             ELSE
    1972             :             {
    1973             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1974     1208000 :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    1975             : #else
    1976             :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    1977             : #endif
    1978     1208000 :                 move32();
    1979             :             }
    1980             : 
    1981     5536500 :             p_proto_buffer_fx[2 * ( num_freq_bands + l )] = re1; // q_cldfb+min_q_shift
    1982     5536500 :             move32();
    1983     5536500 :             p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = im1; // q_cldfb+min_q_shift
    1984     5536500 :             move32();
    1985             : 
    1986     5536500 :             temp = Madd_32_32( Mpy_32_32( re2, re2 ), im2, im2 ); // 2*(q_cldfb+min_q_shift)-31
    1987             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1988     5536500 :             IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    1989             : #else
    1990             :             IF( LT_16( q_temp, *q_proto_power_smooth ) )
    1991             : #endif
    1992             :             {
    1993             : #ifdef FIX_867_CLDFB_NRG_SCALE
    1994     4328500 :                 proto_power_smooth_fx[l + ( 2 * num_freq_bands )] = L_add( L_shr( proto_power_smooth_fx[l + ( 2 * num_freq_bands )], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    1995             : #else
    1996             :                 proto_power_smooth_fx[l + ( 2 * num_freq_bands )] = L_add( L_shr( proto_power_smooth_fx[l + ( 2 * num_freq_bands )], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    1997             : #endif
    1998     4328500 :                 move32();
    1999             :             }
    2000             :             ELSE
    2001             :             {
    2002             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2003     1208000 :                 proto_power_smooth_fx[l + ( 2 * num_freq_bands )] = L_add( proto_power_smooth_fx[l + ( 2 * num_freq_bands )], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2004             : #else
    2005             :                 proto_power_smooth_fx[l + ( 2 * num_freq_bands )] = L_add( proto_power_smooth_fx[l + ( 2 * num_freq_bands )], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2006             : #endif
    2007     1208000 :                 move32();
    2008             :             }
    2009             : 
    2010     5536500 :             p_proto_buffer_fx[4 * num_freq_bands + 2 * l] = re2; // q_cldfb+min_q_shift
    2011     5536500 :             move32();
    2012     5536500 :             p_proto_buffer_fx[4 * num_freq_bands + 2 * l + 1] = im2; // q_cldfb+min_q_shift
    2013     5536500 :             move32();
    2014             : 
    2015     5536500 :             proto_frame_f_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    2016     5536500 :             move32();
    2017     5536500 :             proto_frame_f_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    2018     5536500 :             move32();
    2019             : 
    2020     5536500 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l] = re1; // q_cldfb+min_q_shift
    2021     5536500 :             move32();
    2022     5536500 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l + 1] = im1; // q_cldfb+min_q_shift
    2023     5536500 :             move32();
    2024     5536500 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l] = re2; // q_cldfb+min_q_shift
    2025     5536500 :             move32();
    2026     5536500 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l + 1] = im2; // q_cldfb+min_q_shift
    2027     5536500 :             move32();
    2028             :         }
    2029             :     }
    2030       81199 :     ELSE IF( stereo_type_detect != NULL )
    2031             :     {
    2032       61943 :         p_proto_buffer_fx = proto_direct_buffer_f_fx + ( slot_index * 4 * num_freq_bands ); // q_proto_direct_buffer_f
    2033             : 
    2034       61943 :         left_bb_power_fx = 0;
    2035       61943 :         move32();
    2036       61943 :         right_bb_power_fx = 0;
    2037       61943 :         move32();
    2038       61943 :         total_bb_power_fx = 0;
    2039       61943 :         move32();
    2040             : 
    2041       61943 :         left_hi_power_fx = 0;
    2042       61943 :         move32();
    2043       61943 :         right_hi_power_fx = 0;
    2044       61943 :         move32();
    2045       61943 :         total_hi_power_fx = 0;
    2046       61943 :         move32();
    2047             : 
    2048       61943 :         dipole_freq_range[0] = stereo_type_detect->dipole_freq_range[0];
    2049       61943 :         move16();
    2050       61943 :         dipole_freq_range[1] = stereo_type_detect->dipole_freq_range[1];
    2051       61943 :         move16();
    2052             : 
    2053       61943 :         a_fx = 21474836; /*0.01 in Q31*/ /* Temporal smoothing coefficient */
    2054       61943 :         move32();
    2055       61943 :         b_fx = L_sub( ONE_IN_Q31, a_fx ); /* Temporal smoothing coefficient q31*/
    2056       61943 :         move32();
    2057       61943 :         a2_fx = 214748365; /*0.1 in Q31*/ /* Temporal smoothing coefficient */
    2058       61943 :         move32();
    2059       61943 :         b2_fx = L_sub( ONE_IN_Q31, a2_fx ); /* Temporal smoothing coefficient */
    2060             : 
    2061       61943 :         IF( stereo_type_detect->interpolator > 0 )
    2062             :         {
    2063          30 :             IF( EQ_16( stereo_type_detect->type_change_direction, MASA_STEREO_SPACED_MICS ) )
    2064             :             {
    2065             :                 /* interpolatorSpaced = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS ); */
    2066          30 :                 interpolatorSpaced_fx = i_mult_sat( stereo_type_detect->interpolator, 2048 /* 1 / MASA_STEREO_INTERPOLATION_SLOTS in Q15 */ ); /* Q15 */
    2067          30 :                 interpolatorDmx_fx = sub( MAX16B, interpolatorSpaced_fx );                                                                     /* Q15 */
    2068             :             }
    2069             :             ELSE
    2070             :             {
    2071             :                 /* interpolatorDmx = ( (float) ( stereo_type_detect->interpolator ) ) / ( (float) MASA_STEREO_INTERPOLATION_SLOTS ); */
    2072           0 :                 interpolatorDmx_fx = i_mult_sat( stereo_type_detect->interpolator, 2048 /* 1 / MASA_STEREO_INTERPOLATION_SLOTS in Q15 */ ); /* Q15 */
    2073           0 :                 interpolatorSpaced_fx = sub( MAX16B, interpolatorDmx_fx );                                                                  /* Q15 */
    2074             :             }
    2075             :         }
    2076             : 
    2077             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2078             :         Word16 total_shift[2];
    2079             :         /* total_shift shift required to get common Q of sum power values */
    2080       61943 :         total_shift[0] = shl( s_max( 0, sub( min_q_shift[0], min_q_shift[1] ) ), 1 );
    2081       61943 :         total_shift[1] = shl( s_max( 0, sub( min_q_shift[1], min_q_shift[0] ) ), 1 );
    2082       61943 :         min_q_shift[0] = sub( min_q_shift[0], idiv1616( find_guarded_bits_fx( num_freq_bands ), 2 ) );
    2083       61943 :         min_q_shift[1] = sub( min_q_shift[1], idiv1616( find_guarded_bits_fx( num_freq_bands ), 2 ) );
    2084       61943 :         q_temp[0] = sub( add( add( q_cldfb, min_q_shift[0] ), add( q_cldfb, min_q_shift[0] ) ), 31 );
    2085       61943 :         q_temp[1] = sub( add( add( q_cldfb, min_q_shift[1] ), add( q_cldfb, min_q_shift[1] ) ), 31 );
    2086             : #else
    2087             :         min_q_shift = sub( min_q_shift, idiv1616( find_guarded_bits_fx( num_freq_bands ), 2 ) );
    2088             : 
    2089             :         q_temp = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    2090             : #endif
    2091             : 
    2092       61943 :         head_room = 63;
    2093       61943 :         move16();
    2094     3338383 :         FOR( l = 0; l < num_freq_bands; l++ )
    2095             :         {
    2096             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2097     3276440 :             Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    2098     3276440 :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2099     3276440 :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2100     3276440 :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2101     3276440 :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2102             : #else
    2103             :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    2104             :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+min_q_shift
    2105             :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+min_q_shift
    2106             :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+min_q_shift
    2107             : #endif
    2108             : 
    2109     3276440 :             W_tmp1 = W_add( W_mult0_32_32( re1, re1 ), W_mult0_32_32( im1, im1 ) );
    2110     3276440 :             W_tmp2 = W_add( W_mult0_32_32( re2, re2 ), W_mult0_32_32( im2, im2 ) );
    2111             : 
    2112     3276440 :             head_room = s_min( head_room, W_norm( W_add( W_tmp1, W_tmp2 ) ) );
    2113             :         }
    2114       61943 :         head_room = sub( head_room, find_guarded_bits_fx( num_freq_bands ) );
    2115             : 
    2116             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2117             : #if ( MASA_SUM_FREQ_RANGE_BINS > CLDFB_NO_CHANNELS_HALF )
    2118             : #error MASA_SUM_FREQ_RANGE_BINS must be less than CLDFB_NO_CHANNELS_HALF
    2119             : #endif
    2120       61943 :         q_Left_Right_power = add( shl( add( q_cldfb, min_q_shift[0] ), 1 ), sub( head_room, 32 ) );
    2121             : #else
    2122             :         q_Left_Right_power = add( shl( add( q_cldfb, min_q_shift ), 1 ), sub( head_room, 32 ) );
    2123             : #endif
    2124       61943 :         Word16 exp_left_hi_power = 0, exp_right_hi_power = 0, exp_total_hi_power = 0, exp_temppp;
    2125       61943 :         move16();
    2126       61943 :         move16();
    2127       61943 :         move16();
    2128     3338383 :         FOR( l = 0; l < num_freq_bands; l++ )
    2129             :         {
    2130             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2131     3276440 :             Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    2132     3276440 :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2133     3276440 :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2134     3276440 :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2135     3276440 :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+min_q_shift
    2136             : #else
    2137             :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift );             // q_cldfb+min_q_shift
    2138             :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift );             // q_cldfb+min_q_shift
    2139             :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift );             // q_cldfb+min_q_shift
    2140             :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift );             // q_cldfb+min_q_shift
    2141             : #endif
    2142             : 
    2143             :             /* Compute sum signal */
    2144     3276440 :             Real_aux_fx = L_add( re1, re2 ); // q_cldfb+min_q_shift
    2145     3276440 :             Imag_aux_fx = L_add( im1, im2 ); // q_cldfb+min_q_shift
    2146             : 
    2147             :             /* Compute reference power */
    2148             :             // Left_power_fx = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 );
    2149     3276440 :             W_tmp1 = W_add( W_mult0_32_32( re1, re1 ), W_mult0_32_32( im1, im1 ) ); // 2*(q_cldfb+min_q_shift)
    2150     3276440 :             Left_power_fx = W_extract_h( W_shl( W_tmp1, head_room ) );              // q_Left_Right_power
    2151             : 
    2152             :             // Right_power_fx = Madd_32_32( Mpy_32_32( re2, re2 ), im2, im2 );
    2153     3276440 :             W_tmp2 = W_add( W_mult0_32_32( re2, re2 ), W_mult0_32_32( im2, im2 ) ); // 2*(q_cldfb+min_q_shift)
    2154     3276440 :             Right_power_fx = W_extract_h( W_shl( W_tmp2, head_room ) );             // q_Left_Right_power
    2155             : 
    2156             :             // reference_power_fx[l] = L_add( Left_power_fx, Right_power_fx );
    2157     3276440 :             reference_power_64fx[l] = W_add( W_tmp1, W_tmp2 ); // 2*(q_cldfb+min_q_shift)
    2158     3276440 :             move64();
    2159             : 
    2160             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2161     3276440 :             Left_power_fx = L_shr( Left_power_fx, total_shift[qidx] );
    2162     3276440 :             Right_power_fx = L_shr( Right_power_fx, total_shift[qidx] );
    2163     3276440 :             left_bb_power_fx = L_add( left_bb_power_fx, Left_power_fx );    // q_Left_Right_power
    2164     3276440 :             right_bb_power_fx = L_add( right_bb_power_fx, Right_power_fx ); // q_Left_Right_power
    2165             :             // total_bb_power_fx = L_add( total_bb_power_fx, reference_power_fx[l] );
    2166     3276440 :             total_bb_power_fx = L_add( total_bb_power_fx, W_extract_h( W_shl( reference_power_64fx[l], sub( head_room, total_shift[qidx] ) ) ) ); // q_Left_Right_power
    2167             : #else
    2168             :             left_bb_power_fx = L_add( left_bb_power_fx, Left_power_fx );    // q_Left_Right_power
    2169             :             right_bb_power_fx = L_add( right_bb_power_fx, Right_power_fx ); // q_Left_Right_power
    2170             :             // total_bb_power_fx = L_add( total_bb_power_fx, reference_power_fx[l] );
    2171             :             total_bb_power_fx = L_add( total_bb_power_fx, W_extract_h( W_shl( reference_power_64fx[l], head_room ) ) ); // q_Left_Right_power
    2172             : #endif
    2173     3276440 :             IF( GT_16( l, MASA_HI_FREQ_START_BIN ) )
    2174             :             {
    2175     2347295 :                 W_tmp1 = W_add( W_mult0_32_32( RealBuffer_fx[0][0][l], RealBuffer_fx[0][0][l] ), W_mult0_32_32( ImagBuffer_fx[0][0][l], ImagBuffer_fx[0][0][l] ) );
    2176     2347295 :                 q_shift = W_norm( W_tmp1 );
    2177     2347295 :                 Left_power_fx = W_extract_h( W_shl( W_tmp1, q_shift ) );
    2178     2347295 :                 exp_temppp = sub( 31, sub( add( shl( q_cldfb, 1 ), q_shift ), 32 ) );
    2179             : 
    2180     2347295 :                 left_hi_power_fx = BASOP_Util_Add_Mant32Exp( left_hi_power_fx, exp_left_hi_power, Left_power_fx, exp_temppp, &exp_left_hi_power ); // exp:exp_left_hi_power
    2181             : 
    2182     2347295 :                 W_tmp2 = W_add( W_mult0_32_32( RealBuffer_fx[1][0][l], RealBuffer_fx[1][0][l] ), W_mult0_32_32( ImagBuffer_fx[1][0][l], ImagBuffer_fx[1][0][l] ) );
    2183     2347295 :                 q_shift = W_norm( W_tmp2 );
    2184     2347295 :                 Right_power_fx = W_extract_h( W_shl( W_tmp2, q_shift ) );
    2185     2347295 :                 exp_temppp = sub( 31, sub( add( shl( q_cldfb, 1 ), q_shift ), 32 ) );
    2186             : 
    2187     2347295 :                 right_hi_power_fx = BASOP_Util_Add_Mant32Exp( right_hi_power_fx, exp_right_hi_power, Right_power_fx, exp_temppp, &exp_right_hi_power ); // exp:exp_right_hi_power
    2188             : 
    2189     2347295 :                 W_tmp2 = W_add( W_tmp1, W_tmp2 );
    2190     2347295 :                 q_shift = W_norm( W_tmp2 );
    2191     2347295 :                 Total_power_fx = W_extract_h( W_shl( W_tmp2, q_shift ) );
    2192     2347295 :                 exp_temppp = sub( 31, sub( add( shl( q_cldfb, 1 ), q_shift ), 32 ) );
    2193             : 
    2194     2347295 :                 total_hi_power_fx = BASOP_Util_Add_Mant32Exp( total_hi_power_fx, exp_total_hi_power, Total_power_fx, exp_temppp, &exp_total_hi_power ); // exp:exp_total_hi_power
    2195             :             }
    2196             : 
    2197     3276440 :             IF( LT_16( l, s_min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ) ) )
    2198             :             {
    2199     1524575 :                 re_aux = L_add( L_shr( RealBuffer_fx[0][0][l], 1 ), L_shr( RealBuffer_fx[1][0][l], 1 ) );
    2200     1524575 :                 im_aux = L_add( L_shr( ImagBuffer_fx[0][0][l], 1 ), L_shr( ImagBuffer_fx[1][0][l], 1 ) );
    2201             : 
    2202     1524575 :                 W_tmp3 = W_add( W_mult0_32_32( re_aux, re_aux ), W_mult0_32_32( im_aux, im_aux ) );
    2203     1524575 :                 q_shift = W_norm( W_tmp3 );
    2204     1524575 :                 sum_power_fx = W_extract_h( W_shl( W_tmp3, q_shift ) );
    2205     1524575 :                 exp_temppp = sub( 31, sub( add( shl( sub( q_cldfb, 1 ), 1 ), q_shift ), 32 ) );
    2206             : 
    2207     1524575 :                 stereo_type_detect->sum_power_fx[l] = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, sum_power_fx ), exp_temppp, Mpy_32_32( b_fx, stereo_type_detect->sum_power_fx[l] ), stereo_type_detect->exp_sum_power[l], &stereo_type_detect->exp_sum_power[l] );
    2208     1524575 :                 move32();
    2209     1524575 :                 W_tmp1 = W_add( W_mult0_32_32( RealBuffer_fx[0][0][l], RealBuffer_fx[0][0][l] ), W_mult0_32_32( ImagBuffer_fx[0][0][l], ImagBuffer_fx[0][0][l] ) );
    2210     1524575 :                 W_tmp2 = W_add( W_mult0_32_32( RealBuffer_fx[1][0][l], RealBuffer_fx[1][0][l] ), W_mult0_32_32( ImagBuffer_fx[1][0][l], ImagBuffer_fx[1][0][l] ) );
    2211             : 
    2212     1524575 :                 W_tmp2 = W_add( W_tmp1, W_tmp2 );
    2213     1524575 :                 q_shift = W_norm( W_tmp2 );
    2214     1524575 :                 Total_power_fx = W_extract_h( W_shl( W_tmp2, q_shift ) );
    2215     1524575 :                 exp_temppp = sub( 31, sub( add( shl( q_cldfb, 1 ), q_shift ), 32 ) );
    2216             : 
    2217     1524575 :                 stereo_type_detect->total_power_fx[l] = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, Total_power_fx ), exp_temppp, Mpy_32_32( b_fx, stereo_type_detect->total_power_fx[l] ), stereo_type_detect->exp_total_power[l], &stereo_type_detect->exp_total_power[l] );
    2218     1524575 :                 move32();
    2219             : 
    2220     1524575 :                 test();
    2221     1524575 :                 IF( ( stereo_type_detect->sum_power_fx[l] == 0 ) && ( stereo_type_detect->total_power_fx[l] == 0 ) )
    2222             :                 {
    2223         152 :                     sum_total_ratio_fx[l] = MAX_32; // q15
    2224         152 :                     move32();
    2225         152 :                     exp_sum_total_ratio_fx[l] = 16;
    2226         152 :                     move16();
    2227             :                 }
    2228     1524423 :                 ELSE IF( stereo_type_detect->total_power_fx[l] == 0 )
    2229             :                 {
    2230           0 :                     sum_total_ratio_fx[l] = MAX_32; // q15
    2231           0 :                     move32();
    2232           0 :                     exp_sum_total_ratio_fx[l] = 16;
    2233           0 :                     move16();
    2234             :                 }
    2235             :                 ELSE
    2236             :                 {
    2237     1524423 :                     sum_total_ratio_fx[l] = BASOP_Util_Divide3232_Scale_newton( stereo_type_detect->sum_power_fx[l], stereo_type_detect->total_power_fx[l], &exp ); // 15-(exp+s_min( stereo_type_detect->q_total_power, q_temp )-s_min( stereo_type_detect->q_sum_power, q_temp2 ))
    2238     1524423 :                     move32();
    2239     1524423 :                     exp_sum_total_ratio_fx[l] = add( exp, sub( stereo_type_detect->exp_sum_power[l], stereo_type_detect->exp_total_power[l] ) );
    2240     1524423 :                     move16();
    2241             :                 }
    2242     1524575 :                 q_sum_total_ratio = s_max( q_sum_total_ratio, exp_sum_total_ratio_fx[l] );
    2243             :             }
    2244             : 
    2245     3276440 :             IF( l == 0 )
    2246             :             {
    2247       61943 :                 RealSubtract_fx = L_sub( re1, re2 ); // q_cldfb+min_q_shift
    2248       61943 :                 ImagSubtract_fx = L_sub( im1, im2 ); // q_cldfb+min_q_shift
    2249             : 
    2250       61943 :                 temp = Madd_32_32( Mpy_32_32( RealSubtract_fx, RealSubtract_fx ), ImagSubtract_fx, ImagSubtract_fx ); // 2*(q_cldfb+min_q_shift)-31
    2251             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2252       61943 :                 assert( qidx == 0 );
    2253       61943 :                 IF( LT_16( q_temp[qidx], stereo_type_detect->q_subtract_power_y ) )
    2254             :                 {
    2255       23768 :                     stereo_type_detect->subtract_power_y_fx = L_add( L_shr( stereo_type_detect->subtract_power_y_fx, sub( stereo_type_detect->q_subtract_power_y, q_temp[qidx] ) ), temp ); // q_temp
    2256       23768 :                     move32();
    2257       23768 :                     stereo_type_detect->q_subtract_power_y = q_temp[qidx];
    2258       23768 :                     move16();
    2259             :                 }
    2260             :                 ELSE
    2261             :                 {
    2262       38175 :                     stereo_type_detect->subtract_power_y_fx = L_add( stereo_type_detect->subtract_power_y_fx, L_shr( temp, sub( q_temp[qidx], stereo_type_detect->q_subtract_power_y ) ) ); // stereo_type_detect->q_subtract_power_y
    2263       38175 :                     move32();
    2264             :                 }
    2265             : 
    2266             : #else
    2267             :                 IF( LT_16( q_temp, stereo_type_detect->q_subtract_power_y ) )
    2268             :                 {
    2269             :                     stereo_type_detect->subtract_power_y_fx = L_add( L_shr( stereo_type_detect->subtract_power_y_fx, sub( stereo_type_detect->q_subtract_power_y, q_temp ) ), temp ); // q_temp
    2270             :                     move32();
    2271             :                     stereo_type_detect->q_subtract_power_y = q_temp;
    2272             :                     move16();
    2273             :                 }
    2274             :                 ELSE
    2275             :                 {
    2276             :                     stereo_type_detect->subtract_power_y_fx = L_add( stereo_type_detect->subtract_power_y_fx, L_shr( temp, sub( q_temp, stereo_type_detect->q_subtract_power_y ) ) ); // stereo_type_detect->q_subtract_power_y
    2277             :                     move32();
    2278             :                 }
    2279             : #endif
    2280             :             }
    2281             : 
    2282             :             /* Compute protos (and their power) for direct sound rendering */
    2283             : 
    2284             :             /* W prototype */
    2285     3276440 :             IF( stereo_type_detect->interpolator > 0 )
    2286             :             {
    2287        1800 :                 IF( L_or( LT_16( l, sub( dipole_freq_range[1], 1 ) ), GE_16( l, MASA_SUM_PROTO_START_BIN ) ) )
    2288             :                 {
    2289        1650 :                     Real_aux_fx = Madd_32_16( Mpy_32_16_1( Real_aux_fx, shr( interpolatorSpaced_fx, 1 ) ), Real_aux_fx, interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2290        1650 :                     Imag_aux_fx = Madd_32_16( Mpy_32_16_1( Imag_aux_fx, shr( interpolatorSpaced_fx, 1 ) ), Imag_aux_fx, interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2291             : 
    2292        1650 :                     temp = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx ); // 2*(q_cldfb+min_q_shift) -31
    2293             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2294        1650 :                     IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2295             : #else
    2296             :                     IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2297             : #endif
    2298             :                     {
    2299             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2300        1650 :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2301             : #else
    2302             :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2303             : #endif
    2304        1650 :                         move32();
    2305             :                     }
    2306             :                     ELSE
    2307             :                     {
    2308             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2309           0 :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2310             : #else
    2311             :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2312             : #endif
    2313           0 :                         move32();
    2314             :                     }
    2315             : 
    2316        1650 :                     p_proto_buffer_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    2317        1650 :                     move32();
    2318        1650 :                     p_proto_buffer_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    2319        1650 :                     move32();
    2320             :                 }
    2321             :                 ELSE
    2322             :                 {
    2323         150 :                     tempSpaced_fx = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 );                              // 2*(q_cldfb+min_q_shift)-31
    2324         150 :                     tempDmx_fx = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx ); // 2*(q_cldfb+min_q_shift)-31
    2325             : 
    2326         150 :                     temp = Madd_32_16( Mpy_32_16_1( tempSpaced_fx, interpolatorSpaced_fx ), tempDmx_fx, interpolatorDmx_fx ); // 2*(q_cldfb+min_q_shift)-31
    2327             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2328         150 :                     IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2329             : #else
    2330             :                     IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2331             : #endif
    2332             :                     {
    2333             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2334         150 :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2335             : #else
    2336             :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2337             : #endif
    2338         150 :                         move32();
    2339             :                     }
    2340             :                     ELSE
    2341             :                     {
    2342             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2343           0 :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2344             : #else
    2345             :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2346             : #endif
    2347           0 :                         move32();
    2348             :                     }
    2349             : 
    2350         150 :                     p_proto_buffer_fx[2 * l] = Madd_32_16( Mpy_32_16_1( re1, interpolatorSpaced_fx ), Real_aux_fx, interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2351         150 :                     move32();
    2352         150 :                     p_proto_buffer_fx[2 * l + 1] = Madd_32_16( Mpy_32_16_1( im1, interpolatorSpaced_fx ), Imag_aux_fx, interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2353         150 :                     move32();
    2354             :                 }
    2355             :             }
    2356     3274640 :             ELSE IF( EQ_16( stereo_type_detect->masa_stereo_type, MASA_STEREO_SPACED_MICS ) )
    2357             :             {
    2358      526080 :                 IF( L_or( LT_16( l, sub( dipole_freq_range[1], 1 ) ), GE_16( l, MASA_SUM_PROTO_START_BIN ) ) )
    2359             :                 {
    2360      482240 :                     Real_aux_fx = L_shr( Real_aux_fx, 1 );                                                // q_cldfb+min_q_shift
    2361      482240 :                     Imag_aux_fx = L_shr( Imag_aux_fx, 1 );                                                // q_cldfb+min_q_shift
    2362      482240 :                     temp = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx ); // 2*(q_cldfb+min_q_shift)-31
    2363             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2364      482240 :                     IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2365             : #else
    2366             :                     IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2367             : #endif
    2368             :                     {
    2369             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2370      482240 :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2371             : #else
    2372             :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2373             : #endif
    2374      482240 :                         move32();
    2375             :                     }
    2376             :                     ELSE
    2377             :                     {
    2378             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2379           0 :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2380             : #else
    2381             :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2382             : #endif
    2383           0 :                         move32();
    2384             :                     }
    2385             : 
    2386      482240 :                     p_proto_buffer_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    2387      482240 :                     move32();
    2388      482240 :                     p_proto_buffer_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    2389      482240 :                     move32();
    2390             :                 }
    2391             :                 ELSE
    2392             :                 {
    2393       43840 :                     temp = Madd_32_32( Mpy_32_32( re1, re1 ), im1, im1 ); // 2*(q_cldfb+min_q_shift)-31
    2394             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2395       43840 :                     IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2396             : #else
    2397             :                     IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2398             : #endif
    2399             :                     {
    2400             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2401       43840 :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2402             : #else
    2403             :                         proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2404             : #endif
    2405       43840 :                         move32();
    2406             :                     }
    2407             :                     ELSE
    2408             :                     {
    2409             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2410           0 :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2411             : #else
    2412             :                         proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2413             : #endif
    2414           0 :                         move32();
    2415             :                     }
    2416             : 
    2417       43840 :                     p_proto_buffer_fx[2 * l] = re1; // q_cldfb+min_q_shift
    2418       43840 :                     move32();
    2419       43840 :                     p_proto_buffer_fx[2 * l + 1] = im1; // q_cldfb+min_q_shift
    2420       43840 :                     move32();
    2421             :                 }
    2422             :             }
    2423             :             ELSE
    2424             :             {
    2425     2748560 :                 temp = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx ); // 2*(q_cldfb+min_q_shift)-31
    2426             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2427     2748560 :                 IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2428             : #else
    2429             :                 IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2430             : #endif
    2431             :                 {
    2432             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2433     2748050 :                     proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2434             : #else
    2435             :                     proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2436             : #endif
    2437     2748050 :                     move32();
    2438             :                 }
    2439             :                 ELSE
    2440             :                 {
    2441             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2442         510 :                     proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2443             : #else
    2444             :                     proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2445             : #endif
    2446         510 :                     move32();
    2447             :                 }
    2448             : 
    2449     2748560 :                 p_proto_buffer_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    2450     2748560 :                 move32();
    2451     2748560 :                 p_proto_buffer_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    2452     2748560 :                 move32();
    2453             :             }
    2454             : 
    2455             :             /* Y prototype */
    2456     3276440 :             IF( stereo_type_detect->interpolator > 0 )
    2457             :             {
    2458        1800 :                 IF( LT_16( l, dipole_freq_range[0] ) )
    2459             :                 {
    2460          30 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = Madd_32_16( Mpy_32_16_1( p_proto_buffer_fx[2 * l], interpolatorSpaced_fx ), L_sub( re1, re2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2461          30 :                     move32();
    2462          30 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = Madd_32_16( Mpy_32_16_1( p_proto_buffer_fx[2 * l + 1], interpolatorSpaced_fx ), L_sub( im1, im2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2463          30 :                     move32();
    2464             :                 }
    2465        1770 :                 ELSE IF( LT_16( l, dipole_freq_range[1] ) )
    2466             :                 {
    2467          60 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = Madd_32_16( Mpy_32_16_1( L_sub( im1, im2 ), interpolatorSpaced_fx ), L_sub( re1, re2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2468          60 :                     move32();
    2469          60 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = Madd_32_16( Mpy_32_16_1( -L_sub( re1, re2 ), interpolatorSpaced_fx ), L_sub( im1, im2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2470          60 :                     move32();
    2471             :                 }
    2472             :                 ELSE
    2473             :                 {
    2474        1710 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = Madd_32_16( Mpy_32_16_1( p_proto_buffer_fx[2 * l], interpolatorSpaced_fx ), L_sub( re1, re2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2475        1710 :                     move32();
    2476        1710 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = Madd_32_16( Mpy_32_16_1( p_proto_buffer_fx[2 * l + 1], interpolatorSpaced_fx ), L_sub( im1, im2 ), interpolatorDmx_fx ); // q_cldfb+min_q_shift
    2477        1710 :                     move32();
    2478             :                 }
    2479             : 
    2480        1800 :                 temp = Madd_32_32( Mpy_32_32( p_proto_buffer_fx[2 * ( num_freq_bands + l )], p_proto_buffer_fx[2 * ( num_freq_bands + l )] ), p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1], p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] ); // 2*(q_cldfb+min_q_shift)-31
    2481             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2482        1800 :                 IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2483             : #else
    2484             :                 IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2485             : #endif
    2486             :                 {
    2487             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2488        1800 :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2489             : #else
    2490             :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2491             : #endif
    2492        1800 :                     move32();
    2493             :                 }
    2494             :                 ELSE
    2495             :                 {
    2496             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2497           0 :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2498             : #else
    2499             :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2500             : #endif
    2501           0 :                     move32();
    2502             :                 }
    2503             :             }
    2504     3274640 :             ELSE IF( EQ_16( stereo_type_detect->masa_stereo_type, MASA_STEREO_SPACED_MICS ) )
    2505             :             {
    2506      526080 :                 IF( LT_16( l, dipole_freq_range[0] ) ) /* proto = W */
    2507             :                 {
    2508        8768 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = p_proto_buffer_fx[2 * l]; // q_cldfb+min_q_shift
    2509        8768 :                     move32();
    2510        8768 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = p_proto_buffer_fx[2 * l + 1]; // q_cldfb+min_q_shift
    2511        8768 :                     move32();
    2512        8768 :                     proto_power_smooth_fx[l + num_freq_bands] = proto_power_smooth_fx[l];
    2513        8768 :                     move32();
    2514             :                 }
    2515      517312 :                 ELSE IF( LT_16( l, dipole_freq_range[1] ) ) /* proto = -i * (x1-x2) * eq */
    2516             :                 {
    2517       17536 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = L_sub( im1, im2 ); // q_cldfb+min_q_shift
    2518       17536 :                     move32();
    2519       17536 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = -L_sub( re1, re2 ); // q_cldfb+min_q_shift
    2520       17536 :                     move32();
    2521             : 
    2522       17536 :                     temp = Madd_32_32( Mpy_32_32( p_proto_buffer_fx[2 * ( num_freq_bands + l )], p_proto_buffer_fx[2 * ( num_freq_bands + l )] ), p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1], p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] ); // 2*(q_cldfb+min_q_shift)-31
    2523             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2524       17536 :                     IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2525             : #else
    2526             :                     IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2527             : #endif
    2528             :                     {
    2529             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2530       17536 :                         proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2531             : #else
    2532             :                         proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2533             : #endif
    2534       17536 :                         move32();
    2535             :                     }
    2536             :                     ELSE
    2537             :                     {
    2538             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2539           0 :                         proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2540             : #else
    2541             :                         proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2542             : #endif
    2543           0 :                         move32();
    2544             :                     }
    2545             :                 }
    2546             :                 ELSE /* proto = W */
    2547             :                 {
    2548      499776 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l )] = p_proto_buffer_fx[2 * l]; // q_cldfb+min_q_shift
    2549      499776 :                     move32();
    2550      499776 :                     p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = p_proto_buffer_fx[2 * l + 1]; // q_cldfb+min_q_shift
    2551      499776 :                     move32();
    2552      499776 :                     proto_power_smooth_fx[l + num_freq_bands] = proto_power_smooth_fx[l]; // min( q_temp, *q_proto_power_smooth )
    2553      499776 :                     move32();
    2554             :                 }
    2555             :             }
    2556             :             ELSE
    2557             :             {
    2558     2748560 :                 p_proto_buffer_fx[2 * ( num_freq_bands + l )] = L_sub( re1, re2 ); // q_cldfb+min_q_shift
    2559     2748560 :                 move32();
    2560     2748560 :                 p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = L_sub( im1, im2 ); // q_cldfb+min_q_shift
    2561     2748560 :                 move32();
    2562             : 
    2563     2748560 :                 temp = Madd_32_32( Mpy_32_32( p_proto_buffer_fx[2 * ( num_freq_bands + l )], p_proto_buffer_fx[2 * ( num_freq_bands + l )] ), p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1], p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] ); // 2*(q_cldfb+min_q_shift)-31
    2564             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2565     2748560 :                 IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2566             : #else
    2567             :                 IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2568             : #endif
    2569             :                 {
    2570             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2571     2748050 :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2572             : #else
    2573             :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2574             : #endif
    2575     2748050 :                     move32();
    2576             :                 }
    2577             :                 ELSE
    2578             :                 {
    2579             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2580         510 :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2581             : #else
    2582             :                     proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2583             : #endif
    2584         510 :                     move32();
    2585             :                 }
    2586             :             }
    2587             : 
    2588             :             /* Compute protos for decorrelation */
    2589     3276440 :             proto_frame_f_fx[2 * l] = Real_aux_fx; // q_cldfb+min_q_shift
    2590     3276440 :             move32();
    2591     3276440 :             proto_frame_f_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+min_q_shift
    2592     3276440 :             move32();
    2593     3276440 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l] = re1; // q_cldfb+min_q_shift
    2594     3276440 :             move32();
    2595     3276440 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l + 1] = im1; // q_cldfb+min_q_shift
    2596     3276440 :             move32();
    2597     3276440 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l] = re2; // q_cldfb+min_q_shift
    2598     3276440 :             move32();
    2599     3276440 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l + 1] = im2; // q_cldfb+min_q_shift
    2600     3276440 :             move32();
    2601             :         }
    2602             : 
    2603       61943 :         IF( stereo_type_detect->interpolator > 0 )
    2604             :         {
    2605          30 :             stereo_type_detect->interpolator++;
    2606          30 :             IF( EQ_16( stereo_type_detect->interpolator, MASA_STEREO_INTERPOLATION_SLOTS ) )
    2607             :             {
    2608           2 :                 stereo_type_detect->interpolator = 0;
    2609           2 :                 move16();
    2610           2 :                 stereo_type_detect->current_stereo_type = stereo_type_detect->type_change_direction;
    2611           2 :                 move32();
    2612             :             }
    2613             :         }
    2614             : 
    2615       61943 :         temp = Mpy_32_32( a_fx, left_bb_power_fx ); // q_Left_Right_power
    2616       61943 :         IF( LT_16( q_Left_Right_power, stereo_type_detect->q_left_bb_power ) )
    2617             :         {
    2618         607 :             stereo_type_detect->left_bb_power_fx = L_add( temp, L_shr( Mpy_32_32( b_fx, stereo_type_detect->left_bb_power_fx ), sub( stereo_type_detect->q_left_bb_power, q_Left_Right_power ) ) ); // q_Left_Right_power
    2619         607 :             move32();
    2620         607 :             stereo_type_detect->q_left_bb_power = q_Left_Right_power;
    2621         607 :             move16();
    2622             :         }
    2623             :         ELSE
    2624             :         {
    2625       61336 :             stereo_type_detect->left_bb_power_fx = Madd_32_32( L_shr( temp, sub( q_Left_Right_power, stereo_type_detect->q_left_bb_power ) ), b_fx, stereo_type_detect->left_bb_power_fx ); // stereo_type_detect->q_left_bb_power
    2626       61336 :             move32();
    2627             :         }
    2628             : 
    2629       61943 :         temp = Mpy_32_32( a_fx, right_bb_power_fx ); // q_Left_Right_power
    2630       61943 :         IF( LT_16( q_Left_Right_power, stereo_type_detect->q_right_bb_power ) )
    2631             :         {
    2632         607 :             stereo_type_detect->right_bb_power_fx = L_add( temp, L_shr( Mpy_32_32( b_fx, stereo_type_detect->right_bb_power_fx ), sub( stereo_type_detect->q_right_bb_power, q_Left_Right_power ) ) ); // q_Left_Right_power
    2633         607 :             move32();
    2634         607 :             stereo_type_detect->q_right_bb_power = q_Left_Right_power;
    2635         607 :             move16();
    2636             :         }
    2637             :         ELSE
    2638             :         {
    2639       61336 :             stereo_type_detect->right_bb_power_fx = Madd_32_32( L_shr( temp, sub( q_Left_Right_power, stereo_type_detect->q_right_bb_power ) ), b_fx, stereo_type_detect->right_bb_power_fx ); // stereo_type_detect->q_right_bb_power
    2640       61336 :             move32();
    2641             :         }
    2642             : 
    2643       61943 :         temp = Mpy_32_32( a_fx, total_bb_power_fx ); // q_Left_Right_power
    2644       61943 :         IF( LT_16( q_Left_Right_power, stereo_type_detect->q_total_bb_power ) )
    2645             :         {
    2646         607 :             stereo_type_detect->total_bb_power_fx = L_add( temp, L_shr( Mpy_32_32( b_fx, stereo_type_detect->total_bb_power_fx ), sub( stereo_type_detect->q_total_bb_power, q_Left_Right_power ) ) ); // q_Left_Right_power
    2647         607 :             move32();
    2648         607 :             stereo_type_detect->q_total_bb_power = q_Left_Right_power;
    2649         607 :             move16();
    2650             :         }
    2651             :         ELSE
    2652             :         {
    2653       61336 :             stereo_type_detect->total_bb_power_fx = Madd_32_32( L_shr( temp, sub( q_Left_Right_power, stereo_type_detect->q_total_bb_power ) ), b_fx, stereo_type_detect->total_bb_power_fx ); // stereo_type_detect->q_total_bb_power
    2654       61336 :             move32();
    2655             :         }
    2656             : 
    2657       61943 :         IF( LT_16( stereo_type_detect->q_left_bb_power, stereo_type_detect->q_right_bb_power ) )
    2658             :         {
    2659           0 :             lr_bb_power_fx = L_min( stereo_type_detect->left_bb_power_fx, L_shr( stereo_type_detect->right_bb_power_fx, sub( stereo_type_detect->q_right_bb_power, stereo_type_detect->q_left_bb_power ) ) ); // stereo_type_detect->q_left_bb_power
    2660           0 :             move32();
    2661           0 :             q_lr_bb_power = stereo_type_detect->q_left_bb_power;
    2662           0 :             move16();
    2663             :         }
    2664             :         ELSE
    2665             :         {
    2666       61943 :             lr_bb_power_fx = L_min( L_shr( stereo_type_detect->left_bb_power_fx, sub( stereo_type_detect->q_left_bb_power, stereo_type_detect->q_right_bb_power ) ), stereo_type_detect->right_bb_power_fx ); // stereo_type_detect->q_right_bb_power
    2667       61943 :             move32();
    2668       61943 :             q_lr_bb_power = stereo_type_detect->q_right_bb_power;
    2669       61943 :             move16();
    2670             :         }
    2671       61943 :         q_lr_bb_power = sub( q_lr_bb_power, 1 );                                                                                       /* = (lr_bb_power_fx * 2) */
    2672       61943 :         temp = BASOP_Util_Divide3232_Scale_newton( lr_bb_power_fx, L_add( stereo_type_detect->total_bb_power_fx, EPSILON_FX ), &exp ); // Q(31-(exp+stereo_type_detect->q_total_bb_power-q_lr_bb_power))
    2673       61943 :         exp = sub( 31, add( sub( 31, exp ), sub( q_lr_bb_power, stereo_type_detect->q_total_bb_power ) ) );
    2674       61943 :         temp = BASOP_Util_Log2( temp ); // q25
    2675       61943 :         IF( NE_32( temp, MIN_32 ) )
    2676             :         {
    2677       61936 :             temp = Mpy_32_32( L_add( L_shl( exp, Q25 ), temp ), LOG10_2_Q31 ); // Q25
    2678             :         }
    2679             :         // 20480 = 10 in Q11
    2680       61943 :         lr_total_bb_ratio_fx = Mpy_32_16_1( temp, 20480 ); // Q21
    2681             : 
    2682             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2683       61943 :         stereo_type_detect->left_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, left_hi_power_fx ), exp_left_hi_power, Mpy_32_32( b2_fx, stereo_type_detect->left_hi_power_fx ), sub( 31, stereo_type_detect->q_left_hi_power ), &stereo_type_detect->q_left_hi_power );
    2684       61943 :         move32();
    2685       61943 :         stereo_type_detect->q_left_hi_power = sub( 31, stereo_type_detect->q_left_hi_power );
    2686       61943 :         move16();
    2687             : 
    2688       61943 :         stereo_type_detect->right_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, right_hi_power_fx ), exp_right_hi_power, Mpy_32_32( b2_fx, stereo_type_detect->right_hi_power_fx ), sub( 31, stereo_type_detect->q_right_hi_power ), &stereo_type_detect->q_right_hi_power );
    2689       61943 :         move32();
    2690       61943 :         stereo_type_detect->q_right_hi_power = sub( 31, stereo_type_detect->q_right_hi_power );
    2691       61943 :         move16();
    2692       61943 :         stereo_type_detect->total_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, total_hi_power_fx ), exp_total_hi_power, Mpy_32_32( b2_fx, stereo_type_detect->total_hi_power_fx ), sub( 31, stereo_type_detect->q_total_hi_power ), &stereo_type_detect->q_total_hi_power );
    2693       61943 :         move32();
    2694             : #else
    2695             :         stereo_type_detect->left_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, left_hi_power_fx ), sub( 31, q_temp ), Mpy_32_32( b2_fx, stereo_type_detect->left_hi_power_fx ), sub( 31, stereo_type_detect->q_left_hi_power ), &stereo_type_detect->q_left_hi_power );
    2696             :         move32();
    2697             :         stereo_type_detect->q_left_hi_power = sub( 31, stereo_type_detect->q_left_hi_power );
    2698             :         move16();
    2699             :         stereo_type_detect->right_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, right_hi_power_fx ), sub( 31, q_temp ), Mpy_32_32( b2_fx, stereo_type_detect->right_hi_power_fx ), sub( 31, stereo_type_detect->q_right_hi_power ), &stereo_type_detect->q_right_hi_power );
    2700             :         move32();
    2701             :         stereo_type_detect->q_right_hi_power = sub( 31, stereo_type_detect->q_right_hi_power );
    2702             :         move16();
    2703             :         stereo_type_detect->total_hi_power_fx = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a2_fx, total_hi_power_fx ), sub( 31, q_temp ), Mpy_32_32( b2_fx, stereo_type_detect->total_hi_power_fx ), sub( 31, stereo_type_detect->q_total_hi_power ), &stereo_type_detect->q_total_hi_power );
    2704             :         move32();
    2705             : #endif
    2706       61943 :         stereo_type_detect->q_total_hi_power = sub( 31, stereo_type_detect->q_total_hi_power );
    2707       61943 :         move16();
    2708             : 
    2709       61943 :         IF( LT_16( stereo_type_detect->q_left_hi_power, stereo_type_detect->q_right_hi_power ) )
    2710             :         {
    2711       22677 :             lr_hi_power_fx = L_min( stereo_type_detect->left_hi_power_fx, L_shr( stereo_type_detect->right_hi_power_fx, sub( stereo_type_detect->q_right_hi_power, stereo_type_detect->q_left_hi_power ) ) ); // stereo_type_detect->q_left_hi_power
    2712       22677 :             move32();
    2713       22677 :             q_lr_hi_power = stereo_type_detect->q_left_hi_power;
    2714             :         }
    2715             :         ELSE
    2716             :         {
    2717       39266 :             lr_hi_power_fx = L_min( L_shr( stereo_type_detect->left_hi_power_fx, sub( stereo_type_detect->q_left_hi_power, stereo_type_detect->q_right_hi_power ) ), stereo_type_detect->right_hi_power_fx ); // stereo_type_detect->q_right_hi_power
    2718       39266 :             move32();
    2719       39266 :             q_lr_hi_power = stereo_type_detect->q_right_hi_power;
    2720             :         }
    2721       61943 :         q_lr_hi_power = sub( q_lr_hi_power, 1 );                                                                                       /* = (q_lr_hi_power * 2) */
    2722       61943 :         temp = BASOP_Util_Divide3232_Scale_newton( lr_hi_power_fx, L_add( stereo_type_detect->total_hi_power_fx, EPSILON_FX ), &exp ); // Q=31-(exp+ stereo_type_detect->q_total_hi_power-q_lr_hi_power)
    2723       61943 :         exp = sub( 31, add( sub( 31, exp ), sub( q_lr_hi_power, stereo_type_detect->q_total_hi_power ) ) );
    2724       61943 :         temp = BASOP_Util_Log2( temp ); // q25
    2725       61943 :         IF( NE_32( temp, MIN_32 ) )
    2726             :         {
    2727       61936 :             temp = Mpy_32_32( L_add( L_shl( exp, Q25 ), temp ), LOG10_2_Q31 ); // Q25
    2728             :         }
    2729             :         // 20480 = 10 in Q11
    2730       61943 :         lr_total_hi_ratio_fx = Mpy_32_16_1( temp, 20480 ); // Q21
    2731             : 
    2732     1586518 :         FOR( Word16 i = 0; i < s_min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ); i++ )
    2733             :         {
    2734     1524575 :             sum_total_ratio_fx[i] = L_shl( sum_total_ratio_fx[i], sub( exp_sum_total_ratio_fx[i], q_sum_total_ratio ) );
    2735     1524575 :             move32();
    2736             :         }
    2737             : 
    2738       61943 :         minimum_l( sum_total_ratio_fx, s_min( num_freq_bands, MASA_SUM_FREQ_RANGE_BINS ), &min_sum_total_ratio_fx ); // q_sum_total_ratio
    2739       61943 :         exp = q_sum_total_ratio;
    2740       61943 :         move16();
    2741       61943 :         temp = BASOP_Util_Log2( min_sum_total_ratio_fx ); // q25
    2742       61943 :         IF( NE_32( temp, MIN_32 ) )
    2743             :         {
    2744       61942 :             temp = Mpy_32_32( L_add( L_shl( exp, Q25 ), temp ), LOG10_2_Q31 ); // Q25
    2745             :         }
    2746             :         // 20480 = 10 in Q11
    2747       61943 :         min_sum_total_ratio_db_fx = Mpy_32_16_1( temp, 20480 ); // Q21
    2748             : 
    2749       61943 :         stereo_type_detect->lr_total_bb_ratio_db_fx = lr_total_bb_ratio_fx; // Q21
    2750       61943 :         move32();
    2751       61943 :         stereo_type_detect->lr_total_hi_ratio_db_fx = lr_total_hi_ratio_fx; // Q21
    2752       61943 :         move32();
    2753       61943 :         stereo_type_detect->min_sum_total_ratio_db_fx = min_sum_total_ratio_db_fx; // Q21
    2754       61943 :         move32();
    2755             : 
    2756       61943 :         ivas_masa_stereotype_detection_fx( stereo_type_detect );
    2757             :     }
    2758             :     ELSE
    2759             :     {
    2760       19256 :         p_proto_buffer_fx = proto_direct_buffer_f_fx + ( slot_index * num_freq_bands * 4 ); // q_proto_direct_buffer_f
    2761             : 
    2762             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2763       19256 :         q_temp[0] = sub( add( add( q_cldfb, min_q_shift[0] ), add( q_cldfb, min_q_shift[0] ) ), 31 );
    2764       19256 :         q_temp[1] = sub( add( add( q_cldfb, min_q_shift[1] ), add( q_cldfb, min_q_shift[1] ) ), 31 );
    2765             : #else
    2766             :         q_temp = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    2767             : #endif
    2768             : 
    2769     1078616 :         FOR( l = 0; l < num_freq_bands; l++ )
    2770             :         {
    2771             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2772     1059360 :             Word16 qidx = s_min( 1, s_max( 0, sub( l, CLDFB_NO_CHANNELS_HALF - 1 ) ) );
    2773     1059360 :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+ min_q_shift
    2774     1059360 :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+ min_q_shift
    2775     1059360 :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift[qidx] ); // q_cldfb+ min_q_shift
    2776     1059360 :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift[qidx] ); // q_cldfb+ min_q_shift
    2777             : #else
    2778             :             re1 = L_shl( RealBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+ min_q_shift
    2779             :             re2 = L_shl( RealBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+ min_q_shift
    2780             :             im1 = L_shl( ImagBuffer_fx[0][0][l], min_q_shift ); // q_cldfb+ min_q_shift
    2781             :             im2 = L_shl( ImagBuffer_fx[1][0][l], min_q_shift ); // q_cldfb+ min_q_shift
    2782             : #endif
    2783             : 
    2784     1059360 :             Real_aux_fx = L_add( re1, re2 ); // q_cldfb+ min_q_shift
    2785     1059360 :             Imag_aux_fx = L_add( im1, im2 ); // q_cldfb+ min_q_shift
    2786             : 
    2787             :             // reference_power_fx[l] = Madd_32_32( Mpy_32_32( Real_aux_fx, Real_aux_fx ), Imag_aux_fx, Imag_aux_fx );
    2788     1059360 :             reference_power_64fx[l] = W_add( W_mult0_32_32( Real_aux_fx, Real_aux_fx ), W_mult0_32_32( Imag_aux_fx, Imag_aux_fx ) ); // q_temp
    2789     1059360 :             move64();
    2790             : 
    2791             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2792     1059360 :             IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2793             : #else
    2794             :             IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2795             : #endif
    2796             :             {
    2797             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2798      743200 :                 proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), W_extract_l( W_shr( reference_power_64fx[l], 31 ) ) ); // q_temp
    2799             : #else
    2800             :                 proto_power_smooth_fx[l] = L_add( L_shr( proto_power_smooth_fx[l], sub( *q_proto_power_smooth, q_temp ) ), W_extract_l( W_shr( reference_power_64fx[l], 31 ) ) ); // q_temp
    2801             : #endif
    2802      743200 :                 move32();
    2803             :             }
    2804             :             ELSE
    2805             :             {
    2806             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2807      316160 :                 proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( W_extract_l( W_shr( reference_power_64fx[l], 31 ) ), sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2808             : #else
    2809             :                 proto_power_smooth_fx[l] = L_add( proto_power_smooth_fx[l], L_shr( W_extract_l( W_shr( reference_power_64fx[l], 31 ) ), sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2810             : #endif
    2811      316160 :                 move32();
    2812             :             }
    2813             : 
    2814     1059360 :             p_proto_buffer_fx[2 * l] = Real_aux_fx; // q_cldfb+ min_q_shift
    2815     1059360 :             move32();
    2816     1059360 :             p_proto_buffer_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+ min_q_shift
    2817     1059360 :             move32();
    2818             : 
    2819     1059360 :             p_proto_buffer_fx[2 * ( num_freq_bands + l )] = L_sub( re1, re2 ); // q_cldfb+ min_q_shift
    2820     1059360 :             move32();
    2821     1059360 :             p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] = L_sub( im1, im2 ); // q_cldfb+ min_q_shift
    2822     1059360 :             move32();
    2823             : 
    2824     1059360 :             temp = Madd_32_32( Mpy_32_32( p_proto_buffer_fx[2 * ( num_freq_bands + l )], p_proto_buffer_fx[2 * ( num_freq_bands + l )] ), p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1], p_proto_buffer_fx[2 * ( num_freq_bands + l ) + 1] ); // q_temp
    2825             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2826     1059360 :             IF( LT_16( q_temp[qidx], q_proto_power_smooth[qidx] ) )
    2827             : #else
    2828             :             IF( LT_16( q_temp, *q_proto_power_smooth ) )
    2829             : #endif
    2830             :             {
    2831             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2832      743200 :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( q_proto_power_smooth[qidx], q_temp[qidx] ) ), temp ); // q_temp
    2833             : #else
    2834             :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( L_shr( proto_power_smooth_fx[l + num_freq_bands], sub( *q_proto_power_smooth, q_temp ) ), temp ); // q_temp
    2835             : #endif
    2836      743200 :                 move32();
    2837             :             }
    2838             :             ELSE
    2839             :             {
    2840             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2841      316160 :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp[qidx], q_proto_power_smooth[qidx] ) ) ); // q_proto_power_smooth
    2842             : #else
    2843             :                 proto_power_smooth_fx[l + num_freq_bands] = L_add( proto_power_smooth_fx[l + num_freq_bands], L_shr( temp, sub( q_temp, *q_proto_power_smooth ) ) ); // q_proto_power_smooth
    2844             : #endif
    2845      316160 :                 move32();
    2846             :             }
    2847             : 
    2848     1059360 :             proto_frame_f_fx[2 * l] = Real_aux_fx; // q_cldfb+ min_q_shift
    2849     1059360 :             move32();
    2850     1059360 :             proto_frame_f_fx[2 * l + 1] = Imag_aux_fx; // q_cldfb+ min_q_shift
    2851     1059360 :             move32();
    2852             : 
    2853     1059360 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l] = re1; // q_cldfb+ min_q_shift
    2854     1059360 :             move32();
    2855     1059360 :             proto_frame_f_fx[2 * num_freq_bands + 2 * l + 1] = im1; // q_cldfb+ min_q_shift
    2856     1059360 :             move32();
    2857     1059360 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l] = re2; // q_cldfb+ min_q_shift
    2858     1059360 :             move32();
    2859     1059360 :             proto_frame_f_fx[4 * num_freq_bands + 2 * l + 1] = im2; // q_cldfb+ min_q_shift
    2860     1059360 :             move32();
    2861             :         }
    2862             :     }
    2863             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2864      175074 :     q_reference_power_64fx = shl( add( q_cldfb, min_q_shift[0] ), 1 );
    2865             : 
    2866      175074 :     Word16 norm_shift = 63;
    2867      175074 :     move16();
    2868     5355294 :     FOR( l = 0; l < s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ); l++ )
    2869             :     {
    2870     5180220 :         IF( reference_power_64fx[l] )
    2871             :         {
    2872     5178905 :             norm_shift = s_min( norm_shift, W_norm( reference_power_64fx[l] ) );
    2873             :         }
    2874             :     }
    2875     5355294 :     FOR( l = 0; l < s_min( num_freq_bands, CLDFB_NO_CHANNELS_HALF ); l++ )
    2876             :     {
    2877     5180220 :         reference_power_fx[l] = W_extract_h( W_shl( reference_power_64fx[l], norm_shift ) ); // q_reference_power_64fx+norm_shift-32
    2878     5180220 :         move32();
    2879             :     }
    2880      175074 :     q_reference_power[0] = sub( add( q_reference_power_64fx, norm_shift ), 32 );
    2881      175074 :     move16();
    2882             : 
    2883      175074 :     q_reference_power_64fx = shl( add( q_cldfb, min_q_shift[1] ), 1 );
    2884      175074 :     norm_shift = 63;
    2885      175074 :     move16();
    2886     4867154 :     FOR( l = CLDFB_NO_CHANNELS_HALF; l < num_freq_bands; l++ )
    2887             :     {
    2888     4692080 :         IF( reference_power_64fx[l] )
    2889             :         {
    2890     4690834 :             norm_shift = s_min( norm_shift, W_norm( reference_power_64fx[l] ) );
    2891             :         }
    2892             :     }
    2893     4867154 :     FOR( l = CLDFB_NO_CHANNELS_HALF; l < num_freq_bands; l++ )
    2894             :     {
    2895     4692080 :         reference_power_fx[l] = W_extract_h( W_shl( reference_power_64fx[l], norm_shift ) ); // q_reference_power_64fx+norm_shift-32
    2896     4692080 :         move32();
    2897             :     }
    2898      175074 :     q_reference_power[1] = sub( add( q_reference_power_64fx, norm_shift ), 32 );
    2899      175074 :     move16();
    2900             : #else
    2901             :     q_reference_power_64fx = shl( add( q_cldfb, min_q_shift ), 1 );
    2902             :     Word16 norm_shift = 63;
    2903             :     move16();
    2904             :     FOR( l = 0; l < num_freq_bands; l++ )
    2905             :     {
    2906             :         IF( reference_power_64fx[l] )
    2907             :         {
    2908             :             norm_shift = s_min( norm_shift, W_norm( reference_power_64fx[l] ) );
    2909             :         }
    2910             :     }
    2911             :     FOR( l = 0; l < num_freq_bands; l++ )
    2912             :     {
    2913             :         reference_power_fx[l] = W_extract_h( W_shl( reference_power_64fx[l], norm_shift ) ); // q_reference_power_64fx+norm_shift-32
    2914             :         move32();
    2915             :     }
    2916             :     *q_reference_power = sub( add( q_reference_power_64fx, norm_shift ), 32 );
    2917             :     move16();
    2918             : #endif
    2919             : 
    2920             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2921      175074 :     IF( GT_16( min_q_shift[0], min_q_shift[1] ) )
    2922             :     {
    2923           8 :         FOR( l = 0; l < i_mult( num_proto, num_freq_bands ); l += num_freq_bands )
    2924             :         {
    2925           6 :             Scale_sig32( proto_frame_f_fx + shl( l, 1 ), shl( s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ), 1 ), sub( min_q_shift[1], min_q_shift[0] ) );
    2926             :         }
    2927           2 :         *q_proto_frame_f = add( q_cldfb, min_q_shift[1] );
    2928           2 :         move16();
    2929             :     }
    2930             :     ELSE
    2931             :     {
    2932      619089 :         FOR( l = 0; l < i_mult( num_proto, num_freq_bands ); l += num_freq_bands )
    2933             :         {
    2934      444017 :             Scale_sig32( proto_frame_f_fx + shl( add( l, CLDFB_NO_CHANNELS_HALF ), 1 ), shl( s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), 1 ), sub( min_q_shift[0], min_q_shift[1] ) );
    2935             :         }
    2936      175072 :         *q_proto_frame_f = add( q_cldfb, min_q_shift[0] );
    2937      175072 :         move16();
    2938             :     }
    2939      175074 :     IF( GT_16( min_q_shift[0], min_q_shift[1] ) )
    2940             :     {
    2941           8 :         FOR( l = 0; l < i_mult( num_proto, num_freq_bands ); l += num_freq_bands )
    2942             :         {
    2943           6 :             Scale_sig32( p_proto_buffer_fx + shl( l, 1 ), shl( s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ), 1 ), sub( min_q_shift[1], min_q_shift[0] ) );
    2944             :         }
    2945           2 :         *q_proto_direct_buffer_f = add( q_cldfb, min_q_shift[1] );
    2946           2 :         move16();
    2947             :     }
    2948             :     ELSE
    2949             :     {
    2950      619089 :         FOR( l = 0; l < i_mult( num_proto, num_freq_bands ); l += num_freq_bands )
    2951             :         {
    2952      444017 :             Scale_sig32( p_proto_buffer_fx + shl( add( l, CLDFB_NO_CHANNELS_HALF ), 1 ), shl( s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), 1 ), sub( min_q_shift[0], min_q_shift[1] ) );
    2953             :         }
    2954      175072 :         *q_proto_direct_buffer_f = add( q_cldfb, min_q_shift[0] );
    2955      175072 :         move16();
    2956             :     }
    2957             : #else
    2958             :     *q_proto_frame_f = add( q_cldfb, min_q_shift );
    2959             :     move16();
    2960             :     *q_proto_direct_buffer_f = add( q_cldfb, min_q_shift );
    2961             :     move16();
    2962             : #endif
    2963             : #ifdef FIX_867_CLDFB_NRG_SCALE
    2964      175074 :     q_proto_power_smooth[0] = s_min( q_proto_power_smooth[0], q_temp[0] );
    2965      175074 :     q_proto_power_smooth[1] = s_min( q_proto_power_smooth[1], q_temp[1] );
    2966      175074 :     move16();
    2967      175074 :     move16();
    2968             : #else
    2969             :     *q_proto_power_smooth = s_min( *q_proto_power_smooth, q_temp );
    2970             :     move16();
    2971             : #endif
    2972             : 
    2973      175074 :     return;
    2974             : }
    2975             : 
    2976             : 
    2977             : /*-------------------------------------------------------------------------
    2978             :  * protoSignalComputation4()
    2979             :  *
    2980             :  *
    2981             :  *-------------------------------------------------------------------------*/
    2982             : 
    2983       72000 : void protoSignalComputation4_fx(
    2984             :     Word32 RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    2985             :     Word32 ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    2986             :     Word32 *proto_frame_f_fx,                                                   /*proto_frame_f_q*/
    2987             :     Word16 *proto_frame_f_q,
    2988             :     Word32 *proto_direct_buffer_f_fx, /*proto_direct_buffer_f_q*/
    2989             :     Word16 *proto_direct_buffer_f_q,
    2990             :     Word32 *reference_power_fx, /*reference_power_q*/
    2991             :     Word16 *reference_power_q,
    2992             :     Word32 *proto_power_smooth_fx, /*proto_power_smooth_q*/
    2993             :     Word16 *proto_power_smooth_q,
    2994             :     const Word16 slot_index,
    2995             :     const Word16 num_outputs_diff,
    2996             :     const Word16 num_freq_bands,
    2997             :     const Word32 *mtx_hoa_decoder, /*q29*/
    2998             :     const Word16 nchan_transport,
    2999             :     const Word16 *sba_map_tc_ind,
    3000             :     Word16 q_cldfb )
    3001             : {
    3002             :     Word16 k, l, idx, idx2;
    3003             :     Word16 n, offset;
    3004             :     Word32 sq_tmp_fx;
    3005             :     Word32 *p_proto_buffer_fx;
    3006             :     Word16 min_q_shift, q_shift;
    3007             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3008             :     Word16 min_q_shift2, q_shift2;
    3009             : #endif
    3010             :     Word32 re, im;
    3011             :     Word16 proto_power_smooth_fx_q, sq_tmp_q;
    3012             : 
    3013       72000 :     min_q_shift = Q31;
    3014       72000 :     move16();
    3015       72000 :     q_shift = Q31;
    3016       72000 :     move16();
    3017             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3018       72000 :     min_q_shift2 = Q31;
    3019       72000 :     move16();
    3020       72000 :     q_shift2 = Q31;
    3021       72000 :     move16();
    3022             : #endif
    3023       72000 :     sq_tmp_q = 0;
    3024       72000 :     move16();
    3025             : 
    3026       72000 :     set_zero_fx( reference_power_fx, num_freq_bands );
    3027             : 
    3028             :     /* calculate the shift possible for both RealBuffer_fx and ImagBuffer_fx buffers*/
    3029      367040 :     FOR( k = 0; k < s_max( 4, nchan_transport ); k++ )
    3030             :     {
    3031             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3032      295040 :         q_shift = L_norm_arr( RealBuffer_fx[k][0], s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ) );
    3033      295040 :         min_q_shift = s_min( q_shift, min_q_shift );
    3034      295040 :         q_shift = L_norm_arr( ImagBuffer_fx[k][0], s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ) );
    3035      295040 :         min_q_shift = s_min( q_shift, min_q_shift );
    3036      295040 :         q_shift2 = L_norm_arr( RealBuffer_fx[k][0] + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    3037      295040 :         min_q_shift2 = s_min( q_shift2, min_q_shift2 );
    3038      295040 :         q_shift2 = L_norm_arr( ImagBuffer_fx[k][0] + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ) );
    3039      295040 :         min_q_shift2 = s_min( q_shift2, min_q_shift2 );
    3040             : #else
    3041             :         q_shift = L_norm_arr( RealBuffer_fx[k][0], num_freq_bands );
    3042             :         min_q_shift = s_min( q_shift, min_q_shift );
    3043             :         q_shift = L_norm_arr( ImagBuffer_fx[k][0], num_freq_bands );
    3044             :         min_q_shift = s_min( q_shift, min_q_shift );
    3045             : #endif
    3046             :     }
    3047       72000 :     q_shift = min_q_shift;
    3048       72000 :     min_q_shift = sub( min_q_shift, find_guarded_bits_fx( i_mult( 2, 4 ) ) );
    3049             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3050       72000 :     q_shift2 = min_q_shift2;
    3051       72000 :     min_q_shift2 = sub( min_q_shift2, find_guarded_bits_fx( i_mult( 2, 4 ) ) );
    3052             : #endif
    3053             : 
    3054      360000 :     FOR( k = 0; k < 4; k++ )
    3055             :     {
    3056             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3057     8928000 :         FOR( l = 0; l < s_min( CLDFB_NO_CHANNELS_HALF, num_freq_bands ); l++ )
    3058             :         {
    3059     8640000 :             re = L_shl( RealBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    3060     8640000 :             im = L_shl( ImagBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    3061             : 
    3062     8640000 :             sq_tmp_fx = Madd_32_32( Mpy_32_32( re, re ), im, im ); // 2*(q_cldfb+min_q_shift)-31
    3063             : 
    3064     8640000 :             reference_power_fx[l] = Madd_32_16( reference_power_fx[l], sq_tmp_fx, 16384 /*0.5 in Q15*/ ); // 2*(q_cldfb+min_q_shift)-31
    3065     8640000 :             move32();
    3066             :         }
    3067     8928000 :         FOR( l = CLDFB_NO_CHANNELS_HALF; l < num_freq_bands; l++ )
    3068             :         {
    3069     8640000 :             re = L_shl( RealBuffer_fx[k][0][l], min_q_shift2 ); // q_cldfb+min_q_shift
    3070     8640000 :             im = L_shl( ImagBuffer_fx[k][0][l], min_q_shift2 ); // q_cldfb+min_q_shift
    3071             : 
    3072     8640000 :             sq_tmp_fx = Madd_32_32( Mpy_32_32( re, re ), im, im ); // 2*(q_cldfb+min_q_shift)-31
    3073             : 
    3074     8640000 :             reference_power_fx[l] = Madd_32_16( reference_power_fx[l], sq_tmp_fx, 16384 /*0.5 in Q15*/ ); // 2*(q_cldfb+min_q_shift)-31
    3075     8640000 :             move32();
    3076             :         }
    3077             : #else
    3078             :         FOR( l = 0; l < num_freq_bands; l++ )
    3079             :         {
    3080             :             re = L_shl( RealBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    3081             :             im = L_shl( ImagBuffer_fx[k][0][l], min_q_shift ); // q_cldfb+min_q_shift
    3082             : 
    3083             :             sq_tmp_fx = Madd_32_32( Mpy_32_32( re, re ), im, im ); // 2*(q_cldfb+min_q_shift)-31
    3084             : 
    3085             :             reference_power_fx[l] = Madd_32_16( reference_power_fx[l], sq_tmp_fx, 16384 /*0.5 in Q15*/ ); // 2*(q_cldfb+min_q_shift)-31
    3086             :             move32();
    3087             :         }
    3088             : #endif
    3089             :     }
    3090             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3091       72000 :     sq_tmp_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    3092       72000 :     reference_power_q[0] = sq_tmp_q;
    3093       72000 :     move16();
    3094       72000 :     sq_tmp_q = sub( add( add( q_cldfb, min_q_shift2 ), add( q_cldfb, min_q_shift2 ) ), 31 );
    3095       72000 :     reference_power_q[1] = sq_tmp_q;
    3096       72000 :     move16();
    3097             : 
    3098       72000 :     min_q_shift = sub( s_min( q_shift, q_shift2 ), find_guarded_bits_fx( 2 ) );
    3099             : #else
    3100             :     sq_tmp_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    3101             :     *reference_power_q = sub( add( sq_tmp_q, Q15 ), 15 );
    3102             :     move16();
    3103             : 
    3104             :     min_q_shift = sub( q_shift, find_guarded_bits_fx( 2 ) );
    3105             : #endif
    3106             : 
    3107             :     /*For decorrelated diffuseness*/
    3108      784000 :     FOR( l = 0; l < num_outputs_diff; l++ )
    3109             :     {
    3110    43432000 :         FOR( k = 0; k < num_freq_bands; k++ )
    3111             :         {
    3112    42720000 :             idx = add( i_mult( i_mult( 2, l ), num_freq_bands ), i_mult( 2, k ) );
    3113    42720000 :             proto_frame_f_fx[idx] = 0;
    3114    42720000 :             move32();
    3115    42720000 :             proto_frame_f_fx[idx + 1] = 0;
    3116    42720000 :             move32();
    3117   218246400 :             FOR( n = 0; n < nchan_transport; n++ )
    3118             :             {
    3119   175526400 :                 idx2 = add( i_mult( l, 16 ), sba_map_tc_ind[n] );
    3120             : 
    3121   175526400 :                 re = L_shl( RealBuffer_fx[n][0][k], min_q_shift ); // q_cldfb+min_q_shift
    3122   175526400 :                 im = L_shl( ImagBuffer_fx[n][0][k], min_q_shift ); // q_cldfb+min_q_shift
    3123             : 
    3124   175526400 :                 proto_frame_f_fx[idx] = Madd_32_32( proto_frame_f_fx[idx], re, mtx_hoa_decoder[idx2] ); // q_cldfb+min_q_shift+29-31 => q_cldfb+min_q_shift-2
    3125   175526400 :                 move32();
    3126   175526400 :                 proto_frame_f_fx[idx + 1] = Madd_32_32( proto_frame_f_fx[idx + 1], im, mtx_hoa_decoder[idx2] ); // q_cldfb+min_q_shift-2
    3127   175526400 :                 move32();
    3128             :             }
    3129             :         }
    3130             :     }
    3131       72000 :     *proto_frame_f_q = sub( add( add( min_q_shift, q_cldfb ), Q29 ), 31 );
    3132       72000 :     move16();
    3133             : 
    3134             :     /* calculate the shift possible to up scale the buffer proto_power_smooth_fx*/
    3135       72000 :     min_q_shift = getScaleFactor32( proto_power_smooth_fx, i_mult( num_outputs_diff, num_freq_bands ) );
    3136       72000 :     min_q_shift = sub( min_q_shift, find_guarded_bits_fx( 2 ) );
    3137             : 
    3138       72000 :     Scale_sig32( proto_power_smooth_fx, i_mult( num_outputs_diff, num_freq_bands ), min_q_shift ); // proto_power_smooth_q+min_q_shift
    3139       72000 :     *proto_power_smooth_q = add( *proto_power_smooth_q, min_q_shift );
    3140       72000 :     move16();
    3141       72000 :     proto_power_smooth_fx_q = *proto_power_smooth_q;
    3142       72000 :     move16();
    3143             : 
    3144             :     /* calculate the shift possible to up scale the values of the buffer proto_frame_f_fx*/
    3145       72000 :     min_q_shift = getScaleFactor32( proto_frame_f_fx, i_mult( 2, i_mult( num_outputs_diff, num_freq_bands ) ) );
    3146       72000 :     min_q_shift = sub( min_q_shift, find_guarded_bits_fx( 2 ) );
    3147             : 
    3148       72000 :     Scale_sig32( proto_frame_f_fx, i_mult( 2, i_mult( num_outputs_diff, num_freq_bands ) ), min_q_shift ); // proto_frame_f_q+min_q_shift
    3149       72000 :     *proto_frame_f_q = add( *proto_frame_f_q, min_q_shift );
    3150       72000 :     move16();
    3151             : 
    3152       72000 :     offset = i_mult( i_mult( slot_index, 2 ), i_mult( num_freq_bands, num_outputs_diff ) );
    3153       72000 :     p_proto_buffer_fx = proto_direct_buffer_f_fx + offset; // proto_direct_buffer_f_q
    3154             : 
    3155      784000 :     FOR( k = 0; k < num_outputs_diff; k++ )
    3156             :     {
    3157    43432000 :         FOR( l = 0; l < num_freq_bands; l++ )
    3158             :         {
    3159    42720000 :             idx = 2 * ( k * num_freq_bands + l );
    3160             : 
    3161    42720000 :             sq_tmp_fx = Madd_32_32( Mpy_32_32( proto_frame_f_fx[idx], proto_frame_f_fx[idx] ), proto_frame_f_fx[idx + 1], proto_frame_f_fx[idx + 1] ); // 2*(proto_frame_f_q)-31
    3162    42720000 :             sq_tmp_q = sub( add( *proto_frame_f_q, *proto_frame_f_q ), 31 );
    3163             : #ifdef OPT_SBA_REND_V1_BE
    3164    42720000 :             proto_power_smooth_fx_q = s_min( *proto_power_smooth_q, sq_tmp_q );
    3165             : 
    3166    42720000 :             proto_power_smooth_fx[l + ( k * num_freq_bands )] = L_add( L_shr( proto_power_smooth_fx[l + ( k * num_freq_bands )], sub( *proto_power_smooth_q, proto_power_smooth_fx_q ) ), L_shr( sq_tmp_fx, sub( sq_tmp_q, proto_power_smooth_fx_q ) ) ); // proto_power_smooth_fx_q
    3167             : #else                                                                                                                                                                                                                                                     /* OPT_SBA_REND_V1_BE */
    3168             :             IF( LT_16( *proto_power_smooth_q, sq_tmp_q ) )
    3169             :             {
    3170             :                 proto_power_smooth_fx[l + ( k * num_freq_bands )] = L_add( proto_power_smooth_fx[l + ( k * num_freq_bands )], L_shr( sq_tmp_fx, sub( sq_tmp_q, *proto_power_smooth_q ) ) ); // proto_power_smooth_q
    3171             :                 move32();
    3172             :                 proto_power_smooth_fx_q = *proto_power_smooth_q;
    3173             :                 move16();
    3174             :             }
    3175             :             ELSE
    3176             :             {
    3177             :                 proto_power_smooth_fx[l + ( k * num_freq_bands )] = L_add( L_shr( proto_power_smooth_fx[l + ( k * num_freq_bands )], sub( *proto_power_smooth_q, sq_tmp_q ) ), sq_tmp_fx ); // sq_tmp_q
    3178             :                 move32();
    3179             :                 proto_power_smooth_fx_q = sq_tmp_q;
    3180             :                 move16();
    3181             :             }
    3182             : #endif                                                                                                                                                                                                                                                    /* OPT_SBA_REND_V1_BE */
    3183    42720000 :             p_proto_buffer_fx[idx] = proto_frame_f_fx[idx];                                                                                                                                                                                               // proto_frame_f_q
    3184    42720000 :             move32();
    3185    42720000 :             p_proto_buffer_fx[idx + 1] = proto_frame_f_fx[idx + 1]; // proto_frame_f_q
    3186    42720000 :             move32();
    3187             :         }
    3188             :     }
    3189             : 
    3190       72000 :     *proto_direct_buffer_f_q = *proto_frame_f_q;
    3191       72000 :     move16();
    3192             : #ifdef FIX_867_CLDFB_NRG_SCALE
    3193       72000 :     proto_power_smooth_q[0] = proto_power_smooth_fx_q;
    3194       72000 :     proto_power_smooth_q[1] = proto_power_smooth_fx_q;
    3195       72000 :     move16();
    3196       72000 :     move16();
    3197             : #else
    3198             :     *proto_power_smooth_q = proto_power_smooth_fx_q;
    3199             :     move16();
    3200             : #endif
    3201             : 
    3202       72000 :     return;
    3203             : }
    3204             : 
    3205             : 
    3206             : /*-------------------------------------------------------------------------
    3207             :  * ivas_dirac_dec_compute_diffuse_proto()
    3208             :  *
    3209             :  * Compute diffuse prototype buffer and smooth power, only for decorrelated bands
    3210             :  *------------------------------------------------------------------------*/
    3211             : 
    3212      346111 : void ivas_dirac_dec_compute_diffuse_proto_fx(
    3213             :     DIRAC_REND_HANDLE hDirACRend,
    3214             :     const Word16 num_freq_bands,
    3215             :     const Word16 slot_idx /* i  : slot index         */
    3216             : )
    3217             : {
    3218             :     Word16 k, l;
    3219             :     Word16 num_freq_bands_diff;
    3220             :     DIRAC_OUTPUT_SYNTHESIS_PARAMS *h_dirac_output_synthesis_params;
    3221             :     DIRAC_OUTPUT_SYNTHESIS_STATE *h_dirac_output_synthesis_state;
    3222             :     Word16 m;
    3223             : 
    3224             :     Word32 *proto_frame_dec_f_fx;
    3225             :     Word32 *p_diff_buffer_fx, *p_diff_buffer_1_fx;
    3226             :     Word32 *p_proto_diff_fx, *p_power_smooth_fx;
    3227             :     Word32 *p_hoa_enc_fx;
    3228             : 
    3229      346111 :     proto_frame_dec_f_fx = hDirACRend->proto_frame_dec_f_fx; // hDirACRend->proto_frame_dec_f_q
    3230      346111 :     h_dirac_output_synthesis_params = &( hDirACRend->h_output_synthesis_psd_params );
    3231      346111 :     h_dirac_output_synthesis_state = &( hDirACRend->h_output_synthesis_psd_state );
    3232             : 
    3233      346111 :     num_freq_bands_diff = h_dirac_output_synthesis_params->max_band_decorr;
    3234      346111 :     move16();
    3235             : 
    3236      346111 :     IF( num_freq_bands_diff == 0 )
    3237             :     {
    3238       72000 :         return;
    3239             :     }
    3240             : 
    3241      274111 :     p_diff_buffer_fx = h_dirac_output_synthesis_state->proto_diffuse_buffer_f_fx + ( ( ( slot_idx * num_freq_bands_diff ) << 1 ) * hDirACRend->hOutSetup.nchan_out_woLFE ); // proto_diffuse_buffer_f_q
    3242      274111 :     p_diff_buffer_1_fx = p_diff_buffer_fx + 1;
    3243      274111 :     p_power_smooth_fx = h_dirac_output_synthesis_state->proto_power_diff_smooth_fx; // h_dirac_output_synthesis_state->proto_power_diff_smooth_q
    3244             : 
    3245      274111 :     Word16 diffuse_start = imult1616( slot_idx, shl( imult1616( num_freq_bands_diff, hDirACRend->hOutSetup.nchan_out_woLFE ), 1 ) );
    3246             :     Word16 diff_e, smooth_e, proto_e, max_e;
    3247             :     Word32 diff_square, diff_square_1, diff_square_sum;
    3248             :     Word16 diff_square_e;
    3249             :     Word16 old_diff_e;
    3250      274111 :     diff_e = sub( 31, h_dirac_output_synthesis_state->proto_diffuse_buffer_f_q );
    3251      274111 :     old_diff_e = diff_e;
    3252      274111 :     move16();
    3253      274111 :     smooth_e = sub( 31, h_dirac_output_synthesis_state->proto_power_diff_smooth_q );
    3254      274111 :     proto_e = sub( 31, hDirACRend->proto_frame_dec_f_q );
    3255             : 
    3256      274111 :     IF( NE_32( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_PSD_SHD ) )
    3257             :     {
    3258      209971 :         diff_square_e = add( shl( proto_e, 1 ), 1 );
    3259      209971 :         max_e = add( s_max( diff_square_e, smooth_e ), 1 );
    3260      209971 :         Scale_sig32( p_power_smooth_fx, h_dirac_output_synthesis_state->proto_power_diff_smooth_len, sub( smooth_e, max_e ) ); // 31-max_e
    3261     1682448 :         FOR( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
    3262             :         {
    3263     1472477 :             p_proto_diff_fx = proto_frame_dec_f_fx + shl( imult1616( k, num_freq_bands ), 1 ); // hDirACRend->proto_frame_dec_f_q
    3264    23559632 :             FOR( l = 0; l < num_freq_bands_diff; l++ )
    3265             :             {
    3266    22087155 :                 *p_diff_buffer_fx = *( p_proto_diff_fx++ ); // hDirACRend->proto_frame_dec_f_q
    3267    22087155 :                 move32();
    3268    22087155 :                 *p_diff_buffer_1_fx = *( p_proto_diff_fx++ ); // hDirACRend->proto_frame_dec_f_q
    3269    22087155 :                 move32();
    3270             : 
    3271    22087155 :                 diff_square = L_shr( Mpy_32_32( *p_diff_buffer_fx, *p_diff_buffer_fx ), 1 );       // 2 * proto_e + 1
    3272    22087155 :                 diff_square_1 = L_shr( Mpy_32_32( *p_diff_buffer_1_fx, *p_diff_buffer_1_fx ), 1 ); // 2 * proto_e + 1
    3273    22087155 :                 diff_square_sum = L_add( diff_square, diff_square_1 );                             // Q(31- diff_square_e)
    3274    22087155 :                 diff_square_sum = L_shr( diff_square_sum, sub( max_e, diff_square_e ) );           // Q(31-max_e)
    3275    22087155 :                 *p_power_smooth_fx = L_add( *p_power_smooth_fx, diff_square_sum );                 // Q(31-max_e)
    3276    22087155 :                 move32();
    3277             : 
    3278    22087155 :                 p_power_smooth_fx++;
    3279    22087155 :                 p_diff_buffer_fx += 2;
    3280    22087155 :                 p_diff_buffer_1_fx += 2;
    3281             :             }
    3282             :         }
    3283      209971 :         diff_e = proto_e;
    3284      209971 :         move16();
    3285             :     }
    3286             :     ELSE
    3287             :     {
    3288             :         /*DIRAC_SYNTHESIS_PSD_SHD: Virtual LS->HOA encoding*/
    3289       64140 :         Word16 gb = find_guarded_bits_fx( hDirACRend->num_outputs_diff );
    3290       64140 :         diff_square_e = add( shl( add( proto_e, gb ), 1 ), 1 );
    3291       64140 :         max_e = add( s_max( diff_square_e, smooth_e ), 1 );
    3292       64140 :         Scale_sig32( p_power_smooth_fx, h_dirac_output_synthesis_state->proto_power_diff_smooth_len, sub( smooth_e, max_e ) ); // 31-max_e
    3293      587100 :         FOR( k = 0; k < hDirACRend->hOutSetup.nchan_out_woLFE; k++ )
    3294             :         {
    3295     8367360 :             FOR( l = 0; l < num_freq_bands_diff; l++ )
    3296             :             {
    3297     7844400 :                 p_hoa_enc_fx = hDirACRend->hoa_encoder_fx + k;        // q31
    3298     7844400 :                 p_proto_diff_fx = proto_frame_dec_f_fx + shl( l, 1 ); // hDirACRend->proto_frame_dec_f_q
    3299             : 
    3300     7844400 :                 *p_diff_buffer_fx = 0;
    3301     7844400 :                 move32();
    3302     7844400 :                 *p_diff_buffer_1_fx = 0;
    3303     7844400 :                 move32();
    3304             : 
    3305             :                 /*LS to HOA*/
    3306    70599600 :                 FOR( m = 0; m < hDirACRend->num_outputs_diff; m++ )
    3307             :                 {
    3308    62755200 :                     *p_diff_buffer_fx = L_add( *p_diff_buffer_fx, L_shr( Mpy_32_32( *p_hoa_enc_fx, *p_proto_diff_fx ), gb ) ); // hDirACRend->proto_frame_dec_f_q-gb
    3309    62755200 :                     move32();
    3310    62755200 :                     *p_diff_buffer_1_fx = L_add( *p_diff_buffer_1_fx, L_shr( Mpy_32_32( *p_hoa_enc_fx, *( p_proto_diff_fx + 1 ) ), gb ) ); // hDirACRend->proto_frame_dec_f_q-gb
    3311    62755200 :                     move32();
    3312    62755200 :                     p_hoa_enc_fx += hDirACRend->hOutSetup.nchan_out_woLFE;
    3313    62755200 :                     p_proto_diff_fx += shl( num_freq_bands, 1 );
    3314             :                 }
    3315             : 
    3316     7844400 :                 diff_square = L_shr( Mpy_32_32( *p_diff_buffer_fx, *p_diff_buffer_fx ), 1 );       // 2*( proto_e + gb) + 1
    3317     7844400 :                 diff_square_1 = L_shr( Mpy_32_32( *p_diff_buffer_1_fx, *p_diff_buffer_1_fx ), 1 ); // 2*( proto_e + gb) + 1
    3318     7844400 :                 diff_square_sum = L_add( diff_square, diff_square_1 );                             // 2*( proto_e + gb) + 1
    3319     7844400 :                 diff_square_sum = L_shr( diff_square_sum, sub( max_e, diff_square_e ) );           // 31-max_e
    3320     7844400 :                 *p_power_smooth_fx = L_add( *p_power_smooth_fx, diff_square_sum );                 // 31-max_e
    3321     7844400 :                 move32();
    3322             : 
    3323     7844400 :                 p_power_smooth_fx++;
    3324             : 
    3325     7844400 :                 p_diff_buffer_fx += 2;
    3326     7844400 :                 p_diff_buffer_1_fx += 2;
    3327             :             }
    3328             :         }
    3329       64140 :         diff_e = add( proto_e, gb );
    3330             :     }
    3331      274111 :     h_dirac_output_synthesis_state->proto_power_diff_smooth_q = sub( 31, max_e );
    3332      274111 :     move16();
    3333             : 
    3334      274111 :     Word16 new_diff_e = s_max( diff_e, old_diff_e );
    3335      274111 :     Scale_sig32( h_dirac_output_synthesis_state->proto_diffuse_buffer_f_fx, diffuse_start, sub( old_diff_e, new_diff_e ) ); // 31-new_diff_e
    3336      274111 :     Scale_sig32( h_dirac_output_synthesis_state->proto_diffuse_buffer_f_fx + diffuse_start,
    3337      274111 :                  i_mult( shl( num_freq_bands_diff, 1 ), hDirACRend->hOutSetup.nchan_out_woLFE ), sub( diff_e, new_diff_e ) ); // 31-new_diff_e
    3338      274111 :     h_dirac_output_synthesis_state->proto_diffuse_buffer_f_q = sub( 31, new_diff_e );
    3339      274111 :     move16();
    3340             : 
    3341      274111 :     return;
    3342             : }
    3343             : 
    3344             : 
    3345             : /*-------------------------------------------------------------------------
    3346             :  * computeDirectionAngles()
    3347             :  *
    3348             :  *------------------------------------------------------------------------*/
    3349             : 
    3350      694835 : void computeDirectionAngles_fx(
    3351             :     Word32 *intensity_real_x_fx, /*q_intensity_real*/
    3352             :     Word32 *intensity_real_y_fx, /*q_intensity_real*/
    3353             :     Word32 *intensity_real_z_fx, /*q_intensity_real*/
    3354             :     Word16 q_intensity_real,
    3355             :     const Word16 num_frequency_bands,
    3356             :     Word16 *azimuth,
    3357             :     Word16 *elevation )
    3358             : {
    3359             :     Word16 k;
    3360             :     Word32 intensityNorm;
    3361             :     Word32 x, y, z, radius;
    3362             :     Word32 temp;
    3363             :     Word16 res, q_intensityNorm, exp, q_temp;
    3364             :     Word16 q_x, q_y, q_z, exp1, exp2, exp3, q_tmp1, q_tmp2;
    3365             :     Word32 x_re, y_re, z_re, tmp1, tmp2;
    3366             : 
    3367      694835 :     exp = 0;
    3368      694835 :     move16();
    3369             : 
    3370     8338020 :     FOR( k = 0; k < num_frequency_bands; ++k )
    3371             :     {
    3372     7643185 :         exp1 = norm_l( intensity_real_x_fx[k] );
    3373     7643185 :         exp2 = norm_l( intensity_real_y_fx[k] );
    3374     7643185 :         exp3 = norm_l( intensity_real_z_fx[k] );
    3375     7643185 :         x_re = L_shl( intensity_real_x_fx[k], exp1 ); /*q_intensity_real+exp1*/
    3376     7643185 :         y_re = L_shl( intensity_real_y_fx[k], exp2 ); /*q_intensity_real+exp2*/
    3377     7643185 :         z_re = L_shl( intensity_real_z_fx[k], exp3 ); /*q_intensity_real+exp3*/
    3378             : 
    3379     7643185 :         tmp1 = Mpy_32_32( x_re, x_re ); /*2*(q_intensity_real+exp1)-31*/
    3380     7643185 :         q_tmp1 = sub( add( add( q_intensity_real, exp1 ), add( q_intensity_real, exp1 ) ), 31 );
    3381     7643185 :         tmp2 = Mpy_32_32( y_re, y_re ); /*2*(q_intensity_real+exp2)-31*/
    3382     7643185 :         q_tmp2 = sub( add( add( q_intensity_real, exp2 ), add( q_intensity_real, exp2 ) ), 31 );
    3383             : 
    3384     7643185 :         temp = BASOP_Util_Add_Mant32Exp( tmp1, sub( 31, q_tmp1 ), tmp2, sub( 31, q_tmp2 ), &exp ); // 31-exp
    3385             : 
    3386     7643185 :         tmp2 = Mpy_32_32( z_re, z_re ); /*2*(q_intensity_real+exp3)-31*/
    3387     7643185 :         q_tmp2 = sub( add( add( q_intensity_real, exp3 ), add( q_intensity_real, exp3 ) ), 31 );
    3388             : 
    3389     7643185 :         intensityNorm = BASOP_Util_Add_Mant32Exp( temp, exp, tmp2, sub( 31, q_tmp2 ), &exp ); // 31-exp
    3390     7643185 :         q_intensityNorm = sub( 31, exp );
    3391             : 
    3392     7643185 :         IF( LE_32( intensityNorm, EPSILON_FX ) )
    3393             :         {
    3394       25045 :             intensityNorm = ONE_IN_Q30; // q30
    3395       25045 :             move32();
    3396       25045 :             x = ONE_IN_Q30; // q30
    3397       25045 :             move32();
    3398       25045 :             q_x = Q30;
    3399       25045 :             move16();
    3400       25045 :             y = 0;
    3401       25045 :             move32();
    3402       25045 :             q_y = Q30;
    3403       25045 :             move16();
    3404       25045 :             z = 0;
    3405       25045 :             move32();
    3406       25045 :             q_z = Q30;
    3407       25045 :             move16();
    3408             :         }
    3409             :         ELSE
    3410             :         {
    3411     7618140 :             temp = BASOP_Util_Divide3232_Scale_newton( ONE_IN_Q30, intensityNorm, &exp ); // Q=31-(exp-(30-q_intensityNorm))
    3412     7618140 :             exp = sub( exp, sub( Q30, q_intensityNorm ) );
    3413     7618140 :             temp = Sqrt32( temp, &exp ); // Q=31-exp
    3414     7618140 :             q_temp = sub( 31, exp );
    3415             : 
    3416     7618140 :             x = Mpy_32_32( x_re, temp ); /*Q=q_intensity_real+exp1+q_temp-31*/
    3417     7618140 :             q_x = sub( add( add( q_intensity_real, exp1 ), q_temp ), 31 );
    3418     7618140 :             y = Mpy_32_32( y_re, temp ); /*Q=q_intensity_real+exp2+q_temp-31*/
    3419     7618140 :             q_y = sub( add( add( q_intensity_real, exp2 ), q_temp ), 31 );
    3420     7618140 :             z = Mpy_32_32( z_re, temp ); /*Q=q_intensity_real+exp3+q_temp-31*/
    3421     7618140 :             q_z = sub( add( add( q_intensity_real, exp3 ), q_temp ), 31 );
    3422             :         }
    3423             : 
    3424     7643185 :         tmp1 = Mpy_32_32( x, x ); /*Q=2*q_x-31*/
    3425     7643185 :         q_tmp1 = sub( add( q_x, q_x ), 31 );
    3426             : 
    3427     7643185 :         tmp2 = Mpy_32_32( y, y ); /*Q=2*q_y-31*/
    3428     7643185 :         q_tmp2 = sub( add( q_y, q_y ), 31 );
    3429             : 
    3430     7643185 :         temp = BASOP_Util_Add_Mant32Exp( tmp1, sub( 31, q_tmp1 ), tmp2, sub( 31, q_tmp2 ), &exp ); // Q=31-exp
    3431     7643185 :         radius = Sqrt32( temp, &exp );                                                             // Q=31-exp
    3432             : 
    3433     7643185 :         res = BASOP_util_atan2( y, x, sub( sub( 31, q_y ), sub( 31, q_x ) ) );                                                          /* Q13 */
    3434     7643185 :         res = add( s_max( -23040 /*-180 q7*/, s_min( 23040 /*180 q7*/, mult( res, _180_OVER_PI_Q9 /*180/pi q9*/ ) ) ), 64 /*0.5 q7*/ ); /* Q7 */
    3435     7643185 :         azimuth[k] = shr( abs_s( res ), Q7 );                                                                                           /* Q0 */
    3436     7643185 :         move16();
    3437     7643185 :         IF( res < 0 )
    3438             :         {
    3439     4642030 :             azimuth[k] = negate( azimuth[k] );
    3440     4642030 :             move16();
    3441             :         }
    3442             : 
    3443     7643185 :         res = BASOP_util_atan2( z, radius, sub( sub( 31, q_z ), exp ) );                                                               /* Q13 */
    3444     7643185 :         res = add( s_max( -11520 /*-90 q7*/, s_min( 23040 /*180 q7*/, mult( res, _180_OVER_PI_Q9 /*180/pi q9*/ ) ) ), 64 /*0.5 q7*/ ); /* Q7 */
    3445     7643185 :         elevation[k] = shr( abs_s( res ), Q7 );                                                                                        /* Q0 */
    3446     7643185 :         move16();
    3447     7643185 :         IF( res < 0 )
    3448             :         {
    3449     3843976 :             elevation[k] = negate( elevation[k] );
    3450     3843976 :             move16();
    3451             :         }
    3452             :     }
    3453             : 
    3454      694835 :     return;
    3455             : }
    3456             : 
    3457             : 
    3458             : /*-------------------------------------------------------------------------
    3459             :  * ivas_masa_init_stereotype_detection()
    3460             :  *
    3461             :  * Initialize stereo transport signal type detection
    3462             :  *------------------------------------------------------------------------*/
    3463             : 
    3464         144 : void ivas_masa_init_stereotype_detection_fx(
    3465             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect )
    3466             : {
    3467         144 :     stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
    3468         144 :     move32();
    3469         144 :     stereo_type_detect->current_stereo_type = MASA_STEREO_DOWNMIX;
    3470         144 :     move32();
    3471         144 :     stereo_type_detect->type_change_direction = MASA_STEREO_DOWNMIX;
    3472         144 :     move32();
    3473             : 
    3474         144 :     stereo_type_detect->counter = 0;
    3475         144 :     move16();
    3476         144 :     stereo_type_detect->interpolator = 0;
    3477         144 :     move16();
    3478             : 
    3479         144 :     stereo_type_detect->dipole_freq_range[0] = 1;
    3480         144 :     move16();
    3481         144 :     stereo_type_detect->dipole_freq_range[1] = 3;
    3482         144 :     move16();
    3483             : 
    3484         144 :     stereo_type_detect->left_bb_power_fx = 0; /* Broadband estimates */
    3485         144 :     move32();
    3486         144 :     stereo_type_detect->q_left_bb_power = Q31;
    3487         144 :     move16();
    3488         144 :     stereo_type_detect->right_bb_power_fx = 0;
    3489         144 :     move32();
    3490         144 :     stereo_type_detect->q_right_bb_power = Q31;
    3491         144 :     move16();
    3492         144 :     stereo_type_detect->total_bb_power_fx = 0;
    3493         144 :     move32();
    3494         144 :     stereo_type_detect->q_total_bb_power = Q31;
    3495         144 :     move16();
    3496             : 
    3497         144 :     stereo_type_detect->left_hi_power_fx = 0; /* High-frequency estimates */
    3498         144 :     move32();
    3499         144 :     stereo_type_detect->q_left_hi_power = Q31;
    3500         144 :     move16();
    3501         144 :     stereo_type_detect->right_hi_power_fx = 0;
    3502         144 :     move32();
    3503         144 :     stereo_type_detect->q_right_hi_power = Q31;
    3504         144 :     move16();
    3505         144 :     stereo_type_detect->total_hi_power_fx = 0;
    3506         144 :     move32();
    3507         144 :     stereo_type_detect->q_total_hi_power = Q31;
    3508         144 :     move16();
    3509             : 
    3510         144 :     set32_fx( stereo_type_detect->sum_power_fx, 0, MASA_SUM_FREQ_RANGE_BINS );
    3511         144 :     set32_fx( stereo_type_detect->total_power_fx, 0, MASA_SUM_FREQ_RANGE_BINS );
    3512             : 
    3513         144 :     set16_fx( stereo_type_detect->exp_sum_power, 0, MASA_SUM_FREQ_RANGE_BINS );
    3514         144 :     set16_fx( stereo_type_detect->exp_total_power, 0, MASA_SUM_FREQ_RANGE_BINS );
    3515             : 
    3516         144 :     stereo_type_detect->subtract_power_y_fx = 0;
    3517         144 :     move32();
    3518         144 :     stereo_type_detect->q_subtract_power_y = Q31;
    3519         144 :     move16();
    3520         144 :     stereo_type_detect->subtract_power_y_smooth_fx = 0;
    3521         144 :     move32();
    3522         144 :     stereo_type_detect->q_subtract_power_y_smooth = Q31;
    3523         144 :     move16();
    3524         144 :     stereo_type_detect->target_power_y_smooth_fx = 0;
    3525         144 :     move32();
    3526         144 :     stereo_type_detect->q_target_power_y_smooth = 31;
    3527         144 :     move16();
    3528             : 
    3529         144 :     stereo_type_detect->lr_total_bb_ratio_db_fx = 0;
    3530         144 :     move32();
    3531         144 :     stereo_type_detect->lr_total_hi_ratio_db_fx = 0;
    3532         144 :     move32();
    3533         144 :     stereo_type_detect->min_sum_total_ratio_db_fx = 0;
    3534         144 :     move32();
    3535         144 :     stereo_type_detect->subtract_target_ratio_db_fx = 0;
    3536         144 :     move32();
    3537             : 
    3538         144 :     return;
    3539             : }
    3540             : 
    3541             : 
    3542             : /*-------------------------------------------------------------------------
    3543             :  * ivas_masa_stereotype_detection()
    3544             :  *
    3545             :  * Detect the type of the transport audio signals
    3546             :  *------------------------------------------------------------------------*/
    3547             : 
    3548       61943 : void ivas_masa_stereotype_detection_fx(
    3549             :     MASA_STEREO_TYPE_DETECT *stereo_type_detect )
    3550             : {
    3551             :     Word32 lr_total_bb_ratio_db_fx;
    3552             :     Word32 lr_total_hi_ratio_db_fx;
    3553             :     Word32 min_sum_total_ratio_db_fx;
    3554             :     Word32 subtract_target_ratio_db_fx;
    3555             :     Word32 change_to_spaced_fx;
    3556             :     Word16 change_to_spaced_selection;
    3557             :     Word32 change_to_downmix_fx;
    3558             :     Word32 change_to_downmix2_fx;
    3559             :     Word16 change_to_downmix_selection;
    3560             :     Word32 subtract_temp_fx;
    3561             :     Word32 min_sum_temp_fx;
    3562             :     Word32 lr_total_bb_temp_fx;
    3563             :     Word32 lr_total_hi_temp_fx;
    3564             :     Word32 temp;
    3565             : 
    3566       61943 :     lr_total_bb_ratio_db_fx = stereo_type_detect->lr_total_bb_ratio_db_fx; // q21
    3567       61943 :     move32();
    3568       61943 :     lr_total_hi_ratio_db_fx = stereo_type_detect->lr_total_hi_ratio_db_fx; // q21
    3569       61943 :     move32();
    3570       61943 :     min_sum_total_ratio_db_fx = stereo_type_detect->min_sum_total_ratio_db_fx; // q21
    3571       61943 :     move32();
    3572       61943 :     subtract_target_ratio_db_fx = stereo_type_detect->subtract_target_ratio_db_fx; // q21
    3573       61943 :     move32();
    3574             : 
    3575             :     /* Determine if the determined features match the spaced mic type */
    3576       61943 :     change_to_spaced_selection = 0;
    3577       61943 :     move16();
    3578       61943 :     IF( LT_32( subtract_target_ratio_db_fx, -THREE_Q21 /*-3 q21*/ ) )
    3579             :     {
    3580             :         /* subtract_temp = ( -subtract_target_ratio_db - 3.0f ) / 3.0f; */
    3581       19588 :         temp = L_sub( L_shr( -subtract_target_ratio_db_fx, 1 ), L_shr( THREE_Q21, 1 ) ); // q20
    3582       19588 :         subtract_temp_fx = Mpy_32_32( temp, 715827883 /* 1 / 3.0f in Q31 */ );           // q20
    3583       19588 :         subtract_temp_fx = L_shl( subtract_temp_fx, 1 );                                 /* Q21 */
    3584             : 
    3585             :         /* min_sum_temp = max( -min_sum_total_ratio_db / 6.0f, 0.0f ); */
    3586       19588 :         min_sum_temp_fx = Mpy_32_32_r( -min_sum_total_ratio_db_fx, 357913941 /* 1 / 6.0f in Q31 */ ); /* Q21 */
    3587       19588 :         min_sum_temp_fx = L_max( min_sum_temp_fx, 0 );                                                /*q21*/
    3588             : 
    3589             :         /* lr_total_bb_temp = lr_total_bb_ratio_db / 6.0f; */
    3590       19588 :         lr_total_bb_temp_fx = Mpy_32_32_r( lr_total_bb_ratio_db_fx, 357913941 /* 1 / 6.0f in Q31 */ ); /* Q21 */
    3591             : 
    3592       19588 :         change_to_spaced_fx = L_add( L_add( subtract_temp_fx, min_sum_temp_fx ), lr_total_bb_temp_fx ); /* Q21 */
    3593             : 
    3594       19588 :         IF( GE_32( change_to_spaced_fx, ONE_IN_Q21 ) )
    3595             :         {
    3596       10482 :             change_to_spaced_selection = 1;
    3597       10482 :             move16();
    3598             :         }
    3599             :     }
    3600             : 
    3601             :     /* Determine if the determined features match the downmix type, according to a metric */
    3602       61943 :     change_to_downmix_selection = 0;
    3603       61943 :     move16();
    3604       61943 :     IF( subtract_target_ratio_db_fx > 0 )
    3605             :     {
    3606             :         /* subtract_temp = subtract_target_ratio_db / 3.0f; */
    3607       25601 :         subtract_temp_fx = Mpy_32_32( subtract_target_ratio_db_fx, 715827883 /* 1 / 3.0f in Q31 */ ); /* Q21 */
    3608             : 
    3609             :         /* min_sum_temp = ( min_sum_total_ratio_db + 1.0f ) / 6.0f; */
    3610       25601 :         min_sum_temp_fx = Mpy_32_32_r( L_add( min_sum_total_ratio_db_fx, ONE_IN_Q21 ), 357913941 /* 1 / 6.0f in Q31 */ ); /* Q21 */
    3611             : 
    3612             :         /* lr_total_bb_temp = -lr_total_bb_ratio_db / 6.0f; */
    3613       25601 :         lr_total_bb_temp_fx = Mpy_32_32_r( -lr_total_bb_ratio_db_fx, 357913941 /* 1 / 6.0f in Q31 */ ); /* Q21 */
    3614             : 
    3615       25601 :         change_to_downmix_fx = L_add( L_add( subtract_temp_fx, min_sum_temp_fx ), lr_total_bb_temp_fx ); /* Q21 */
    3616             : 
    3617       25601 :         IF( GE_32( change_to_downmix_fx, ONE_IN_Q21 /*1 q21*/ ) )
    3618             :         {
    3619       11652 :             change_to_downmix_selection = 1;
    3620       11652 :             move16();
    3621             :         }
    3622             :     }
    3623             : 
    3624             :     /* Determine if the determined features match the downmix type, according to another metric */
    3625       61943 :     IF( LT_32( lr_total_hi_ratio_db_fx, -25165824 ) ) // 25165824 = 12.0 in Q21
    3626             :     {
    3627             :         /* subtract_temp = ( subtract_target_ratio_db + 4.0f ) / 3.0f; */
    3628         408 :         subtract_temp_fx = Mpy_32_32( L_add( subtract_target_ratio_db_fx, 8388608 /* 4.0 in Q21 */ ), 715827883 /* 1 / 3.0f in Q31 */ );
    3629             : 
    3630             :         /* min_sum_temp = min_sum_total_ratio_db / 6.0f; */
    3631         408 :         min_sum_temp_fx = Mpy_32_32_r( min_sum_total_ratio_db_fx, 357913941 /* 1 / 6.0f in Q31 */ );
    3632             : 
    3633             :         /* lr_total_hi_temp = ( -lr_total_hi_ratio_db - 12.0f ) / 3.0f; */
    3634         408 :         lr_total_hi_temp_fx = Mpy_32_32( L_sub( -lr_total_hi_ratio_db_fx, 25165824 /* 12.0 in Q21 */ ), 715827883 /* 1 / 3.0f in Q31 */ );
    3635             : 
    3636         408 :         change_to_downmix2_fx = L_add( L_add( subtract_temp_fx, min_sum_temp_fx ), lr_total_hi_temp_fx ); // Q21
    3637             : 
    3638         408 :         IF( GE_32( change_to_downmix2_fx, ONE_IN_Q21 /*1 q21*/ ) )
    3639             :         {
    3640         373 :             change_to_downmix_selection = 1;
    3641         373 :             move16();
    3642             :         }
    3643             :     }
    3644             : 
    3645       61943 :     IF( LT_16( stereo_type_detect->counter, 400 ) )
    3646             :     {
    3647       19559 :         stereo_type_detect->counter++;
    3648             :     }
    3649             :     ELSE
    3650             :     {
    3651       42384 :         IF( EQ_16( change_to_spaced_selection, 1 ) )
    3652             :         {
    3653        8800 :             stereo_type_detect->masa_stereo_type = MASA_STEREO_SPACED_MICS;
    3654        8800 :             move32();
    3655             :         }
    3656       33584 :         ELSE IF( EQ_16( change_to_downmix_selection, 1 ) )
    3657             :         {
    3658        6628 :             stereo_type_detect->masa_stereo_type = MASA_STEREO_DOWNMIX;
    3659        6628 :             move32();
    3660             :         }
    3661             :     }
    3662             : 
    3663       61943 :     IF( stereo_type_detect->interpolator == 0 )
    3664             :     {
    3665       61915 :         IF( NE_16( stereo_type_detect->current_stereo_type, stereo_type_detect->masa_stereo_type ) )
    3666             :         {
    3667           2 :             stereo_type_detect->interpolator = 1;
    3668           2 :             move16();
    3669           2 :             stereo_type_detect->type_change_direction = stereo_type_detect->masa_stereo_type;
    3670           2 :             move32();
    3671             :         }
    3672             :     }
    3673             : 
    3674       61943 :     return;
    3675             : }
    3676             : 
    3677             : 
    3678             : /*-------------------------------------------------------------------------
    3679             :  * computeIntensityVector_dec()
    3680             :  *
    3681             :  *
    3682             :  *------------------------------------------------------------------------*/
    3683             : 
    3684      694835 : void computeIntensityVector_dec_fx(
    3685             :     Word32 Cldfb_RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    3686             :     Word32 Cldfb_ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    3687             :     Word16 q_cldfb,
    3688             :     const Word16 num_frequency_bands,
    3689             :     Word32 *intensity_real_x_fx, /*q_intensity_real*/
    3690             :     Word32 *intensity_real_y_fx, /*q_intensity_real*/
    3691             :     Word32 *intensity_real_z_fx, /*q_intensity_real*/
    3692             :     Word16 *q_intensity_real )
    3693             : {
    3694             :     /*
    3695             :      * W = a + ib; Y = c + id
    3696             :      * real(W*Y') = ac + bd
    3697             :      */
    3698             :     Word16 i;
    3699             :     Word32 re1, re2, im1, im2;
    3700             :     Word16 min_q_shift;
    3701             : 
    3702      694835 :     min_q_shift = Q31;
    3703      694835 :     move32();
    3704             :     /* calculate the max possible shift for the buffers Cldfb_RealBuffer_fx and Cldfb_ImagBuffer_fx*/
    3705     3474175 :     FOR( i = 0; i < 4; i++ )
    3706             :     {
    3707     2779340 :         min_q_shift = s_min( min_q_shift, s_min( L_norm_arr( Cldfb_RealBuffer_fx[i][0], num_frequency_bands ), L_norm_arr( Cldfb_ImagBuffer_fx[i][0], num_frequency_bands ) ) );
    3708             :     }
    3709             : 
    3710      694835 :     min_q_shift = sub( min_q_shift, 1 ); // guard bits
    3711             : 
    3712     8338020 :     FOR( i = 0; i < num_frequency_bands; ++i )
    3713             :     {
    3714     7643185 :         re1 = L_shl( Cldfb_RealBuffer_fx[0][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3715     7643185 :         im1 = L_shl( Cldfb_ImagBuffer_fx[0][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3716     7643185 :         re2 = L_shl( Cldfb_RealBuffer_fx[3][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3717     7643185 :         im2 = L_shl( Cldfb_ImagBuffer_fx[3][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3718             : 
    3719     7643185 :         intensity_real_x_fx[i] = Madd_32_32( Mpy_32_32( re2, re1 ), im2, im1 ); /*2*(q_cldfb+min_q_shift)-31*/
    3720     7643185 :         move32();
    3721             : 
    3722     7643185 :         re2 = L_shl( Cldfb_RealBuffer_fx[1][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3723     7643185 :         im2 = L_shl( Cldfb_ImagBuffer_fx[1][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3724             : 
    3725     7643185 :         intensity_real_y_fx[i] = Madd_32_32( Mpy_32_32( re2, re1 ), im2, im1 ); /*2*(q_cldfb+min_q_shift)-31*/
    3726     7643185 :         move32();
    3727             : 
    3728     7643185 :         re2 = L_shl( Cldfb_RealBuffer_fx[2][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3729     7643185 :         im2 = L_shl( Cldfb_ImagBuffer_fx[2][0][i], min_q_shift ); /*q_cldfb+min_q_shift*/
    3730             : 
    3731     7643185 :         intensity_real_z_fx[i] = Madd_32_32( Mpy_32_32( re2, re1 ), im2, im1 ); /*2*(q_cldfb+min_q_shift)-31*/
    3732     7643185 :         move32();
    3733             :     }
    3734             : 
    3735      694835 :     *q_intensity_real = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    3736      694835 :     move16();
    3737             : 
    3738      694835 :     return;
    3739             : }
    3740             : 
    3741             : 
    3742             : /*-------------------------------------------------------------------------
    3743             :  * ivas_lfe_synth_with_cldfb()
    3744             :  *
    3745             :  *
    3746             :  *------------------------------------------------------------------------*/
    3747             : 
    3748       23440 : void ivas_lfe_synth_with_cldfb_fx(
    3749             :     MCMASA_LFE_SYNTH_DATA_HANDLE hMasaLfeSynth,
    3750             :     Word32 RealBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],  /*q_cldfb*/
    3751             :     Word32 ImagBuffer_fx[][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX],  /*q_cldfb*/
    3752             :     Word32 RealBufferLfe_fx[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    3753             :     Word32 ImagBufferLfe_fx[MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX], /*q_cldfb*/
    3754             :     const Word16 slot_index,
    3755             :     const Word16 subframe_index,
    3756             :     const Word16 nchan_transport,
    3757             :     Word16 q_cldfb )
    3758             : {
    3759             :     Word16 i;
    3760             : 
    3761             :     Word16 lfeGain_fx, transportGain_fx;
    3762             :     Word32 protoLfeReal_fx, protoLfeImag_fx;
    3763             :     Word32 transportEne_fx, protoLfeEne_fx, targetEneLfe_fx, targetEneTrans_fx;
    3764             :     Word16 transportEne_q, protoLfeEne_q, targetEneLfe_q, targetEneTrans_q;
    3765       23440 :     Word16 temp, temp_q = 0;
    3766             :     Word16 transportGain_q_fx, lfeGain_q_fx;
    3767             :     Word16 exp, min_q_shift;
    3768             :     Word32 re, im;
    3769       23440 :     move16(); // temp_q
    3770             : 
    3771       23440 :     exp = Q31;
    3772       23440 :     move16();
    3773       23440 :     min_q_shift = Q31;
    3774       23440 :     move16();
    3775             : 
    3776       23440 :     set_zero_fx( RealBufferLfe_fx[slot_index], CLDFB_NO_CHANNELS_MAX );
    3777       23440 :     set_zero_fx( ImagBufferLfe_fx[slot_index], CLDFB_NO_CHANNELS_MAX );
    3778             : 
    3779       47360 :     FOR( i = 0; i < nchan_transport; i++ )
    3780             :     {
    3781       23920 :         IF( RealBuffer_fx[i][0][0] )
    3782             :         {
    3783       23911 :             exp = norm_l( RealBuffer_fx[i][0][0] );
    3784             :         }
    3785       23920 :         min_q_shift = s_min( min_q_shift, exp );
    3786       23920 :         IF( ImagBuffer_fx[i][0][0] )
    3787             :         {
    3788       23912 :             exp = norm_l( ImagBuffer_fx[i][0][0] );
    3789             :         }
    3790       23920 :         min_q_shift = s_min( min_q_shift, exp );
    3791             :     }
    3792       23440 :     min_q_shift = sub( sub( min_q_shift, find_guarded_bits_fx( nchan_transport ) ), 1 );
    3793             : 
    3794       23440 :     re = L_shl( RealBuffer_fx[0][0][0], min_q_shift ); // q_cldfb+min_q_shift
    3795       23440 :     im = L_shl( ImagBuffer_fx[0][0][0], min_q_shift ); // q_cldfb+min_q_shift
    3796             : 
    3797       23440 :     protoLfeReal_fx = re; // q_cldfb+min_q_shift
    3798       23440 :     move32();
    3799       23440 :     protoLfeImag_fx = im; // q_cldfb+min_q_shift
    3800       23440 :     move32();
    3801       23440 :     transportEne_fx = Madd_32_32( Mpy_32_32( re, re ), im, im ); // 2*(q_cldfb+min_q_shift)-31
    3802       23920 :     FOR( i = 1; i < nchan_transport; i++ )
    3803             :     {
    3804         480 :         re = L_shl( RealBuffer_fx[i][0][0], min_q_shift ); // q_cldfb+min_q_shift
    3805         480 :         im = L_shl( ImagBuffer_fx[i][0][0], min_q_shift ); // q_cldfb+min_q_shift
    3806             : 
    3807         480 :         protoLfeReal_fx = L_add( protoLfeReal_fx, re );                                        // q_cldfb+min_q_shift
    3808         480 :         protoLfeImag_fx = L_add( protoLfeImag_fx, im );                                        // q_cldfb+min_q_shift
    3809         480 :         transportEne_fx = L_add( transportEne_fx, Madd_32_32( Mpy_32_32( re, re ), im, im ) ); // 2*(q_cldfb+min_q_shift)-31
    3810             :     }
    3811       23440 :     transportEne_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    3812             : 
    3813       23440 :     protoLfeEne_fx = Madd_32_32( Mpy_32_32( protoLfeReal_fx, protoLfeReal_fx ), protoLfeImag_fx, protoLfeImag_fx ); // 2*(q_cldfb+min_q_shift)-31
    3814       23440 :     protoLfeEne_q = sub( add( add( q_cldfb, min_q_shift ), add( q_cldfb, min_q_shift ) ), 31 );
    3815             : 
    3816       23440 :     targetEneLfe_fx = Mpy_32_16_1( transportEne_fx, hMasaLfeSynth->lfeToTotalEnergyRatio_fx[subframe_index] ); // targetEneLfe_q+14-15
    3817       23440 :     targetEneLfe_q = sub( add( transportEne_q, Q14 ), 15 );
    3818             : 
    3819       23440 :     temp = s_max( sub( ONE_IN_Q14 /*1 q14*/, hMasaLfeSynth->lfeToTotalEnergyRatio_fx[subframe_index] ), 164 /*0.01 in Q14*/ ); // q14
    3820       23440 :     targetEneTrans_fx = Mpy_32_16_1( transportEne_fx, temp );                                                                  /*transportEne_q+Q14 -15*/
    3821       23440 :     targetEneTrans_q = sub( add( transportEne_q, Q14 ), 15 );
    3822             : 
    3823       23440 :     hMasaLfeSynth->transportEneSmooth_fx = Mpy_32_16_1( hMasaLfeSynth->transportEneSmooth_fx, MCMASA_LFE_SYNTH_ALPHA_Q15 ); // hMasaLfeSynth->transportEneSmooth_q+15-15
    3824       23440 :     move32();
    3825       23440 :     hMasaLfeSynth->protoLfeEneSmooth_fx = Mpy_32_16_1( hMasaLfeSynth->protoLfeEneSmooth_fx, MCMASA_LFE_SYNTH_ALPHA_Q15 ); // hMasaLfeSynth->protoLfeEneSmooth_q+15-15
    3826       23440 :     move32();
    3827       23440 :     hMasaLfeSynth->targetEneLfeSmooth_fx = Mpy_32_16_1( hMasaLfeSynth->targetEneLfeSmooth_fx, MCMASA_LFE_SYNTH_ALPHA_Q15 ); // hMasaLfeSynth->targetEneLfeSmooth_q+15-15
    3828       23440 :     move32();
    3829       23440 :     hMasaLfeSynth->targetEneTransSmooth_fx = Mpy_32_16_1( hMasaLfeSynth->targetEneTransSmooth_fx, MCMASA_LFE_SYNTH_ALPHA_Q15 ); // hMasaLfeSynth->targetEneTransSmooth_q+15-15
    3830       23440 :     move32();
    3831             : 
    3832       23440 :     hMasaLfeSynth->transportEneSmooth_fx = BASOP_Util_Add_Mant32Exp( hMasaLfeSynth->transportEneSmooth_fx, sub( 31, hMasaLfeSynth->transportEneSmooth_q ), transportEne_fx, sub( 31, transportEne_q ), &temp_q ); // 31-temp_q
    3833       23440 :     move32();
    3834       23440 :     hMasaLfeSynth->transportEneSmooth_q = sub( 31, temp_q );
    3835       23440 :     move16();
    3836             : 
    3837       23440 :     hMasaLfeSynth->protoLfeEneSmooth_fx = BASOP_Util_Add_Mant32Exp( hMasaLfeSynth->protoLfeEneSmooth_fx, sub( 31, hMasaLfeSynth->protoLfeEneSmooth_q ), protoLfeEne_fx, sub( 31, protoLfeEne_q ), &temp_q ); // 31-temp_q
    3838       23440 :     move32();
    3839       23440 :     hMasaLfeSynth->protoLfeEneSmooth_q = sub( 31, temp_q );
    3840       23440 :     move16();
    3841             : 
    3842       23440 :     hMasaLfeSynth->targetEneLfeSmooth_fx = BASOP_Util_Add_Mant32Exp( hMasaLfeSynth->targetEneLfeSmooth_fx, sub( 31, hMasaLfeSynth->targetEneLfeSmooth_q ), targetEneLfe_fx, sub( 31, targetEneLfe_q ), &temp_q ); // 31-temp_q
    3843       23440 :     move32();
    3844       23440 :     hMasaLfeSynth->targetEneLfeSmooth_q = sub( 31, temp_q );
    3845       23440 :     move16();
    3846             : 
    3847       23440 :     hMasaLfeSynth->targetEneTransSmooth_fx = BASOP_Util_Add_Mant32Exp( hMasaLfeSynth->targetEneTransSmooth_fx, sub( 31, hMasaLfeSynth->targetEneTransSmooth_q ), targetEneTrans_fx, sub( 31, targetEneTrans_q ), &temp_q ); // 31-temp_q
    3848       23440 :     move32();
    3849       23440 :     hMasaLfeSynth->targetEneTransSmooth_q = sub( 31, temp_q );
    3850       23440 :     move16();
    3851             : 
    3852       23440 :     temp = BASOP_Util_Divide3232_Scale( hMasaLfeSynth->targetEneLfeSmooth_fx, L_add( EPSILON_FX, hMasaLfeSynth->protoLfeEneSmooth_fx ), &temp_q ); // 15-(temp_q-(hMasaLfeSynth->targetEneLfeSmooth_q-hMasaLfeSynth->protoLfeEneSmooth_q))
    3853       23440 :     temp_q = add( sub( hMasaLfeSynth->targetEneLfeSmooth_q, hMasaLfeSynth->protoLfeEneSmooth_q ), sub( Q15, temp_q ) );
    3854             : 
    3855       23440 :     exp = sub( Q15, temp_q );
    3856       23440 :     lfeGain_fx = Sqrt16( temp, &exp ); // 15-exp
    3857       23440 :     lfeGain_q_fx = sub( Q15, exp );
    3858             : 
    3859       23440 :     IF( GT_32( L_shr( lfeGain_fx, sub( lfeGain_q_fx, Q14 ) ), ONE_IN_Q14 /*1 q14*/ ) )
    3860             :     {
    3861           0 :         lfeGain_fx = ONE_IN_Q14; /*1 q14*/
    3862           0 :         move16();
    3863           0 :         lfeGain_q_fx = Q14;
    3864           0 :         move16();
    3865             :     }
    3866             : 
    3867       23440 :     temp = BASOP_Util_Divide3232_Scale( hMasaLfeSynth->targetEneTransSmooth_fx, L_add( EPSILON_FX, hMasaLfeSynth->transportEneSmooth_fx ), &temp_q ); // Q=15-(temp_q-(hMasaLfeSynth->targetEneTransSmooth_q-hMasaLfeSynth->transportEneSmooth_q))
    3868       23440 :     temp_q = add( sub( hMasaLfeSynth->targetEneTransSmooth_q, hMasaLfeSynth->transportEneSmooth_q ), sub( Q15, temp_q ) );
    3869             : 
    3870       23440 :     exp = sub( Q15, temp_q );
    3871       23440 :     transportGain_fx = Sqrt16( temp, &exp ); // q=15-exp
    3872       23440 :     transportGain_q_fx = sub( Q15, exp );
    3873             : 
    3874       23440 :     IF( GT_32( L_shr( transportGain_fx, sub( transportGain_q_fx, Q14 ) ), ONE_IN_Q14 /*1 q14*/ ) )
    3875             :     {
    3876           0 :         transportGain_fx = ONE_IN_Q14; /*1 q14*/
    3877           0 :         move16();
    3878           0 :         transportGain_q_fx = Q14;
    3879           0 :         move16();
    3880             :     }
    3881             : 
    3882       23440 :     RealBufferLfe_fx[slot_index][0] = L_shr( Mpy_32_16_1( protoLfeReal_fx, lfeGain_fx ), add( min_q_shift, sub( lfeGain_q_fx, Q15 ) ) ); // q_cldfb
    3883       23440 :     move32();
    3884       23440 :     ImagBufferLfe_fx[slot_index][0] = L_shr( Mpy_32_16_1( protoLfeImag_fx, lfeGain_fx ), add( min_q_shift, sub( lfeGain_q_fx, Q15 ) ) ); // q_cldfb
    3885       23440 :     move32();
    3886             : 
    3887       23440 :     RealBuffer_fx[0][0][0] = L_shr( Mpy_32_16_1( RealBuffer_fx[0][0][0], transportGain_fx ), sub( transportGain_q_fx, Q15 ) ); // Q = q_cldfb
    3888       23440 :     move32();
    3889       23440 :     ImagBuffer_fx[0][0][0] = L_shr( Mpy_32_16_1( ImagBuffer_fx[0][0][0], transportGain_fx ), sub( transportGain_q_fx, Q15 ) ); // Q = q_cldfb
    3890       23440 :     move32();
    3891       23920 :     FOR( i = 1; i < nchan_transport; i++ )
    3892             :     {
    3893         480 :         RealBuffer_fx[i][0][0] = L_shr( Mpy_32_16_1( RealBuffer_fx[i][0][0], transportGain_fx ), sub( transportGain_q_fx, Q15 ) ); // Q = q_cldfb
    3894         480 :         move32();
    3895         480 :         ImagBuffer_fx[i][0][0] = L_shr( Mpy_32_16_1( ImagBuffer_fx[i][0][0], transportGain_fx ), sub( transportGain_q_fx, Q15 ) ); // Q = q_cldfb
    3896         480 :         move32();
    3897             :     }
    3898             : 
    3899       23440 :     return;
    3900             : }
    3901             : 
    3902             : 
    3903             : /*-------------------------------------------------------------------------
    3904             :  * rotateAziEle_DirAC()
    3905             :  *
    3906             :  * Apply rotation to DirAC DOAs
    3907             :  *------------------------------------------------------------------------*/
    3908             : 
    3909      147200 : void rotateAziEle_DirAC_fx(
    3910             :     Word16 *azi,            /* i/o: array of azimuth values               */
    3911             :     Word16 *ele,            /* i/o: array of elevation values             */
    3912             :     const Word16 band1,     /* i  : bands to work on (lower limit)        */
    3913             :     const Word16 band2,     /* i  : bands to work on (upper bound)        */
    3914             :     const Word32 *p_Rmat_fx /* i  : pointer to real-space rotation matrix q30*/
    3915             : )
    3916             : {
    3917             :     Word16 b;
    3918             :     Word32 dv_0_fx, dv_1_fx, dv_2_fx;
    3919             :     Word32 dv_r_0_fx, dv_r_1_fx, dv_r_2_fx, tmp, w_fx;
    3920             :     Word16 exp, temp;
    3921             :     Word32 *ptr_sin, *ptr_cos;
    3922             : 
    3923      147200 :     ptr_sin = &sine_table_Q31[180]; // sin[x] = sine_table_Q31[180 + x] q31
    3924      147200 :     ptr_cos = cosine_table_Q31;     // q31
    3925             : 
    3926      147200 :     push_wmops( "rotateAziEle_DirAC" );
    3927             : 
    3928     4160000 :     FOR( b = band1; b < band2; b++ )
    3929             :     {
    3930             :         /*Conversion spherical to cartesian coordinates*/
    3931     4012800 :         IF( GT_16( abs_s( ele[b] ), 180 ) )
    3932             :         {
    3933             :             // cos(180 + x) = -cos(x)
    3934           0 :             w_fx = L_negate( ptr_cos[abs( ele[b] ) - 180] ); // Q31
    3935             :         }
    3936             :         ELSE
    3937             :         {
    3938     4012800 :             w_fx = ptr_cos[abs_s( ele[b] )]; // Q31
    3939     4012800 :             move32();
    3940             :         }
    3941             : 
    3942     4012800 :         IF( GT_16( abs_s( azi[b] ), 180 ) )
    3943             :         {
    3944             :             // cos(180 + x) = -cos(x)
    3945     1931328 :             tmp = L_negate( ptr_cos[abs( azi[b] ) - 180] ); // Q31
    3946             :         }
    3947             :         ELSE
    3948             :         {
    3949     2081472 :             tmp = ptr_cos[abs( azi[b] )]; // Q31
    3950     2081472 :             move32();
    3951             :         }
    3952             : 
    3953     4012800 :         dv_0_fx = Mpy_32_32( w_fx, tmp ); // Q31
    3954             : 
    3955             : 
    3956     4012800 :         IF( GT_16( azi[b], 180 ) )
    3957             :         {
    3958             :             // sin(180 + x) = -sin(x)
    3959     1931328 :             tmp = L_negate( ptr_sin[azi[b] - 180] ); // Q31
    3960     1931328 :             move32();
    3961             :         }
    3962     2081472 :         ELSE IF( LT_16( azi[b], -180 ) )
    3963             :         {
    3964             :             // sin(-(180 + x)) = sin(180 + x) = sinx
    3965           0 :             tmp = ptr_sin[abs( azi[b] ) - 180]; // Q31
    3966           0 :             move32();
    3967             :         }
    3968             :         ELSE
    3969             :         {
    3970     2081472 :             tmp = ptr_sin[azi[b]]; // Q31
    3971     2081472 :             move32();
    3972             :         }
    3973     4012800 :         dv_1_fx = Mpy_32_32( w_fx, tmp ); // Q31
    3974             : 
    3975     4012800 :         IF( GT_16( ele[b], 180 ) )
    3976             :         {
    3977             :             // sin(180 + x) = -sin(x)
    3978           0 :             dv_2_fx = L_negate( ptr_sin[ele[b] - 180] ); // Q31
    3979           0 :             move32();
    3980             :         }
    3981     4012800 :         ELSE IF( LT_16( ele[b], -180 ) )
    3982             :         {
    3983             :             // sin(-(180 + x)) = -sin(180 + x) = sinx
    3984           0 :             dv_2_fx = ptr_sin[abs( ele[b] ) - 180]; // Q31
    3985           0 :             move32();
    3986             :         }
    3987             :         ELSE
    3988             :         {
    3989     4012800 :             dv_2_fx = ptr_sin[ele[b]]; // Q31
    3990     4012800 :             move32();
    3991             :         }
    3992             : 
    3993     4012800 :         dv_r_0_fx = Madd_32_32( Madd_32_32( Mpy_32_32( L_shr( p_Rmat_fx[0], Q1 ), dv_0_fx ), L_shr( p_Rmat_fx[1], Q1 ), dv_1_fx ), L_shr( p_Rmat_fx[2], Q1 ), dv_2_fx ); // Q29
    3994     4012800 :         dv_r_1_fx = Madd_32_32( Madd_32_32( Mpy_32_32( L_shr( p_Rmat_fx[3], Q1 ), dv_0_fx ), L_shr( p_Rmat_fx[4], Q1 ), dv_1_fx ), L_shr( p_Rmat_fx[5], Q1 ), dv_2_fx ); // Q29
    3995     4012800 :         dv_r_2_fx = Madd_32_32( Madd_32_32( Mpy_32_32( L_shr( p_Rmat_fx[6], Q1 ), dv_0_fx ), L_shr( p_Rmat_fx[7], Q1 ), dv_1_fx ), L_shr( p_Rmat_fx[8], Q1 ), dv_2_fx ); // Q29
    3996             : 
    3997             :         /*Conversion spherical to cartesian coordinates*/
    3998     4012800 :         temp = BASOP_util_atan2( dv_r_1_fx, dv_r_0_fx, 0 ); // Q13
    3999     4012800 :         azi[b] = shr( mult( temp, _180_OVER_PI_Q9 ), 7 );   // Q0;
    4000     4012800 :         move16();
    4001             : 
    4002     4012800 :         tmp = L_add( Mpy_32_32( dv_r_0_fx, dv_r_0_fx ), Mpy_32_32( dv_r_1_fx, dv_r_1_fx ) ); // Q27
    4003     4012800 :         exp = sub( 31, Q27 );
    4004     4012800 :         tmp = Sqrt32( tmp, &exp );                                            // q=31-exp
    4005     4012800 :         temp = BASOP_util_atan2( dv_r_2_fx, tmp, sub( sub( 31, 29 ), exp ) ); // Q13
    4006     4012800 :         ele[b] = shr( mult( temp, _180_OVER_PI_Q9 ), 7 );                     // Q0
    4007     4012800 :         move16();
    4008             :     }
    4009             : 
    4010      147200 :     pop_wmops();
    4011             : 
    4012      147200 :     return;
    4013             : }
    4014             : 
    4015             : 
    4016             : /* A reduced rewrite of the corresponding decoder side function */
    4017       20400 : static void ivas_masa_ext_dirac_render_sf_fx(
    4018             :     MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: IVAS decoder structure                                  */
    4019             :     Word32 *output_f_fx[]              /* i/o: synthesized core-coder transport channels/DirAC output q11*/
    4020             : )
    4021             : {
    4022             :     Word16 i, ch, idx_in, idx_lfe;
    4023             :     DIRAC_REND_HANDLE hDirACRend;
    4024             :     Word16 subframe_idx;
    4025             :     Word16 slot_idx, index_slot;
    4026             :     Word16 slot_idx_start, slot_idx_start_cldfb_synth, md_idx;
    4027             :     Word16 nchan_transport;
    4028             :     Word16 masa_band_mapping[MASA_FREQUENCY_BANDS + 1];
    4029             : 
    4030             :     /* local copies of azi, ele, diffuseness */
    4031             :     Word16 azimuth[CLDFB_NO_CHANNELS_MAX];
    4032             :     Word16 elevation[CLDFB_NO_CHANNELS_MAX];
    4033             : 
    4034             :     Word32 diffuseness_vector_fx[CLDFB_NO_CHANNELS_MAX];
    4035             :     Word32 Cldfb_RealBuffer_fx[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
    4036             :     Word32 Cldfb_ImagBuffer_fx[MAX_OUTPUT_CHANNELS][MAX_PARAM_SPATIAL_SUBFRAMES][CLDFB_NO_CHANNELS_MAX];
    4037       20400 :     Word16 q_cldfb = 0;
    4038             :     Word32 surCohRatio_fx[CLDFB_NO_CHANNELS_MAX];
    4039             :     Word16 Q_surCohRatio, surCohRatio_exp[CLDFB_NO_CHANNELS_MAX];
    4040             :     Word32 dirEne_fx;
    4041             :     Word32 surCohEner_fx;
    4042       20400 :     move16();
    4043             : 
    4044      346800 :     FOR( Word16 ii = 0; ii < MAX_OUTPUT_CHANNELS; ii++ )
    4045             :     {
    4046     1632000 :         FOR( Word16 jj = 0; jj < MAX_PARAM_SPATIAL_SUBFRAMES; jj++ )
    4047             :         {
    4048     1305600 :             set_zero_fx( Cldfb_RealBuffer_fx[ii][jj], CLDFB_NO_CHANNELS_MAX );
    4049     1305600 :             set_zero_fx( Cldfb_ImagBuffer_fx[ii][jj], CLDFB_NO_CHANNELS_MAX );
    4050             :         }
    4051             :     }
    4052             : 
    4053             :     DIRAC_DEC_STACK_MEM DirAC_mem;
    4054             : 
    4055       20400 :     Word32 *onset_filter_fx, *onset_filter_subframe_fx, *p_onset_filter_fx = NULL;
    4056             :     Word32 *reference_power_smooth_fx, *reference_power_fix;
    4057             :     UWord16 coherence_flag;
    4058             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    4059             : 
    4060       20400 :     push_wmops( "ivas_masa_ext_dirac_render_sf" );
    4061             : 
    4062             :     /* Initialize aux buffers */
    4063       20400 :     hDirACRend = hMasaExtRend->hDirACRend;
    4064       20400 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    4065       20400 :     nchan_transport = hMasaExtRend->nchan_input;
    4066       20400 :     move16();
    4067             : 
    4068       20400 :     DirAC_mem = hDirACRend->stack_mem;
    4069             : 
    4070       20400 :     onset_filter_fx = DirAC_mem.onset_filter_fx;        // q31
    4071       20400 :     reference_power_fix = DirAC_mem.reference_power_fx; // DirAC_mem.reference_power_q
    4072       20400 :     IF( ( DirAC_mem.reference_power_fx == NULL ) )
    4073             :     {
    4074        3600 :         reference_power_smooth_fx = NULL;
    4075             :     }
    4076             :     ELSE
    4077             :     {
    4078       16800 :         reference_power_smooth_fx = DirAC_mem.reference_power_fx + hSpatParamRendCom->num_freq_bands; // DirAC_mem.reference_power_q
    4079             :     }
    4080       20400 :     IF( ( DirAC_mem.onset_filter_fx == NULL ) )
    4081             :     {
    4082        1200 :         onset_filter_subframe_fx = NULL;
    4083             :     }
    4084             :     ELSE
    4085             :     {
    4086       19200 :         onset_filter_subframe_fx = DirAC_mem.onset_filter_fx + hSpatParamRendCom->num_freq_bands; // q31
    4087             :     }
    4088       20400 :     coherence_flag = 1; /* There is always coherence assumed for ext rend of MASA */
    4089       20400 :     move16();
    4090             :     /* Construct default MASA band mapping */
    4091      530400 :     FOR( i = 0; i < MASA_FREQUENCY_BANDS + 1; i++ )
    4092             :     {
    4093      510000 :         masa_band_mapping[i] = i;
    4094      510000 :         move16();
    4095             :     }
    4096             : 
    4097             :     /* Subframe loop */
    4098       20400 :     slot_idx_start = hSpatParamRendCom->slots_rendered;
    4099       20400 :     move16();
    4100       20400 :     slot_idx_start_cldfb_synth = 0;
    4101       20400 :     move16();
    4102             : 
    4103       20400 :     subframe_idx = hSpatParamRendCom->subframes_rendered;
    4104       20400 :     move16();
    4105       20400 :     md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    4106       20400 :     move16();
    4107             : 
    4108             :     DIRAC_OUTPUT_SYNTHESIS_STATE *h_dirac_output_synthesis_state;
    4109       20400 :     h_dirac_output_synthesis_state = &( hDirACRend->h_output_synthesis_psd_state );
    4110             : 
    4111             :     /* copy parameters into local buffers*/
    4112             : 
    4113       20400 :     Copy( hSpatParamRendCom->azimuth[hSpatParamRendCom->render_to_md_map[subframe_idx]], azimuth, hSpatParamRendCom->num_freq_bands );
    4114       20400 :     Copy( hSpatParamRendCom->elevation[hSpatParamRendCom->render_to_md_map[subframe_idx]], elevation, hSpatParamRendCom->num_freq_bands );
    4115       20400 :     Copy32( hSpatParamRendCom->diffuseness_vector_fx[hSpatParamRendCom->render_to_md_map[subframe_idx]], diffuseness_vector_fx, hSpatParamRendCom->num_freq_bands ); // q30
    4116             : 
    4117       20400 :     IF( NE_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4118             :     {
    4119       16800 :         set32_fx( reference_power_smooth_fx, 0, hSpatParamRendCom->num_freq_bands );
    4120             :     }
    4121             :     ELSE
    4122             :     {
    4123        3600 :         set32_fx( onset_filter_subframe_fx, 0, hSpatParamRendCom->num_freq_bands );
    4124             :     }
    4125             : 
    4126             :     /* compute response */
    4127       20400 :     IF( NE_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4128             :     {
    4129       16800 :         ivas_dirac_dec_compute_power_factors_fx( hSpatParamRendCom->num_freq_bands,
    4130             :                                                  diffuseness_vector_fx,
    4131       16800 :                                                  hDirACRend->h_output_synthesis_psd_params.max_band_decorr,
    4132             :                                                  hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx,
    4133             :                                                  hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx );
    4134             : 
    4135       16800 :         hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q = Q29;
    4136       16800 :         move16();
    4137       16800 :         hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q = Q29;
    4138       16800 :         move16();
    4139             : 
    4140             : 
    4141       16800 :         IF( coherence_flag )
    4142             :         {
    4143       16800 :             Word16 temp_exp = MIN_16;
    4144       16800 :             move16();
    4145     1024800 :             FOR( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    4146             :             {
    4147     1008000 :                 dirEne_fx = hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx[i]; // 29
    4148     1008000 :                 move32();
    4149     1008000 :                 surCohEner_fx = Mpy_32_16_1( hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx[i], hSpatParamRendCom->surroundingCoherence_fx[md_idx][i] );         // Q.29
    4150     1008000 :                 hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx[i] = L_sub( hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx[i], surCohEner_fx ); // q29
    4151     1008000 :                 move32();
    4152     1008000 :                 hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx[i] = L_add( hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx[i], surCohEner_fx ); // q29
    4153     1008000 :                 move32();
    4154             : 
    4155     1008000 :                 surCohRatio_fx[i] = BASOP_Util_Divide3232_Scale_newton( surCohEner_fx, L_add( L_add( 1, dirEne_fx ), surCohEner_fx ), &surCohRatio_exp[i] ); // 31-surCohRatio_exp
    4156     1008000 :                 move32();
    4157     1008000 :                 temp_exp = s_max( temp_exp, surCohRatio_exp[i] );
    4158             :             }
    4159             : 
    4160     1024800 :             FOR( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    4161             :             {
    4162     1008000 :                 surCohRatio_fx[i] = L_shr( surCohRatio_fx[i], sub( temp_exp, surCohRatio_exp[i] ) ); // 31-temp_exp
    4163     1008000 :                 move32();
    4164             :             }
    4165       16800 :             Q_surCohRatio = sub( 31, temp_exp );
    4166             :         }
    4167             :         ELSE
    4168             :         {
    4169           0 :             set32_fx( surCohRatio_fx, 0, hSpatParamRendCom->num_freq_bands );
    4170           0 :             Q_surCohRatio = 31;
    4171           0 :             move16();
    4172             :         }
    4173             :     }
    4174             :     ELSE
    4175             :     {
    4176        3600 :         ivas_dirac_dec_compute_gain_factors_fx( hSpatParamRendCom->num_freq_bands,
    4177        3600 :                                                 hSpatParamRendCom->diffuseness_vector_fx[md_idx],
    4178             :                                                 hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx,
    4179             :                                                 hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx,
    4180             :                                                 &hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q,
    4181             :                                                 &hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q );
    4182        3600 :         hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q = sub( 31, hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q );
    4183        3600 :         move16();
    4184        3600 :         hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q = sub( 31, hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q );
    4185        3600 :         move16();
    4186        3600 :         IF( coherence_flag )
    4187             :         {
    4188      219600 :             FOR( i = 0; i < hSpatParamRendCom->num_freq_bands; i++ )
    4189             :             {
    4190      216000 :                 surCohRatio_fx[i] = L_deposit_h( hSpatParamRendCom->surroundingCoherence_fx[md_idx][i] ); // q31
    4191      216000 :                 move32();
    4192             :             }
    4193             :         }
    4194             :         ELSE
    4195             :         {
    4196           0 :             set32_fx( surCohRatio_fx, 0, hSpatParamRendCom->num_freq_bands );
    4197             :         }
    4198             : 
    4199        3600 :         Q_surCohRatio = Q31;
    4200        3600 :         move16();
    4201             :     }
    4202             : 
    4203       20400 :     ivas_dirac_dec_compute_directional_responses_fx( hSpatParamRendCom,
    4204             :                                                      hDirACRend,
    4205             :                                                      hMasaExtRend->hVBAPdata,
    4206             :                                                      masa_band_mapping,
    4207             :                                                      NULL,
    4208             :                                                      azimuth,
    4209             :                                                      elevation,
    4210             :                                                      md_idx,
    4211             :                                                      surCohRatio_fx,
    4212             :                                                      Q_surCohRatio,
    4213             :                                                      0,
    4214             :                                                      NULL,
    4215             :                                                      0 );
    4216             : 
    4217             :     Word16 proto_direct_buffer_f_temp_q[60];
    4218             :     Word16 temp_proto_frame_q;
    4219             : 
    4220      102000 :     FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4221             :     {
    4222       81600 :         index_slot = add( slot_idx_start, slot_idx );
    4223       81600 :         md_idx = hSpatParamRendCom->render_to_md_map[subframe_idx];
    4224       81600 :         move16();
    4225       81600 :         proto_direct_buffer_f_temp_q[slot_idx] = MAX_16;
    4226       81600 :         move16();
    4227             : 
    4228             :         /* CLDFB Analysis*/
    4229      206400 :         FOR( ch = 0; ch < nchan_transport; ch++ )
    4230             :         {
    4231      124800 :             q_cldfb = 11;
    4232      124800 :             move16();
    4233      124800 :             hMasaExtRend->cldfbAnaRend[ch]->Q_cldfb_state = q_cldfb;
    4234      124800 :             move16();
    4235      124800 :             cldfbAnalysis_ts_fx_fixed_q( &( output_f_fx[ch][hSpatParamRendCom->num_freq_bands * index_slot] ),
    4236      124800 :                                          Cldfb_RealBuffer_fx[ch][0],
    4237      124800 :                                          Cldfb_ImagBuffer_fx[ch][0],
    4238      124800 :                                          hSpatParamRendCom->num_freq_bands,
    4239             :                                          hMasaExtRend->cldfbAnaRend[ch], &q_cldfb );
    4240             :         }
    4241             : 
    4242       81600 :         IF( EQ_16( nchan_transport, 1 ) )
    4243             :         {
    4244             :             /* Need to set second CLDFB channel to zero as further processing assumes CNA content in it */
    4245       38400 :             set32_fx( Cldfb_RealBuffer_fx[1][0], 0, hSpatParamRendCom->num_freq_bands );
    4246       38400 :             set32_fx( Cldfb_ImagBuffer_fx[1][0], 0, hSpatParamRendCom->num_freq_bands );
    4247             :         }
    4248             :         /*-----------------------------------------------------------------*
    4249             :          * prototype signal computation
    4250             :          *-----------------------------------------------------------------*/
    4251             : 
    4252       81600 :         IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4253             :         {
    4254       14400 :             protoSignalComputation_shd_fx( Cldfb_RealBuffer_fx, Cldfb_ImagBuffer_fx,
    4255             :                                            hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx,
    4256             :                                            &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q,
    4257             :                                            hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx,
    4258             :                                            &hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q,
    4259             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4260             :                                            reference_power_fix, DirAC_mem.reference_power_q, slot_idx, nchan_transport,
    4261             : #else
    4262             :                                            reference_power_fix, &DirAC_mem.reference_power_q, slot_idx, nchan_transport,
    4263             : #endif
    4264       14400 :                                            hDirACRend->num_outputs_diff,
    4265       14400 :                                            hSpatParamRendCom->num_freq_bands,
    4266             :                                            0, q_cldfb );
    4267             : 
    4268       14400 :             proto_direct_buffer_f_temp_q[slot_idx] = hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q;
    4269       14400 :             move16();
    4270             :         }
    4271       67200 :         ELSE IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_MONO ) )
    4272             :         {
    4273        4800 :             protoSignalComputation2_fx( Cldfb_RealBuffer_fx, Cldfb_ImagBuffer_fx, hDirACRend->proto_frame_f_fx, &hDirACRend->proto_frame_f_q,
    4274             :                                         hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx,
    4275             :                                         &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q,
    4276             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4277             :                                         reference_power_fix, DirAC_mem.reference_power_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4278        4800 :                                         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4279             : #else
    4280             :                                         reference_power_fix, &DirAC_mem.reference_power_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4281             :                                         &hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4282             : #endif
    4283        4800 :                                         0, slot_idx, hSpatParamRendCom->num_freq_bands, hDirACRend->masa_stereo_type_detect, q_cldfb );
    4284             : 
    4285        4800 :             proto_direct_buffer_f_temp_q[slot_idx] = hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q;
    4286        4800 :             move16();
    4287             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4288        4800 :             IF( LT_16( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) )
    4289             :             {
    4290        2784 :                 FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4291             :                 {
    4292        1392 :                     Scale_sig32( reference_power_fix + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) ); // DirAC_mem.reference_power_q
    4293             :                 }
    4294        1392 :                 DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[0];
    4295        1392 :                 move16();
    4296             :             }
    4297             :             ELSE
    4298             :             {
    4299        3408 :                 Scale_sig32( reference_power_fix, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_smooth_q[0], DirAC_mem.reference_power_q[0] ) ); // DirAC_mem.reference_power_smooth_q
    4300        3408 :                 DirAC_mem.reference_power_q[0] = DirAC_mem.reference_power_smooth_q[0];
    4301        3408 :                 move16();
    4302             :             }
    4303        4800 :             IF( LT_16( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) )
    4304             :             {
    4305        2652 :                 FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4306             :                 {
    4307             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4308        1326 :                     Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) ); // DirAC_mem.reference_power_q
    4309             : #else
    4310             :                     Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) );         // DirAC_mem.reference_power_q
    4311             : #endif
    4312             :                 }
    4313        1326 :                 DirAC_mem.reference_power_smooth_q[1] = DirAC_mem.reference_power_q[1];
    4314        1326 :                 move16();
    4315             :             }
    4316             :             ELSE
    4317             :             {
    4318             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4319        3474 :                 Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) ); // DirAC_mem.reference_power_smooth_q
    4320             : #else
    4321             :                 Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) );                 // DirAC_mem.reference_power_smooth_q
    4322             : #endif
    4323        3474 :                 DirAC_mem.reference_power_q[1] = DirAC_mem.reference_power_smooth_q[1];
    4324        3474 :                 move16();
    4325             :             }
    4326             : #else
    4327             :             IF( LT_16( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) )
    4328             :             {
    4329             :                 Scale_sig32( reference_power_fix + hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_len, hSpatParamRendCom->num_freq_bands ), sub( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) ); // DirAC_mem.reference_power_q
    4330             :                 DirAC_mem.reference_power_smooth_q = DirAC_mem.reference_power_q;
    4331             :                 move16();
    4332             :             }
    4333             :             ELSE
    4334             :             {
    4335             :                 Scale_sig32( reference_power_fix, hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_smooth_q, DirAC_mem.reference_power_q ) ); // DirAC_mem.reference_power_smooth_q
    4336             :                 DirAC_mem.reference_power_q = DirAC_mem.reference_power_smooth_q;
    4337             :                 move16();
    4338             :             }
    4339             : #endif
    4340        4800 :             temp_proto_frame_q = sub( getScaleFactor32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len ), 2 );
    4341        4800 :             Scale_sig32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len, temp_proto_frame_q ); // hDirACRend->proto_frame_f_q+temp_proto_frame_q
    4342        4800 :             hDirACRend->proto_frame_f_q = add( hDirACRend->proto_frame_f_q, temp_proto_frame_q );
    4343        4800 :             move16();
    4344             :         }
    4345             :         ELSE
    4346             :         {
    4347       62400 :             SWITCH( nchan_transport )
    4348             :             {
    4349       38400 :                 case 2:
    4350       38400 :                     protoSignalComputation2_fx( Cldfb_RealBuffer_fx, Cldfb_ImagBuffer_fx,
    4351             :                                                 hDirACRend->proto_frame_f_fx,
    4352             :                                                 &hDirACRend->proto_frame_f_q,
    4353             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx,
    4354             :                                                 &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q,
    4355             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4356             :                                                 reference_power_fix, DirAC_mem.reference_power_q,
    4357             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4358       38400 :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4359             : #else
    4360             :                                                 reference_power_fix, &DirAC_mem.reference_power_q,
    4361             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4362             :                                                 &hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4363             : #endif
    4364       38400 :                                                 hDirACRend->hOutSetup.is_loudspeaker_setup,
    4365             :                                                 slot_idx,
    4366       38400 :                                                 hSpatParamRendCom->num_freq_bands,
    4367             :                                                 hDirACRend->masa_stereo_type_detect, q_cldfb );
    4368             : 
    4369       38400 :                     proto_direct_buffer_f_temp_q[slot_idx] = hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q;
    4370       38400 :                     move16();
    4371             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4372       38400 :                     IF( LT_16( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) )
    4373             :                     {
    4374       22272 :                         FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4375             :                         {
    4376       11136 :                             Scale_sig32( reference_power_fix + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) ); // DirAC_mem.reference_power_q
    4377             :                         }
    4378       11136 :                         DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[0];
    4379       11136 :                         move16();
    4380             :                     }
    4381             :                     ELSE
    4382             :                     {
    4383       27264 :                         Scale_sig32( reference_power_fix, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_smooth_q[0], DirAC_mem.reference_power_q[0] ) ); // DirAC_mem.reference_power_smooth_q
    4384       27264 :                         DirAC_mem.reference_power_q[0] = DirAC_mem.reference_power_smooth_q[0];
    4385       27264 :                         move16();
    4386             :                     }
    4387       38400 :                     IF( LT_16( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) )
    4388             :                     {
    4389       21216 :                         FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4390             :                         {
    4391             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4392       10608 :                             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) ); // DirAC_mem.reference_power_q
    4393             : #else
    4394             :                             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) ); // DirAC_mem.reference_power_q
    4395             : #endif
    4396             :                         }
    4397             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4398       10608 :                         DirAC_mem.reference_power_smooth_q[1] = DirAC_mem.reference_power_q[1];
    4399             : #else
    4400             :                         DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[1];
    4401             : #endif
    4402       10608 :                         move16();
    4403             :                     }
    4404             :                     ELSE
    4405             :                     {
    4406             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4407       27792 :                         Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) ); // DirAC_mem.reference_power_smooth_q
    4408             : #else
    4409             :                         Scale_sig32( reference_power_fix, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) );                                                                                                                                                                                                          // DirAC_mem.reference_power_smooth_q
    4410             : #endif
    4411       27792 :                         DirAC_mem.reference_power_q[1] = DirAC_mem.reference_power_smooth_q[1];
    4412       27792 :                         move16();
    4413             :                     }
    4414             : 
    4415             : #else
    4416             :                     IF( LT_16( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) )
    4417             :                     {
    4418             :                         Scale_sig32( reference_power_fix + hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_len, hSpatParamRendCom->num_freq_bands ), sub( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) ); // DirAC_mem.reference_power_q
    4419             :                         DirAC_mem.reference_power_smooth_q = DirAC_mem.reference_power_q;
    4420             :                         move16();
    4421             :                     }
    4422             :                     ELSE
    4423             :                     {
    4424             :                         Scale_sig32( reference_power_fix, hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_smooth_q, DirAC_mem.reference_power_q ) ); // DirAC_mem.reference_power_smooth_q
    4425             :                         DirAC_mem.reference_power_q = DirAC_mem.reference_power_smooth_q;
    4426             :                         move16();
    4427             :                     }
    4428             : #endif
    4429       38400 :                     temp_proto_frame_q = sub( getScaleFactor32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len ), 2 );
    4430       38400 :                     Scale_sig32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len, temp_proto_frame_q ); // hDirACRend->proto_frame_f_q+temp_proto_frame_q
    4431       38400 :                     hDirACRend->proto_frame_f_q = add( hDirACRend->proto_frame_f_q, temp_proto_frame_q );
    4432       38400 :                     move16();
    4433             : 
    4434       38400 :                     BREAK;
    4435       24000 :                 case 1:
    4436       24000 :                     protoSignalComputation1_fx( Cldfb_RealBuffer_fx, Cldfb_ImagBuffer_fx,
    4437             :                                                 hDirACRend->proto_frame_f_fx,
    4438             :                                                 &hDirACRend->proto_frame_f_q,
    4439             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx,
    4440             :                                                 &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q,
    4441             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4442             :                                                 reference_power_fix, DirAC_mem.reference_power_q,
    4443             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4444       24000 :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4445             : #else
    4446             :                                                 reference_power_fix, &DirAC_mem.reference_power_q,
    4447             :                                                 hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx,
    4448             :                                                 &hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q,
    4449             : #endif
    4450             :                                                 slot_idx,
    4451       24000 :                                                 hDirACRend->num_protos_diff,
    4452       24000 :                                                 hSpatParamRendCom->num_freq_bands, q_cldfb );
    4453             : 
    4454       24000 :                     proto_direct_buffer_f_temp_q[slot_idx] = hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q;
    4455       24000 :                     move16();
    4456             : 
    4457             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4458       24000 :                     IF( LT_16( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) )
    4459             :                     {
    4460       15420 :                         FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4461             :                         {
    4462        7710 :                             Scale_sig32( reference_power_fix + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) ); // DirAC_mem.reference_power_q
    4463             :                         }
    4464        7710 :                         DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[0];
    4465        7710 :                         move16();
    4466             :                     }
    4467             :                     ELSE
    4468             :                     {
    4469       16290 :                         Scale_sig32( reference_power_fix, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_smooth_q[0], DirAC_mem.reference_power_q[0] ) ); // DirAC_mem.reference_power_smooth_q
    4470       16290 :                         DirAC_mem.reference_power_q[0] = DirAC_mem.reference_power_smooth_q[0];
    4471       16290 :                         move16();
    4472             :                     }
    4473       24000 :                     IF( LT_16( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) )
    4474             :                     {
    4475       15420 :                         FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4476             :                         {
    4477             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4478        7710 :                             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) ); // DirAC_mem.reference_power_q
    4479             : #else
    4480             :                             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) );                                                                                                                                                                         // DirAC_mem.reference_power_q
    4481             : #endif
    4482             :                         }
    4483        7710 :                         DirAC_mem.reference_power_smooth_q[1] = DirAC_mem.reference_power_q[1];
    4484        7710 :                         move16();
    4485             :                     }
    4486             :                     ELSE
    4487             :                     {
    4488             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4489       16290 :                         Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) ); // DirAC_mem.reference_power_smooth_q
    4490             : #else
    4491             :                         Scale_sig32( reference_power_fix, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) );                                                                                                                                                                                                          // DirAC_mem.reference_power_smooth_q
    4492             : #endif
    4493       16290 :                         DirAC_mem.reference_power_q[1] = DirAC_mem.reference_power_smooth_q[1];
    4494       16290 :                         move16();
    4495             :                     }
    4496             : #else
    4497             :                     IF( LT_16( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) )
    4498             :                     {
    4499             :                         Scale_sig32( reference_power_fix + hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_len, hSpatParamRendCom->num_freq_bands ), sub( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) ); // DirAC_mem.reference_power_q
    4500             :                         DirAC_mem.reference_power_smooth_q = DirAC_mem.reference_power_q;
    4501             :                         move16();
    4502             :                     }
    4503             :                     ELSE
    4504             :                     {
    4505             :                         Scale_sig32( reference_power_fix, hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_smooth_q, DirAC_mem.reference_power_q ) ); // DirAC_mem.reference_power_smooth_q
    4506             :                         DirAC_mem.reference_power_q = DirAC_mem.reference_power_smooth_q;
    4507             :                         move16();
    4508             :                     }
    4509             : #endif
    4510             : 
    4511       24000 :                     temp_proto_frame_q = sub( getScaleFactor32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len ), 2 );
    4512       24000 :                     Scale_sig32( hDirACRend->proto_frame_f_fx, hDirACRend->proto_frame_f_len, temp_proto_frame_q ); // hDirACRend->proto_frame_f_q+temp_proto_frame_q
    4513       24000 :                     hDirACRend->proto_frame_f_q = add( hDirACRend->proto_frame_f_q, temp_proto_frame_q );
    4514       24000 :                     move16();
    4515             : 
    4516       24000 :                     BREAK;
    4517           0 :                 default:
    4518           0 :                     return;
    4519             :             }
    4520             :         }
    4521             : 
    4522             :         /*-----------------------------------------------------------------*
    4523             :          * frequency domain decorrelation
    4524             :          *-----------------------------------------------------------------*/
    4525       81600 :         IF( EQ_16( hDirACRend->proto_signal_decorr_on, 1 ) )
    4526             :         {
    4527             :             /* decorrelate prototype frame */
    4528       76800 :             IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4529             :             {
    4530       28800 :                 ivas_dirac_dec_decorr_process_fx( hSpatParamRendCom->num_freq_bands,
    4531       14400 :                                                   hDirACRend->num_outputs_diff,
    4532       14400 :                                                   hDirACRend->num_protos_diff,
    4533             :                                                   hDirACRend->synthesisConf,
    4534             :                                                   nchan_transport,
    4535       14400 :                                                   hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff,
    4536       14400 :                                                   hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q,
    4537       14400 :                                                   hDirACRend->num_protos_diff,
    4538       14400 :                                                   hDirACRend->proto_index_diff,
    4539       14400 :                                                   hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx + slot_idx * 2 * hSpatParamRendCom->num_freq_bands * hDirACRend->num_outputs_diff + 2 * hSpatParamRendCom->num_freq_bands * s_min( 4, nchan_transport ),
    4540             :                                                   &hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q,
    4541             :                                                   onset_filter_fx,
    4542             :                                                   hDirACRend->h_freq_domain_decorr_ap_params,
    4543             :                                                   hDirACRend->h_freq_domain_decorr_ap_state );
    4544             : 
    4545       14400 :                 v_multc_fixed( onset_filter_fx, 536870912 /* 0.25f in Q31 */, onset_filter_fx, hSpatParamRendCom->num_freq_bands );            /* Q31 */
    4546       14400 :                 v_add_fixed_no_hdrm( onset_filter_fx, onset_filter_subframe_fx, onset_filter_subframe_fx, hSpatParamRendCom->num_freq_bands ); /* Q31 */
    4547       14400 :                 p_onset_filter_fx = onset_filter_subframe_fx;                                                                                  /*q31*/
    4548             :             }
    4549             :             ELSE
    4550             :             {
    4551       62400 :                 set_zero_fx( DirAC_mem.frame_dec_f_fx, DirAC_mem.frame_dec_f_len );
    4552             : 
    4553       62400 :                 ivas_dirac_dec_decorr_process_fx( hSpatParamRendCom->num_freq_bands,
    4554       62400 :                                                   hDirACRend->num_outputs_diff,
    4555       62400 :                                                   hDirACRend->num_protos_diff,
    4556             :                                                   hDirACRend->synthesisConf,
    4557             :                                                   nchan_transport,
    4558       62400 :                                                   hDirACRend->proto_frame_f_fx,
    4559       62400 :                                                   hDirACRend->proto_frame_f_q,
    4560       62400 :                                                   hDirACRend->num_protos_diff,
    4561       62400 :                                                   hDirACRend->proto_index_diff,
    4562             :                                                   DirAC_mem.frame_dec_f_fx,
    4563             :                                                   &DirAC_mem.frame_dec_f_q,
    4564             :                                                   onset_filter_fx,
    4565             :                                                   hDirACRend->h_freq_domain_decorr_ap_params,
    4566             :                                                   hDirACRend->h_freq_domain_decorr_ap_state );
    4567             : 
    4568       62400 :                 hDirACRend->proto_frame_dec_f_fx = DirAC_mem.frame_dec_f_fx; // DirAC_mem.frame_dec_f_q
    4569       62400 :                 hDirACRend->proto_frame_dec_f_q = DirAC_mem.frame_dec_f_q;
    4570       62400 :                 move16();
    4571       62400 :                 p_onset_filter_fx = onset_filter_fx; // q31
    4572             :             }
    4573             :         }
    4574             :         ELSE
    4575             :         {
    4576        4800 :             IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4577             :             {
    4578           0 :                 set32_fx( onset_filter_subframe_fx, ONE_IN_Q31, hSpatParamRendCom->num_freq_bands ); // q31
    4579           0 :                 p_onset_filter_fx = onset_filter_subframe_fx;                                        // q31
    4580             :             }
    4581             :             ELSE
    4582             :             {
    4583             :                 /* no frequency domain decorrelation: use prototype frame */
    4584        4800 :                 hDirACRend->proto_frame_dec_f_fx = hDirACRend->proto_frame_f_fx; // q31
    4585        4800 :                 hDirACRend->proto_frame_dec_f_q = hDirACRend->proto_frame_f_q;
    4586        4800 :                 p_onset_filter_fx = NULL;
    4587             :             }
    4588             :         }
    4589             : 
    4590             :         /*-----------------------------------------------------------------*
    4591             :          * output synthesis
    4592             :          *-----------------------------------------------------------------*/
    4593             : 
    4594       81600 :         test();
    4595       81600 :         IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_PSD_LS ) || EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_PSD_SHD ) )
    4596             :         {
    4597             :             /* Compute diffuse prototypes */
    4598       62400 :             ivas_dirac_dec_compute_diffuse_proto_fx( hDirACRend, hSpatParamRendCom->num_freq_bands, slot_idx );
    4599             :         }
    4600       81600 :         Scale_sig32( h_dirac_output_synthesis_state->diffuse_power_factor_fx, hSpatParamRendCom->num_freq_bands, sub( 31, h_dirac_output_synthesis_state->diffuse_power_factor_q ) ); // q31
    4601       81600 :         h_dirac_output_synthesis_state->diffuse_power_factor_q = Q31;
    4602       81600 :         move16();
    4603             : 
    4604       81600 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx, hSpatParamRendCom->num_freq_bands, sub( 31, hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q ) ); // q31
    4605       81600 :         hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q = Q31;
    4606       81600 :         move16();
    4607             : 
    4608       81600 :         ivas_dirac_dec_output_synthesis_process_slot_fx( reference_power_fix,
    4609             :                                                          DirAC_mem.reference_power_q,
    4610             :                                                          p_onset_filter_fx,
    4611             :                                                          azimuth,
    4612             :                                                          elevation,
    4613       81600 :                                                          hSpatParamRendCom->diffuseness_vector_fx[md_idx],
    4614       81600 :                                                          hSpatParamRendCom->q_diffuseness_vector,
    4615             :                                                          hSpatParamRendCom,
    4616             :                                                          hDirACRend,
    4617             :                                                          0,
    4618             :                                                          0,
    4619             :                                                          hMasaExtRend->hVBAPdata,
    4620             :                                                          hDirACRend->hOutSetup,
    4621             :                                                          nchan_transport,
    4622             :                                                          md_idx,
    4623             :                                                          0,
    4624             :                                                          0 );
    4625             : 
    4626       81600 :         IF( NE_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4627             :         {
    4628       67200 :             Scale_sig32( DirAC_mem.reference_power_fx, DirAC_mem.reference_power_len, -1 ); // DirAC_mem.reference_power_q-1
    4629             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4630       67200 :             DirAC_mem.reference_power_q[0] = sub( DirAC_mem.reference_power_q[0], 1 );
    4631       67200 :             DirAC_mem.reference_power_q[1] = sub( DirAC_mem.reference_power_q[1], 1 );
    4632       67200 :             move16();
    4633       67200 :             move16();
    4634       67200 :             DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[0];
    4635       67200 :             DirAC_mem.reference_power_smooth_q[1] = DirAC_mem.reference_power_q[1];
    4636       67200 :             move16();
    4637       67200 :             move16();
    4638             : #else
    4639             :             DirAC_mem.reference_power_q = sub( DirAC_mem.reference_power_q, 1 );
    4640             :             move16();
    4641             :             DirAC_mem.reference_power_smooth_q = DirAC_mem.reference_power_q;
    4642             :             move16();
    4643             : #endif
    4644       67200 :             v_add_fixed_no_hdrm( reference_power_fix, reference_power_smooth_fx, reference_power_smooth_fx, hSpatParamRendCom->num_freq_bands ); // DirAC_mem.reference_power_smooth_q
    4645             :         }
    4646             :     }
    4647             :     /*Rescaling proto_direct_buffer_f*/
    4648       20400 :     Word16 temp = MAX_16;
    4649       20400 :     move16();
    4650             : 
    4651      102000 :     FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4652             :     {
    4653       81600 :         temp = s_min( temp, proto_direct_buffer_f_temp_q[slot_idx] );
    4654             :     }
    4655             : 
    4656       20400 :     IF( hDirACRend->hOutSetup.is_loudspeaker_setup )
    4657             :     {
    4658       66000 :         FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4659             :         {
    4660       52800 :             Scale_sig32( &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx[( slot_idx * ( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len / hSpatParamRendCom->subframe_nbslots[subframe_idx] ) )], idiv1616( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len, hSpatParamRendCom->subframe_nbslots[subframe_idx] ), sub( temp, proto_direct_buffer_f_temp_q[slot_idx] ) ); // temp
    4661             :         }
    4662             :     }
    4663             :     ELSE
    4664             :     {
    4665       36000 :         FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4666             :         {
    4667       28800 :             Scale_sig32( &hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx[( slot_idx * ( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len / hSpatParamRendCom->subframe_nbslots[subframe_idx] ) )], idiv1616( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len, hSpatParamRendCom->subframe_nbslots[subframe_idx] ), sub( temp, proto_direct_buffer_f_temp_q[slot_idx] ) ); // temp
    4668             :         }
    4669             :     }
    4670       20400 :     hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q = temp;
    4671       20400 :     move16();
    4672             : 
    4673             : 
    4674       20400 :     ivas_dirac_dec_output_synthesis_get_interpolator_fx( &hDirACRend->h_output_synthesis_psd_params, hSpatParamRendCom->subframe_nbslots[subframe_idx] );
    4675             : 
    4676             :     /*Memories Scaling*/
    4677       20400 :     Scale_sig32( hDirACRend->h_output_synthesis_psd_state.direct_power_factor_fx, hSpatParamRendCom->num_freq_bands, sub( 31, hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q ) ); // q31
    4678       20400 :     hDirACRend->h_output_synthesis_psd_state.direct_power_factor_q = Q31;
    4679       20400 :     move16();
    4680       20400 :     Scale_sig32( hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_fx, hSpatParamRendCom->num_freq_bands, sub( 31, hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q ) ); // q31
    4681       20400 :     hDirACRend->h_output_synthesis_psd_state.diffuse_power_factor_q = Q31;
    4682       20400 :     move16();
    4683       20400 :     Scale_sig32( hDirACRend->h_output_synthesis_psd_state.direct_responses_fx, hDirACRend->num_outputs_dir * hSpatParamRendCom->num_freq_bands, sub( 31, hDirACRend->h_output_synthesis_psd_state.direct_responses_q ) ); // q31
    4684       20400 :     hDirACRend->h_output_synthesis_psd_state.direct_responses_q = Q31;
    4685       20400 :     move16();
    4686             : 
    4687       20400 :     IF( EQ_16( hDirACRend->synthesisConf, DIRAC_SYNTHESIS_GAIN_SHD ) )
    4688             :     {
    4689       38400 :         FOR( ch = 0; ch < hDirACRend->hOutSetup.nchan_out_woLFE; ch++ )
    4690             :         {
    4691      174000 :             FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4692             :             {
    4693      139200 :                 Scale_sig32( Cldfb_RealBuffer_fx[ch][slot_idx],
    4694      139200 :                              hSpatParamRendCom->num_freq_bands, sub( 6, q_cldfb ) ); // q6
    4695      139200 :                 Scale_sig32( Cldfb_ImagBuffer_fx[ch][slot_idx],
    4696      139200 :                              hSpatParamRendCom->num_freq_bands, sub( 6, q_cldfb ) ); // q6
    4697             :             }
    4698             :         }
    4699        3600 :         q_cldfb = 6;
    4700        3600 :         move16();
    4701             : 
    4702        3600 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_fx, hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_len, sub( 26, hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth ) ); // q26
    4703        3600 :         hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth = Q26;
    4704        3600 :         move16();
    4705             : 
    4706        3600 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_fx, hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_len, sub( 26, hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth ) ); // q26
    4707        3600 :         hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth = Q26;
    4708        3600 :         move16();
    4709             : 
    4710        3600 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.cy_auto_diff_smooth_prev_len, sub( 26, hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth_prev ) ); // q26
    4711        3600 :         hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth_prev = Q26;
    4712        3600 :         move16();
    4713             : 
    4714        3600 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.cy_cross_dir_smooth_prev_len, sub( 26, hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth_prev ) ); // q26
    4715        3600 :         hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth_prev = Q26;
    4716        3600 :         move16();
    4717             : 
    4718        3600 :         ivas_dirac_dec_output_synthesis_process_subframe_gain_shd_fx( Cldfb_RealBuffer_fx,
    4719             :                                                                       Cldfb_ImagBuffer_fx,
    4720             :                                                                       hSpatParamRendCom,
    4721             :                                                                       hDirACRend,
    4722             :                                                                       nchan_transport,
    4723        3600 :                                                                       hSpatParamRendCom->subframe_nbslots[subframe_idx],
    4724             :                                                                       p_onset_filter_fx,
    4725             :                                                                       diffuseness_vector_fx,
    4726             :                                                                       0,
    4727             :                                                                       0,
    4728             :                                                                       &hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth_prev,
    4729             :                                                                       &hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth_prev );
    4730        3600 :         q_cldfb = -1;
    4731        3600 :         move16();
    4732        3600 :         hDirACRend->h_output_synthesis_psd_state.gains_dir_prev_q = hDirACRend->h_output_synthesis_psd_state.q_cy_cross_dir_smooth_prev;
    4733        3600 :         move16();
    4734        3600 :         hDirACRend->h_output_synthesis_psd_state.gains_diff_prev_q = hDirACRend->h_output_synthesis_psd_state.q_cy_auto_diff_smooth_prev;
    4735        3600 :         move16();
    4736             :     }
    4737             :     ELSE
    4738             :     {
    4739       16800 :         IF( hDirACRend->proto_signal_decorr_on )
    4740             :         {
    4741       15600 :             Word16 new_proto_diffuse_buffer_f_q = getScaleFactor32( hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx, hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_len );
    4742       15600 :             Word16 new_proto_direct_buffer_f_q = getScaleFactor32( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx, hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len );
    4743             : 
    4744       15600 :             scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_fx, hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_len, new_proto_diffuse_buffer_f_q ); // hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q + new_proto_diffuse_buffer_f_q
    4745       15600 :             scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_fx, hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_len, new_proto_direct_buffer_f_q );    // hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q + new_proto_direct_buffer_f_q
    4746       15600 :             hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q = add( hDirACRend->h_output_synthesis_psd_state.proto_direct_buffer_f_q, new_proto_direct_buffer_f_q );
    4747       15600 :             move16();
    4748       15600 :             hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q = add( hDirACRend->h_output_synthesis_psd_state.proto_diffuse_buffer_f_q, new_proto_diffuse_buffer_f_q );
    4749       15600 :             move16();
    4750             :         }
    4751             : 
    4752       16800 :         Word16 reference_power_temp_q = getScaleFactor32( DirAC_mem.reference_power_fx, DirAC_mem.reference_power_len );
    4753       16800 :         scale_sig32( DirAC_mem.reference_power_fx, DirAC_mem.reference_power_len, reference_power_temp_q ); /*DirAC_mem.reference_power_q + reference_power_temp_q*/
    4754             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4755       16800 :         DirAC_mem.reference_power_q[0] = add( DirAC_mem.reference_power_q[0], reference_power_temp_q );
    4756       16800 :         DirAC_mem.reference_power_q[1] = add( DirAC_mem.reference_power_q[1], reference_power_temp_q );
    4757       16800 :         move16();
    4758       16800 :         DirAC_mem.reference_power_smooth_q[0] = add( DirAC_mem.reference_power_q[0], reference_power_temp_q );
    4759       16800 :         DirAC_mem.reference_power_smooth_q[1] = add( DirAC_mem.reference_power_q[1], reference_power_temp_q );
    4760       16800 :         move16();
    4761             : #else
    4762             :         DirAC_mem.reference_power_q = add( DirAC_mem.reference_power_q, reference_power_temp_q );
    4763             :         move16();
    4764             :         DirAC_mem.reference_power_smooth_q = add( DirAC_mem.reference_power_q, reference_power_temp_q );
    4765             :         move16();
    4766             : #endif
    4767             : 
    4768       16800 :         Word16 q_cy_auto_diff_smooth = getScaleFactor32( h_dirac_output_synthesis_state->cy_auto_diff_smooth_fx, h_dirac_output_synthesis_state->cy_auto_diff_smooth_len );
    4769       16800 :         Scale_sig32( h_dirac_output_synthesis_state->cy_auto_diff_smooth_fx, h_dirac_output_synthesis_state->cy_auto_diff_smooth_len, q_cy_auto_diff_smooth ); // h_dirac_output_synthesis_state->q_cy_auto_diff_smooth+ q_cy_auto_diff_smooth
    4770       16800 :         h_dirac_output_synthesis_state->q_cy_auto_diff_smooth = add( h_dirac_output_synthesis_state->q_cy_auto_diff_smooth, q_cy_auto_diff_smooth );
    4771       16800 :         move16();
    4772             : 
    4773       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.direct_responses_square_fx, hDirACRend->num_outputs_dir * hSpatParamRendCom->num_freq_bands, sub( 31, hDirACRend->h_output_synthesis_psd_state.direct_responses_square_q ) ); // q31
    4774       16800 :         hDirACRend->h_output_synthesis_psd_state.direct_responses_square_q = Q31;
    4775       16800 :         move16();
    4776       16800 :         Scale_sig32( h_dirac_output_synthesis_state->diffuse_power_factor_fx, hSpatParamRendCom->num_freq_bands, sub( 31, h_dirac_output_synthesis_state->diffuse_power_factor_q ) ); // q31
    4777       16800 :         h_dirac_output_synthesis_state->diffuse_power_factor_q = Q31;
    4778       16800 :         move16();
    4779       16800 :         Scale_sig32( h_dirac_output_synthesis_state->direct_responses_fx, hSpatParamRendCom->num_freq_bands, sub( 31, h_dirac_output_synthesis_state->direct_responses_q ) ); // q31
    4780       16800 :         h_dirac_output_synthesis_state->direct_responses_q = Q31;
    4781       16800 :         move16();
    4782             : 
    4783             :         /*Q-adjustment*/
    4784       16800 :         IF( hDirACRend->masa_stereo_type_detect )
    4785             :         {
    4786        4800 :             hDirACRend->masa_stereo_type_detect->subtract_power_y_smooth_fx = L_shl( hDirACRend->masa_stereo_type_detect->subtract_power_y_smooth_fx, sub( s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y ), hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth ) ); // s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y )
    4787        4800 :             move32();
    4788        4800 :             hDirACRend->masa_stereo_type_detect->subtract_power_y_fx = L_shl( hDirACRend->masa_stereo_type_detect->subtract_power_y_fx, sub( s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y ), hDirACRend->masa_stereo_type_detect->q_subtract_power_y ) ); // s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y )
    4789        4800 :             move32();
    4790             : 
    4791        4800 :             hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth = s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y );
    4792        4800 :             move16();
    4793        4800 :             hDirACRend->masa_stereo_type_detect->q_subtract_power_y = s_min( hDirACRend->masa_stereo_type_detect->q_subtract_power_y_smooth, hDirACRend->masa_stereo_type_detect->q_subtract_power_y );
    4794        4800 :             move16();
    4795             :         }
    4796             : 
    4797             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4798      147600 :         FOR( i = 0; hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4799             :         {
    4800      130800 :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q )
    4801             :         }
    4802       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0] = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] );
    4803       16800 :         move16();
    4804      147600 :         FOR( i = 0; hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4805             :         {
    4806             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4807      130800 :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q )
    4808             : #else
    4809             :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q )
    4810             : #endif
    4811             :         }
    4812       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] );
    4813       16800 :         move16();
    4814             : 
    4815       50400 :         FOR( i = 0; hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4816             :         {
    4817       33600 :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_fx + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q )
    4818             :         }
    4819       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[0], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[0] );
    4820       16800 :         move16();
    4821             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4822       50400 :         FOR( i = 0; hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4823             :         {
    4824       33600 :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_fx + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q )
    4825             : #else
    4826             :         FOR( i = 0; hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4827             :         {
    4828             :             Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_fx + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1] ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q )
    4829             : #endif
    4830             :         }
    4831       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q[1], hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q[1] );
    4832       16800 :         move16();
    4833             : #else
    4834             :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_len, sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q )
    4835             :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q );
    4836             :         move16();
    4837             :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_len, sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q ), hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q )
    4838             :         hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_smooth_prev_q );
    4839             :         move16();
    4840             : #endif
    4841             : 
    4842       16800 :         Word16 proto_power_diff_smooth_prev_temp_q = getScaleFactor32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_len );
    4843       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_len, proto_power_diff_smooth_prev_temp_q ); // hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q + proto_power_diff_smooth_prev_temp_q
    4844       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q = add( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q, proto_power_diff_smooth_prev_temp_q );
    4845       16800 :         move16();
    4846             : 
    4847       16800 :         Word16 proto_power_diff_smooth_temp_q = getScaleFactor32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_len );
    4848       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_len, proto_power_diff_smooth_temp_q ); // hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q + proto_power_diff_smooth_temp_q
    4849       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q = add( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q, proto_power_diff_smooth_temp_q );
    4850       16800 :         move16();
    4851             : 
    4852       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_len, sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q ), hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q )
    4853       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_fx, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_len, sub( s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q ), hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q ) );                // s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q )
    4854       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q );
    4855       16800 :         move16();
    4856       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q = s_min( hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q, hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q );
    4857       16800 :         move16();
    4858             : 
    4859       16800 :         Scale_sig32( diffuseness_vector_fx, hSpatParamRendCom->num_freq_bands, 1 );
    4860             : 
    4861             :         /*Buffer rescaling*/
    4862             : 
    4863             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4864       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_fx, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0], DirAC_mem.reference_power_q[0] ), hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4865             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4866       16800 :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_fx + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] ), hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4867             : #else
    4868             :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_fx + CLDFB_NO_CHANNELS_HALF, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] ), hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1] ) );                               // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4869             : #endif
    4870       50400 :         FOR( i = 0; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4871             :         {
    4872       33600 :             Scale_sig32( DirAC_mem.reference_power_fx + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0], DirAC_mem.reference_power_q[0] ), DirAC_mem.reference_power_q[0] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4873             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4874       33600 :             Scale_sig32( DirAC_mem.reference_power_fx + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] ), DirAC_mem.reference_power_q[1] ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4875             : #else
    4876             :             Scale_sig32( DirAC_mem.reference_power_fx + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] ), DirAC_mem.reference_power_q[1] ) );                                                                                                             // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4877             : #endif
    4878             :         }
    4879       16800 :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0], DirAC_mem.reference_power_q[0] );
    4880       16800 :         move16();
    4881       16800 :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] );
    4882       16800 :         move16();
    4883       16800 :         DirAC_mem.reference_power_q[0] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0], DirAC_mem.reference_power_q[0] );
    4884       16800 :         move16();
    4885       16800 :         DirAC_mem.reference_power_q[1] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] );
    4886       16800 :         move16();
    4887       16800 :         DirAC_mem.reference_power_smooth_q[0] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0], DirAC_mem.reference_power_q[0] );
    4888       16800 :         move16();
    4889       16800 :         DirAC_mem.reference_power_smooth_q[1] = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1], DirAC_mem.reference_power_q[1] );
    4890       16800 :         move16();
    4891             : #else
    4892             :         Scale_sig32( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_fx, hSpatParamRendCom->num_freq_bands, sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q ), hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q ) ); // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4893             :         Scale_sig32( DirAC_mem.reference_power_fx, DirAC_mem.reference_power_len, sub( s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q ), DirAC_mem.reference_power_q ) );                                                                                           // s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q )
    4894             : 
    4895             :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q );
    4896             :         move16();
    4897             :         DirAC_mem.reference_power_q = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q );
    4898             :         move16();
    4899             :         DirAC_mem.reference_power_smooth_q = s_min( hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q, DirAC_mem.reference_power_q );
    4900             :         move16();
    4901             : #endif
    4902             : 
    4903       16800 :         ivas_dirac_dec_output_synthesis_process_subframe_psd_ls_fx( Cldfb_RealBuffer_fx,
    4904             :                                                                     Cldfb_ImagBuffer_fx,
    4905             :                                                                     hSpatParamRendCom,
    4906             :                                                                     hDirACRend,
    4907       16800 :                                                                     hSpatParamRendCom->subframe_nbslots[subframe_idx],
    4908             :                                                                     diffuseness_vector_fx,
    4909             :                                                                     reference_power_smooth_fx,
    4910             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4911             :                                                                     DirAC_mem.reference_power_smooth_q,
    4912             : #else
    4913             :                                                                     &DirAC_mem.reference_power_smooth_q,
    4914             : #endif
    4915             :                                                                     ONE_IN_Q31,
    4916             :                                                                     0, &q_cldfb );
    4917             : 
    4918       16800 :         hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_prev_q = hDirACRend->h_output_synthesis_psd_state.proto_power_diff_smooth_q;
    4919       16800 :         move16();
    4920             : #ifdef FIX_867_CLDFB_NRG_SCALE
    4921       16800 :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[0] = DirAC_mem.reference_power_smooth_q[0];
    4922       16800 :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q[1] = DirAC_mem.reference_power_smooth_q[1];
    4923       16800 :         move16();
    4924       16800 :         IF( LT_16( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) )
    4925             :         {
    4926           0 :             FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4927             :             {
    4928           0 :                 Scale_sig32( reference_power_fix + i, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_q[0], DirAC_mem.reference_power_smooth_q[0] ) ); // DirAC_mem.reference_power_q
    4929             :             }
    4930           0 :             DirAC_mem.reference_power_smooth_q[0] = DirAC_mem.reference_power_q[0];
    4931           0 :             move16();
    4932             :         }
    4933             :         ELSE
    4934             :         {
    4935       16800 :             Scale_sig32( reference_power_fix, s_min( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ), sub( DirAC_mem.reference_power_smooth_q[0], DirAC_mem.reference_power_q[0] ) ); // DirAC_mem.reference_power_smooth_q
    4936       16800 :             DirAC_mem.reference_power_q[0] = DirAC_mem.reference_power_smooth_q[0];
    4937       16800 :             move16();
    4938             :         }
    4939       16800 :         IF( LT_16( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) )
    4940             :         {
    4941           0 :             FOR( i = hSpatParamRendCom->num_freq_bands; DirAC_mem.reference_power_len > i; i = add( i, hSpatParamRendCom->num_freq_bands ) )
    4942             :             {
    4943             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4944           0 :                 Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) ); // DirAC_mem.reference_power_q
    4945             : #else
    4946             :                 Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF + i, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_q[1], DirAC_mem.reference_power_smooth_q[1] ) );                                                                                                                                                                                               // DirAC_mem.reference_power_q
    4947             : #endif
    4948             :             }
    4949           0 :             DirAC_mem.reference_power_smooth_q[1] = DirAC_mem.reference_power_q[1];
    4950           0 :             move16();
    4951             :         }
    4952             :         ELSE
    4953             :         {
    4954             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4955       16800 :             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_max( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) ); // DirAC_mem.reference_power_smooth_q
    4956             : #else
    4957             :             Scale_sig32( reference_power_fix + CLDFB_NO_CHANNELS_HALF, s_min( 0, sub( hSpatParamRendCom->num_freq_bands, CLDFB_NO_CHANNELS_HALF ) ), sub( DirAC_mem.reference_power_smooth_q[1], DirAC_mem.reference_power_q[1] ) );                                                                                                                                                                                                       // DirAC_mem.reference_power_smooth_q
    4958             : #endif
    4959       16800 :             DirAC_mem.reference_power_q[1] = DirAC_mem.reference_power_smooth_q[1];
    4960       16800 :             move16();
    4961             :         }
    4962             : #else
    4963             :         hDirACRend->h_output_synthesis_psd_state.reference_power_smooth_prev_q = DirAC_mem.reference_power_smooth_q;
    4964             :         move16();
    4965             :         IF( LT_16( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) )
    4966             :         {
    4967             :             Scale_sig32( reference_power_fix + hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_len, hSpatParamRendCom->num_freq_bands ), sub( DirAC_mem.reference_power_q, DirAC_mem.reference_power_smooth_q ) ); // DirAC_mem.reference_power_q
    4968             :             DirAC_mem.reference_power_smooth_q = DirAC_mem.reference_power_q;
    4969             :             move16();
    4970             :         }
    4971             :         ELSE
    4972             :         {
    4973             :             Scale_sig32( reference_power_fix, hSpatParamRendCom->num_freq_bands, sub( DirAC_mem.reference_power_smooth_q, DirAC_mem.reference_power_q ) ); // DirAC_mem.reference_power_smooth_q
    4974             :             DirAC_mem.reference_power_q = DirAC_mem.reference_power_smooth_q;
    4975             :             move16();
    4976             :         }
    4977             : #endif
    4978             :     }
    4979             : 
    4980             :     /*-----------------------------------------------------------------*
    4981             :      * CLDFB synthesis (and binaural rendering)
    4982             :      *-----------------------------------------------------------------*/
    4983             : 
    4984       20400 :     index_slot = slot_idx_start_cldfb_synth;
    4985       20400 :     move16();
    4986             : 
    4987             :     {
    4988             :         Word16 outchannels;
    4989             :         Word32 *RealBuffer_fx[MAX_PARAM_SPATIAL_SUBFRAMES];
    4990             :         Word32 *ImagBuffer_fx[MAX_PARAM_SPATIAL_SUBFRAMES];
    4991       20400 :         Word16 scale_factor = 31;
    4992       20400 :         move16();
    4993             : 
    4994      186000 :         FOR( ch = 0; ch < hDirACRend->num_outputs_dir; ch++ )
    4995             :         {
    4996      828000 :             FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    4997             :             {
    4998             : #ifdef FIX_1737_FIX_867_CLDFB_NRG_SCALE
    4999      662400 :                 scale_factor = s_min( scale_factor, L_norm_arr( Cldfb_RealBuffer_fx[ch][slot_idx],
    5000      662400 :                                                                 hSpatParamRendCom->num_freq_bands ) );
    5001      662400 :                 scale_factor = s_min( scale_factor, L_norm_arr( Cldfb_ImagBuffer_fx[ch][slot_idx],
    5002      662400 :                                                                 hSpatParamRendCom->num_freq_bands ) );
    5003             : #else
    5004             :                 scale_factor = s_min( scale_factor, getScaleFactor32( Cldfb_RealBuffer_fx[ch][slot_idx],
    5005             :                                                                       hSpatParamRendCom->num_freq_bands ) );
    5006             :                 scale_factor = s_min( scale_factor, getScaleFactor32( Cldfb_ImagBuffer_fx[ch][slot_idx],
    5007             :                                                                       hSpatParamRendCom->num_freq_bands ) );
    5008             : #endif
    5009             :             }
    5010             :         }
    5011       20400 :         scale_factor = sub( scale_factor, find_guarded_bits_fx( hSpatParamRendCom->num_freq_bands ) ); // guard bits
    5012             : 
    5013      186000 :         FOR( ch = 0; ch < hDirACRend->num_outputs_dir; ch++ )
    5014             :         {
    5015      828000 :             FOR( slot_idx = 0; slot_idx < hSpatParamRendCom->subframe_nbslots[subframe_idx]; slot_idx++ )
    5016             :             {
    5017      662400 :                 scale_sig32( Cldfb_RealBuffer_fx[ch][slot_idx],
    5018      662400 :                              hSpatParamRendCom->num_freq_bands, scale_factor ); // q_cldfb+scale_factor
    5019      662400 :                 scale_sig32( Cldfb_ImagBuffer_fx[ch][slot_idx],
    5020      662400 :                              hSpatParamRendCom->num_freq_bands, scale_factor ); // q_cldfb+scale_factor
    5021             :             }
    5022             :         }
    5023       20400 :         q_cldfb = add( q_cldfb, scale_factor );
    5024             : 
    5025       20400 :         idx_in = 0;
    5026       20400 :         move16();
    5027       20400 :         idx_lfe = 0;
    5028       20400 :         move16();
    5029             : 
    5030       20400 :         outchannels = add( hDirACRend->hOutSetup.nchan_out_woLFE, hDirACRend->hOutSetup.num_lfe );
    5031             : 
    5032             :         /* Note here that compared to decoder path, there is no separate channel ever for MASA ext rend path */
    5033      196800 :         FOR( ch = 0; ch < outchannels; ch++ )
    5034             :         {
    5035      176400 :             test();
    5036      176400 :             IF( hDirACRend->hOutSetup.num_lfe > 0 && ( EQ_16( hDirACRend->hOutSetup.index_lfe[idx_lfe], ch ) ) )
    5037             :             {
    5038             :                 /* No LFE for MASA rendering */
    5039       12000 :                 set32_fx( &( output_f_fx[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), 0, imult1616( hSpatParamRendCom->subframe_nbslots[subframe_idx], hSpatParamRendCom->num_freq_bands ) );
    5040             : 
    5041       12000 :                 IF( LT_16( idx_lfe, sub( hDirACRend->hOutSetup.num_lfe, 1 ) ) )
    5042             :                 {
    5043           0 :                     idx_lfe = add( idx_lfe, 1 );
    5044             :                 }
    5045             :             }
    5046             :             ELSE
    5047             :             {
    5048             :                 /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    5049             :                 /* cldfb_state_fx should be in 1 less q-factor compared to cld buffers for cldfbSynthesis_ivas_fx function */
    5050      164400 :                 Word16 q_out = sub( q_cldfb, 1 );
    5051      164400 :                 Word16 max_shift = L_norm_arr( hMasaExtRend->cldfbSynRend[idx_in]->cldfb_state_fx, hMasaExtRend->cldfbSynRend[idx_in]->cldfb_state_length );
    5052      164400 :                 IF( GT_16( max_shift, sub( q_out, hMasaExtRend->cldfbSynRend[idx_in]->Q_cldfb_state ) ) )
    5053             :                 {
    5054      164400 :                     scale_sig32( hMasaExtRend->cldfbSynRend[idx_in]->cldfb_state_fx, hMasaExtRend->cldfbSynRend[idx_in]->cldfb_state_length, sub( q_out, hMasaExtRend->cldfbSynRend[idx_in]->Q_cldfb_state ) ); // q_out
    5055      164400 :                     hMasaExtRend->cldfbSynRend[idx_in]->Q_cldfb_state = q_out;
    5056      164400 :                     move16();
    5057             :                 }
    5058             :                 ELSE
    5059             :                 {
    5060           0 :                     FOR( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    5061             :                     {
    5062           0 :                         scale_sig32( Cldfb_RealBuffer_fx[idx_in][i], hSpatParamRendCom->num_freq_bands, sub( hMasaExtRend->cldfbSynRend[idx_in]->Q_cldfb_state, q_out ) );
    5063           0 :                         scale_sig32( Cldfb_ImagBuffer_fx[idx_in][i], hSpatParamRendCom->num_freq_bands, sub( hMasaExtRend->cldfbSynRend[idx_in]->Q_cldfb_state, q_out ) );
    5064             :                     }
    5065             :                 }
    5066      822000 :                 FOR( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe_idx]; i++ )
    5067             :                 {
    5068      657600 :                     RealBuffer_fx[i] = Cldfb_RealBuffer_fx[idx_in][i]; // q_cldfb
    5069      657600 :                     ImagBuffer_fx[i] = Cldfb_ImagBuffer_fx[idx_in][i]; // q_cldfb
    5070             :                 }
    5071      164400 :                 Word16 out_size = imult1616( hSpatParamRendCom->num_freq_bands, hSpatParamRendCom->subframe_nbslots[subframe_idx] );
    5072      164400 :                 cldfbSynthesis_ivas_fx( RealBuffer_fx, ImagBuffer_fx, &( output_f_fx[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), out_size, 0, 0, hMasaExtRend->cldfbSynRend[idx_in] );
    5073      164400 :                 scale_sig32( &( output_f_fx[ch][index_slot * hSpatParamRendCom->num_freq_bands] ), out_size, sub( 11, q_out ) ); // q11
    5074      164400 :                 idx_in++;
    5075             :             }
    5076             :         }
    5077             :     }
    5078       20400 :     hSpatParamRendCom->slots_rendered = add( hSpatParamRendCom->subframe_nbslots[subframe_idx], hSpatParamRendCom->slots_rendered );
    5079       20400 :     move16();
    5080       20400 :     hSpatParamRendCom->subframes_rendered++;
    5081             : 
    5082       20400 :     pop_wmops();
    5083             : 
    5084       20400 :     return;
    5085             : }
    5086             : 
    5087             : 
    5088       12750 : void ivas_masa_ext_dirac_render_fx(
    5089             :     MASA_EXT_REND_HANDLE hMasaExtRend, /* i/o: MASA renderer structure             */
    5090             :     Word32 *output_f[],                /* i/o: input/output signals in time domain q11*/
    5091             :     const Word16 num_subframes         /* i  : number of subframes to render       */
    5092             : )
    5093             : {
    5094             :     Word16 subframe_idx;
    5095             :     Word32 *output_f_local[MAX_OUTPUT_CHANNELS];
    5096             :     Word16 n, n_samples_sf;
    5097             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    5098             : 
    5099       12750 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    5100             : 
    5101       12750 :     n_samples_sf = i_mult( JBM_CLDFB_SLOTS_IN_SUBFRAME, hSpatParamRendCom->slot_size );
    5102             : 
    5103      216750 :     FOR( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
    5104             :     {
    5105      204000 :         output_f_local[n] = output_f[n]; // q11
    5106             :     }
    5107             : 
    5108       12750 :     hSpatParamRendCom->subframes_rendered = hSpatParamRendCom->dirac_read_idx;
    5109       12750 :     move16();
    5110             : 
    5111       33150 :     FOR( subframe_idx = 0; subframe_idx < num_subframes; subframe_idx++ )
    5112             :     {
    5113       20400 :         hSpatParamRendCom->slots_rendered = 0;
    5114       20400 :         move16();
    5115       20400 :         ivas_masa_ext_dirac_render_sf_fx( hMasaExtRend, output_f_local );
    5116      346800 :         FOR( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
    5117             :         {
    5118      326400 :             output_f_local[n] += n_samples_sf;
    5119             :         }
    5120             : 
    5121       20400 :         hSpatParamRendCom->dirac_read_idx = add( hSpatParamRendCom->dirac_read_idx, 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    5122       20400 :         move16();
    5123             :     }
    5124             : 
    5125       12750 :     return;
    5126             : }

Generated by: LCOV version 1.14