LCOV - code coverage report
Current view: top level - lib_dec - ivas_sba_dirac_stereo_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 0cd7d42dabc5060fab661f1bb61691d00471dffd Lines: 338 343 98.5 %
Date: 2025-11-29 07:05:19 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include <math.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "ivas_cnst.h"
      38             : #include "prot.h"
      39             : #include "ivas_prot.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "ivas_rom_dec.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include "wmc_auto.h"
      46             : 
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * ivas_get_sba_dirac_stereo_flag()
      50             :  *
      51             :  * Set sba_dirac_stereo_flag
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54       67294 : int16_t ivas_get_sba_dirac_stereo_flag(
      55             :     Decoder_Struct *st_ivas /* i/o: IVAS decoder structure        */
      56             : )
      57             : {
      58             :     int16_t sba_dirac_stereo_flag;
      59             :     AUDIO_CONFIG output_config;
      60             : 
      61       67294 :     sba_dirac_stereo_flag = 0;
      62       67294 :     output_config = st_ivas->hDecoderConfig->output_config;
      63             : 
      64       67294 :     if ( st_ivas->ivas_format == SBA_FORMAT || ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) )
      65             :     {
      66       22874 :         if ( st_ivas->ivas_format == SBA_FORMAT )
      67             :         {
      68       17447 :             if ( output_config == IVAS_AUDIO_CONFIG_STEREO || ( output_config == IVAS_AUDIO_CONFIG_MONO && st_ivas->nchan_transport == 1 ) )
      69             :             {
      70        1516 :                 sba_dirac_stereo_flag = 1;
      71             :             }
      72             :         }
      73             :         else
      74             :         {
      75        5427 :             if ( st_ivas->nchan_transport == 1 && output_config == IVAS_AUDIO_CONFIG_STEREO )
      76             :             {
      77         171 :                 sba_dirac_stereo_flag = 1;
      78             :             }
      79             :         }
      80             :     }
      81       44420 :     else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
      82             :     {
      83       40574 :         if ( output_config == IVAS_AUDIO_CONFIG_STEREO )
      84             :         {
      85        2730 :             sba_dirac_stereo_flag = 1;
      86             :         }
      87             :     }
      88             : 
      89       67294 :     return sba_dirac_stereo_flag;
      90             : }
      91             : 
      92             : 
      93             : /*-------------------------------------------------------------------*
      94             :  * ivas_sba_dirac_stereo_config()
      95             :  *
      96             :  * DFT Stereo Configuration for SBA DirAC stereo output
      97             :  *-------------------------------------------------------------------*/
      98             : 
      99      189003 : void ivas_sba_dirac_stereo_config(
     100             :     STEREO_DFT_CONFIG_DATA_HANDLE hConfig /* o  : DFT stereo configuration                */
     101             : )
     102             : {
     103      189003 :     if ( hConfig != NULL )
     104             :     {
     105             : #ifndef DEBUG_STEREO_DFT_NOSTEREO
     106      189003 :         hConfig->dmx_active = STEREO_DFT_DMX_ACTIVE;
     107             : #else
     108             :         hConfig->dmx_active = 0;
     109             : #endif
     110      189003 :         hConfig->band_res = STEREO_DFT_BAND_RES_HIGH;
     111      189003 :         hConfig->prm_res = 2;
     112             : 
     113             : #ifdef DEBUG_MODE_DFT
     114             :         hConfig->itd_mode = 0;
     115             :         hConfig->gipd_mode = 0;
     116             : #endif
     117      189003 :         hConfig->res_pred_mode = STEREO_DFT_RESPRED_ESF;
     118             : 
     119      189003 :         hConfig->res_cod_mode = STEREO_DFT_RES_COD_OFF;
     120      189003 :         hConfig->ada_wb_res_cod_mode = 0;
     121             : 
     122      189003 :         hConfig->hybrid_itd_flag = 0;
     123             :     }
     124             : 
     125      189003 :     return;
     126             : }
     127             : 
     128             : 
     129             : /*-------------------------------------------------------------------*
     130             :  * ivas_sba_dirac_stereo_band_config()
     131             :  *
     132             :  * Band Configuration for SBA DirAC stereo output
     133             :  *-------------------------------------------------------------------*/
     134             : 
     135      185357 : static int16_t ivas_sba_dirac_stereo_band_config(
     136             :     int16_t *band_limits,    /* o  : DFT band limits                     */
     137             :     const int32_t output_Fs, /* i  : output sampling rate                */
     138             :     const int16_t NFFT,      /* i  : analysis/synthesis window length    */
     139             :     const int16_t spar_flag  /* i  : SPAR or DirAC band grouping         */
     140             : )
     141             : {
     142             :     int16_t i;
     143             :     int16_t bins_per_cldfb_band;
     144             :     int16_t nbands, num_cldfb_bands;
     145             : 
     146      185357 :     nbands = SBA_DIRAC_STEREO_NUM_BANDS;
     147             : 
     148      185357 :     if ( spar_flag )
     149             :     {
     150      176798 :         nbands = IVAS_MAX_NUM_BANDS;
     151             :     }
     152             :     else
     153             :     {
     154        8559 :         nbands = 5;
     155             :     }
     156             : 
     157      185357 :     num_cldfb_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
     158             : 
     159      185357 :     bins_per_cldfb_band = NFFT / ( 2 * num_cldfb_bands );
     160             : 
     161      185357 :     band_limits[0] = 1;
     162     1992435 :     for ( i = 1; i < nbands; i++ )
     163             :     {
     164     1927702 :         if ( spar_flag )
     165             :         {
     166     1893466 :             band_limits[i] = DirAC_band_grouping_12[i] * bins_per_cldfb_band;
     167             :         }
     168             :         else
     169             :         {
     170       34236 :             band_limits[i] = DirAC_band_grouping_5[i] * bins_per_cldfb_band;
     171             :         }
     172     1927702 :         if ( band_limits[i] >= NFFT / 2 )
     173             :         {
     174      120624 :             nbands = i;
     175      120624 :             break;
     176             :         }
     177             :     }
     178      185357 :     band_limits[nbands] = NFFT / 2;
     179             : 
     180      185357 :     return nbands;
     181             : }
     182             : 
     183             : 
     184      171180 : static float get_panning(
     185             :     const int16_t aziDeg,
     186             :     const int16_t eleDeg )
     187             : {
     188             :     float aziRad, eleRad, y, mappedX, aziRadMapped, panning;
     189             :     float A, A2, A3;
     190      171180 :     aziRad = aziDeg * PI_OVER_180;
     191      171180 :     eleRad = eleDeg * PI_OVER_180;
     192      171180 :     y = ( sinf( aziRad ) * cosf( eleRad ) );
     193      171180 :     mappedX = sqrtf( max( 0.0f, 1.0f - ( y * y ) ) );
     194      171180 :     aziRadMapped = atan2f( y, mappedX );
     195             : 
     196             :     /* Determine the real valued amplitude panning gains */
     197      171180 :     panning = 0.0f;
     198      171180 :     if ( aziRadMapped >= LS_ANGLE_RAD_30_DEG )
     199             :     { /* Left side */
     200       14173 :         panning = 1.0f;
     201             :     }
     202      157007 :     else if ( aziRadMapped <= -LS_ANGLE_RAD_30_DEG )
     203             :     { /* Right side */
     204       23747 :         panning = -1.0f;
     205             :     }
     206             :     else /* Tangent panning law */
     207             :     {
     208      133260 :         A = tanf( aziRadMapped ) * INV_TAN_LS_ANGLE_RAD_30_DEG;
     209      133260 :         A2 = ( A - 1.0f ) / max( 0.001f, A + 1.0f );
     210      133260 :         A3 = 1.0f / ( A2 * A2 + 1.0f );
     211      133260 :         panning = 2 * A3 - 1.0f;
     212             :     }
     213      171180 :     return panning;
     214             : }
     215             : 
     216             : 
     217             : /*-------------------------------------------------------------------*
     218             :  * map_params_dirac_to_stereo()
     219             :  *
     220             :  * Compute DFT Stereo parameters from DirAC parameters
     221             :  *-------------------------------------------------------------------*/
     222             : 
     223      185357 : static void map_params_dirac_to_stereo(
     224             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft,  /* i/o: decoder DFT stereo handle           */
     225             :     const IVAS_QMETADATA_HANDLE hQMetaData, /* i  : frame of MASA q_metadata            */
     226             :     float synth[],                          /* i  : decoded downmix signal              */
     227             :     float DFT[STEREO_DFT_BUF_MAX],          /* i/o: DFT buffer                          */
     228             :     const uint8_t b_wide_panning,           /* i  : flag indicating wider panning       */
     229             :     const int16_t L_frame,                  /* i  : core signal length                  */
     230             :     const int16_t mcmasa                    /* i  : McMASA flag                         */
     231             : )
     232             : {
     233             :     int16_t i, b, k;
     234             :     int16_t k_offset;
     235             :     int16_t nbands, nBlocks;
     236             :     int16_t block;
     237             :     int16_t block_len;
     238             :     int16_t azimuth[MAX_PARAM_SPATIAL_SUBFRAMES][SBA_DIRAC_STEREO_NUM_BANDS];
     239             :     int16_t elevation[MAX_PARAM_SPATIAL_SUBFRAMES][SBA_DIRAC_STEREO_NUM_BANDS];
     240             :     float diffuseness[SBA_DIRAC_STEREO_NUM_BANDS];
     241             :     float block_nrg[MAX_PARAM_SPATIAL_SUBFRAMES];
     242             :     float nrg_norm1, nrg_norm2;
     243             :     float *pSynth;
     244             :     float surrCoh[SBA_DIRAC_STEREO_NUM_BANDS];
     245             :     float *pDFT;
     246             :     float subframe_band_nrg[NB_DIV][SBA_DIRAC_STEREO_NUM_BANDS];
     247             :     float smooth_long_avg[NB_DIV][SBA_DIRAC_STEREO_NUM_BANDS];
     248             :     float smooth_short_avg[NB_DIV][SBA_DIRAC_STEREO_NUM_BANDS];
     249             :     float *side_gain, *res_pred_gain;
     250             :     IVAS_QDIRECTION *q_direction;
     251             : 
     252      185357 :     nBlocks = MAX_PARAM_SPATIAL_SUBFRAMES;
     253      185357 :     nbands = hStereoDft->nbands;
     254      185357 :     k_offset = STEREO_DFT_OFFSET;
     255      185357 :     side_gain = hStereoDft->side_gain + k_offset * STEREO_DFT_BAND_MAX;
     256      185357 :     res_pred_gain = hStereoDft->res_pred_gain + k_offset * STEREO_DFT_BAND_MAX;
     257      185357 :     q_direction = &( hQMetaData->q_direction[0] );
     258             : 
     259             :     /* gain smoothing factor */
     260      185357 :     pDFT = DFT;
     261      556071 :     for ( k = 0; k < NB_DIV; k++ )
     262             :     {
     263     4355584 :         for ( b = 0; b < hStereoDft->nbands; b++ )
     264             :         {
     265             :             /* calculate band-wise subframe energies */
     266     3984870 :             subframe_band_nrg[k][b] = 0.f;
     267   124426316 :             for ( i = hStereoDft->band_limits[b]; i < hStereoDft->band_limits[b + 1]; i++ )
     268             :             {
     269   120441446 :                 subframe_band_nrg[k][b] += pDFT[2 * i] * pDFT[2 * i] + pDFT[2 * i + 1] * pDFT[2 * i + 1];
     270             :             }
     271     3984870 :             subframe_band_nrg[k][b] = sqrtf( subframe_band_nrg[k][b] );
     272     3984870 :             hStereoDft->smooth_buf[b][NB_DIV - 1 - k] = subframe_band_nrg[k][b];
     273             : 
     274             :             /* calculate short and long energy averages */
     275     3984870 :             smooth_short_avg[k][b] = EPSILON;
     276    15939480 :             for ( i = NB_DIV - 1 - k; i < SBA_DIRAC_NRG_SMOOTH_SHORT + NB_DIV - 1 - k; i++ )
     277             :             {
     278    11954610 :                 smooth_short_avg[k][b] += hStereoDft->smooth_buf[b][i];
     279             :             }
     280             : 
     281     3984870 :             smooth_long_avg[k][b] = smooth_short_avg[k][b];
     282    31878960 :             for ( i = NB_DIV - 1 - k + SBA_DIRAC_NRG_SMOOTH_SHORT; i < SBA_DIRAC_NRG_SMOOTH_LONG + NB_DIV - 1 - k; i++ )
     283             :             {
     284    27894090 :                 smooth_long_avg[k][b] += hStereoDft->smooth_buf[b][i];
     285             :             }
     286     3984870 :             smooth_short_avg[k][b] /= SBA_DIRAC_NRG_SMOOTH_SHORT;
     287     3984870 :             smooth_long_avg[k][b] /= SBA_DIRAC_NRG_SMOOTH_LONG;
     288             : 
     289             :             /* calculate smoothing factor based on energy averages */
     290             :             /* reduce factor for higher short-term energy */
     291     3984870 :             hStereoDft->smooth_fac[k][b] = min( 1.f, smooth_long_avg[k][b] / smooth_short_avg[k][b] );
     292             :             /* map factor to range [0;1] */
     293     3984870 :             hStereoDft->smooth_fac[k][b] = max( 0.f, hStereoDft->smooth_fac[k][b] - (float) SBA_DIRAC_NRG_SMOOTH_SHORT / SBA_DIRAC_NRG_SMOOTH_LONG ) * ( (float) SBA_DIRAC_NRG_SMOOTH_LONG / ( SBA_DIRAC_NRG_SMOOTH_LONG - SBA_DIRAC_NRG_SMOOTH_SHORT ) );
     294             :             /* compress factor (higher compression in lowest bands) */
     295     3984870 :             if ( b < 2 )
     296             :             {
     297      741428 :                 hStereoDft->smooth_fac[k][b] = powf( hStereoDft->smooth_fac[k][b], 0.25f );
     298             :             }
     299             :             else
     300             :             {
     301     3243442 :                 hStereoDft->smooth_fac[k][b] = powf( hStereoDft->smooth_fac[k][b], 0.5f );
     302             :             }
     303             : 
     304             :             /* apply upper bounds depending on band */
     305     3984870 :             hStereoDft->smooth_fac[0][b] = max( hStereoDft->min_smooth_gains[b], min( hStereoDft->max_smooth_gains[b], hStereoDft->smooth_fac[0][b] ) );
     306     3984870 :             hStereoDft->smooth_fac[1][b] = max( hStereoDft->min_smooth_gains[b], min( hStereoDft->max_smooth_gains[b], hStereoDft->smooth_fac[1][b] ) );
     307             :         }
     308      370714 :         pDFT += STEREO_DFT32MS_N_MAX;
     309             :     }
     310             : 
     311      185357 :     if ( mcmasa )
     312             :     {
     313             :         /* calculate block energies for side gain weighting (combine angles of 2 DirAC blocks to side gain for 1 DFT Stereo subframe; 4 blocks and 2 subframes overall) */
     314        8559 :         pSynth = synth;
     315        8559 :         block_len = L_frame / nBlocks;
     316       42795 :         for ( block = 0; block < nBlocks; block++ )
     317             :         {
     318       34236 :             block_nrg[block] = 0.f;
     319     2609916 :             for ( i = 0; i < block_len; i++ )
     320             :             {
     321     2575680 :                 block_nrg[block] += pSynth[i] * pSynth[i];
     322             :             }
     323       34236 :             block_nrg[block] = sqrtf( block_nrg[block] );
     324       34236 :             pSynth += block_len;
     325             :         }
     326        8559 :         nrg_norm1 = 1 / ( block_nrg[0] + block_nrg[1] + EPSILON );
     327        8559 :         nrg_norm2 = 1 / ( block_nrg[2] + block_nrg[3] + EPSILON );
     328             : 
     329             :         /* extract DirAC parameters from metadata */
     330       51354 :         for ( b = 0; b < nbands; b++ )
     331             :         {
     332       42795 :             diffuseness[b] = 1.0f - q_direction->band_data[b].energy_ratio[0];
     333       42795 :             if ( hQMetaData->surcoh_band_data != NULL )
     334             :             {
     335       36495 :                 surrCoh[b] = hQMetaData->surcoh_band_data[b].surround_coherence[0] / 255.0f;
     336             :             }
     337             :             else
     338             :             {
     339        6300 :                 surrCoh[b] = 0.0f;
     340             :             }
     341             : 
     342      213975 :             for ( block = 0; block < nBlocks; block++ )
     343             :             {
     344             :                 int16_t block_metadata;
     345             : 
     346      171180 :                 if ( hQMetaData->useLowerRes )
     347             :                 {
     348           0 :                     block_metadata = 0;
     349             :                 }
     350             :                 else
     351             :                 {
     352      171180 :                     block_metadata = block;
     353             :                 }
     354      171180 :                 if ( q_direction->band_data[b].azimuth[block_metadata] < 0.f )
     355             :                 {
     356       60125 :                     q_direction->band_data[b].azimuth[block_metadata] += 360.f;
     357             :                 }
     358      171180 :                 azimuth[block][b] = (int16_t) q_direction->band_data[b].azimuth[block_metadata];
     359      171180 :                 elevation[block][b] = (int16_t) q_direction->band_data[b].elevation[block_metadata];
     360             :             }
     361             :         }
     362             : 
     363             :         /* map angles (azi, ele), surround coherence, and diffuseness to DFT Stereo side and prediction gains */
     364       51354 :         for ( b = 0; b < hStereoDft->nbands; b++ )
     365             :         {
     366             :             /* combine angles of first 2 blocks to side gain of first subframe */
     367       42795 :             side_gain[b] = 0.f;
     368      128385 :             for ( block = 0; block < nBlocks / 2; block++ )
     369             :             {
     370       85590 :                 if ( b_wide_panning == 1 )
     371             :                 {
     372             :                     /* panning between left and ride, saturate at the stereo ls positions (+/- 30deg azi) */
     373       85590 :                     side_gain[b] += nrg_norm1 * block_nrg[block] * get_panning( azimuth[block][b], elevation[block][b] );
     374             :                 }
     375             :                 else
     376             :                 {
     377           0 :                     side_gain[b] += nrg_norm1 * block_nrg[block] * sinf( azimuth[block][b] * EVS_PI / 180 ) * cosf( elevation[block][b] * EVS_PI / 180 );
     378             :                 }
     379             :             }
     380             : 
     381             :             /* combine angles of last 2 blocks to side gain of second subframe */
     382       42795 :             side_gain[b + STEREO_DFT_BAND_MAX] = 0.f;
     383      128385 :             for ( block = nBlocks / 2; block < nBlocks; block++ )
     384             :             {
     385       85590 :                 if ( b_wide_panning == 1 )
     386             :                 {
     387             :                     /* panning between left and ride, saturate at the stereo ls positions (+/- 30deg azi) */
     388       85590 :                     side_gain[b + STEREO_DFT_BAND_MAX] += nrg_norm2 * block_nrg[block] * get_panning( azimuth[block][b], elevation[block][b] );
     389             :                 }
     390             :                 else
     391             :                 {
     392           0 :                     side_gain[b + STEREO_DFT_BAND_MAX] += nrg_norm2 * block_nrg[block] * sinf( azimuth[block][b] * EVS_PI / 180 ) * cosf( elevation[block][b] * EVS_PI / 180 );
     393             :                 }
     394             :             }
     395             : 
     396             :             /* Clamp values here. [-1, 1] is the allowed range, but due to precision issues they can be slightly off which can cause problems later. */
     397       42795 :             side_gain[b] *= sqrtf( 1.f - diffuseness[b] );
     398       42795 :             side_gain[b] = max( min( side_gain[b], 1 ), -1 );
     399       42795 :             side_gain[b + STEREO_DFT_BAND_MAX] *= sqrtf( 1.f - diffuseness[b] );
     400       42795 :             side_gain[b + STEREO_DFT_BAND_MAX] = max( min( side_gain[b + STEREO_DFT_BAND_MAX], 1 ), -1 );
     401             :             /* for residual prediction gain, allowed range is [0, 1]*/
     402       42795 :             res_pred_gain[b] = diffuseness[b] * ( 1.0f - surrCoh[b] );
     403       42795 :             res_pred_gain[b] = max( min( res_pred_gain[b], 1 ), 0 );
     404       42795 :             res_pred_gain[b + STEREO_DFT_BAND_MAX] = diffuseness[b] * ( 1.0f - surrCoh[b] );
     405       42795 :             res_pred_gain[b + STEREO_DFT_BAND_MAX] = max( min( res_pred_gain[b + STEREO_DFT_BAND_MAX], 1 ), 0 );
     406             :         }
     407             :     }
     408             : 
     409      185357 :     hStereoDft->frame_nodata = 0;
     410      185357 :     hStereoDft->frame_sid_nodata = 0;
     411      185357 :     hStereoDft->frame_sid = 0;
     412             : 
     413      185357 :     return;
     414             : }
     415             : 
     416             : 
     417             : /*-------------------------------------------------------------------*
     418             :  * ivas_sba_dirac_stereo_compute_td_stefi_nrgs()
     419             :  *
     420             :  * Compute energies for TD stereo filling
     421             :  *-------------------------------------------------------------------*/
     422             : 
     423       73721 : static void ivas_sba_dirac_stereo_compute_td_stefi_nrgs(
     424             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft, /* i/o: decoder DFT stereo handle           */
     425             :     const float hb_synth[L_FRAME48k],      /* i  : HB signal                           */
     426             :     const int16_t core,                    /* i  : core decision                       */
     427             :     const int16_t output_frame,            /* i  : output frame length per channel     */
     428             :     const int16_t fd_cng_flag              /* i  : FD-CNG indicator                    */
     429             : )
     430             : {
     431             :     int16_t i;
     432       73721 :     float hb_nrg = EPSILON;
     433       73721 :     float hb_nrg2 = EPSILON;
     434             : 
     435       73721 :     if ( ( core == ACELP_CORE && !fd_cng_flag ) || hStereoDft->core_hist[1] == ACELP_CORE )
     436             :     {
     437    12774497 :         for ( i = 0; i < output_frame / 2; i++ )
     438             :         {
     439    12732640 :             hb_nrg2 += hb_synth[i] * hb_synth[i];
     440             :         }
     441             : 
     442       41857 :         hStereoDft->hb_nrg_subr[0] = hb_nrg2;
     443       41857 :         hb_nrg += hb_nrg2;
     444       41857 :         hb_nrg2 = EPSILON;
     445             : 
     446    12774497 :         for ( ; i < output_frame; i++ )
     447             :         {
     448    12732640 :             hb_nrg2 += hb_synth[i] * hb_synth[i];
     449             :         }
     450             : 
     451       41857 :         hStereoDft->hb_nrg_subr[1] = hb_nrg2;
     452       41857 :         hb_nrg += hb_nrg2;
     453             : 
     454       41857 :         mvr2r( hb_synth, hStereoDft->hb_stefi_sig + hStereoDft->hb_stefi_delay, output_frame );
     455             :     }
     456             :     else
     457             :     {
     458       31864 :         set_zero( hStereoDft->hb_stefi_sig + hStereoDft->hb_stefi_delay, output_frame );
     459             :     }
     460             : 
     461       73721 :     hStereoDft->hb_nrg_subr[0] *= hStereoDft->NFFT / 2;
     462       73721 :     hStereoDft->hb_nrg_subr[1] *= hStereoDft->NFFT / 2;
     463       73721 :     hStereoDft->hb_nrg[0] = hb_nrg;
     464       73721 :     hStereoDft->td_gain[0] = 0;
     465       73721 :     hStereoDft->core_hist[0] = ( fd_cng_flag ) ? TCX_20_CORE : core; /* full signal available for DTX with FD-CNG, thus apply stereo filling on full spectrum like in TCX */
     466             : 
     467       73721 :     return;
     468             : }
     469             : 
     470             : 
     471             : /*-------------------------------------------------------------------*
     472             :  * ivas_sba_dirac_stereo_compute_hb_gain()
     473             :  *
     474             :  * Compute HB gains
     475             :  *-------------------------------------------------------------------*/
     476             : 
     477       39622 : static void ivas_sba_dirac_stereo_compute_hb_gain(
     478             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft, /* i/o: decoder DFT stereo handle           */
     479             :     float hb_gain[NB_DIV]                  /* o  : side gains for HB signal            */
     480             : )
     481             : {
     482             :     int16_t k_offset;
     483             : 
     484       39622 :     k_offset = STEREO_DFT_OFFSET;
     485             : 
     486             :     /* last band covers whole HB range, no averaging needed */
     487       39622 :     hb_gain[0] = hStereoDft->side_gain[k_offset * STEREO_DFT_BAND_MAX + hStereoDft->nbands - 1];
     488       39622 :     hb_gain[1] = hStereoDft->side_gain[( k_offset + 1 ) * STEREO_DFT_BAND_MAX + hStereoDft->nbands - 1];
     489             : 
     490       39622 :     return;
     491             : }
     492             : 
     493             : 
     494             : /*-------------------------------------------------------------------*
     495             :  * ivas_sba_dirac_stereo_upmix_hb()
     496             :  *
     497             :  * Apply panning to HB signal
     498             :  *-------------------------------------------------------------------*/
     499             : 
     500       39622 : static void ivas_sba_dirac_stereo_upmix_hb(
     501             :     float hb_stereo_synth[CPE_CHANNELS][L_FRAME48k], /* i/o: stereo HB synthesis signal           */
     502             :     float hb_synth[L_FRAME48k],                      /* i  : HB signal                            */
     503             :     float hb_gain[NB_DIV],                           /* i  : side gains for HB signal             */
     504             :     const int16_t output_frame,                      /* i  : output frame length per channel      */
     505             :     const int16_t mcmasa,                            /* i  : McMASA flag                          */
     506             :     const int16_t sba_mono_flag,                     /* i  : flag for mono output                 */
     507             :     const int16_t bwidth,                            /* i  : bandwidth of signal                  */
     508             :     const STEREO_DFT_DEC_DATA_HANDLE hStereoDft      /* i  : Stereo DFT handle for mixing matrix  */
     509             : )
     510             : {
     511             :     int16_t i;
     512             :     float gp, gm;
     513             :     float gain_fac;
     514             : 
     515       39622 :     if ( !mcmasa )
     516             :     {
     517       37157 :         gain_fac = ( bwidth == FB ) ? 0.25f : 0.33f; /* last matrix element not used for SWB, divide by 3 instead of 4*/
     518       37157 :         if ( sba_mono_flag )
     519             :         {
     520       11364 :             gp = hStereoDft->mixer_mat_smooth[0][0][8] + hStereoDft->mixer_mat_smooth[0][0][9] +
     521       11364 :                  hStereoDft->mixer_mat_smooth[0][0][10];
     522       11364 :             if ( bwidth == FB )
     523             :             {
     524        1365 :                 gp += hStereoDft->mixer_mat_smooth[0][0][11];
     525             :             }
     526     3235044 :             for ( i = 0; i < output_frame / 2; i++ )
     527             :             {
     528     3223680 :                 hb_stereo_synth[0][i] = hb_synth[i] * gain_fac * gp;
     529             :             }
     530       11364 :             gp = hStereoDft->mixer_mat_smooth[0][0][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][0][9 + IVAS_MAX_NUM_BANDS] +
     531       11364 :                  hStereoDft->mixer_mat_smooth[0][0][10 + IVAS_MAX_NUM_BANDS];
     532       11364 :             if ( bwidth == FB )
     533             :             {
     534        1365 :                 gp += hStereoDft->mixer_mat_smooth[0][0][11 + IVAS_MAX_NUM_BANDS];
     535             :             }
     536     3235044 :             for ( i = output_frame / 2; i < output_frame; i++ )
     537             :             {
     538     3223680 :                 hb_stereo_synth[0][i] = hb_synth[i] * gain_fac * gp;
     539             :             }
     540             :         }
     541             :         else
     542             :         {
     543       25793 :             gp = hStereoDft->mixer_mat_smooth[0][0][8] + hStereoDft->mixer_mat_smooth[1][0][8] +
     544       25793 :                  hStereoDft->mixer_mat_smooth[0][0][9] + hStereoDft->mixer_mat_smooth[1][0][9] +
     545       25793 :                  hStereoDft->mixer_mat_smooth[0][0][10] + hStereoDft->mixer_mat_smooth[1][0][10];
     546       25793 :             if ( bwidth == FB )
     547             :             {
     548        1455 :                 gp += hStereoDft->mixer_mat_smooth[0][0][11] + hStereoDft->mixer_mat_smooth[1][0][11];
     549             :             }
     550             : 
     551       25793 :             gm = hStereoDft->mixer_mat_smooth[0][0][8] - hStereoDft->mixer_mat_smooth[1][0][8] +
     552       25793 :                  hStereoDft->mixer_mat_smooth[0][0][9] - hStereoDft->mixer_mat_smooth[1][0][9] +
     553       25793 :                  hStereoDft->mixer_mat_smooth[0][0][10] - hStereoDft->mixer_mat_smooth[1][0][10];
     554       25793 :             if ( bwidth == FB )
     555             :             {
     556        1455 :                 gm += hStereoDft->mixer_mat_smooth[0][0][11] - hStereoDft->mixer_mat_smooth[1][0][11];
     557             :             }
     558             : 
     559     7886753 :             for ( i = 0; i < output_frame / 2; i++ )
     560             :             {
     561     7860960 :                 hb_stereo_synth[0][i] = 0.5f * hb_synth[i] * gain_fac * gp;
     562     7860960 :                 hb_stereo_synth[1][i] = 0.5f * hb_synth[i] * gain_fac * gm;
     563             :             }
     564             : 
     565       25793 :             gp = hStereoDft->mixer_mat_smooth[0][0][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][0][8 + IVAS_MAX_NUM_BANDS] +
     566       25793 :                  hStereoDft->mixer_mat_smooth[0][0][9 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][0][9 + IVAS_MAX_NUM_BANDS] +
     567       25793 :                  hStereoDft->mixer_mat_smooth[0][0][10 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][0][10 + IVAS_MAX_NUM_BANDS];
     568       25793 :             if ( bwidth == FB )
     569             :             {
     570        1455 :                 gp += hStereoDft->mixer_mat_smooth[0][0][11 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][0][11 + IVAS_MAX_NUM_BANDS];
     571             :             }
     572             : 
     573       25793 :             gm = hStereoDft->mixer_mat_smooth[0][0][8 + IVAS_MAX_NUM_BANDS] - hStereoDft->mixer_mat_smooth[1][0][8 + IVAS_MAX_NUM_BANDS] +
     574       25793 :                  hStereoDft->mixer_mat_smooth[0][0][9 + IVAS_MAX_NUM_BANDS] - hStereoDft->mixer_mat_smooth[1][0][9 + IVAS_MAX_NUM_BANDS] +
     575       25793 :                  hStereoDft->mixer_mat_smooth[0][0][10 + IVAS_MAX_NUM_BANDS] - hStereoDft->mixer_mat_smooth[1][0][10 + IVAS_MAX_NUM_BANDS];
     576       25793 :             if ( bwidth == FB )
     577             :             {
     578        1455 :                 gm += hStereoDft->mixer_mat_smooth[0][0][11 + IVAS_MAX_NUM_BANDS] - hStereoDft->mixer_mat_smooth[1][0][11 + IVAS_MAX_NUM_BANDS];
     579             :             }
     580             : 
     581     7886753 :             for ( i = output_frame / 2; i < output_frame; i++ )
     582             :             {
     583     7860960 :                 hb_stereo_synth[0][i] = 0.5f * hb_synth[i] * gain_fac * gp;
     584     7860960 :                 hb_stereo_synth[1][i] = 0.5f * hb_synth[i] * gain_fac * gm;
     585             :             }
     586             :         }
     587             :     }
     588             :     else
     589             :     {
     590      899425 :         for ( i = 0; i < output_frame / 2; i++ )
     591             :         {
     592      896960 :             hb_stereo_synth[0][i] = 0.5f * hb_synth[i] + 0.5f * hb_gain[0] * hb_synth[i];
     593      896960 :             hb_stereo_synth[1][i] = 0.5f * hb_synth[i] - 0.5f * hb_gain[0] * hb_synth[i];
     594             :         }
     595             : 
     596      899425 :         for ( i = output_frame / 2; i < output_frame; i++ )
     597             :         {
     598      896960 :             hb_stereo_synth[0][i] = 0.5f * hb_synth[i] + 0.5f * hb_gain[1] * hb_synth[i];
     599      896960 :             hb_stereo_synth[1][i] = 0.5f * hb_synth[i] - 0.5f * hb_gain[1] * hb_synth[i];
     600             :         }
     601             :     }
     602             : 
     603       39622 :     return;
     604             : }
     605             : 
     606             : 
     607             : /*-------------------------------------------------------------------*
     608             :  * ivas_sba_dirac_stereo_apply_td_stefi()
     609             :  *
     610             :  * Apply TD stereo filling for ACELP HB
     611             :  *-------------------------------------------------------------------*/
     612             : 
     613       28258 : static void ivas_sba_dirac_stereo_apply_td_stefi(
     614             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft, /* i/o: decoder DFT stereo handle       */
     615             :     float *output[CPE_CHANNELS],           /* i/o: output synthesis signal         */
     616             :     const int16_t output_frame,            /* i  : output frame length per channel */
     617             :     const int16_t spar_flag                /* i  : SPAR flag                       */
     618             : )
     619             : {
     620             :     int16_t i;
     621             :     int16_t dftOvlLen;
     622             :     float win_in, win_out;
     623             :     float tmp;
     624             :     const float *win_dft;
     625             : 
     626       28258 :     if ( spar_flag )
     627             :     {
     628       25793 :         win_dft = hStereoDft->win32ms;
     629       25793 :         dftOvlLen = hStereoDft->dft32ms_ovl;
     630             : 
     631             :         float g_W_1, g_Y_1;
     632             :         float g_W_2, g_Y_2;
     633             :         float g_L, g_R;
     634             :         float stefi_L, stefi_R;
     635             : 
     636       25793 :         g_W_1 = ( hStereoDft->mixer_mat_smooth[0][1][8] + hStereoDft->mixer_mat_smooth[0][2][8] + hStereoDft->mixer_mat_smooth[0][3][8] ) + ( hStereoDft->mixer_mat_smooth[0][1][9] + hStereoDft->mixer_mat_smooth[0][2][9] + hStereoDft->mixer_mat_smooth[0][3][9] ) + ( hStereoDft->mixer_mat_smooth[0][1][10] + hStereoDft->mixer_mat_smooth[0][2][10] + hStereoDft->mixer_mat_smooth[0][3][10] );
     637             : 
     638       25793 :         g_Y_1 = ( hStereoDft->mixer_mat_smooth[1][1][8] + hStereoDft->mixer_mat_smooth[1][2][8] + hStereoDft->mixer_mat_smooth[1][3][8] ) + ( hStereoDft->mixer_mat_smooth[1][1][9] + hStereoDft->mixer_mat_smooth[1][2][9] + hStereoDft->mixer_mat_smooth[1][3][9] ) + ( hStereoDft->mixer_mat_smooth[1][1][10] + hStereoDft->mixer_mat_smooth[1][2][10] + hStereoDft->mixer_mat_smooth[1][3][10] );
     639             : 
     640       25793 :         g_W_2 = ( hStereoDft->mixer_mat_smooth[0][1][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][2][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][3][8 + IVAS_MAX_NUM_BANDS] ) + ( hStereoDft->mixer_mat_smooth[0][1][9 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][2][9 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][3][9 + IVAS_MAX_NUM_BANDS] ) + ( hStereoDft->mixer_mat_smooth[0][1][10 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][2][10 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[0][3][10 + IVAS_MAX_NUM_BANDS] );
     641             : 
     642       25793 :         g_Y_2 = ( hStereoDft->mixer_mat_smooth[1][1][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][2][8 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][3][8 + IVAS_MAX_NUM_BANDS] ) + ( hStereoDft->mixer_mat_smooth[1][1][9 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][2][9 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][3][9 + IVAS_MAX_NUM_BANDS] ) + ( hStereoDft->mixer_mat_smooth[1][1][10 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][2][10 + IVAS_MAX_NUM_BANDS] + hStereoDft->mixer_mat_smooth[1][3][10 + IVAS_MAX_NUM_BANDS] );
     643             : 
     644       25793 :         g_L = 0.16f * ( g_W_1 + g_W_2 - g_Y_1 - g_Y_2 );
     645       25793 :         g_R = 0.16f * ( g_W_1 + g_W_2 + g_Y_1 + g_Y_2 );
     646             : 
     647     2482343 :         for ( i = 0; i < dftOvlLen; i++ )
     648             :         {
     649     2456550 :             win_in = win_dft[STEREO_DFT32MS_STEP * i] * win_dft[STEREO_DFT32MS_STEP * i];
     650     2456550 :             win_out = 1 - win_in;
     651             : 
     652     2456550 :             stefi_L = ( win_out * hStereoDft->g_L_prev + win_in * g_L ) * 0.5f * hStereoDft->hb_stefi_sig[i];
     653     2456550 :             stefi_R = ( win_out * hStereoDft->g_R_prev + win_in * g_R ) * 0.5f * hStereoDft->hb_stefi_sig[i];
     654             : 
     655     2456550 :             output[0][i] += stefi_L;
     656     2456550 :             output[1][i] += stefi_R;
     657             :         }
     658    13291163 :         for ( i = dftOvlLen; i < output_frame; i++ )
     659             :         {
     660             : 
     661    13265370 :             stefi_L = g_L * 0.5f * hStereoDft->hb_stefi_sig[i];
     662    13265370 :             stefi_R = g_R * 0.5f * hStereoDft->hb_stefi_sig[i];
     663             : 
     664    13265370 :             output[0][i] += stefi_L;
     665    13265370 :             output[1][i] += stefi_R;
     666             :         }
     667       25793 :         hStereoDft->g_L_prev = g_L;
     668       25793 :         hStereoDft->g_R_prev = g_R;
     669             :     }
     670        2465 :     else if ( max( hStereoDft->td_gain[0], hStereoDft->td_gain[1] ) > 0 )
     671             :     {
     672        2008 :         win_dft = hStereoDft->win32ms;
     673        2008 :         dftOvlLen = hStereoDft->dft32ms_ovl;
     674             : 
     675      239058 :         for ( i = 0; i < dftOvlLen; i++ )
     676             :         {
     677      237050 :             win_in = win_dft[STEREO_DFT32MS_STEP * i] * win_dft[STEREO_DFT32MS_STEP * i];
     678      237050 :             win_out = 1 - win_in;
     679      237050 :             tmp = ( win_in * hStereoDft->td_gain[0] + win_out * hStereoDft->td_gain[1] ) * 0.5f * hStereoDft->hb_stefi_sig[i];
     680             : 
     681      237050 :             output[0][i] += tmp;
     682      237050 :             output[1][i] -= tmp;
     683             :         }
     684     1282078 :         for ( i = dftOvlLen; i < output_frame; i++ )
     685             :         {
     686     1280070 :             tmp = hStereoDft->td_gain[0] * 0.5f * hStereoDft->hb_stefi_sig[i];
     687     1280070 :             output[0][i] += tmp;
     688     1280070 :             output[1][i] -= tmp;
     689             :         }
     690             :     }
     691             : 
     692       28258 :     return;
     693             : }
     694             : 
     695             : 
     696             : /*-------------------------------------------------------------------*
     697             :  * ivas_sba_dirac_stereo_smooth_parameters()
     698             :  *
     699             :  * Smooth DFT Stereo parameters
     700             :  *-------------------------------------------------------------------*/
     701             : 
     702      168305 : void ivas_sba_dirac_stereo_smooth_parameters(
     703             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft, /* i/o: decoder DFT stereo handle            */
     704             :     ivas_spar_md_dec_state_t *hMdDec,      /* i/o: SPAR MD handle for upmixing          */
     705             :     const int16_t cross_fade_start_offset, /* i  : SPAR mixer delay compensation        */
     706             :     const int32_t output_Fs,               /* i  : Fs for delay calculation             */
     707             :     const int16_t num_md_sub_frames        /* i  : number of subframes in mixing matrix */
     708             : )
     709             : {
     710             :     int16_t i, j, k, i_sf;
     711             :     int16_t b;
     712             :     int16_t k_offset;
     713             :     float *side_gain, *prev_side_gain;
     714             :     float *res_pred_gain, *prev_res_pred_gain;
     715             : 
     716      168305 :     k_offset = STEREO_DFT_OFFSET;
     717      168305 :     prev_side_gain = hStereoDft->side_gain;
     718      168305 :     side_gain = hStereoDft->side_gain + k_offset * STEREO_DFT_BAND_MAX;
     719      168305 :     prev_res_pred_gain = hStereoDft->res_pred_gain;
     720      168305 :     res_pred_gain = hStereoDft->res_pred_gain + k_offset * STEREO_DFT_BAND_MAX;
     721             : 
     722      168305 :     if ( !hMdDec )
     723             :     {
     724             :         /* Smoothing of side and prediction gains between ftrames */
     725       48258 :         for ( b = hStereoDft->res_pred_band_min; b < hStereoDft->nbands; b++ )
     726             :         {
     727       40215 :             if ( hStereoDft->attackPresent )
     728             :             {
     729           0 :                 res_pred_gain[b] *= 0.8f;
     730           0 :                 res_pred_gain[b + STEREO_DFT_BAND_MAX] *= 0.8f;
     731             :             }
     732             :             else
     733             :             {
     734       40215 :                 side_gain[b] = hStereoDft->smooth_fac[0][b] * prev_side_gain[b] + ( 1.f - hStereoDft->smooth_fac[0][b] ) * side_gain[b];
     735       40215 :                 side_gain[b + STEREO_DFT_BAND_MAX] = hStereoDft->smooth_fac[1][b] * side_gain[b] + ( 1.f - hStereoDft->smooth_fac[1][b] ) * side_gain[b + STEREO_DFT_BAND_MAX];
     736       40215 :                 res_pred_gain[b] = hStereoDft->smooth_fac[0][b] * prev_res_pred_gain[b] + ( 1.f - hStereoDft->smooth_fac[0][b] ) * res_pred_gain[b];
     737       40215 :                 res_pred_gain[b + STEREO_DFT_BAND_MAX] = hStereoDft->smooth_fac[1][b] * res_pred_gain[b] + ( 1.f - hStereoDft->smooth_fac[1][b] ) * res_pred_gain[b + STEREO_DFT_BAND_MAX];
     738             :             }
     739             :         }
     740             :     }
     741             : 
     742      168305 :     if ( hMdDec != 0 )
     743             :     {
     744             :         float xfade_start_ns;
     745             :         int16_t xfade_delay_subframes;
     746             :         int16_t i_hist;
     747             :         int16_t md_sf;
     748             : 
     749      160262 :         xfade_start_ns = cross_fade_start_offset / (float) output_Fs * 1000000000.f - IVAS_FB_ENC_DELAY_NS;
     750      160262 :         xfade_delay_subframes = (int16_t) ( xfade_start_ns / ( FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES ) );
     751             : 
     752      160262 :         i_hist = 4 - xfade_delay_subframes;
     753             : 
     754      480786 :         for ( k = 0; k < 2; k++ )
     755             :         {
     756      961572 :             for ( i_sf = k * 2; i_sf < ( k + 1 ) * 2; i_sf++ )
     757             :             {
     758             : 
     759      641048 :                 md_sf = ( num_md_sub_frames == MAX_PARAM_SPATIAL_SUBFRAMES ) ? i_sf : 0;
     760             : 
     761      641048 :                 if ( hStereoDft->first_frame )
     762             :                 {
     763       41772 :                     for ( i = 0; i < 2; i++ )
     764             :                     {
     765      139240 :                         for ( j = 0; j < 4; j++ )
     766             :                         {
     767     1337792 :                             for ( b = 0; b < hStereoDft->nbands; b++ )
     768             :                             {
     769     1226400 :                                 hStereoDft->mixer_mat_smooth[i][j][b + k * IVAS_MAX_NUM_BANDS] = hMdDec->mixer_mat[i][j][b];
     770             :                             }
     771      221696 :                             for ( ; b < IVAS_MAX_NUM_BANDS; b++ )
     772             :                             {
     773      110304 :                                 hStereoDft->mixer_mat_smooth[i][j][b + k * IVAS_MAX_NUM_BANDS] = 0.f;
     774             :                             }
     775             :                         }
     776             :                     }
     777             :                 }
     778             :                 else
     779             :                 {
     780     1881372 :                     for ( i = 0; i < 2; i++ )
     781             :                     {
     782     6271240 :                         for ( j = 0; j < 4; j++ )
     783             :                         {
     784    60383744 :                             for ( b = 0; b < hStereoDft->nbands; b++ )
     785             :                             {
     786    55366752 :                                 float beta = hStereoDft->smooth_fac[k][b];
     787    55366752 :                                 hStereoDft->mixer_mat_smooth[i][j][b + k * IVAS_MAX_NUM_BANDS] =
     788    55366752 :                                     beta * hStereoDft->mixer_mat_smooth[i][j][b + k * IVAS_MAX_NUM_BANDS] + ( 1 - beta ) * hMdDec->mixer_mat_prev[i_hist][i][j][b];
     789             :                             }
     790             :                         }
     791             :                     }
     792             :                 }
     793             : 
     794      641048 :                 mvr2r( hMdDec->mixer_mat_prev[1][0][0], hMdDec->mixer_mat_prev[0][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
     795      641048 :                 mvr2r( hMdDec->mixer_mat_prev[2][0][0], hMdDec->mixer_mat_prev[1][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
     796      641048 :                 mvr2r( hMdDec->mixer_mat_prev[3][0][0], hMdDec->mixer_mat_prev[2][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
     797      641048 :                 mvr2r( hMdDec->mixer_mat_prev[4][0][0], hMdDec->mixer_mat_prev[3][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
     798             : 
     799     1923144 :                 for ( i = 0; i < 2; i++ )
     800             :                 {
     801     6410480 :                     for ( j = 0; j < 4; j++ )
     802             :                     {
     803    61721536 :                         for ( b = 0; b < hStereoDft->nbands; b++ )
     804             :                         {
     805    56593152 :                             hMdDec->mixer_mat_prev[4][i][j][b] = hMdDec->mixer_mat[i][j][b + md_sf * IVAS_MAX_NUM_BANDS];
     806             :                         }
     807             :                     }
     808             :                 }
     809             :             }
     810             :         }
     811             : 
     812      160262 :         hStereoDft->first_frame = 0;
     813             :     }
     814             : 
     815      168305 :     return;
     816             : }
     817             : 
     818             : 
     819             : /*-------------------------------------------------------------------*
     820             :  * ivas_sba_dirac_stereo_dec()
     821             :  *
     822             :  * Create stereo output for SBA DirAC via DFT stereo
     823             :  *-------------------------------------------------------------------*/
     824             : 
     825      185357 : void ivas_sba_dirac_stereo_dec(
     826             :     Decoder_Struct *st_ivas,     /* i/o: IVAS decoder structure              */
     827             :     float *output[CPE_CHANNELS], /* i/o: output synthesis signal             */
     828             :     const int16_t output_frame,  /* i  : output frame length per channel     */
     829             :     const int16_t mcmasa         /* i  : McMASA flag                         */
     830             : )
     831             : {
     832             :     int16_t dtx_flag, fd_cng_flag;
     833             :     int16_t sba_mono_flag;
     834             :     int16_t memOffset;
     835             :     float tmp_buf[NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS )];
     836             :     float tmp_synth[L_FRAME16k];
     837             :     float hb_gain[NB_DIV];
     838             :     float hb_synth_stereo[CPE_CHANNELS][L_FRAME48k];
     839             :     float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX];
     840             :     SCE_DEC_HANDLE hSCE;
     841             :     CPE_DEC_HANDLE hCPE;
     842             :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft;
     843             : 
     844      185357 :     hSCE = st_ivas->hSCE[0];
     845      185357 :     hCPE = st_ivas->hCPE[0];
     846      185357 :     hStereoDft = hCPE->hStereoDft;
     847             : 
     848      185357 :     if ( st_ivas->nchan_transport > 1 && !mcmasa )
     849             :     {
     850      111636 :         dtx_flag = 0;
     851      111636 :         fd_cng_flag = 0;
     852             :     }
     853             :     else
     854             :     {
     855       73721 :         dtx_flag = ( hSCE->hCoreCoder[0]->core_brate <= SID_2k40 );
     856       73721 :         fd_cng_flag = ( dtx_flag && hSCE->hCoreCoder[0]->cng_type == FD_CNG );
     857             :     }
     858      185357 :     sba_mono_flag = ( st_ivas->hDecoderConfig->nchan_out == 1 ) ? 1 : 0;
     859             : 
     860      185357 :     memOffset = NS2SA( output_frame * FRAMES_PER_SEC, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS );
     861             : 
     862      185357 :     ivas_sba_dirac_stereo_config( hStereoDft->hConfig );
     863      185357 :     hStereoDft->nbands = ivas_sba_dirac_stereo_band_config( hStereoDft->band_limits, st_ivas->hDecoderConfig->output_Fs, hStereoDft->NFFT, ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) && !mcmasa ) );
     864      185357 :     stereo_dft_dec_update( hStereoDft, output_frame, 1 /*st_ivas->sba_dirac_stereo_flag*/ );
     865      185357 :     if ( st_ivas->nchan_transport > 1 )
     866             :     {
     867      111636 :         stereo_dft_dec_analyze( hCPE, output[0], DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB, 0, 0 );
     868      111636 :         stereo_dft_dec_analyze( hCPE, output[1], DFT, 1, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB, 0, 0 );
     869      111636 :         hStereoDft->core_hist[0] = hCPE->hCoreCoder[0]->core;
     870             :     }
     871             :     else
     872             :     {
     873             :         /* nrg calculation for TD Stereo Filling, as done in ICBWE which is not used in this case */
     874       73721 :         ivas_sba_dirac_stereo_compute_td_stefi_nrgs( hStereoDft, hSCE->save_hb_synth, hSCE->hCoreCoder[0]->core, output_frame, fd_cng_flag );
     875             : 
     876             :         /* do DFT Stereo core switching (including DFT analysis) here as CPE element was not available in SCE decoder */
     877       73721 :         mvr2r( hSCE->save_synth, tmp_synth, hSCE->hCoreCoder[0]->L_frame );
     878       73721 :         stereo_dft_dec_core_switching( hCPE, output[0] /*hSCE->save_output*/, hSCE->save_synth, hSCE->save_hb_synth, DFT, output_frame, 0, dtx_flag );
     879             : 
     880             :         /* do updates here after skipping this in SCE decoder (needs to be done after core switching) */
     881       73721 :         updt_dec_common( hSCE->hCoreCoder[0], NORMAL_HQ_CORE, -1, hSCE->save_synth );
     882             :     }
     883             : 
     884             :     /* mapping of DirAC parameters (azimuth, elevation, diffuseness) to DFT Stereo parameters (side gain, prediction gain) */
     885      185357 :     map_params_dirac_to_stereo( hStereoDft, st_ivas->hQMetaData, tmp_synth, DFT[0], st_ivas->ivas_format == MC_FORMAT,
     886      185357 :                                 ( ( st_ivas->ivas_format != SBA_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT ) || mcmasa ) ? hSCE->hCoreCoder[0]->L_frame : output_frame,
     887      185357 :                                 ( ( st_ivas->ivas_format != SBA_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT ) || mcmasa ) );
     888             : 
     889      185357 :     if ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) && !mcmasa )
     890             :     {
     891      176798 :         set_f( hStereoDft->res_pred_gain, 1.f, 3 * STEREO_DFT_BAND_MAX );
     892             :     }
     893             : 
     894             :     /* DFT Stereo upmix */
     895      185357 :     stereo_dft_dec( hStereoDft, hCPE->hCoreCoder[0], DFT, NULL, NULL, 1 /*st_ivas->sba_dirac_stereo_flag*/, sba_mono_flag, ( st_ivas->hSpar != NULL && !mcmasa ) ? st_ivas->hSpar->hMdDec : NULL, ( st_ivas->hSpar != NULL && !mcmasa ) ? st_ivas->hSpar->hFbMixer->cross_fade_start_offset : 0,
     896      185357 :                     st_ivas->hDecoderConfig->output_Fs, st_ivas->nchan_transport, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
     897             : 
     898             :     /* DFT synthesis */
     899      185357 :     stereo_dft_dec_synthesize( hCPE, DFT, 0, output[0], output_frame );
     900      185357 :     if ( !sba_mono_flag )
     901             :     {
     902      172001 :         stereo_dft_dec_synthesize( hCPE, DFT, 1, output[1], output_frame );
     903             :     }
     904             : 
     905      185357 :     synchro_synthesis( st_ivas->hDecoderConfig->ivas_total_brate, hCPE, output, output_frame, 1 /*st_ivas->sba_dirac_stereo_flag*/ );
     906             : 
     907             :     /* output scaling */
     908      185357 :     if ( !sba_mono_flag )
     909             :     {
     910      172001 :         v_multc( output[0], 0.5f, output[0], output_frame );
     911      172001 :         v_multc( output[1], 0.5f, output[1], output_frame );
     912             :     }
     913             : 
     914             :     /* delay HB synth */
     915      185357 :     if ( st_ivas->nchan_transport == 1 )
     916             :     {
     917       73721 :         mvr2r( hSCE->save_hb_synth + output_frame - memOffset, tmp_buf, memOffset );
     918       73721 :         mvr2r( hSCE->save_hb_synth, hSCE->save_hb_synth + memOffset, output_frame - memOffset );
     919       73721 :         mvr2r( hSCE->prev_hb_synth, hSCE->save_hb_synth, memOffset );
     920       73721 :         mvr2r( tmp_buf, hSCE->prev_hb_synth, memOffset );
     921             :     }
     922             : 
     923      185357 :     if ( ( hCPE->hCoreCoder[0]->core == ACELP_CORE || hCPE->hCoreCoder[0]->last_core == ACELP_CORE ) && !fd_cng_flag && st_ivas->nchan_transport == 1 )
     924             :     {
     925             :         /* upmix ACELP BWE */
     926       39622 :         ivas_sba_dirac_stereo_compute_hb_gain( hStereoDft, hb_gain );
     927             : 
     928       39622 :         ivas_sba_dirac_stereo_upmix_hb( hb_synth_stereo, hSCE->save_hb_synth, hb_gain, output_frame, ( ( st_ivas->ivas_format != SBA_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT ) || mcmasa ), sba_mono_flag, hSCE->hCoreCoder[0]->bwidth, hStereoDft );
     929             : 
     930             :         /* add HB to ACELP core */
     931       39622 :         v_add( output[0], hb_synth_stereo[0], output[0], output_frame );
     932       39622 :         if ( !sba_mono_flag )
     933             :         {
     934       28258 :             v_add( output[1], hb_synth_stereo[1], output[1], output_frame );
     935             : 
     936             :             /* apply TD Stereo Filling as is done in ICBWE */
     937       28258 :             ivas_sba_dirac_stereo_apply_td_stefi( hStereoDft, output, output_frame, ( ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT ) && !mcmasa ) );
     938             :         }
     939             :     }
     940             : 
     941             :     /* reset the other channels to 0 (they are not used since here) */
     942      300386 :     for ( int16_t ch = CPE_CHANNELS; ch < st_ivas->nchan_transport; ch++ )
     943             :     {
     944      115029 :         set_zero( output[ch], output_frame );
     945             :     }
     946             : 
     947      185357 :     return;
     948             : }

Generated by: LCOV version 1.14