LCOV - code coverage report
Current view: top level - lib_enc - ivas_stereo_mdct_stereo_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 390 405 96.3 %
Date: 2025-07-08 08:36:31 Functions: 17 17 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 "ivas_cnst.h"
      38             : #include "ivas_prot.h"
      39             : #include "prot.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "ivas_rom_enc.h"
      42             : #include "wmc_auto.h"
      43             : #include "stat_enc.h"
      44             : #ifdef DEBUG_PLOT
      45             : #include "deb_out.h"
      46             : #endif
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * Local constants
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : #define OFFSET_BITS_TCX20 126
      54             : #define OFFSET_BITS_TCX10 222
      55             : 
      56             : 
      57             : /*-------------------------------------------------------------------*
      58             :  * Local function prototypes
      59             :  *-------------------------------------------------------------------*/
      60             : 
      61             : static void convertToBwMS( const int16_t startLine, const int16_t stopLine, float x0[], float x1[], const float norm_fac );
      62             : 
      63             : void convertToMS( const int16_t L_frame, float x0[], float x1[], const float norm_fac );
      64             : 
      65             : static float GetChannelEnergyRatio( Encoder_State **st, const int16_t iFirstSubframe, const int16_t iLastSubframe, const uint8_t ratioInRmsDomain );
      66             : 
      67             : static void MsStereoDecision( STEREO_MDCT_BAND_PARAMETERS *sfbParam, float *specL, float *specR, float *specM, float *specS, int16_t *mdct_stereo_mode, int16_t *msMask, const int16_t nBitsAvailable );
      68             : 
      69             : 
      70             : /*-------------------------------------------------------------------*
      71             :  * dft_ana_init()
      72             :  *
      73             :  * Initialization function for dft analysis handle within
      74             :  * MDCT-stereo
      75             :  *-------------------------------------------------------------------*/
      76             : 
      77         151 : static void dft_ana_init(
      78             :     DFT_ANA_HANDLE hDft_ana, /*i  : DFT analysis handle         */
      79             :     const int32_t input_Fs   /*i  : Input sampling frequency    */
      80             : )
      81             : {
      82         151 :     hDft_ana->N = (int16_t) ( STEREO_DFT_HOP_MAX_ENC * input_Fs / 48000 );
      83         151 :     hDft_ana->NFFT = (int16_t) ( STEREO_DFT_N_MAX_ENC * input_Fs / 48000 );
      84         151 :     hDft_ana->dft_ovl = (int16_t) ( STEREO_DFT_OVL_MAX * input_Fs / 48000 );
      85         151 :     hDft_ana->dft_zp = (int16_t) ( STEREO_DFT_ZP_MAX_ENC * input_Fs / 48000 );
      86             : 
      87         151 :     hDft_ana->dft_trigo_32k = dft_trigo_32k;
      88             : 
      89         151 :     if ( input_Fs == 16000 )
      90             :     {
      91           5 :         hDft_ana->dft_trigo = dft_trigo_32k;
      92           5 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP;
      93           5 :         hDft_ana->win_ana = win_ana_16k;
      94             :     }
      95         146 :     else if ( input_Fs == 32000 )
      96             :     {
      97          59 :         hDft_ana->dft_trigo = dft_trigo_32k;
      98          59 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_32k_STEP;
      99          59 :         hDft_ana->win_ana = win_ana_32k;
     100             :     }
     101             :     else
     102             :     {
     103          87 :         assert( input_Fs == 48000 );
     104          87 :         hDft_ana->dft_trigo = dft_trigo_48k;
     105          87 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_48k_STEP;
     106          87 :         hDft_ana->win_ana = win_ana_48k;
     107             :     }
     108             : 
     109         151 :     return;
     110             : }
     111             : 
     112             : 
     113             : /*-------------------------------------------------------------------*
     114             :  * write_itd_data()
     115             :  *
     116             :  * Bitstream writing of ITDs
     117             :  *-------------------------------------------------------------------*/
     118             : 
     119       11754 : static void write_itd_data(
     120             :     ITD_DATA_HANDLE hItd, /* i  : ITD data handle     */
     121             :     BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle    */
     122             : )
     123             : {
     124             :     int16_t k_offset;
     125             :     int16_t itd;
     126             : 
     127       11754 :     k_offset = 1;
     128             : 
     129       11754 :     push_next_indice( hBstr, ( hItd->itd[k_offset] != 0 ), STEREO_DFT_ITD_MODE_NBITS );
     130             : 
     131       11754 :     if ( hItd->itd[k_offset] )
     132             :     {
     133        3533 :         itd = hItd->itd_index[k_offset];
     134        3533 :         if ( itd > 255 )
     135             :         {
     136        2395 :             itd -= 256;
     137             : 
     138        2395 :             if ( itd < 20 )
     139             :             {
     140        1604 :                 push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
     141        1604 :                 push_next_indice( hBstr, 1, 1 ); /* negative */
     142        1604 :                 push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
     143             :             }
     144             :             else
     145             :             {
     146         791 :                 push_next_indice( hBstr, 0, 1 ); /* don't use Huffman */
     147         791 :                 push_next_indice( hBstr, 1, 1 ); /* negative */
     148         791 :                 push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS - 1 );
     149             :             }
     150             :         }
     151             :         else
     152             :         {
     153        1138 :             if ( itd < 20 )
     154             :             {
     155         866 :                 push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
     156         866 :                 push_next_indice( hBstr, 0, 1 ); /* positive */
     157         866 :                 push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
     158             :             }
     159             :             else
     160             :             {
     161             :                 /* don't use Huffman and positive*/
     162         272 :                 push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS + 1 );
     163             :             }
     164             :         }
     165             :     }
     166       11754 :     return;
     167             : }
     168             : 
     169             : 
     170             : /*-------------------------------------------------------------------*
     171             :  * stereo_coder_tcx()
     172             :  *
     173             :  *
     174             :  *-------------------------------------------------------------------*/
     175             : 
     176      139797 : void stereo_coder_tcx(
     177             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,        /* i/o: Stereo MDCT encoder structure   */
     178             :     Encoder_State **sts,                            /* i/o: encoder state structure         */
     179             :     int16_t ms_mask[NB_DIV][MAX_SFB],               /* i  : bandwise MS mask                */
     180             :     float *mdst_spectrum[CPE_CHANNELS][NB_DIV],     /* i/o: MDST spectrum                   */
     181             :     float *inv_spectrum[CPE_CHANNELS][NB_DIV],      /* i/o: inverse spectrum                */
     182             :     float *inv_mdst_spectrum[CPE_CHANNELS][NB_DIV], /* i/o: inverse MDST spectrum           */
     183             :     const int16_t mct_on                            /* i  : flag mct block (1) or stereo (0)  */
     184             : )
     185             : {
     186      139797 :     STEREO_MDCT_BAND_PARAMETERS *sfbConf = NULL;
     187             :     float nrgRatio[CPE_CHANNELS];
     188             :     float nonQNrgRatio[CPE_CHANNELS];
     189             :     int16_t k;
     190             :     int16_t nSubframes, L_frameTCX;
     191             :     int16_t nAvailBitsMS[NB_DIV];
     192      139797 :     push_wmops( "stereo_coder_tcx" );
     193             : 
     194      139797 :     set_s( nAvailBitsMS, 0, NB_DIV );
     195             : 
     196      139797 :     nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
     197      139797 :     L_frameTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
     198             : 
     199      139797 :     set_s( &ms_mask[0][0], 0, MAX_SFB );
     200      139797 :     set_s( &ms_mask[1][0], 0, MAX_SFB );
     201             : 
     202      139797 :     if ( !mct_on )
     203             :     {
     204       42789 :         if ( sts[0]->core == sts[1]->core )
     205             :         {
     206       86203 :             for ( k = 0; k < nSubframes; k++ )
     207             :             {
     208       43676 :                 nonQNrgRatio[k] = GetChannelEnergyRatio( sts, k, k, 1 );
     209             : 
     210       43676 :                 hStereoMdct->global_ild[k] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[k] + 0.5f ) ) );
     211       43676 :                 nrgRatio[k] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[k] - 1; /* nrgRatio = nrg[1]/nrg[0] */
     212             : 
     213       43676 :                 nonQNrgRatio[k] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[k] ) );
     214       43676 :                 nonQNrgRatio[k] = 1.0f / nonQNrgRatio[k] - 1.0f;
     215             :             }
     216             : #ifdef DEBUG_MODE_MDCT
     217             :             {
     218             :                 float tmp;
     219             :                 for ( k = 0; k < 2; k++ )
     220             :                 {
     221             :                     tmp = GetChannelEnergyRatio( sts, k, k, 1 );
     222             :                     if ( nSubframes == 1 && k == 1 )
     223             :                     {
     224             :                         tmp = 0;
     225             :                     }
     226             :                     dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
     227             :                 }
     228             :             }
     229             : #endif
     230             :         }
     231             :         else
     232             :         {
     233         262 :             nonQNrgRatio[0] = nonQNrgRatio[1] = GetChannelEnergyRatio( sts, 0, nSubframes - 1,
     234             :                                                                        1 );
     235         262 :             hStereoMdct->global_ild[0] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[0] + 0.5f ) ) );
     236         262 :             nrgRatio[0] = nrgRatio[1] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[0] - 1; /* nrgRatio = nrg[1]/nrg[0] */
     237         262 :             nonQNrgRatio[0] = nonQNrgRatio[1] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[0] ) );
     238         262 :             nonQNrgRatio[0] = nonQNrgRatio[1] = 1.0f / nonQNrgRatio[0] - 1.0f;
     239             : #ifdef DEBUG_MODE_MDCT
     240             :             {
     241             :                 float tmp;
     242             :                 for ( k = 0; k < 2; k++ )
     243             :                 {
     244             :                     tmp = GetChannelEnergyRatio( sts, 0, nSubframes - 1, 1 );
     245             :                     if ( nSubframes == 1 && k == 1 )
     246             :                     {
     247             :                         tmp = 0;
     248             :                     }
     249             :                     dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
     250             :                 }
     251             :             }
     252             : #endif
     253             :         }
     254             : 
     255       86989 :         for ( k = 0; k < nSubframes; k++ )
     256             :         {
     257       44200 :             if ( ( nrgRatio[k] > 1.0f ) && ( k < ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV ) ) )
     258             :             {
     259       21449 :                 L_frameTCX = sts[1]->hTcxEnc->L_frameTCX + ( ( sts[1]->last_core == 0 ) ? sts[1]->hTcxEnc->L_frameTCX / 4 : 0 );
     260       21449 :                 L_frameTCX /= ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV );
     261             : 
     262       21449 :                 v_multc( sts[1]->hTcxEnc->spectrum[k], 1.0f / nrgRatio[k], sts[1]->hTcxEnc->spectrum[k], L_frameTCX );
     263       21449 :                 v_multc( mdst_spectrum[1][k], 1.0f / nrgRatio[k], mdst_spectrum[1][k], L_frameTCX );
     264             :             }
     265       22751 :             else if ( ( nrgRatio[k] < 1.0f ) && k < ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV ) )
     266             :             {
     267        9562 :                 L_frameTCX = sts[0]->hTcxEnc->L_frameTCX + ( ( sts[0]->last_core == 0 ) ? sts[0]->hTcxEnc->L_frameTCX / 4 : 0 );
     268        9562 :                 L_frameTCX /= ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV );
     269             : 
     270        9562 :                 v_multc( sts[0]->hTcxEnc->spectrum[k], nrgRatio[k], sts[0]->hTcxEnc->spectrum[k], L_frameTCX );
     271        9562 :                 v_multc( mdst_spectrum[0][k], nrgRatio[k], mdst_spectrum[0][k], L_frameTCX );
     272             :             }
     273             :         }
     274             :     }
     275             : 
     276      139797 :     if (
     277             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
     278             :         hStereoMdct->fDualMono ||
     279             : #endif
     280      139797 :         ( sts[0]->hTcxEnc->transform_type[0] != sts[1]->hTcxEnc->transform_type[0] ) || ( sts[0]->hTcxEnc->transform_type[1] != sts[1]->hTcxEnc->transform_type[1] ) || ( sts[0]->last_core != sts[1]->last_core && ( sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE ) ) || sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE )
     281             :     {
     282         933 :         hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
     283         933 :         hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
     284             : 
     285             : #ifdef DEBUG_MODE_MDCT
     286             :         for ( k = 0; k < 2; k++ )
     287             :         {
     288             :             nAvailBitsMS[k] = -1;
     289             :             dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
     290             :         }
     291             : #endif
     292             : 
     293         933 :         if ( sts[0]->igf )
     294             :         {
     295         663 :             hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
     296         663 :             hStereoMdct->IGFStereoMode[1] = SMDCT_DUAL_MONO;
     297             :         }
     298             : #ifdef DEBUG_MODE_MDCT
     299             :         /* MDCT stereo data */
     300             :         {
     301             :             float Em[2];
     302             :             int16_t ch;
     303             :             getChannelEnergies( sts, Em, 2 );
     304             :             dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
     305             :             for ( k = 0; k < 2; k++ )
     306             :             {
     307             :                 dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
     308             :                 dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
     309             :                 dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
     310             :                 dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
     311             :             }
     312             :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     313             :             {
     314             :                 for ( k = 0; k < 2; k++ )
     315             :                 {
     316             :                     dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
     317             :                     dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
     318             :                 }
     319             :             }
     320             :         }
     321             : #endif
     322         933 :         hStereoMdct->sw_uncorr = 1;
     323             : 
     324         933 :         pop_wmops();
     325         933 :         return;
     326             :     }
     327             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
     328             :     else if ( hStereoMdct->fMSstereo )
     329             :     {
     330             :         hStereoMdct->mdct_stereo_mode[0] = SMDCT_MS_FULL;
     331             :         hStereoMdct->mdct_stereo_mode[1] = SMDCT_MS_FULL;
     332             :         if ( sts[0]->igf )
     333             :         {
     334             :             hStereoMdct->IGFStereoMode[0] = SMDCT_MS_FULL;
     335             :             hStereoMdct->IGFStereoMode[1] = SMDCT_MS_FULL;
     336             :         }
     337             :         for ( k = 0; k < nSubframes; k++ )
     338             :         {
     339             :             convertToMS( L_frameTCX, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], SQRT2_OVER_2 );
     340             : 
     341             :             /* Make sure that the MDST is processed in the correct way also */
     342             :             set_s( &ms_mask[k][0], 1, MAX_SFB );
     343             :         }
     344             : 
     345             :         pop_wmops();
     346             :         return;
     347             :     }
     348             : #endif
     349             :     else /* decide based on signal */
     350             :     {
     351      280893 :         for ( k = 0; k < nSubframes; k++ )
     352             :         {
     353      142029 :             sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     354      142029 :             if ( sts[0]->last_core == ACELP_CORE )
     355             :             {
     356           0 :                 sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     357             :             }
     358      142029 :             if ( ( sts[0]->element_brate < IVAS_80k ) && ( sts[0]->core == sts[1]->core ) && !mct_on ) /* band-wise HF ILD alignment to increase channel compaction */
     359             :             {
     360       25298 :                 int16_t sfb = 1;
     361             : 
     362      417842 :                 while ( ( sfb < sfbConf->nBandsStereoCore ) && ( sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] < 12 ) )
     363             :                 {
     364      392544 :                     sfb++; /* find start offset */
     365             :                 }
     366             : 
     367      420696 :                 for ( sfb--; sfb < sfbConf->nBandsStereoCore; sfb++ ) /* start one SFB early for the fade-in */
     368             :                 {
     369      395398 :                     const int16_t startLine = sfbConf->sfbOffset[sfb];
     370      395398 :                     const int16_t endLine = sfbConf->sfbOffset[sfb + 1];
     371      395398 :                     const int16_t sfbWidth = endLine - startLine;
     372             : 
     373      395398 :                     nrgRatio[0] = sum2_f( &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     374      395398 :                     nrgRatio[1] = sum2_f( &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     375             : 
     376      395398 :                     if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] ) /* no TNS in either ch */
     377             :                     {
     378      331658 :                         nrgRatio[0] += sum2_f( &mdst_spectrum[0][k][startLine], sfbWidth );
     379      331658 :                         nrgRatio[1] += sum2_f( &mdst_spectrum[1][k][startLine], sfbWidth );
     380             :                     }
     381      395398 :                     if ( ( nrgRatio[0] > 0.f ) && ( nrgRatio[1] > 0.f ) && ( nrgRatio[0] != nrgRatio[1] ) )
     382             :                     {
     383      395068 :                         float fTemp = 0.5f * ( nrgRatio[0] + nrgRatio[1] );
     384      395068 :                         nrgRatio[0] = sqrtf( fTemp / nrgRatio[0] );
     385      395068 :                         nrgRatio[1] = sqrtf( fTemp / nrgRatio[1] );
     386             : 
     387      395068 :                         nrgRatio[0] = max( 0.25f, min( 4.f, nrgRatio[0] ) );
     388      395068 :                         nrgRatio[1] = max( 0.25f, min( 4.f, nrgRatio[1] ) );
     389             : 
     390      395068 :                         if ( ( sfbWidth < 12 && sfb + 1 < sfbConf->nBandsStereoCore ) || ( sts[0]->element_brate > IVAS_48k ) ) /* attenuate ILD alignment in the first SFB or at 64k */
     391             :                         {
     392      208776 :                             nrgRatio[0] = powf( nrgRatio[0], 0.25f );
     393      208776 :                             nrgRatio[1] = powf( nrgRatio[1], 0.25f );
     394             :                         }
     395             :                         else
     396             :                         {
     397      186292 :                             nrgRatio[0] = sqrtf( nrgRatio[0] );
     398      186292 :                             nrgRatio[1] = sqrtf( nrgRatio[1] );
     399             :                         }
     400      395068 :                         v_multc( &sts[0]->hTcxEnc->spectrum[k][startLine], nrgRatio[0], &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     401      395068 :                         v_multc( &mdst_spectrum[0][k][startLine], nrgRatio[0], &mdst_spectrum[0][k][startLine], sfbWidth );
     402             : 
     403      395068 :                         v_multc( &sts[1]->hTcxEnc->spectrum[k][startLine], nrgRatio[1], &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     404      395068 :                         v_multc( &mdst_spectrum[1][k][startLine], nrgRatio[1], &mdst_spectrum[1][k][startLine], sfbWidth );
     405             :                     }
     406             :                 }
     407             :             }
     408             : 
     409             :             /* set mask to zero */
     410      142029 :             set_s( &ms_mask[k][0], 0, MAX_SFB );
     411             : 
     412      142029 :             nAvailBitsMS[k] = ( ( mct_on ? 2 * sts[0]->bits_frame_channel : sts[0]->bits_frame_nominal ) - sts[0]->side_bits_frame_channel - sts[1]->side_bits_frame_channel - ( nSubframes == 2 ? OFFSET_BITS_TCX10 : OFFSET_BITS_TCX20 ) ) / nSubframes;
     413             : 
     414             : #ifdef NONBE_1329_FIX_OSBA_CRASH
     415      142029 :             if ( mct_on && nAvailBitsMS[k] <= 0 ) /*Force M/S when bit-budget is low for MCT*/
     416             :             {
     417           0 :                 hStereoMdct->mdct_stereo_mode[k] = 1;
     418           0 :                 hStereoMdct->IGFStereoMode[k] = 1;
     419           0 :                 set_s( ms_mask[k], 1, sfbConf->sfbCnt );
     420             :             }
     421             :             else
     422             :             {
     423             : #endif
     424      142029 :                 MsStereoDecision( sfbConf, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], &hStereoMdct->mdct_stereo_mode[k], &ms_mask[k][0], nAvailBitsMS[k] );
     425             : 
     426      142029 :                 if ( sts[0]->igf )
     427             :                 {
     428       92706 :                     IGFEncStereoEncoder( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], &ms_mask[k][0],
     429       92706 :                                          &hStereoMdct->IGFStereoMode[k], hStereoMdct->mdct_stereo_mode[k], sts[0]->core == TCX_20_CORE, sts[0]->last_core == ACELP_CORE );
     430             :                 }
     431             :                 else
     432             :                 {
     433       49323 :                     hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
     434             :                 }
     435             : #ifdef NONBE_1329_FIX_OSBA_CRASH
     436             :             }
     437             : #endif
     438             : 
     439      142029 :             if ( hStereoMdct->mdct_stereo_mode[k] != SMDCT_DUAL_MONO || hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
     440             :             {
     441      134600 :                 ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], sfbConf->sfbCnt );
     442      134600 :                 ms_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], sfbConf->sfbCnt );
     443             : 
     444      134600 :                 if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
     445             :                 {
     446      111788 :                     sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
     447      111788 :                     sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
     448      111788 :                     ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], inv_mdst_spectrum[0][k], inv_mdst_spectrum[1][k], -1 );
     449      111788 :                     ms_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], sfbConf->sfbCnt );
     450             :                 }
     451             :             }
     452             :         } /* for k */
     453             : 
     454             : #ifdef DEBUG_MODE_MDCT
     455             :         for ( k = 0; k < 2; k++ )
     456             :         {
     457             :             dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
     458             :         }
     459             : #endif
     460             :     }
     461             :     /*  for bitrate switching determine correlation depending on m/s decision */
     462             :     {
     463             :         int16_t ms_bands[2];
     464             :         float sw_uncorr[2], sw_uncorr_mean;
     465      280893 :         for ( k = 0; k < nSubframes; k++ )
     466             :         {
     467      142029 :             sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     468      142029 :             ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore );
     469      142029 :             sw_uncorr[k] = ( 1.0f - (float) ms_bands[k] / sfbConf->nBandsStereoCore );
     470             :         }
     471      138864 :         if ( sts[0]->core == TCX_20_CORE )
     472             :         {
     473      135699 :             sw_uncorr_mean = sw_uncorr[0];
     474             :         }
     475             :         else
     476             :         {
     477        3165 :             sw_uncorr_mean = ( sw_uncorr[0] + sw_uncorr[1] ) * 0.5f;
     478             :         }
     479      138864 :         hStereoMdct->sw_uncorr = ( sw_uncorr_mean > 0.6f );
     480             :     }
     481             : 
     482      138864 :     pop_wmops();
     483             : 
     484             : #ifdef DEBUG_MODE_MDCT
     485             :     /* MDCT stereo data */
     486             :     {
     487             :         float Em[2];
     488             :         int16_t ch;
     489             :         getChannelEnergies( sts, Em, 2 );
     490             :         dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
     491             :         for ( k = 0; k < 2; k++ )
     492             :         {
     493             :             dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
     494             :             dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
     495             :             dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
     496             :             dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
     497             :         }
     498             :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     499             :         {
     500             :             for ( k = 0; k < 2; k++ )
     501             :             {
     502             :                 dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
     503             :                 dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
     504             :             }
     505             :         }
     506             :     }
     507             : #endif
     508             : 
     509      138864 :     return;
     510             : }
     511             : 
     512             : 
     513             : /*-------------------------------------------------------------------*
     514             :  * ms_processing()
     515             :  *
     516             :  *
     517             :  *-------------------------------------------------------------------*/
     518             : 
     519      246388 : void ms_processing(
     520             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     521             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     522             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     523             :     const int16_t iSubframe,                 /* i  : subframe number                 */
     524             :     float x_0[],                             /* i/o: spectrum 1                      */
     525             :     float x_1[],                             /* i/o: spectrum 1                      */
     526             :     int16_t maxSfb                           /* i  : number of stereo frequency bands*/
     527             : )
     528             : {
     529             :     int16_t sfb;
     530             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     531             : 
     532      246388 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     533             : 
     534      246388 :     if ( sts[0]->last_core == ACELP_CORE )
     535             :     {
     536           0 :         assert( sts[1]->last_core == ACELP_CORE );
     537           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     538             :     }
     539             : 
     540      246388 :     if ( maxSfb == -1 )
     541             :     {
     542           0 :         maxSfb = sfbConf->sfbCnt;
     543             :     }
     544             : 
     545    10835317 :     for ( sfb = 0; sfb < maxSfb; sfb++ )
     546             :     {
     547    10588929 :         if ( ms_mask[iSubframe][sfb] )
     548             :         {
     549     8690091 :             convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0, x_1, SQRT2_OVER_2 );
     550             :         }
     551             :     }
     552             : 
     553      246388 :     return;
     554             : }
     555             : 
     556             : 
     557             : /*-------------------------------------------------------------------*
     558             :  * ms_inv_mask_processing()
     559             :  *
     560             :  *
     561             :  *-------------------------------------------------------------------*/
     562             : 
     563      246388 : void ms_inv_mask_processing(
     564             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     565             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     566             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     567             :     const int16_t iSubframe,                 /* i  : subframe number                 */
     568             :     const float x_0[],                       /* i  : spectrum 1                      */
     569             :     const float x_1[],                       /* i  : spectrum 2                      */
     570             :     float x_inv_0[],                         /* o  : inverse spectrum 1              */
     571             :     float x_inv_1[],                         /* o  : inverse spectrum 2              */
     572             :     int16_t maxSfb                           /* i  : number of stereo frequency bands*/
     573             : )
     574             : {
     575             :     int16_t sfb;
     576             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     577             :     int16_t nSubframes, L_subframeTCX;
     578             : 
     579      246388 :     nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     580      246388 :     L_subframeTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
     581      246388 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     582             : 
     583      246388 :     if ( sts[0]->last_core == ACELP_CORE )
     584             :     {
     585           0 :         assert( sts[1]->last_core == ACELP_CORE );
     586           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     587             :     }
     588             : 
     589      246388 :     if ( maxSfb == -1 )
     590             :     {
     591      111788 :         maxSfb = sfbConf->sfbCnt;
     592             :     }
     593             : 
     594    10835317 :     for ( sfb = 0; sfb < maxSfb; sfb++ )
     595             :     {
     596    10588929 :         mvr2r( &x_0[sfbConf->sfbOffset[sfb]], &x_inv_0[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
     597    10588929 :         mvr2r( &x_1[sfbConf->sfbOffset[sfb]], &x_inv_1[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
     598             : 
     599    10588929 :         if ( ms_mask[iSubframe][sfb] == 0 )
     600             :         {
     601     1898838 :             convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_inv_0, x_inv_1, SQRT2_OVER_2 );
     602             :         }
     603             :     }
     604             : 
     605             :     /* set rest of inverse spectrum to zero */
     606      246388 :     if ( L_subframeTCX > sfbConf->sfbOffset[maxSfb] )
     607             :     {
     608      146681 :         set_zero( &x_inv_0[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
     609      146681 :         set_zero( &x_inv_1[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
     610             :     }
     611             : 
     612      246388 :     return;
     613             : }
     614             : 
     615             : 
     616             : /*-------------------------------------------------------------------*
     617             :  * write_stereo_to_bitstream()
     618             :  *
     619             :  *
     620             :  *-------------------------------------------------------------------*/
     621             : 
     622      134891 : int16_t write_stereo_to_bitstream(
     623             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     624             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     625             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     626             :     const int16_t mct_on,                    /* i  : flag mct block (1) or stereo (0)*/
     627             :     BSTR_ENC_HANDLE hBstr                    /* i/o: bitstream handle                */
     628             : )
     629             : {
     630             :     int16_t i, k, nSubframes;
     631             :     uint16_t mdct_stereo_mode, stereo_mode_bits;
     632             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     633      134891 :     int16_t start_bits = hBstr->nb_bits_tot;
     634             : 
     635      134891 :     if ( !mct_on )
     636             :     {
     637       42789 :         assert( ( ( sts[0]->hTcxEnc->transform_type[0] == sts[1]->hTcxEnc->transform_type[0] ) && ( sts[0]->hTcxEnc->transform_type[1] == sts[1]->hTcxEnc->transform_type[1] ) ) || ( ( hStereoMdct->mdct_stereo_mode[0] == SMDCT_DUAL_MONO ) && ( hStereoMdct->mdct_stereo_mode[1] == SMDCT_DUAL_MONO ) ) );
     638             :     }
     639             : 
     640      134891 :     nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
     641      134891 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     642             : 
     643      134891 :     if ( sts[0]->last_core == ACELP_CORE )
     644             :     {
     645         654 :         assert( sts[1]->ini_frame == 0 || sts[1]->last_core == ACELP_CORE );
     646         654 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     647             :     }
     648             : 
     649      134891 :     if ( hStereoMdct->hItd != NULL )
     650             :     {
     651       11754 :         write_itd_data( hStereoMdct->hItd, hBstr );
     652             :     }
     653             : 
     654      273200 :     for ( k = 0; k < nSubframes; k++ )
     655             :     {
     656      138309 :         mdct_stereo_mode = 0;
     657      138309 :         stereo_mode_bits = 1;
     658             : 
     659      138309 :         switch ( hStereoMdct->mdct_stereo_mode[k] )
     660             :         {
     661        3799 :             case SMDCT_DUAL_MONO:
     662        3799 :                 mdct_stereo_mode = 0;
     663        3799 :                 stereo_mode_bits = 1;
     664        3799 :                 break;
     665       83450 :             case SMDCT_MS_FULL:
     666       83450 :                 mdct_stereo_mode = 2;
     667       83450 :                 stereo_mode_bits = 2;
     668       83450 :                 break;
     669       51060 :             case SMDCT_BW_MS:
     670       51060 :                 mdct_stereo_mode = 3;
     671       51060 :                 stereo_mode_bits = 2;
     672       51060 :                 break;
     673           0 :             default:
     674           0 :                 assert( !"Not supported MDCT stereo mode\n" );
     675             :         }
     676             : 
     677      138309 :         push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
     678             : 
     679      138309 :         if ( !mct_on )
     680             :         {
     681       44200 :             if ( ( sts[0]->core == sts[1]->core ) || ( k == 0 ) )
     682             :             {
     683       43938 :                 push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
     684             :             }
     685             :         }
     686             : 
     687      138309 :         if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
     688             :         {
     689     2119109 :             for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
     690             :             {
     691     2068049 :                 push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
     692             :             }
     693             :         }
     694             : 
     695      138309 :         if ( sts[0]->igf )
     696             :         {
     697       92071 :             mdct_stereo_mode = 0;
     698       92071 :             stereo_mode_bits = 1;
     699       92071 :             switch ( hStereoMdct->IGFStereoMode[k] )
     700             :             {
     701       37325 :                 case SMDCT_DUAL_MONO:
     702       37325 :                     mdct_stereo_mode = 0;
     703       37325 :                     stereo_mode_bits = 1;
     704       37325 :                     break;
     705       40496 :                 case SMDCT_MS_FULL:
     706       40496 :                     mdct_stereo_mode = 2;
     707       40496 :                     stereo_mode_bits = 2;
     708       40496 :                     break;
     709       14250 :                 case SMDCT_BW_MS:
     710       14250 :                     mdct_stereo_mode = 3;
     711       14250 :                     stereo_mode_bits = 2;
     712       14250 :                     break;
     713           0 :                 default:
     714           0 :                     assert( !"Not supported MDCT stereo mode\n" );
     715             :             }
     716             : 
     717       92071 :             push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
     718             : 
     719             : 
     720       92071 :             if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
     721             :             {
     722       89998 :                 for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
     723             :                 {
     724       75748 :                     push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
     725             :                 }
     726             :             }
     727             :         }
     728             :     }
     729             : 
     730      134891 :     return ( hBstr->nb_bits_tot - start_bits );
     731             : }
     732             : 
     733             : /*-------------------------------------------------------------------*
     734             :  * Band-wise M/S stereo processing
     735             :  *
     736             :  *
     737             :  *-------------------------------------------------------------------*/
     738             : 
     739    10681123 : static void convertToBwMS(
     740             :     const int16_t startLine, /* i  : start line of sfb               */
     741             :     const int16_t stopLine,  /* i  : stop line of sfb                */
     742             :     float x0[],              /* i/o: mid/left channel coefficients   */
     743             :     float x1[],              /* i/o: side/right channel coefficients */
     744             :     const float norm_fac     /* i  : normalization factor            */
     745             : )
     746             : {
     747             :     int16_t j;
     748             :     float tmpValue;
     749             : 
     750   198123125 :     for ( j = startLine; j < stopLine; j++ )
     751             :     {
     752   187442002 :         tmpValue = x0[j];
     753   187442002 :         x0[j] = ( x0[j] + x1[j] ) * norm_fac;
     754   187442002 :         x1[j] = ( tmpValue - x1[j] ) * norm_fac;
     755             :     }
     756             : 
     757    10681123 :     return;
     758             : }
     759             : 
     760             : /*-------------------------------------------------------------------*
     761             :  * convertToMS()
     762             :  *
     763             :  *
     764             :  *-------------------------------------------------------------------*/
     765             : 
     766       92194 : void convertToMS(
     767             :     const int16_t L_frame, /* i  : frame length                    */
     768             :     float x0[],            /* i/o: mid/left channel coefficients   */
     769             :     float x1[],            /* i/o: side/right channel coefficients */
     770             :     const float norm_fac   /* i  : normalization factor            */
     771             : )
     772             : {
     773       92194 :     convertToBwMS( 0, L_frame, x0, x1, norm_fac );
     774             : 
     775       92194 :     return;
     776             : }
     777             : 
     778             : /*-------------------------------------------------------------------*
     779             :  * SQ_gain_estimate_stereo()
     780             :  *
     781             :  *
     782             :  *-------------------------------------------------------------------*/
     783             : 
     784             : /*! r: SQ gain */
     785      142029 : static float SQ_gain_estimate_stereo(
     786             :     float xL[],            /* i  : L vector to quantize        */
     787             :     float xR[],            /* i  : R vector to quantize        */
     788             :     const int16_t nbitsSQ, /* i  : number of bits targeted     */
     789             :     const int16_t lg       /* i  : vector size (2048 max)      */
     790             : )
     791             : {
     792             :     int16_t i, q, iter;
     793             :     float ener, tmp, target, fac, offset;
     794             :     float en[N_MAX / 2];
     795             :     int16_t lg2, lg_4, lg2_4;
     796             : 
     797      142029 :     lg_4 = lg >> 2;
     798      142029 :     lg2_4 = 2 * lg_4;
     799      142029 :     lg2 = lg2_4 << 2;
     800      142029 :     i = 0;
     801             : 
     802      142029 :     set_f( en, 0.01f, N_MAX / 2 );
     803             : 
     804             :     /* energy of quadruples with 9dB offset */
     805             :     /* ignore that we may take no all lines into account, max. 3 lines at the upper end of the spectrum can be missed (if lg is not a multiple of 4, happens also in SQGain()*/
     806    20307899 :     for ( q = 0; q < lg_4; q++ )
     807             :     {
     808    20165870 :         ener = 0.01f + xL[i] * xL[i] + xL[i + 1] * xL[i + 1] + xL[i + 2] * xL[i + 2] + xL[i + 3] * xL[i + 3];
     809    20165870 :         en[q] = log10f( ener ); /* saves a MAC */
     810    20165870 :         i += 4;
     811             :     }
     812      142029 :     i = 0;
     813    20307899 :     for ( ; q < lg2_4; q++ )
     814             :     {
     815    20165870 :         ener = 0.01f + xR[i] * xR[i] + xR[i + 1] * xR[i + 1] + xR[i + 2] * xR[i + 2] + xR[i + 3] * xR[i + 3];
     816    20165870 :         en[q] = log10f( ener ); /* saves a MAC */
     817    20165870 :         i += 4;
     818             :     }
     819             : 
     820             :     /* SQ scale: 4 bits / 6 dB per quadruple */
     821      142029 :     target = 0.15f * (float) ( nbitsSQ - ( lg2 >> 4 ) );
     822      142029 :     fac = 12.8f;
     823      142029 :     offset = fac;
     824             : 
     825             :     /* find offset (0 to 128 dB with step of 0.125dB) */
     826     1562319 :     for ( iter = 0; iter < 10; iter++ )
     827             :     {
     828     1420290 :         fac *= 0.5f;
     829     1420290 :         offset -= fac;
     830     1420290 :         ener = 0.0f;
     831             : 
     832   366289466 :         for ( i = 0; i < lg2_4; i++ )
     833             :         {
     834   365451654 :             tmp = en[i] - offset;
     835             : 
     836             :             /* avoid SV with 1 bin of amp < 0.5f */
     837   365451654 :             if ( tmp > 0.3f )
     838             :             {
     839   202028061 :                 ener += tmp;
     840             : 
     841             :                 /* if ener is above target -> break and increase offset */
     842   202028061 :                 if ( ener > target )
     843             :                 {
     844      582478 :                     offset += fac;
     845      582478 :                     break;
     846             :                 }
     847             :             }
     848             :         }
     849             :     }
     850             : 
     851             :     /* return gain */
     852      142029 :     return powf( 10.0f, 0.45f + 0.5f * offset );
     853             : }
     854             : 
     855             : /*-------------------------------------------------------------------*
     856             :  * QuantSpecEstimateBits()
     857             :  *
     858             :  *
     859             :  *-------------------------------------------------------------------*/
     860             : 
     861      568116 : static int16_t QuantSpecEstimateBits(
     862             :     float *spec,
     863             :     float G,
     864             :     const int16_t length,
     865             :     const int16_t nBitsAvailable,
     866             :     int16_t sqQ[] )
     867             : {
     868             :     int16_t stop, sqBits, nEncoded;
     869             :     int16_t lastnz;
     870             : 
     871      568116 :     tcx_scalar_quantization( spec, sqQ, length, G, 0.5f, NULL, 1 );
     872             : 
     873      568116 :     stop = 0;
     874             : 
     875      568116 :     sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL );
     876             : 
     877      568116 :     if ( stop != 0 )
     878             :     {
     879       79384 :         sqBits = stop;
     880             :     }
     881             : 
     882      568116 :     return sqBits;
     883             : }
     884             : 
     885             : /*-------------------------------------------------------------------*
     886             :  * context_update()
     887             :  *
     888             :  *
     889             :  *-------------------------------------------------------------------*/
     890             : 
     891    11297742 : static void context_update(
     892             :     HANDLE_RC_CONTEXT_MEM ctxSrc,
     893             :     HANDLE_RC_CONTEXT_MEM ctxTarget,
     894             :     const int16_t endLine )
     895             : {
     896             :     int16_t last_nz;
     897             : 
     898             :     /* check if last_nz of target is smaller than endLine, save and update */
     899    11297742 :     last_nz = max( ctxTarget->lastnz, endLine );
     900             : 
     901    11297742 :     mvc2c( (uint8_t *) ctxSrc, (uint8_t *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
     902    11297742 :     ctxTarget->lastnz = last_nz;
     903             : 
     904    11297742 :     return;
     905             : }
     906             : 
     907             : /*-------------------------------------------------------------------*
     908             :  * GetChannelEnergyRatio()
     909             :  *
     910             :  *
     911             :  *-------------------------------------------------------------------*/
     912             : 
     913      126579 : static float GetChannelEnergyRatio(
     914             :     Encoder_State **sts, /* i/o: Encoder state structure */
     915             :     const int16_t iFirstSubframe,
     916             :     const int16_t iLastSubframe,
     917             :     const uint8_t ratioInRmsDomain )
     918             : {
     919             :     int16_t ch, n, i;
     920             :     float nrg[2];
     921             : 
     922             :     /* Calculate energies per channel */
     923      379737 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     924             :     {
     925      253158 :         const Encoder_State *st = sts[ch];
     926      253158 :         const int16_t nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     927      253158 :         int16_t L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
     928             : 
     929      253158 :         if ( st->last_core == ACELP_CORE )
     930             :         {
     931        2668 :             L_subframeTCX += L_subframeTCX / 4;
     932             :         }
     933      253158 :         assert( iFirstSubframe >= 0 && iFirstSubframe <= iLastSubframe );
     934             : 
     935      253158 :         nrg[ch] = 0;
     936      510743 :         for ( n = iFirstSubframe; n <= min( nSubframes - 1, iLastSubframe ); n++ )
     937             :         {
     938   194717105 :             for ( i = 0; i < L_subframeTCX; i++ )
     939             :             {
     940   194459520 :                 nrg[ch] += st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i];
     941             :             }
     942             :         }
     943      253158 :         if ( ratioInRmsDomain )
     944             :         {
     945      242908 :             nrg[ch] = sqrtf( nrg[ch] );
     946             :         }
     947             :     }
     948             : 
     949      126579 :     return ( nrg[0] + nrg[1] ) > 0 ? nrg[0] / ( nrg[0] + nrg[1] ) : -1.0f;
     950             : }
     951             : 
     952             : /*-------------------------------------------------------------------*
     953             :  * FindSplitRatio()
     954             :  *
     955             :  *
     956             :  *-------------------------------------------------------------------*/
     957             : 
     958       82641 : void FindSplitRatio(
     959             :     CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure       */
     960             :     Encoder_State **sts  /* i/o: Encoder state structure     */
     961             : )
     962             : {
     963       82641 :     const uint8_t highRateMdctStereo = ( sts[0]->element_brate < IVAS_80k && sts[0]->core == sts[1]->core && sts[0]->element_mode == IVAS_CPE_MDCT && sts[0]->hTcxEnc->enc_ste_pre_corr_past ? 0 : 1 );
     964             :     float ratio;
     965             : 
     966             :     /* Calculate split ratio and quantize it */
     967       82641 :     hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
     968             : 
     969       82641 :     ratio = GetChannelEnergyRatio( sts, 0, 1, highRateMdctStereo );
     970             : 
     971       82641 :     if ( ratio >= 0 )
     972             :     {
     973       82620 :         hCPE->hStereoMdct->split_ratio = (uint16_t) ( SMDCT_BITRATE_RATIO_RANGE * ratio + 0.5f );
     974             :         /* Tuning to get closer to the optimal split ratio */
     975       82620 :         if ( ratio < 8.0f / 9.0f && hCPE->hStereoMdct->split_ratio > SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
     976             :         {
     977       62908 :             hCPE->hStereoMdct->split_ratio -= SMDCT_BITRATE_RATIO_RANGE >> 3;
     978             :         }
     979       82620 :         if ( ratio > 1.0f / 9.0f && hCPE->hStereoMdct->split_ratio < SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
     980             :         {
     981        1121 :             hCPE->hStereoMdct->split_ratio += SMDCT_BITRATE_RATIO_RANGE >> 3;
     982             :         }
     983             : 
     984       82620 :         hCPE->hStereoMdct->split_ratio = min( SMDCT_BITRATE_RATIO_RANGE - 1, max( 1, hCPE->hStereoMdct->split_ratio ) );
     985             :     }
     986             : 
     987       82641 :     return;
     988             : }
     989             : 
     990             : /*-------------------------------------------------------------------*
     991             :  * MsStereoDecision()
     992             :  *
     993             :  *
     994             :  *-------------------------------------------------------------------*/
     995             : 
     996      142029 : static void MsStereoDecision(
     997             :     STEREO_MDCT_BAND_PARAMETERS *sfbParam,
     998             :     float *specL,
     999             :     float *specR,
    1000             :     float *specM,              /* scratch buffer for M, use buffer for inverse MS mask spectrum */
    1001             :     float *specS,              /* scratch buffer for M, use buffer for inverse MS mask spectrum */
    1002             :     int16_t *mdct_stereo_mode, /* output */
    1003             :     int16_t *msMask,           /* output */
    1004             :     const int16_t nBitsAvailable )
    1005             : {
    1006      142029 :     int16_t length = sfbParam->sfbOffset[sfbParam->nBandsStereoCore];
    1007             : 
    1008             :     int16_t bitsL, bitsR, bitsM, bitsS;
    1009             :     int16_t bitsBW, bitsLR, bitsMS;
    1010             :     float G;
    1011             :     float GLR;
    1012             :     int16_t sfb, i;
    1013             :     int16_t nMSOn; /* Number of MS active bands */
    1014             :     int16_t quantSpecL[N_MAX];
    1015             :     int16_t quantSpecR[N_MAX];
    1016             :     int16_t quantSpecM[N_MAX];
    1017             :     int16_t quantSpecS[N_MAX];
    1018             :     RC_CONTEXT_MEM ctxMem[4];
    1019             :     HANDLE_RC_CONTEXT_MEM ctxL, ctxR, ctxM, ctxS;
    1020             : 
    1021      142029 :     set_s( quantSpecL, 0, N_MAX );
    1022      142029 :     set_s( quantSpecR, 0, N_MAX );
    1023      142029 :     set_s( quantSpecM, 0, N_MAX );
    1024      142029 :     set_s( quantSpecS, 0, N_MAX );
    1025             : 
    1026      142029 :     assert( nBitsAvailable > 0 );
    1027             : 
    1028      142029 :     ctxL = &ctxMem[0];
    1029      142029 :     ctxR = &ctxMem[1];
    1030      142029 :     ctxM = &ctxMem[2];
    1031      142029 :     ctxS = &ctxMem[3];
    1032             : 
    1033      142029 :     GLR = SQ_gain_estimate_stereo( specL, specR, nBitsAvailable, length );
    1034             : 
    1035    80805509 :     for ( i = 0; i < length; i++ )
    1036             :     {
    1037    80663480 :         specM[i] = ( specL[i] + specR[i] ) * SQRT2_OVER_2;
    1038    80663480 :         specS[i] = ( specL[i] - specR[i] ) * SQRT2_OVER_2;
    1039             :     }
    1040             : 
    1041      142029 :     G = 0.5f * GLR; /* seems to be favourable to underestimate a bit */
    1042             : 
    1043             :     /* do the full spectrum estimates already here, as side effect we get the quantized spectra... */
    1044      142029 :     bitsLR = QuantSpecEstimateBits( specL, G, length, nBitsAvailable, quantSpecL ) + QuantSpecEstimateBits( specR, G, length, nBitsAvailable, quantSpecR );
    1045      142029 :     bitsMS = QuantSpecEstimateBits( specM, G, length, nBitsAvailable, quantSpecM ) + QuantSpecEstimateBits( specS, G, length, nBitsAvailable, quantSpecS );
    1046             : 
    1047             :     /* clean-up MS scratch buffers */
    1048      142029 :     set_zero( specM, length );
    1049      142029 :     set_zero( specS, length );
    1050             : 
    1051      142029 :     nMSOn = 0;
    1052      142029 :     bitsBW = 0;
    1053             : 
    1054      142029 :     RCcontextMapping_encode2_estimate_bandWise_start( quantSpecL, length, nBitsAvailable, ctxL );
    1055      142029 :     RCcontextMapping_encode2_estimate_bandWise_start( quantSpecR, length, nBitsAvailable, ctxR );
    1056             : 
    1057      142029 :     bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecM, length, nBitsAvailable, ctxM );
    1058      142029 :     bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecS, length, nBitsAvailable, ctxS );
    1059             : 
    1060             :     /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
    1061             : 
    1062     5790900 :     for ( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
    1063             :     {
    1064     5648871 :         const int16_t startline = sfbParam->sfbOffset[sfb];
    1065     5648871 :         const int16_t endline = sfbParam->sfbOffset[sfb + 1];
    1066             : 
    1067     5648871 :         bitsL = RCcontextMapping_encode2_estimate_bandWise( quantSpecL, startline, endline, ctxL );
    1068     5648871 :         bitsR = RCcontextMapping_encode2_estimate_bandWise( quantSpecR, startline, endline, ctxR );
    1069     5648871 :         bitsM = RCcontextMapping_encode2_estimate_bandWise( quantSpecM, startline, endline, ctxM );
    1070     5648871 :         bitsS = RCcontextMapping_encode2_estimate_bandWise( quantSpecS, startline, endline, ctxS );
    1071             : 
    1072     5648871 :         if ( bitsM + bitsS <= bitsL + bitsR )
    1073             :         {
    1074     4241894 :             msMask[sfb] = 1;
    1075     4241894 :             ++nMSOn;
    1076     4241894 :             context_update( ctxM, ctxL, endline );
    1077     4241894 :             context_update( ctxS, ctxR, endline );
    1078     4241894 :             bitsBW += bitsM + bitsS;
    1079             :         }
    1080             :         else
    1081             :         {
    1082     1406977 :             msMask[sfb] = 0;
    1083     1406977 :             context_update( ctxL, ctxM, endline );
    1084     1406977 :             context_update( ctxR, ctxS, endline );
    1085     1406977 :             bitsBW += bitsL + bitsR;
    1086             :         }
    1087             : #ifdef DEBUG_MODE_MDCT
    1088             :         dbgwrite( &bitsL, sizeof( int16_t ), 1, 1, "./res/bitsL" );
    1089             :         dbgwrite( &bitsR, sizeof( int16_t ), 1, 1, "./res/bitsR" );
    1090             :         dbgwrite( &bitsM, sizeof( int16_t ), 1, 1, "./res/bitsM" );
    1091             :         dbgwrite( &bitsS, sizeof( int16_t ), 1, 1, "./res/bitsS" );
    1092             : #endif
    1093             :     }
    1094             : 
    1095      142029 :     bitsBW += sfbParam->nBandsStereoCore; /* Signaling bits */
    1096             : 
    1097      142029 :     if ( bitsLR < bitsBW )
    1098             :     {
    1099        8733 :         nMSOn = 0;
    1100        8733 :         set_s( msMask, 0, sfbParam->sfbCnt );
    1101        8733 :         bitsBW = bitsLR;
    1102             :     }
    1103             : 
    1104      142029 :     if ( bitsMS < bitsBW )
    1105             :     {
    1106       83450 :         nMSOn = sfbParam->nBandsStereoCore;
    1107       83450 :         set_s( msMask, 1, sfbParam->sfbCnt );
    1108             :     }
    1109             : 
    1110      142029 :     *mdct_stereo_mode = SMDCT_BW_MS;
    1111      142029 :     if ( nMSOn == sfbParam->nBandsStereoCore )
    1112             :     {
    1113       83450 :         *mdct_stereo_mode = SMDCT_MS_FULL;
    1114             :     }
    1115       58579 :     else if ( nMSOn == 0 )
    1116             :     {
    1117        7519 :         *mdct_stereo_mode = SMDCT_DUAL_MONO;
    1118             :     }
    1119             : 
    1120      142029 :     return;
    1121             : }
    1122             : 
    1123             : 
    1124             : /*-----------------------------------------------------------------------*
    1125             :  * initMdctStereoEncData()
    1126             :  *
    1127             :  * initialize encoder mdct stereo structure
    1128             :  *-----------------------------------------------------------------------*/
    1129             : 
    1130       75161 : void initMdctStereoEncData(
    1131             :     STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure   */
    1132             :     const IVAS_FORMAT ivas_format,     /* i  : IVAS format                        */
    1133             :     const int16_t element_mode,        /* i  : element mode                       */
    1134             :     const int32_t element_brate,       /* i  : element bitrate                    */
    1135             :     const int16_t bwidth,              /* i  : bandwidth                          */
    1136             :     const int16_t igf,                 /* i  : flag indicating IGF activity       */
    1137             :     const H_IGF_GRID hIgfGrid,         /* i  : IGF grid setup                     */
    1138             :     const int16_t mem_init             /* i  : initialize memory after malloc     */
    1139             : )
    1140             : {
    1141             :     int16_t tcx_coded_lines;
    1142             : 
    1143       75161 :     tcx_coded_lines = getNumTcxCodedLines( bwidth );
    1144             : 
    1145             :     /*initialize mdct stereo mode*/
    1146       75161 :     set_s( hStereoMdct->mdct_stereo_mode, -1, 2 );
    1147             : 
    1148             :     /*Initialize sfb parameteres for TCX20 */
    1149       75161 :     stereo_mdct_init_bands( tcx_coded_lines, TCX_20_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_NORM] : NULL, &hStereoMdct->stbParamsTCX20.sfbOffset[0], &hStereoMdct->stbParamsTCX20.sfbCnt );
    1150             : 
    1151             :     /*Initialize sfb parameteres for TCX10 */
    1152       75161 :     stereo_mdct_init_bands( tcx_coded_lines, TCX_10_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_SHORT] : NULL,
    1153             :                             &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
    1154             : 
    1155             :     /*Initialize sfb parameteres for transitions */
    1156       75161 :     stereo_mdct_init_bands( tcx_coded_lines, -1, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_TRAN] : NULL,
    1157             :                             &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
    1158             : 
    1159       75161 :     set_s( hStereoMdct->IGFStereoMode, -1, 2 );
    1160             : 
    1161             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
    1162             :     /*set all other members to defined states */
    1163             :     hStereoMdct->fDualMono = 0;
    1164             :     hStereoMdct->fMSstereo = 0;
    1165             : 
    1166             :     if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
    1167             :     {
    1168             :         hStereoMdct->fDualMono = 1;
    1169             :     }
    1170             :     else if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
    1171             :     {
    1172             :         hStereoMdct->fMSstereo = 1;
    1173             :     }
    1174             : #endif
    1175             : 
    1176       75161 :     hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE;
    1177       75161 :     set_s( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
    1178             : 
    1179       75161 :     if ( mem_init )
    1180             :     {
    1181        3550 :         hStereoMdct->hItd = NULL;
    1182        3550 :         hStereoMdct->hDft_ana = NULL;
    1183             :     }
    1184             : 
    1185       75161 :     if ( !( element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT ) )
    1186             :     {
    1187       25747 :         if ( hStereoMdct->hDft_ana != NULL )
    1188             :         {
    1189          25 :             free( hStereoMdct->hDft_ana );
    1190          25 :             hStereoMdct->hDft_ana = NULL;
    1191             :         }
    1192             : 
    1193       25747 :         if ( hStereoMdct->hItd != NULL )
    1194             :         {
    1195          25 :             free( hStereoMdct->hItd );
    1196          25 :             hStereoMdct->hItd = NULL;
    1197             :         }
    1198             :     }
    1199             : 
    1200       75161 :     return;
    1201             : }
    1202             : 
    1203             : /*-----------------------------------------------------------------------*
    1204             :  * initMdctItdHandling()
    1205             :  *
    1206             :  * initialize encoder mdct ITD handling structures
    1207             :  *-----------------------------------------------------------------------*/
    1208             : 
    1209         151 : ivas_error initMdctItdHandling(
    1210             :     STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
    1211             :     const int32_t input_Fs             /* i  : input sampling rate              */
    1212             : )
    1213             : {
    1214         151 :     if ( hStereoMdct->hItd == NULL )
    1215             :     {
    1216          67 :         if ( ( hStereoMdct->hItd = (ITD_DATA_HANDLE) malloc( sizeof( ITD_DATA ) ) ) == NULL )
    1217             :         {
    1218           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
    1219             :         }
    1220             :     }
    1221             : 
    1222         151 :     if ( hStereoMdct->hDft_ana == NULL )
    1223             :     {
    1224          67 :         if ( ( hStereoMdct->hDft_ana = (DFT_ANA_HANDLE) malloc( sizeof( DFT_ANA ) ) ) == NULL )
    1225             :         {
    1226           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
    1227             :         }
    1228             :     }
    1229             : 
    1230             :     /*Initialize ITD parameters*/
    1231         151 :     stereo_enc_itd_init( hStereoMdct->hItd );
    1232             : 
    1233             :     /*Initialize DFT analysis parameters*/
    1234         151 :     dft_ana_init( hStereoMdct->hDft_ana, input_Fs );
    1235             : 
    1236         151 :     return IVAS_ERR_OK;
    1237             : }
    1238             : 
    1239             : /*-------------------------------------------------------------------------
    1240             :  * stereo_mdct_enc_destroy()
    1241             :  *
    1242             :  * destroy MDCT stereo handle
    1243             :  *-------------------------------------------------------------------------*/
    1244             : 
    1245        1123 : void stereo_mdct_enc_destroy(
    1246             :     STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
    1247             : )
    1248             : {
    1249        1123 :     if ( ( *hStereoMdct )->hDft_ana != NULL )
    1250             :     {
    1251          42 :         free( ( *hStereoMdct )->hDft_ana );
    1252          42 :         ( *hStereoMdct )->hDft_ana = NULL;
    1253             :     }
    1254             : 
    1255        1123 :     if ( ( *hStereoMdct )->hItd != NULL )
    1256             :     {
    1257          42 :         free( ( *hStereoMdct )->hItd );
    1258          42 :         ( *hStereoMdct )->hItd = NULL;
    1259             :     }
    1260             : 
    1261        1123 :     free( *hStereoMdct );
    1262        1123 :     *hStereoMdct = NULL;
    1263             : 
    1264        1123 :     return;
    1265             : }

Generated by: LCOV version 1.14