LCOV - code coverage report
Current view: top level - lib_enc - ivas_mdct_core_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 531 531 100.0 %
Date: 2025-05-23 08:37:30 Functions: 10 10 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 "ivas_cnst.h"
      34             : #include <assert.h>
      35             : #include <stdint.h>
      36             : #include "options.h"
      37             : #include <math.h>
      38             : #include "cnst.h"
      39             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "rom_com.h"
      42             : #include "ivas_rom_com.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*--------------------------------------------------------------*
      49             :  * Local constants
      50             :  *---------------------------------------------------------------*/
      51             : 
      52             : #define MAX_STE_PRE_NRG      0.875f
      53             : #define MIN_STE_PRE_LEN      36
      54             : #define SILENT_CHANNEL_THRES 100
      55             : 
      56             : 
      57             : /*--------------------------------------------------------------*
      58             :  * enc_prm_pre_mdct()
      59             :  *
      60             :  * encode all side parameters up to MDCT whitening
      61             :  *---------------------------------------------------------------*/
      62             : 
      63      638504 : static void enc_prm_pre_mdct(
      64             :     Encoder_State *st,           /* i/o: encoder state structure                            */
      65             :     int16_t param[],             /* i  : parameters                                         */
      66             :     const int16_t *no_param_tns, /* i  : number of TNS parameters per subframe              */
      67             :     int16_t p_param[2],          /* o  : pointer to parameters for next round of bs writing */
      68             :     const int16_t MCT_flag,      /* i  : hMCT handle allocated (1) or not (0)               */
      69             :     BSTR_ENC_HANDLE hBstr        /* i/o: encoder bitstream handle                           */
      70             : )
      71             : {
      72             :     int16_t nbits_start;
      73             : #ifdef DEBUG_PLOT_BITS
      74             :     int16_t tmp;
      75             : #endif
      76             : 
      77      638504 :     nbits_start = hBstr->nb_bits_tot;
      78             : 
      79             :     /*--------------------------------------------------------------------------------*
      80             :      * Header
      81             :      *--------------------------------------------------------------------------------*/
      82             : 
      83      638504 :     writeTCXMode( st, hBstr, MCT_flag, &nbits_start );
      84             : 
      85             :     /* write last_core for core switching and error concealment */
      86      638504 :     push_next_indice( hBstr, st->last_core != ACELP_CORE, 1 );
      87             : 
      88      638504 :     writeTCXWindowing( hBstr, st->hTcxCfg->tcx_curr_overlap_mode );
      89      638504 :     if ( st->core == TCX_10_CORE )
      90             :     {
      91       14811 :         writeTCXWindowing( hBstr, st->hTcxCfg->tcx_last_overlap_mode );
      92             :     }
      93      638504 :     push_next_indice( hBstr, st->hTcxEnc->kernel_type[0], st->last_core != ACELP_CORE ? 2 : 1 );
      94      638504 :     if ( st->core == TCX_10_CORE )
      95             :     {
      96       14811 :         assert( ( st->hTcxEnc->kernel_type[0] & 1 ) == ( st->hTcxEnc->kernel_type[1] >> 1 ) );
      97       14811 :         push_next_indice( hBstr, st->hTcxEnc->kernel_type[1] & 1, 1 );
      98             :     }
      99             : 
     100      638504 :     st->glr_reset = 0;
     101             : #ifdef DEBUG_PLOT_BITS
     102             :     tmp = hBstr->nb_bits_tot - nbits_start;
     103             :     dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_TCX" );
     104             :     tmp = hBstr->nb_bits_tot;
     105             : #endif
     106             : 
     107             :     /*--------------------------------------------------------------------------------*
     108             :      * TCX20/TCX10 parameters
     109             :      *--------------------------------------------------------------------------------*/
     110             : 
     111      638504 :     writeTCXparam( st, hBstr, NULL, param, 0, 0, 0, no_param_tns, p_param, NULL, 0 );
     112             : 
     113      638504 :     st->side_bits_frame_channel = hBstr->nb_bits_tot - nbits_start;
     114             : 
     115             : #ifdef DEBUG_PLOT_BITS
     116             :     dbgwrite( &st->side_bits_frame_channel, sizeof( int16_t ), 1, 1, "./res/side_bits_pre" );
     117             : #endif
     118             : 
     119      638504 :     return;
     120             : }
     121             : 
     122             : /*--------------------------------------------------------------*
     123             :  * kernel_switch_detect()
     124             :  *
     125             :  * complex-valued detection method for transform kernel switching
     126             :  *---------------------------------------------------------------*/
     127             : 
     128       25640 : static int16_t kernel_switch_detect(
     129             :     float *sigR0,               /* i/o: MDCT samples of the 1st (left) channel  */
     130             :     float *sigR1,               /* i/o: MDCT samples of the 2nd (right) channel */
     131             :     float *sigI0,               /* i/o: MDST samples of the 1st (left) channel  */
     132             :     float *sigI1,               /* i/o: MDST samples of the 2nd (right) channel */
     133             :     const int16_t nSamplesCore, /* i  : number of core-coded samples below IGF  */
     134             :     const int16_t nSamplesMax,  /* i  : transform length (number of MCLT lines) */
     135             :     const int16_t tcxTransType, /* i  : TCX transform type (2*5, 1*10, or 1*20) */
     136             :     float *switchCovPrev,       /* i/o: previous transform's correlation value  */
     137             :     const uint32_t bitRateMode  /* i  : stereo bitrate mode (bps * L_frame)     */
     138             : )
     139             : {
     140             :     int16_t maxLength, anaLength, s;
     141             :     float cov00, cov90, sumR0, sumR1, sumI0, sumI1;
     142             : 
     143       25640 :     maxLength = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     144       25640 :     anaLength = min( maxLength, (int16_t) ( bitRateMode >> 17 ) & 0xFFFE );
     145       25640 :     s = ( nSamplesMax < 512 ? 2 : 4 ); /* exclude DC offset */
     146             : 
     147       25640 :     cov00 = 0.f;
     148       25640 :     cov90 = 0.f;
     149       25640 :     sumR0 = 0.f;
     150       25640 :     sumR1 = 0.f;
     151       25640 :     sumI0 = 0.f;
     152       25640 :     sumI1 = 0.f;
     153             : 
     154       25640 :     if ( tcxTransType == TCX_5 )
     155             :     {
     156         816 :         assert( nSamplesMax < 512 );
     157         816 :         anaLength >>= 1;
     158       42380 :         for ( s = 1; s < anaLength; s++ )
     159             :         {
     160       41564 :             cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     161       41564 :             cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     162       41564 :             sumR0 += sigR0[s] * sigR0[s];
     163       41564 :             sumR1 += sigR1[s] * sigR1[s];
     164       41564 :             sumI0 += sigI0[s] * sigI0[s];
     165       41564 :             sumI1 += sigI1[s] * sigI1[s];
     166             :         }
     167         816 :         anaLength += ( nSamplesMax >> 1 );
     168         816 :         s = 1 + ( nSamplesMax >> 1 ); /* 2nd TCX-5 spectrum is stacked onto 1st */
     169             :     }
     170     5253628 :     for ( ; s < anaLength; s++ )
     171             :     {
     172     5227988 :         cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     173     5227988 :         cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     174     5227988 :         sumR0 += sigR0[s] * sigR0[s];
     175     5227988 :         sumR1 += sigR1[s] * sigR1[s];
     176     5227988 :         sumI0 += sigI0[s] * sigI0[s];
     177     5227988 :         sumI1 += sigI1[s] * sigI1[s];
     178             :     }
     179             : 
     180       25640 :     cov00 /= ( sqrtf( sumR0 * sumR1 ) + sqrtf( sumI0 * sumI1 ) + 1.f );
     181       25640 :     cov90 /= ( sqrtf( sumR0 * sumI1 ) + sqrtf( sumI0 * sumR1 ) + 1.f );
     182       25640 :     sumI0 = max( 0.f, fabsf( cov90 ) - fabsf( cov00 ) );
     183       25640 :     sumI1 = ( switchCovPrev != NULL ? fabsf( *switchCovPrev ) : 0.f );
     184       25640 :     s = ( cov90 < 0.f ? -1 : 1 );
     185             : 
     186       25640 :     if ( switchCovPrev != NULL ) /* update the decision smoothing history */
     187             :     {
     188       25640 :         *switchCovPrev = ( 0.875f * sumI1 + 0.125f * sumI0 ) * s;
     189             :     }
     190             : 
     191       25640 :     return ( ( sumI0 > 0.5f - 0.25f * sumI1 || fabsf( cov90 ) > 0.75f - 0.5f * sumI1 ) && ( sumI1 > 0.0625f ) ? s : 0 ); /* final decision */
     192             : }
     193             : 
     194             : 
     195          31 : static void kernel_switch_trafo(
     196             :     const float *x,
     197             :     float *y,
     198             :     const int16_t l,
     199             :     const int16_t m,
     200             :     const int16_t r,
     201             :     const uint16_t kernelType )
     202             : {
     203          31 :     const float signLeft = ( kernelType >= MDCT_II ? -1.f : 1.f );
     204          31 :     const float signRight = ( kernelType & 1 ? 1.f : -1.f );
     205             :     int16_t i;
     206             :     float inputBuffer[N_MAX];
     207             : 
     208             :     /* Init */
     209        1611 :     for ( i = 0; i < m / 2; i++ )
     210             :     {
     211        1580 :         inputBuffer[m / 2 + r / 2 + i] = -1.0f * x[l + m / 2 - 1 - i];
     212             :     }
     213             : 
     214         891 :     for ( i = 0; i < l / 2; i++ )
     215             :     {
     216         860 :         inputBuffer[m / 2 + r / 2 + m / 2 + i] = signLeft * x[i] - x[l - 1 - i];
     217             :     }
     218             : 
     219        1611 :     for ( i = 0; i < m / 2; i++ )
     220             :     {
     221        1580 :         inputBuffer[m / 2 + r / 2 - 1 - i] = -1.0f * x[l + m / 2 + i];
     222             :     }
     223             : 
     224        1131 :     for ( i = 0; i < r / 2; i++ )
     225             :     {
     226        1100 :         inputBuffer[m / 2 + r / 2 - 1 - m / 2 - i] = -1.0f * x[l + m + i] + signRight * x[l + m + r - 1 - i];
     227             :     }
     228             : 
     229          31 :     edxt( inputBuffer, y, l / 2 + m + r / 2, kernelType, FALSE );
     230             : 
     231          31 :     v_multc( y, sqrtf( (float) NORM_MDCT_FACTOR / ( l / 2 + m + r / 2 ) ), y, l / 2 + m + r / 2 );
     232             : 
     233          31 :     return;
     234             : }
     235             : 
     236             : 
     237      661880 : static void kernel_switch_update_transforms(
     238             :     float *sigR,                /* i/o: MDCT samples of the given channel */
     239             :     float *sigI,                /* i/o: MDST samples of the given channel */
     240             :     const int16_t tcxTransType, /* i  : TCX transform type, cf also above */
     241             :     TCX_CONFIG_HANDLE hTcxCfg,  /* i  : TCX configuration handle, pointer */
     242             :     const int16_t bwidthSwCnt,  /* i  : bandwidth switching counter in st */
     243             :     const uint16_t kernelType,  /* i  : TCX transform kernel type (0 - 3) */
     244             :     const float *tcxTimeSignal, /* i  : hTcxEnc->new_speech_TCX buf in st */
     245             :     const float *speech_TCX,    /* i  : hTcxEnc->speech_TCX buffer in st  */
     246             :     float *windowedTimeSignal,  /* i/o: windowed input and scratch buffer */
     247             :     const int16_t L_subframe    /* i  : transform length (number of bins) */
     248             : )
     249             : {
     250             :     int16_t s, nSubframes;
     251             : 
     252      661880 :     s = L_subframe;
     253      661880 :     nSubframes = ( tcxTransType == TCX_20 ? 1 : NB_DIV );
     254             : 
     255      661880 :     if ( kernelType == MDCT_IV ) /* no updates required here! */
     256             :     {
     257      660963 :         return;
     258             :     }
     259             : 
     260         917 :     if ( kernelType == MDST_IV ) /* swap real and imag. parts */
     261             :     {
     262      227271 :         for ( s = L_subframe - 1; s >= 0; s-- )
     263             :         {
     264      226880 :             const float sigTemp = -sigR[s];
     265             : 
     266      226880 :             sigR[s] = sigI[s];
     267      226880 :             sigI[s] = sigTemp;
     268             :         }
     269             : 
     270         391 :         return;
     271             :     }
     272             : 
     273             :     /* MDCT/MDST-II as real part, keep imag. part for speedup */
     274         526 :     if ( tcxTransType == TCX_20 && hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     275             :     {
     276         495 :         wtda_ext( tcxTimeSignal, windowedTimeSignal, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], s, kernelType );
     277             : 
     278         495 :         edxt( windowedTimeSignal, sigR, s, kernelType, FALSE );
     279             : 
     280         495 :         v_multc( sigR, sqrtf( (float) NORM_MDCT_FACTOR / s ), sigR, s );
     281             :     }
     282             :     else /* 2 TCX5 subframes or 1 TCX10 or 1 transitory TCX20 */
     283             :     {
     284          31 :         const float sign = ( kernelType >= MDCT_II ? 1.f : -1.f );
     285          31 :         const int16_t minWindowLen = hTcxCfg->tcx_mdct_window_min_lengthFB - 1;
     286          31 :         int16_t i, leftOverlap = 0, rightOverlap = 0;
     287             :         const float *left_win, *right_win;
     288             : 
     289          31 :         tcx_get_windows( hTcxCfg, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], &leftOverlap, &left_win, &rightOverlap, &right_win, 1 );
     290          31 :         if ( speech_TCX != NULL && tcxTransType != TCX_20 && (int16_t) windowedTimeSignal[0] == FULL_OVERLAP && s - leftOverlap > minWindowLen )
     291             :         {
     292         249 :             for ( i = minWindowLen; i >= 0; i-- ) /* outer left folding of shortened long ALDO slope */
     293             :             {
     294         240 :                 windowedTimeSignal[2 + leftOverlap + i] += sign * speech_TCX[-1 - i] * hTcxCfg->tcx_aldo_window_1_FB[leftOverlap / 2 + minWindowLen - i] * hTcxCfg->tcx_mdct_window_minimumFB[minWindowLen - i];
     295             :             }
     296             :         }
     297             : 
     298          31 :         if ( tcxTransType == TCX_5 )
     299             :         {
     300             :             float tcx5Win[N_TCX10_MAX / 2 + L_MDCT_OVLP_MAX]; /* temporary buffer for TCX5 windowing */
     301             : 
     302          19 :             assert( L_subframe == nSubframes * hTcxCfg->tcx5SizeFB );
     303             : 
     304             :             /* Outer left folding */
     305         739 :             for ( i = 0; i < leftOverlap / 2; i++ )
     306             :             {
     307         720 :                 windowedTimeSignal[2 + leftOverlap / 2 + i] += sign * windowedTimeSignal[2 + leftOverlap / 2 - 1 - i];
     308             :             }
     309             : 
     310          19 :             s = hTcxCfg->tcx5SizeFB; /* obtain 1st TCX5 again */
     311          19 :             nSubframes *= 2;
     312          19 :             WindowSignal( hTcxCfg, leftOverlap / 2, RECTANGULAR_OVERLAP, MIN_OVERLAP, &leftOverlap, &rightOverlap, windowedTimeSignal + 2, &s, tcx5Win, 0, 1 );
     313          19 :             kernel_switch_trafo( tcx5Win, sigR, leftOverlap, s /* L_subfr. */ - ( leftOverlap + rightOverlap ) / 2, rightOverlap, kernelType );
     314             : 
     315          19 :             if ( kernelType & 1 ) /* 2nd TCX5 is kernelType 3 */
     316             :             {
     317         162 :                 for ( i = L_subframe - 1; i >= s; i-- )
     318             :                 {
     319         160 :                     const float sigTemp = -sigR[i];
     320             : 
     321         160 :                     sigR[i] = sigI[i];
     322         160 :                     sigI[i] = sigTemp;
     323             :                 }
     324             :             }
     325             :         }
     326             :         else /* tcxTransType != TCX_5 */
     327             :         {
     328          12 :             kernel_switch_trafo( windowedTimeSignal + 2, sigR, leftOverlap, s - ( leftOverlap + rightOverlap ) / 2, rightOverlap, kernelType );
     329             :         }
     330             :     }
     331             : 
     332             :     /* high-band gain control, in case of bandwidth switching */
     333         526 :     if ( bwidthSwCnt > 0 )
     334             :     {
     335           9 :         v_multc( sigR + L_FRAME16k / nSubframes, (float) bwidthSwCnt / (float) BWS_TRAN_PERIOD, sigR + L_FRAME16k / nSubframes, s - L_FRAME16k / nSubframes );
     336             :     }
     337             : 
     338         526 :     return;
     339             : }
     340             : 
     341             : 
     342     1572941 : static void applyStereoPreProcessingCplx(
     343             :     float *mdctSample1,
     344             :     float *mdctSample2,
     345             :     float *mdstSample1,
     346             :     float *mdstSample2,
     347             :     const float factIn,
     348             :     const float factDe,
     349             :     const float sign )
     350             : {
     351     1572941 :     const float valI1 = *mdstSample1;
     352     1572941 :     const float valI2 = *mdstSample2;
     353     1572941 :     const float valR1 = *mdctSample1;
     354     1572941 :     const float valR2 = *mdctSample2;
     355             :     float absR1, absR2;
     356             :     float dmxI1, dmxR1, dmxI2, dmxR2;
     357             :     float n, d;
     358             : 
     359     1572941 :     absR1 = fabsf( valR1 );
     360     1572941 :     absR2 = fabsf( valR2 );
     361     1572941 :     dmxR1 = valR1 * factDe + sign * valR2 * factIn; /* cross */
     362     1572941 :     dmxR2 = valR2 * factDe + sign * valR1 * factIn; /* -talk */
     363             : 
     364     1572941 :     if ( fabsf( dmxR1 ) < absR1 + absR2 ) /* avoid destructive summations */
     365             :     {
     366        4116 :         if ( absR1 * factDe < absR2 * factIn )
     367             :         {
     368        2065 :             dmxR1 = valR2 * factIn - sign * valR1 * factDe;
     369        2065 :             dmxI1 = valI2 * factIn - sign * valI1 * factDe;
     370             :         }
     371             :         else
     372             :         {
     373        2051 :             dmxR1 = valR1 * factDe - sign * valR2 * factIn;
     374        2051 :             dmxI1 = valI1 * factDe - sign * valI2 * factIn;
     375             :         }
     376             :     }
     377             :     else
     378             :     {
     379     1568825 :         dmxI1 = valI1 * factDe + sign * valI2 * factIn;
     380             :     }
     381             : 
     382     1572941 :     if ( fabsf( dmxR2 ) < absR1 + absR2 ) /* avoid destructive summations */
     383             :     {
     384        3464 :         if ( absR1 * factIn < absR2 * factDe )
     385             :         {
     386        1744 :             dmxR2 = valR2 * factDe - sign * valR1 * factIn;
     387        1744 :             dmxI2 = valI2 * factDe - sign * valI1 * factIn;
     388             :         }
     389             :         else
     390             :         {
     391        1720 :             dmxR2 = valR1 * factIn - sign * valR2 * factDe;
     392        1720 :             dmxI2 = valI1 * factIn - sign * valI2 * factDe;
     393             :         }
     394             :     }
     395             :     else
     396             :     {
     397     1569477 :         dmxI2 = valI2 * factDe + sign * valI1 * factIn;
     398             :     }
     399             : 
     400     1572941 :     n = valR1 * valR1 + valI1 * valI1; /* maintain spectral magnitude (1) */
     401     1572941 :     d = dmxR1 * dmxR1 + dmxI1 * dmxI1;
     402     1572941 :     d = sqrtf( n / max( 1.f, d ) );
     403     1572941 :     *mdctSample1 = dmxR1 * d;
     404     1572941 :     *mdstSample1 = dmxI1 * d;
     405             : 
     406     1572941 :     n = valR2 * valR2 + valI2 * valI2; /* maintain spectral magnitude (2) */
     407     1572941 :     d = dmxR2 * dmxR2 + dmxI2 * dmxI2;
     408     1572941 :     d = sqrtf( n / max( 1.f, d ) );
     409     1572941 :     *mdctSample2 = dmxR2 * d;
     410     1572941 :     *mdstSample2 = dmxI2 * d;
     411             : 
     412     1572941 :     return;
     413             : }
     414             : 
     415             : 
     416             : /*--------------------------------------------------------------*
     417             :  * enc_ste_pre_mdct()
     418             :  *
     419             :  * encoder-side complex-valued stereo pre-processing (crosstalk)
     420             :  *---------------------------------------------------------------*/
     421             : 
     422       26123 : static uint16_t enc_ste_pre_mdct(
     423             :     float *sigR0,               /* i/o: MDCT samples of the 1st (left) channel  */
     424             :     float *sigR1,               /* i/o: MDCT samples of the 2nd (right) channel */
     425             :     float *sigI0,               /* i/o: MDST samples of the 1st (left) channel  */
     426             :     float *sigI1,               /* i/o: MDST samples of the 2nd (right) channel */
     427             :     const int16_t nSamplesCore, /* i  : number of core-coded samples below IGF  */
     428             :     const int16_t nSamplesMax,  /* i  : transform length (number of MCLT lines) */
     429             :     const int16_t limitWeight,  /* i  : 1 means the pre-processing is softened  */
     430             :     const uint16_t corrIdxPrev, /* i  : previous transform's correlation index  */
     431             :     const uint32_t bitRateMode  /* i  : stereo bitrate mode (bps * L_frame)     */
     432             : )
     433             : {
     434             :     int16_t fadeInLen, fadeInOff, nSampProc, preproLen;
     435             :     float maxSqrValue, stepWeightI, stepWeightD;
     436             :     float corr, sumL, sumR, chanCorrSign, xTalkI, xTalkD;
     437             :     float sumMagnL, sumMagnR, sumPrdLR, sumPrdLL, sumPrdRR;
     438             :     int16_t s;
     439             : 
     440       26123 :     fadeInLen = ( nSamplesMax < 512 ? ( nSamplesMax < 256 ? 10 : 20 ) : 40 );            /* 1000Hz transition */
     441       26123 :     fadeInOff = ( ( bitRateMode * 3 ) >> 19 ) & ( nSamplesMax < 512 ? 0xFFFF : 0xFFFE ); /* offset */
     442       26123 :     nSampProc = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     443       26123 :     preproLen = nSampProc - fadeInOff;
     444       26123 :     maxSqrValue = 16777216.f * preproLen * preproLen;
     445       26123 :     stepWeightI = ( limitWeight > 0 ? 1.f : 2.f ); /* crosstalk weight */
     446       26123 :     stepWeightD = 4.f - stepWeightI;               /* decrement, i.e., 1 - crosstalk */
     447       26123 :     corr = 0.f, sumL = 0.f, sumR = 0.f;
     448       26123 :     sumMagnL = 0.f, sumMagnR = 0.f, sumPrdLR = 0.f, sumPrdLL = 0.f, sumPrdRR = 0.f;
     449             : 
     450       26123 :     if ( fadeInOff + fadeInLen + MIN_STE_PRE_LEN >= nSampProc )
     451             :     {
     452         989 :         return 0;
     453             :     }
     454             : 
     455     5020619 :     for ( s = fadeInOff; s < nSampProc; s++ )
     456             :     {
     457             :         float absMagnL, absMagnR;
     458     4995485 :         absMagnL = sqrtf( sigR0[s] * sigR0[s] + sigI0[s] * sigI0[s] );
     459     4995485 :         absMagnR = sqrtf( sigR1[s] * sigR1[s] + sigI1[s] * sigI1[s] );
     460             : 
     461     4995485 :         corr += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     462     4995485 :         sumL += sigR0[s] + sigI0[s];
     463     4995485 :         sumR += sigR1[s] + sigI1[s];
     464             : 
     465     4995485 :         sumMagnL += absMagnL;
     466     4995485 :         sumMagnR += absMagnR;
     467     4995485 :         sumPrdLR += absMagnL * absMagnR;
     468     4995485 :         sumPrdLL += absMagnL * absMagnL;
     469     4995485 :         sumPrdRR += absMagnR * absMagnR;
     470             :     }
     471       25134 :     corr *= 2.f * preproLen;
     472       25134 :     corr -= sumL * sumR;
     473       25134 :     chanCorrSign = ( corr < -maxSqrValue ) ? -1.f : 1.f;
     474             : 
     475       25134 :     sumL = sumMagnL / (float) preproLen; /* inter-channel correlation of magnitude */
     476       25134 :     sumR = sumMagnR / (float) preproLen;
     477       25134 :     corr = sumPrdLR + sumL * sumR * preproLen - sumMagnL * sumR - sumMagnR * sumL;
     478       25134 :     sumL = sumPrdLL + sumL * sumL * preproLen - sumMagnL * sumL - sumMagnL * sumL;
     479       25134 :     sumR = sumPrdRR + sumR * sumR * preproLen - sumMagnR * sumR - sumMagnR * sumR;
     480       25134 :     corr = ( ( corr <= 0.f ) || ( sumL * sumR <= 0.f ) ? 0.f : ( corr * corr ) / ( sumL * sumR ) );
     481             : 
     482       25134 :     if ( ( corr > 0.75f && corrIdxPrev == 0 ) || /* processing weight fade-in/-out */
     483       19631 :          ( corr <= 0.75f && corrIdxPrev > 0 ) )
     484             :     {
     485        3022 :         stepWeightI = 1.f;
     486        3022 :         stepWeightD = 4.f - stepWeightI;
     487             :     }
     488             : 
     489       25134 :     if ( corr <= 0.75f && corrIdxPrev == 0 ) /* bypass all processing, just return */
     490             :     {
     491       18358 :         return 0;
     492             :     }
     493             : 
     494        6776 :     sigR0 += fadeInOff + 1; /* stereo pre-processing starts with an offset of one! */
     495        6776 :     sigR1 += fadeInOff + 1;
     496        6776 :     sigI0 += fadeInOff + 1;
     497        6776 :     sigI1 += fadeInOff + 1;
     498        6776 :     xTalkI = stepWeightI;
     499        6776 :     xTalkD = stepWeightD * ( 2 * fadeInLen - 1 );
     500             : 
     501      244490 :     for ( s = fadeInLen - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     502             :     {
     503      237714 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     504      237714 :         xTalkI += stepWeightI;
     505      237714 :         xTalkD -= stepWeightD;
     506             :     }
     507             : 
     508     1110742 :     for ( s = preproLen - fadeInLen; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     509             :     {
     510     1103966 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     511             :     }
     512             : 
     513        6776 :     if ( nSampProc < nSamplesMax ) /* fade-out of processing at start of IGF range */
     514             :     {
     515      237690 :         for ( s = min( fadeInLen, nSamplesMax - nSampProc ) - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     516             :         {
     517      231261 :             xTalkI -= stepWeightI;
     518      231261 :             xTalkD += stepWeightD;
     519      231261 :             applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     520             :         }
     521             :     }
     522             : 
     523        6776 :     return ( corr > 0.75f ? 1 : 0 );
     524             : }
     525             : 
     526             : 
     527             : /*--------------------------------------------------------------*
     528             :  * enc_prm_igf_mdct()
     529             :  *
     530             :  * write igf parameters to bitstream
     531             :  *---------------------------------------------------------------*/
     532             : 
     533      374831 : void enc_prm_igf_mdct(
     534             :     Encoder_State *st,    /* i  : Encoder state handle    */
     535             :     BSTR_ENC_HANDLE hBstr /* i/o: Bitstream handle        */
     536             : )
     537             : {
     538             :     int16_t nbits_start, total_nbbits;
     539             : 
     540      374831 :     nbits_start = hBstr->nb_bits_tot;
     541             : 
     542      374831 :     if ( st->core == TCX_20_CORE )
     543             :     {
     544      365038 :         st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     545             : 
     546      365038 :         IGFEncWriteBitstream( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
     547             :     }
     548             :     else
     549             :     {
     550        9793 :         IGFEncWriteConcatenatedBitstream( st->hIGFEnc, hBstr );
     551             :     }
     552             : 
     553      374831 :     total_nbbits = hBstr->nb_bits_tot - nbits_start;
     554      374831 :     st->side_bits_frame_channel += total_nbbits;
     555             : 
     556      374831 :     return;
     557             : }
     558             : 
     559             : 
     560             : /*-------------------------------------------------------------------*
     561             :  * ivas_mdct_core_whitening_enc()
     562             :  *
     563             :  * MCT preprocessing up to whitening the core spectrum
     564             :  *-------------------------------------------------------------------*/
     565             : 
     566      352949 : void ivas_mdct_core_whitening_enc(
     567             :     CPE_ENC_HANDLE hCPE,                        /* i/o: CPE encoder structure                 */
     568             :     float new_samples[CPE_CHANNELS][L_INP],     /* i  : new samples                           */
     569             :     float old_wsp[CPE_CHANNELS][L_WSP],         /* i  : 12.8kHz weighted speech (for LTP      */
     570             :     float pitch_buf[CPE_CHANNELS][NB_SUBFR16k], /* o  : floating pitch for each subframe      */
     571             :     float *mdst_spectrum_long[CPE_CHANNELS],    /* o  : buffer for MDST spectrum              */
     572             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV],      /* o  : buffer TNS bits                       */
     573             :     float *orig_spectrum_long[CPE_CHANNELS],    /* o  : origingal spectrum w/o whitening      */
     574             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV],      /* o  : number of tns parameters put into prm */
     575             :     int16_t p_param[CPE_CHANNELS][NB_DIV],      /* o  : pointer to the parameter table        */
     576             :     BSTR_ENC_HANDLE hBstr,                      /* i/o: encoder bitstream handle              */
     577             :     const int16_t mct_on,                       /* i  : flag mct block (1) or stereo (0)      */
     578             :     const int16_t nChannels                     /* i  : total number of coded channels        */
     579             : )
     580             : {
     581             :     int16_t n, ch, nSubframes, L_subframe, L_subframeTCX, tcx_subframe_coded_lines;
     582             :     float A_q[CPE_CHANNELS][NB_DIV][M + 1];
     583             :     int16_t sns_vq_indices[CPE_CHANNELS * NB_DIV * SNS_MSVQ_NSTAGES_TCX10];
     584             :     int16_t sns_stereo_mode[NB_DIV];
     585             :     int16_t idx;
     586             :     int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW];
     587             :     int16_t param_core[CPE_CHANNELS][2 * NPRM_DIV];
     588             :     int16_t ltpBits[CPE_CHANNELS];
     589             :     int16_t i, T_op[CPE_CHANNELS][3];
     590             :     float *orig_spectrum[CPE_CHANNELS][NB_DIV]; /* Pointers to MDCT output for a short block (L/R) */
     591             :     float temp_buffer[15 * L_FRAME48k / 8];
     592             :     float *windowedSignal[CPE_CHANNELS];
     593      352949 :     float *powerSpec = orig_spectrum_long[0];
     594             :     float *mdst_spectrum[CPE_CHANNELS][NB_DIV];
     595             :     float nrg;
     596             :     Encoder_State *st, **sts;
     597             :     float scf[CPE_CHANNELS][NB_DIV][M];
     598             :     float scf_q[CPE_CHANNELS][NB_DIV][M];
     599             :     float chE[2], chE_tot;
     600             :     int8_t sns_low_br_mode;
     601             :     int16_t nbits_start_sns;
     602             :     int16_t num_sns;
     603             :     int8_t skipped_first_channel;
     604             :     int16_t zero_side_flag[NB_DIV];
     605             : 
     606      352949 :     push_wmops( "mdct_core_whitening" );
     607             : 
     608             :     /*--------------------------------------------------------------*
     609             :      * Initialization
     610             :      *---------------------------------------------------------------*/
     611             : 
     612      352949 :     sts = hCPE->hCoreCoder;
     613             : 
     614     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     615             :     {
     616      705898 :         stereo_tcx_init_enc( sts[ch] );
     617             : 
     618      705898 :         set_s( tnsSize[ch], 0, 2 );
     619      705898 :         set_s( tnsBits[ch], 0, 2 );
     620      705898 :         ltpBits[ch] = 0;
     621             : 
     622     2823592 :         for ( i = 0; i < 3; i++ )
     623             :         {
     624     2117694 :             T_op[ch][i] = sts[ch]->pitch[i];
     625             : 
     626             :             /* check minimum pitch for quantization */
     627     2117694 :             if ( T_op[ch][i] < PIT_MIN_SHORTER )
     628             :             {
     629      357336 :                 T_op[ch][i] *= 2;
     630             :             }
     631             :         }
     632             :     }
     633             : 
     634     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     635             :     {
     636      705898 :         orig_spectrum[ch][0] = orig_spectrum_long[ch];
     637      705898 :         orig_spectrum[ch][1] = orig_spectrum_long[ch] + N_TCX10_MAX;
     638      705898 :         mdst_spectrum[ch][0] = mdst_spectrum_long[ch];
     639      705898 :         mdst_spectrum[ch][1] = mdst_spectrum_long[ch] + N_TCX10_MAX;
     640             :     }
     641             : 
     642      352949 :     windowedSignal[0] = orig_spectrum_long[0]; /* NOTE temporarily available */
     643      352949 :     windowedSignal[1] = temp_buffer;           /* orig_spectrum_long isn't long enough */
     644             : 
     645             :     /*--------------------------------------------------------------*
     646             :      * TCX20/TCX10 switching decision
     647             :      *---------------------------------------------------------------*/
     648             : 
     649      352949 :     if ( mct_on )
     650             :     {
     651      270308 :         sts[0]->hTcxEnc->tfm_mem = sts[1]->hTcxEnc->tfm_mem = sqrtf( 0.5f * ( sts[0]->hTcxEnc->tfm_mem * sts[0]->hTcxEnc->tfm_mem + sts[1]->hTcxEnc->tfm_mem * sts[1]->hTcxEnc->tfm_mem ) ); /* RMS */
     652      270308 :         sts[0]->hTcxEnc->tcxltp_norm_corr_past = sts[1]->hTcxEnc->tcxltp_norm_corr_past = 0.5f * ( sts[0]->hTcxEnc->tcxltp_norm_corr_past + sts[1]->hTcxEnc->tcxltp_norm_corr_past );
     653      810924 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     654             :         {
     655      540616 :             st = sts[ch];
     656      540616 :             SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     657             :         }
     658             :     }
     659             : 
     660      352949 :     sts[0]->core = sts[0]->hTcxEnc->tcxMode;
     661      352949 :     sts[1]->core = sts[1]->hTcxEnc->tcxMode;
     662             : 
     663             :     /*--------------------------------------------------------------*
     664             :      * Core Signal Analysis: MDCT, TNS
     665             :      *---------------------------------------------------------------*/
     666             : 
     667     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     668             :     {
     669      705898 :         st = sts[ch];
     670      705898 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     671             :         {
     672       58829 :             continue;
     673             :         }
     674      647069 :         SetCurrentPsychParams( st->core, 0, st->hTcxCfg );
     675             : 
     676             :         /* tcx ltp analysis on the 12.8kHz weighted speech, saves preproc resampling to sr_core */
     677      647069 :         tcx_ltp_encode( st, st->hTcxEnc->tcxMode, L_FRAME, old_wsp[ch] + L_WSP_MEM + L_LOOK_12k8, NULL, old_wsp[ch] + L_WSP_MEM + L_LOOK_12k8, T_op[ch], &param_core[ch][1 + NOISE_FILL_RANGES], &ltpBits[ch], NULL, 0, IVAS_CPE_MDCT );
     678             : 
     679      647069 :         core_signal_analysis_high_bitrate( new_samples[ch] + L_INP_MEM, T_op[ch], NULL, NULL, st, mdst_spectrum[ch], tnsSize[ch], tnsBits[ch], param_core[ch], &ltpBits[ch], windowedSignal[ch], st->L_frame, st->hTcxEnc->L_frameTCX, hCPE->last_element_mode, 0 );
     680             : 
     681             :         /* BWD in MDCT domain */
     682      647069 :         if ( st->hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     683             :         {
     684      645678 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     685             : 
     686     1306167 :             for ( n = 0; n < nSubframes; n++ )
     687             :             {
     688      660489 :                 bw_detect( st, NULL, st->hTcxEnc->spectrum[n], NULL, MC_FORMAT /*just cannot be ISM_FORMAT*/, mct_on );
     689             : 
     690      660489 :                 if ( nSubframes == NB_DIV && n == 0 )
     691             :                 {
     692       14811 :                     st->last_input_bwidth = st->input_bwidth;
     693             :                 }
     694             :             }
     695             :         }
     696             : 
     697      647069 :         if ( st->last_core == ACELP_CORE ) /* reset past kernel info */
     698             :         {
     699        1391 :             st->hTcxEnc->kernel_switch_corr_past = 0.f;
     700        1391 :             st->hTcxEnc->kernel_symmetry_past = 0;
     701             :         }
     702             :     }
     703             : 
     704             :     /*--------------------------------------------------------------*
     705             :      * Transform Kernel Switching, Stereo Pre-Processing, and TNS
     706             :      *---------------------------------------------------------------*/
     707             : 
     708      352949 :     sts[0]->hTcxEnc->fUseTns[1] = 0;
     709             : 
     710      352949 :     if ( sts[0]->element_brate < IVAS_80k && sts[0]->core == sts[1]->core && sts[0]->element_mode == IVAS_CPE_MDCT && !mct_on && !hCPE->hStereoMdct->isSBAStereoMode )
     711       24881 :     {
     712             :         int16_t nSampCore;
     713             :         int32_t totalRate;
     714             :         TCX_ENC_HANDLE hTcxEnc0, hTcxEnc1;
     715             : 
     716       24881 :         nSampCore = ( sts[0]->igf ) ? max( sts[0]->hIGFEnc->infoStartLine, sts[1]->hIGFEnc->infoStartLine ) : 0;
     717       24881 :         totalRate = sts[0]->element_brate;
     718       24881 :         hTcxEnc0 = sts[0]->hTcxEnc;
     719       24881 :         hTcxEnc1 = sts[1]->hTcxEnc;
     720             : 
     721       24881 :         init_tcx_enc_info( sts[0], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     722             : 
     723       24881 :         if ( nSampCore == 0 )
     724             :         {
     725        5053 :             nSampCore = tcx_subframe_coded_lines;
     726             :         }
     727             : 
     728       24881 :         nrg = 0.25f * ( hTcxEnc0->tcxltp_norm_corr_past + hTcxEnc0->tcxltp_norm_corr_mem +  /* tcxltp_norm_corr_past already contains the */
     729       24881 :                         hTcxEnc1->tcxltp_norm_corr_past + hTcxEnc1->tcxltp_norm_corr_mem ); /* normalized correlation of the current frame */
     730             : 
     731       24881 :         L_subframe = max( 512, L_subframe );
     732       24881 :         nSubframes = ( hTcxEnc0->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     733             : 
     734       50531 :         for ( n = 0; n < nSubframes; n++ )
     735             :         {
     736       25650 :             if ( hTcxEnc0->transform_type[n] == hTcxEnc1->transform_type[n] )
     737             :             {
     738             :                 const int16_t switchKernel = /* these 4 transform types can be applied: 0 = MDCT-IV, 1 = MDST-II, 2 = MDCT-II, 3 = MDST-IV */
     739       25640 :                     kernel_switch_detect( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     740       25640 :                                           L_subframeTCX / nSubframes, hTcxEnc0->transform_type[n], &hTcxEnc0->kernel_switch_corr_past, ( totalRate * L_subframe ) / nSubframes );
     741       25640 :                 if ( switchKernel ) /* apply MDST-IV coding in one of the channels */
     742             :                 {
     743         653 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 1 ) - max( 0, switchKernel );
     744         653 :                     hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 2 : 0 ) + max( 0, switchKernel );
     745             :                 }
     746             :                 else /* switchKernel == 0, switch back to MDCT-IV in both channels */
     747             :                 {
     748       24987 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 2 : 0 );
     749       24987 :                     hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 2 : 0 );
     750             :                 }
     751             :             }
     752             :             else
     753             :             {
     754          10 :                 hTcxEnc0->kernel_switch_corr_past = 0.f; /* don't update the kernel switching state, postpone it to when data is available */
     755          10 :                 hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 0 );
     756          10 :                 hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 3 : 0 );
     757             :             }
     758       25650 :             hTcxEnc0->kernel_symmetry_past = hTcxEnc0->kernel_type[n] & 1;
     759       25650 :             hTcxEnc1->kernel_symmetry_past = hTcxEnc1->kernel_type[n] & 1;
     760             : 
     761       25650 :             kernel_switch_update_transforms( hTcxEnc0->spectrum[n], mdst_spectrum[0][n], hTcxEnc0->transform_type[n], sts[0]->hTcxCfg, sts[0]->bwidth_sw_cnt, hTcxEnc0->kernel_type[n],
     762       51300 :                                              hTcxEnc0->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc0->speech_TCX ), windowedSignal[0] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     763       25650 :             kernel_switch_update_transforms( hTcxEnc1->spectrum[n], mdst_spectrum[1][n], hTcxEnc1->transform_type[n], sts[1]->hTcxCfg, sts[1]->bwidth_sw_cnt, hTcxEnc1->kernel_type[n],
     764       51300 :                                              hTcxEnc1->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc1->speech_TCX ), windowedSignal[1] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     765             :         }
     766       50531 :         for ( n = 0; n < nSubframes; n++ )
     767             :         {
     768       25650 :             if ( hTcxEnc0->transform_type[n] == hTcxEnc1->transform_type[n] && /* do not combine TCX-5 and TCX-10 as this causes artifacts */
     769       25640 :                  sts[0]->hTcxCfg->tcx_curr_overlap_mode == sts[1]->hTcxCfg->tcx_curr_overlap_mode &&
     770       25473 :                  sts[0]->hTcxCfg->tcx_last_overlap_mode == sts[1]->hTcxCfg->tcx_last_overlap_mode )
     771             :             {
     772       25335 :                 if ( hTcxEnc0->transform_type[n] == TCX_5 )
     773             :                 {
     774         788 :                     const int16_t tcx5SizeFB = sts[1]->hTcxCfg->tcx5SizeFB;
     775             : 
     776         788 :                     hTcxEnc0->enc_ste_pre_corr_past =
     777         788 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / ( 2 * NB_DIV ),
     778         788 :                                           L_subframeTCX / ( 2 * NB_DIV ), ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / ( 2 * NB_DIV ) );
     779             : 
     780         788 :                     hTcxEnc0->enc_ste_pre_corr_past =
     781         788 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n] + tcx5SizeFB, hTcxEnc1->spectrum[n] + tcx5SizeFB, mdst_spectrum[0][n] + tcx5SizeFB, mdst_spectrum[1][n] + tcx5SizeFB, nSampCore / ( 2 * NB_DIV ),
     782         788 :                                           L_subframeTCX / ( 2 * NB_DIV ), ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / ( 2 * NB_DIV ) );
     783             :                 }
     784             :                 else
     785             :                 {
     786       24547 :                     hTcxEnc0->enc_ste_pre_corr_past =
     787       24547 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     788       24547 :                                           L_subframeTCX / nSubframes, ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / nSubframes );
     789             :                 }
     790             :             }
     791             :             else
     792             :             {
     793         315 :                 hTcxEnc0->enc_ste_pre_corr_past = 0;
     794             :             }
     795             :         }
     796             :     }
     797             :     else
     798             :     {
     799      328068 :         sts[0]->hTcxEnc->enc_ste_pre_corr_past = 0;
     800      328068 :         sts[0]->hTcxEnc->kernel_switch_corr_past = 0.f;
     801             : 
     802      984204 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     803             :         {
     804      656136 :             TCX_ENC_HANDLE hTcxEncCh = sts[ch]->hTcxEnc;
     805             : 
     806      656136 :             if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     807             :             {
     808       58829 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[0] = 0;
     809             : 
     810       58829 :                 continue;
     811             :             }
     812             : 
     813      597307 :             init_tcx_enc_info( sts[ch], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     814      597307 :             nSubframes = ( hTcxEncCh->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     815             : 
     816     1207887 :             for ( n = 0; n < nSubframes; n++ )
     817             :             {
     818      610580 :                 hTcxEncCh->kernel_type[n] = ( hTcxEncCh->kernel_symmetry_past && sts[ch]->element_mode == IVAS_CPE_MDCT ? 3 - mct_on : 0 );
     819      610580 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[n] & 1;
     820             : 
     821      610580 :                 kernel_switch_update_transforms( hTcxEncCh->spectrum[n], mdst_spectrum[ch][n], hTcxEncCh->transform_type[n], sts[ch]->hTcxCfg, sts[ch]->bwidth_sw_cnt, hTcxEncCh->kernel_type[n],
     822     1221160 :                                                  hTcxEncCh->new_speech_TCX, ( n /*1*/ ? NULL : hTcxEncCh->speech_TCX ), windowedSignal[ch] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     823             :             }
     824             :         }
     825             :     }
     826             : 
     827      352949 :     TNSAnalysisStereo( sts, mdst_spectrum, 0, tnsSize, tnsBits, param_core, mct_on );
     828             : 
     829             :     /*--------------------------------------------------------------*
     830             :      * Envelope Quantization and FDNS
     831             :      *---------------------------------------------------------------*/
     832             : 
     833     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     834             :     {
     835      705898 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     836             :         {
     837       58829 :             continue;
     838             :         }
     839             : 
     840      647069 :         st = sts[ch];
     841      647069 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     842             : 
     843      647069 :         if ( mct_on )
     844             :         {
     845      481787 :             set_zero( chE, NB_DIV );
     846             :         }
     847             : 
     848      647069 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     849             : 
     850      647069 :         L_subframe = L_subframe / nSubframes;
     851      647069 :         L_subframeTCX = ( mct_on ? L_subframeTCX / nSubframes : L_subframe );
     852      647069 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     853             : 
     854     1308949 :         for ( n = 0; n < nSubframes; n++ )
     855             :         {
     856      661880 :             if ( st->hTcxEnc->fUseTns[n] )
     857             :             {
     858     9841565 :                 for ( i = 0; i < L_subframeTCX; i++ )
     859             :                 {
     860     9822976 :                     powerSpec[i] = ( st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i] );
     861             :                 }
     862             :             }
     863             :             else
     864             :             {
     865   491745755 :                 for ( i = 0; i < L_subframeTCX; i++ )
     866             :                 {
     867   491102464 :                     powerSpec[i] = ( mdst_spectrum[ch][n][i] * mdst_spectrum[ch][n][i] + st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i] );
     868             :                 }
     869             :             }
     870             : 
     871      661880 :             if ( mct_on )
     872             :             {
     873      492433 :                 chE[n] = sum_f( powerSpec, L_subframeTCX );
     874             :             }
     875             : 
     876      661880 :             sns_compute_scf( powerSpec, st->hTcxCfg->psychParamsCurrent, st->L_frame, scf[ch][n] );
     877             :         }
     878             : 
     879             :         /* MCT: detect whether there are silent channels and set mct_chan_mode accordingly */
     880      647069 :         if ( mct_on )
     881             :         {
     882      481787 :             chE_tot = sum_f( chE, NB_DIV );
     883             : 
     884      481787 :             if ( chE_tot < SILENT_CHANNEL_THRES && nSubframes == 1 )
     885             :             {
     886        8565 :                 st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     887        8565 :                 st->bits_frame_channel = 0;
     888             :             }
     889             :             else
     890             :             {
     891      473222 :                 st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
     892             :             }
     893             :         }
     894             :     }
     895             : 
     896             :     /* set low br mode, if possible. Can later be discarded, depending on the stereo mode used for SNS parameter decoding */
     897      352949 :     if ( hCPE->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
     898             :     {
     899        1031 :         sns_low_br_mode = !sts[0]->sp_aud_decision0;
     900             :     }
     901             :     else
     902             :     {
     903      351918 :         sns_low_br_mode = 0;
     904             :     }
     905             : 
     906      352949 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
     907             :     {
     908       48065 :         quantize_sns( scf, scf_q, sts, sns_vq_indices, zero_side_flag, sns_stereo_mode );
     909             :     }
     910             :     else
     911             :     {
     912      304884 :         if ( sts[0]->hTcxEnc->tcxMode == TCX_20 && sts[1]->hTcxEnc->tcxMode == TCX_20 &&
     913      295724 :              sts[0]->mct_chan_mode == MCT_CHAN_MODE_REGULAR && sts[1]->mct_chan_mode == MCT_CHAN_MODE_REGULAR )
     914             :         {
     915      233375 :             sns_avq_cod_stereo( scf[0][0], scf[1][0], sts[0]->L_frame, scf_q[0][0], scf_q[1][0], param_lpc[0], param_lpc[1] );
     916             :         }
     917             :         else
     918             :         {
     919             : #ifdef DEBUG_MODE_MDCT
     920             :             {
     921             :                 float ener_side = 0;
     922             :                 float ener_mid = 0;
     923             :                 dbgwrite( &ener_side, sizeof( float ), 1, 1, "./res/ener_side" );
     924             :                 dbgwrite( &ener_mid, sizeof( float ), 1, 1, "./res/ener_mid" );
     925             :             }
     926             : #endif
     927      214527 :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     928             :             {
     929      143018 :                 param_lpc[ch][0] = ch;
     930      143018 :                 if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     931             :                 {
     932       67394 :                     continue;
     933             :                 }
     934       75624 :                 st = sts[ch];
     935             : 
     936       75624 :                 if ( st->hTcxEnc->tcxMode == TCX_20 )
     937             :                 {
     938       63201 :                     sns_avq_cod( scf[ch][0], NULL, scf_q[ch][0], NULL, &param_lpc[ch][1], st->hTcxEnc->tcxMode, st->L_frame, sns_low_br_mode );
     939             :                 }
     940             :                 else
     941             :                 {
     942       12423 :                     sns_avq_cod( scf[ch][1], scf[ch][0], scf_q[ch][1], scf_q[ch][0], &param_lpc[ch][1], st->hTcxEnc->tcxMode, st->L_frame, sns_low_br_mode );
     943             :                 }
     944             :             }
     945             :         }
     946             :     }
     947             : 
     948     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     949             :     {
     950      705898 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     951             :         {
     952       67394 :             continue;
     953             :         }
     954      638504 :         st = sts[ch];
     955      638504 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     956      638504 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     957             : 
     958      638504 :         L_subframe = L_subframe / nSubframes;
     959      638504 :         L_subframeTCX = L_subframeTCX / nSubframes;
     960      638504 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     961             : 
     962     1291819 :         for ( n = 0; n < nSubframes; n++ )
     963             :         {
     964      653315 :             mvr2r( st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], L_subframeTCX );
     965             : 
     966             :             /* Shape spectrum */
     967      653315 :             ShapeSpectrum( st->hTcxCfg, A_q[ch][n], NULL, L_subframe, tcx_subframe_coded_lines, st->hTcxEnc->spectrum[n], st->hTcxEnc->fUseTns[n], st, scf_q[ch][n] );
     968             :         }
     969             :     }
     970             : 
     971             :     /*--------------------------------------------------------------*
     972             :      * TNS
     973             :      *---------------------------------------------------------------*/
     974             : 
     975             :     /* first deinterleave once more */
     976     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     977             :     {
     978      705898 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     979             :         {
     980       67394 :             continue;
     981             :         }
     982      638504 :         st = sts[ch];
     983      638504 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     984             : 
     985     1291819 :         for ( n = 0; n < nSubframes; n++ )
     986             :         {
     987      653315 :             if ( st->hTcxEnc->transform_type[n] == TCX_5 )
     988             :             {
     989       15305 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, st->hTcxEnc->spectrum[n] );
     990       15305 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, mdst_spectrum[ch][n] );
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995      352949 :     TNSAnalysisStereo( sts, mdst_spectrum, 1, tnsSize, tnsBits, param_core, mct_on );
     996             : 
     997     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     998             :     {
     999      705898 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1000             :         {
    1001       67394 :             continue;
    1002             :         }
    1003             : 
    1004      638504 :         st = sts[ch];
    1005      638504 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1006             : 
    1007      638504 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
    1008             : 
    1009      638504 :         L_subframe = L_subframe / nSubframes;
    1010      638504 :         L_subframeTCX = L_subframeTCX / nSubframes;
    1011      638504 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
    1012             : 
    1013     1291819 :         for ( n = 0; n < nSubframes; n++ )
    1014             :         {
    1015      653315 :             if ( !st->hTcxEnc->fUseTns[n] )
    1016             :             {
    1017      580530 :                 ShapeSpectrum( st->hTcxCfg, A_q[ch][n], NULL, L_subframe, tcx_subframe_coded_lines, mdst_spectrum[ch][n], st->hTcxEnc->fUseTns[n], st, scf_q[ch][n] );
    1018             :             }
    1019             :         }
    1020             :     }
    1021     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1022             :     {
    1023             :         /*no need to write last channel bit in case of odd channels*/
    1024      705898 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
    1025             :         {
    1026       58829 :             continue;
    1027             :         }
    1028             : 
    1029      647069 :         st = sts[ch];
    1030             : 
    1031      647069 :         if ( mct_on ) /* signal bits should be written only for MCT*/
    1032             :         {
    1033      481787 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1034             :             {
    1035        8565 :                 push_next_indice( hBstr, 1, 1 );
    1036             :             }
    1037             :             else
    1038             :             {
    1039      473222 :                 assert( st->mct_chan_mode == MCT_CHAN_MODE_REGULAR );
    1040      473222 :                 push_next_indice( hBstr, 0, 1 );
    1041             :             }
    1042             :         }
    1043             :     }
    1044             : 
    1045             :     /* write bitstream with info up to here */
    1046     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1047             :     {
    1048      705898 :         st = sts[ch];
    1049             : 
    1050      705898 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1051             :         {
    1052       67394 :             st->side_bits_frame_channel = 0;
    1053             :             /*dummy initialization to prevent range coder crashing in case all channels are silent and bits are distributed to channel 0 */
    1054       67394 :             *p_param[ch] = 1 + NOISE_FILL_RANGES + LTPSIZE + tnsSize[ch][0] + NPRM_CTX_HM;
    1055       67394 :             continue;
    1056             :         }
    1057             : 
    1058      638504 :         enc_prm_pre_mdct( st, param_core[ch], ( ( ( ch > 0 ) && ( sts[0]->hTcxEnc->fUseTns[0] + sts[0]->hTcxEnc->fUseTns[1] > 0 ) && !mct_on ) ? tnsSize[ch] : NULL ), p_param[ch], mct_on, hBstr );
    1059             : 
    1060      638504 :         if ( ch > 0 && sts[0]->hTcxEnc->fUseTns[0] + sts[0]->hTcxEnc->fUseTns[1] > 0 && !mct_on )
    1061             :         {
    1062        9154 :             if ( st->hTcxEnc->tnsData[0].nFilters < 0 )
    1063             :             {
    1064        2393 :                 tnsBits[ch][0] = 1;
    1065             :             }
    1066             :             else
    1067             :             {
    1068        6761 :                 tnsBits[ch][0]++;
    1069             :             }
    1070             : 
    1071        9154 :             if ( st->core == TCX_10_CORE )
    1072             :             {
    1073        1575 :                 if ( st->hTcxEnc->tnsData[1].nFilters < 0 )
    1074             :                 {
    1075         188 :                     tnsBits[ch][1] = 1;
    1076             :                 }
    1077             :                 else
    1078             :                 {
    1079        1387 :                     tnsBits[ch][1]++;
    1080             :                 }
    1081             :             }
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*--------------------------------------------------------------------------------*
    1086             :      * SNS parameters
    1087             :      *--------------------------------------------------------------------------------*/
    1088             : 
    1089      352949 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
    1090             :     {
    1091       48065 :         idx = 0;
    1092             : 
    1093       48065 :         if ( sts[0]->core == sts[1]->core )
    1094             :         {
    1095       47719 :             nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1096             : 
    1097             :             /* push all stereo mode bits first */
    1098       96459 :             for ( n = 0; n < nSubframes; ++n )
    1099             :             {
    1100       48740 :                 push_next_indice( hBstr, sns_stereo_mode[n], 1 );
    1101       48740 :                 sts[0]->side_bits_frame_channel++;
    1102             :             }
    1103             : 
    1104             :             /* zero side flags only get transmitted if needed */
    1105       96459 :             for ( n = 0; n < nSubframes; ++n )
    1106             :             {
    1107       48740 :                 if ( sns_stereo_mode[n] == SNS_STEREO_MODE_MS )
    1108             :                 {
    1109       45474 :                     push_next_indice( hBstr, zero_side_flag[n], 1 );
    1110       45474 :                     sts[0]->side_bits_frame_channel++;
    1111             :                 }
    1112             :             }
    1113             :         }
    1114             : 
    1115      144195 :         for ( ch = 0; ch < CPE_CHANNELS; ++ch )
    1116             :         {
    1117       96130 :             st = sts[ch];
    1118       96130 :             nbits_start_sns = hBstr->nb_bits_tot;
    1119       96130 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1120      194648 :             for ( n = 0; n < nSubframes; ++n )
    1121             :             {
    1122       98518 :                 const int16_t is_side = ch == 1 && sns_stereo_mode[n] == SNS_STEREO_MODE_MS;
    1123       98518 :                 const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
    1124       98518 :                 int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
    1125             : 
    1126       98518 :                 if ( is_side )
    1127             :                 {
    1128       45474 :                     if ( zero_side_flag[n] )
    1129             :                     {
    1130        9607 :                         continue;
    1131             :                     }
    1132       35867 :                     nStages = SNS_MSVQ_NSTAGES_SIDE;
    1133       35867 :                     bits = ( sts[ch]->core == TCX_20_CORE ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
    1134             :                 }
    1135      369905 :                 for ( int16_t j = 0; j < nStages; ++j, ++idx )
    1136             :                 {
    1137      280994 :                     push_next_indice( hBstr, sns_vq_indices[idx], bits[j] );
    1138             :                 }
    1139             :             }
    1140       96130 :             st->side_bits_frame_channel += hBstr->nb_bits_tot - nbits_start_sns;
    1141             :         }
    1142             :     }
    1143             :     else
    1144             :     {
    1145             :         /* write SNS parameter separately since at the decoder, both channels' cores need to be decoded before, so the joint SNS decoding can be done */
    1146      304884 :         skipped_first_channel = 0;
    1147      914652 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1148             :         {
    1149      609768 :             st = sts[ch];
    1150             : 
    1151      609768 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1152             :             {
    1153       67394 :                 skipped_first_channel = 1;
    1154       67394 :                 continue;
    1155             :             }
    1156             : 
    1157      542374 :             nbits_start_sns = hBstr->nb_bits_tot;
    1158             : 
    1159      542374 :             num_sns = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1160             : 
    1161      542374 :             if ( ch == 0 || skipped_first_channel )
    1162             :             {
    1163      301191 :                 push_next_indice( hBstr, param_lpc[0][0] >> 1, 1 );
    1164             : 
    1165      301191 :                 if ( st->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
    1166             :                 {
    1167             :                     /* write classifier decision to signal low br mode for SNS encoding, for all other configs, low_br mode is not possible */
    1168         114 :                     push_next_indice( hBstr, sns_low_br_mode, 1 );
    1169             :                 }
    1170             :             }
    1171      542374 :             encode_lpc_avq( hBstr, num_sns, param_lpc[ch], st->core, st->element_mode );
    1172             : 
    1173      542374 :             st->side_bits_frame_channel += hBstr->nb_bits_tot - nbits_start_sns;
    1174             :         }
    1175             :     }
    1176             : 
    1177             : 
    1178             :     /*update pitch buffer*/
    1179     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1180             :     {
    1181      705898 :         st = sts[ch];
    1182      705898 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1183             :         {
    1184       67394 :             continue;
    1185             :         }
    1186      638504 :         if ( param_core[ch][1 + NOISE_FILL_RANGES] != 0 )
    1187             :         {
    1188      470413 :             set_f( pitch_buf[ch], ( st->hTcxEnc->tcxltp_pitch_int + (float) st->hTcxEnc->tcxltp_pitch_fr / (float) st->pit_res_max ) * (float) st->sr_core / (float) INT_FS_12k8, NB_SUBFR16k );
    1189             :         }
    1190             :         else
    1191             :         {
    1192      168091 :             set_f( pitch_buf[ch], L_SUBFR, NB_SUBFR16k );
    1193             :         }
    1194             :     }
    1195             : 
    1196      352949 :     pop_wmops();
    1197      352949 :     return;
    1198             : }
    1199             : 
    1200             : 
    1201             : /*--------------------------------------------------------------*
    1202             :  * ivas_mdct_quant_coder()
    1203             :  *
    1204             :  * Spectrum quantization and coding
    1205             :  *---------------------------------------------------------------*/
    1206             : 
    1207      352949 : void ivas_mdct_quant_coder(
    1208             :     CPE_ENC_HANDLE hCPE,                   /* i/o: Encoder CPE handle                   */
    1209             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV], /* i  : bits needed for TNS parameters       */
    1210             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV], /* i  : size of TNS                          */
    1211             :     int16_t p_param[CPE_CHANNELS][NB_DIV], /* i  : pointer to parameter array           */
    1212             :     const int16_t MCT_flag                 /* i  : hMCT handle allocated (1) or not (0) */
    1213             : )
    1214             : {
    1215             :     Encoder_State *st, **sts;
    1216             :     int16_t bitsAvailable, target_bits, nSubframes, ch, n;
    1217             :     int16_t L_frameTCX[CPE_CHANNELS][NB_DIV];           /* full frame length */
    1218             :     int16_t L_frame[CPE_CHANNELS][NB_DIV];              /* frame length */
    1219             :     int16_t L_spec[CPE_CHANNELS][NB_DIV];               /* length of the coded spectrum */
    1220             :     int16_t tcx_offset[CPE_CHANNELS][NB_DIV];           /* folding point offset relative to the end of the previous frame */
    1221             :     int16_t noiseFillingBorder[CPE_CHANNELS][NB_DIV];   /* noise filling border */
    1222             :     float fac_ns[CPE_CHANNELS][NB_DIV];                 /* noise filling level */
    1223             :     int16_t nf_seed[CPE_CHANNELS][NB_DIV];              /* noise filling random seed */
    1224             :     int16_t hm_active[CPE_CHANNELS][NB_DIV];            /* flag indicating if the harmonic model is active */
    1225             :     float ener[CPE_CHANNELS][NB_DIV];                   /* energy of the quantized spectrum */
    1226             :     float gain_tcx[CPE_CHANNELS][NB_DIV];               /* global gain */
    1227             :     float quantized_spectrum_long[CPE_CHANNELS][N_MAX]; /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
    1228             :     float *quantized_spectrum[CPE_CHANNELS][NB_DIV];
    1229             :     int16_t param_core[CPE_CHANNELS][2 * NPRM_DIV];
    1230             :     int16_t ignore_chan[CPE_CHANNELS];
    1231             :     int16_t target_bitsTCX10[CPE_CHANNELS][NB_DIV];
    1232             :     int16_t nbits_start, total_nbbits;
    1233             : 
    1234      352949 :     push_wmops( "mdct_core_Q" );
    1235      352949 :     sts = hCPE->hCoreCoder;
    1236             : 
    1237     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1238             :     {
    1239      705898 :         set_f( fac_ns[ch], 0.0f, NB_DIV );
    1240      705898 :         set_s( nf_seed[ch], 0, NB_DIV );
    1241      705898 :         set_s( hm_active[ch], 0, NB_DIV );
    1242      705898 :         set_f( ener[ch], 0.0f, NB_DIV );
    1243      705898 :         set_f( gain_tcx[ch], 0.0f, NB_DIV );
    1244             :     }
    1245             : 
    1246     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1247             :     {
    1248      705898 :         quantized_spectrum[ch][0] = quantized_spectrum_long[ch];
    1249      705898 :         quantized_spectrum[ch][1] = quantized_spectrum_long[ch] + N_TCX10_MAX;
    1250             : 
    1251      705898 :         st = sts[ch];
    1252             : 
    1253      705898 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1254             :         {
    1255       67249 :             ignore_chan[ch] = 1;
    1256       67249 :             continue;
    1257             :         }
    1258             : 
    1259      638649 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1260      638649 :         ignore_chan[ch] = 0;
    1261             : 
    1262      638649 :         bitsAvailable = st->bits_frame_channel + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ) + tnsBits[ch][0] + tnsBits[ch][1];
    1263             : 
    1264             :         /* calculate TCX10 target bits before to assure minimum amount is distributed between subframes */
    1265      638649 :         if ( st->hTcxEnc->tcxMode == TCX_10 )
    1266             :         {
    1267             :             int16_t nTnsBitsTCX10Tmp[2];
    1268       14811 :             nTnsBitsTCX10Tmp[0] = tnsBits[ch][0];
    1269       14811 :             nTnsBitsTCX10Tmp[1] = tnsBits[ch][1];
    1270             : 
    1271       14811 :             ivas_mdct_tcx10_bit_distribution( target_bitsTCX10[ch], bitsAvailable, nTnsBitsTCX10Tmp );
    1272             :         }
    1273             : #ifdef DEBUG_PLOT_BITS
    1274             :         if ( st->hTcxEnc->tcxMode == TCX_20 )
    1275             :         {
    1276             :             set_s( &target_bitsTCX10[ch][0], 0, NB_DIV );
    1277             :         }
    1278             :         for ( n = 0; n < NB_DIV; n++ )
    1279             :         {
    1280             :             dbgwrite( &target_bitsTCX10[ch][n], sizeof( int16_t ), 1, 1, "./res/bits_tarrget_TCX10" );
    1281             :         }
    1282             : #endif
    1283             : 
    1284     1292109 :         for ( n = 0; n < nSubframes; n++ )
    1285             :         {
    1286      653460 :             if ( nSubframes == 2 )
    1287             :             {
    1288       29622 :                 target_bits = target_bitsTCX10[ch][n];
    1289             :             }
    1290             :             else
    1291             :             {
    1292      623838 :                 target_bits = bitsAvailable / nSubframes - tnsBits[ch][n];
    1293             :             }
    1294             : 
    1295      653460 :             assert( target_bits >= NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL + SMDCT_MINIMUM_ARITH_BITS );
    1296             : 
    1297             :             /*-----------------------------------------------------------*
    1298             :              * Quantize the MDCT spectrum                                *
    1299             :              *-----------------------------------------------------------*/
    1300             : 
    1301      653460 :             QuantizeTCXSpectrum( st, n, st->hTcxEnc->spectrum[n], NULL, NULL, tnsSize[ch][n], target_bits, 0, &L_frameTCX[ch][n],
    1302      653460 :                                  &L_frame[ch][n], &L_spec[ch][n], &tcx_offset[ch][n], &noiseFillingBorder[ch][n], quantized_spectrum[ch][n], NULL,
    1303      653460 :                                  &hm_active[ch][n], NULL, &nf_seed[ch][n], &ener[ch][n], &gain_tcx[ch][n], param_core[ch] + n * NPRM_DIV );
    1304             :         }
    1305             :     }
    1306             : 
    1307      352949 :     EstimateStereoTCXNoiseLevel( sts, quantized_spectrum, gain_tcx, L_frame, noiseFillingBorder, hm_active, ignore_chan, fac_ns, param_core, MCT_flag );
    1308             : 
    1309     1058847 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1310             :     {
    1311      705898 :         st = sts[ch];
    1312             : 
    1313             :         /* update the pointer to the buffer of indices of the second channel */
    1314      705898 :         if ( ch > 0 )
    1315             :         {
    1316      352949 :             st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
    1317             :         }
    1318      705898 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1319             :         {
    1320             :             /*Enable appropriate upadte of tcx_curr_overlap_mode even for uncoded channel index 1*/
    1321       67249 :             L_frameTCX[ch][0] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1322       67249 :             L_frameTCX[ch][1] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1323             : 
    1324       67249 :             L_frame[ch][0] = st->L_frame;
    1325       67249 :             L_frame[ch][1] = st->L_frame;
    1326             :         }
    1327      705898 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1328             : 
    1329     1426613 :         for ( n = 0; n < nSubframes; n++ )
    1330             :         {
    1331             :             /* Update L_frame_past */
    1332      720715 :             st->L_frame_past = L_frame[ch][n];
    1333             : 
    1334             :             /* Update overlap */
    1335      720715 :             if ( ( ( ( L_frameTCX[ch][n] == st->hTcxEnc->L_frameTCX >> 1 ) && n > 0 ) || ( st->hTcxCfg->tcx_last_overlap_mode == TRANSITION_OVERLAP ) ) && ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) )
    1336             :             {
    1337       11371 :                 st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
    1338             :             }
    1339             :         }
    1340             : 
    1341             :         /*--------------------------------------------------------------*
    1342             :          * Generate Bitstream
    1343             :          *---------------------------------------------------------------*/
    1344             : 
    1345      705898 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1346             :         {
    1347       67249 :             continue;
    1348             :         }
    1349      638649 :         nbits_start = st->hBstr->nb_bits_tot;
    1350             : 
    1351      638649 :         writeTCXparam( st, st->hBstr, NULL, param_core[ch], 0, 0, 0, NULL, p_param[ch], target_bitsTCX10[ch], 1 );
    1352             : 
    1353      638649 :         total_nbbits = st->hBstr->nb_bits_tot - nbits_start - nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
    1354             : 
    1355      638649 :         assert( st->bits_frame_channel == total_nbbits );
    1356             :     }
    1357             : 
    1358      352949 :     pop_wmops();
    1359      352949 :     return;
    1360             : }
    1361             : 
    1362             : 
    1363             : /*--------------------------------------------------------------*
    1364             :  * init_tcx_enc_info()
    1365             :  *
    1366             :  * Initialize TCX parameters
    1367             :  *---------------------------------------------------------------*/
    1368             : 
    1369     2546265 : void init_tcx_enc_info(
    1370             :     Encoder_State *st, /* i/o: coder memory state      */
    1371             :     int16_t *L_frame,
    1372             :     int16_t *L_frameTCX,
    1373             :     int16_t *L_spec )
    1374             : {
    1375     2546265 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
    1376     2546265 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1377             :     int16_t tcx_offset, tcx_offsetFB;
    1378             : 
    1379             :     /* Init lengths */
    1380     2546265 :     tcx_offset = hTcxCfg->tcx_offset;
    1381     2546265 :     tcx_offsetFB = hTcxCfg->tcx_offsetFB;
    1382     2546265 :     *L_frame = st->L_frame;
    1383     2546265 :     *L_frameTCX = hTcxEnc->L_frameTCX;
    1384     2546265 :     *L_spec = st->hTcxCfg->tcx_coded_lines;
    1385             : 
    1386     2546265 :     if ( st->last_core == ACELP_CORE )
    1387             :     {
    1388             :         /* if past frame is ACELP */
    1389        5225 :         *L_frame += tcx_offset;
    1390        5225 :         *L_frameTCX += tcx_offsetFB;
    1391        5225 :         *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
    1392             : 
    1393        5225 :         assert( hTcxCfg->lfacNext <= 0 );
    1394        5225 :         *L_frame -= hTcxCfg->lfacNext;
    1395        5225 :         *L_frameTCX -= hTcxCfg->lfacNextFB;
    1396             :     }
    1397             : 
    1398     2546265 :     return;
    1399             : }

Generated by: LCOV version 1.14