LCOV - code coverage report
Current view: top level - lib_enc - ivas_mdct_core_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ 9b04ec3cb36f5e8dc438cf854fa3e349998fa1e9 Lines: 531 531 100.0 %
Date: 2025-10-31 05:34:26 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      419234 : 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      419234 :     nbits_start = hBstr->nb_bits_tot;
      78             : 
      79             :     /*--------------------------------------------------------------------------------*
      80             :      * Header
      81             :      *--------------------------------------------------------------------------------*/
      82             : 
      83      419234 :     writeTCXMode( st, hBstr, MCT_flag, &nbits_start );
      84             : 
      85             :     /* write last_core for core switching and error concealment */
      86      419234 :     push_next_indice( hBstr, st->last_core != ACELP_CORE, 1 );
      87             : 
      88      419234 :     writeTCXWindowing( hBstr, st->hTcxCfg->tcx_curr_overlap_mode );
      89      419234 :     if ( st->core == TCX_10_CORE )
      90             :     {
      91        9496 :         writeTCXWindowing( hBstr, st->hTcxCfg->tcx_last_overlap_mode );
      92             :     }
      93      419234 :     push_next_indice( hBstr, st->hTcxEnc->kernel_type[0], st->last_core != ACELP_CORE ? 2 : 1 );
      94      419234 :     if ( st->core == TCX_10_CORE )
      95             :     {
      96        9496 :         assert( ( st->hTcxEnc->kernel_type[0] & 1 ) == ( st->hTcxEnc->kernel_type[1] >> 1 ) );
      97        9496 :         push_next_indice( hBstr, st->hTcxEnc->kernel_type[1] & 1, 1 );
      98             :     }
      99             : 
     100      419234 :     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      419234 :     writeTCXparam( st, hBstr, NULL, param, 0, 0, 0, no_param_tns, p_param, NULL, 0 );
     112             : 
     113      419234 :     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      419234 :     return;
     120             : }
     121             : 
     122             : /*--------------------------------------------------------------*
     123             :  * kernel_switch_detect()
     124             :  *
     125             :  * complex-valued detection method for transform kernel switching
     126             :  *---------------------------------------------------------------*/
     127             : 
     128       14515 : 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       14515 :     maxLength = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     144       14515 :     anaLength = min( maxLength, (int16_t) ( bitRateMode >> 17 ) & 0xFFFE );
     145       14515 :     s = ( nSamplesMax < 512 ? 2 : 4 ); /* exclude DC offset */
     146             : 
     147       14515 :     cov00 = 0.f;
     148       14515 :     cov90 = 0.f;
     149       14515 :     sumR0 = 0.f;
     150       14515 :     sumR1 = 0.f;
     151       14515 :     sumI0 = 0.f;
     152       14515 :     sumI1 = 0.f;
     153             : 
     154       14515 :     if ( tcxTransType == TCX_5 )
     155             :     {
     156         491 :         assert( nSamplesMax < 512 );
     157         491 :         anaLength >>= 1;
     158       25278 :         for ( s = 1; s < anaLength; s++ )
     159             :         {
     160       24787 :             cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     161       24787 :             cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     162       24787 :             sumR0 += sigR0[s] * sigR0[s];
     163       24787 :             sumR1 += sigR1[s] * sigR1[s];
     164       24787 :             sumI0 += sigI0[s] * sigI0[s];
     165       24787 :             sumI1 += sigI1[s] * sigI1[s];
     166             :         }
     167         491 :         anaLength += ( nSamplesMax >> 1 );
     168         491 :         s = 1 + ( nSamplesMax >> 1 ); /* 2nd TCX-5 spectrum is stacked onto 1st */
     169             :     }
     170     2900742 :     for ( ; s < anaLength; s++ )
     171             :     {
     172     2886227 :         cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     173     2886227 :         cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     174     2886227 :         sumR0 += sigR0[s] * sigR0[s];
     175     2886227 :         sumR1 += sigR1[s] * sigR1[s];
     176     2886227 :         sumI0 += sigI0[s] * sigI0[s];
     177     2886227 :         sumI1 += sigI1[s] * sigI1[s];
     178             :     }
     179             : 
     180       14515 :     cov00 /= ( sqrtf( sumR0 * sumR1 ) + sqrtf( sumI0 * sumI1 ) + 1.f );
     181       14515 :     cov90 /= ( sqrtf( sumR0 * sumI1 ) + sqrtf( sumI0 * sumR1 ) + 1.f );
     182       14515 :     sumI0 = max( 0.f, fabsf( cov90 ) - fabsf( cov00 ) );
     183       14515 :     sumI1 = ( switchCovPrev != NULL ? fabsf( *switchCovPrev ) : 0.f );
     184       14515 :     s = ( cov90 < 0.f ? -1 : 1 );
     185             : 
     186       14515 :     if ( switchCovPrev != NULL ) /* update the decision smoothing history */
     187             :     {
     188       14515 :         *switchCovPrev = ( 0.875f * sumI1 + 0.125f * sumI0 ) * s;
     189             :     }
     190             : 
     191       14515 :     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          26 : 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          26 :     const float signLeft = ( kernelType >= MDCT_II ? -1.f : 1.f );
     204          26 :     const float signRight = ( kernelType & 1 ? 1.f : -1.f );
     205             :     int16_t i;
     206             :     float inputBuffer[N_MAX];
     207             : 
     208             :     /* Init */
     209        1276 :     for ( i = 0; i < m / 2; i++ )
     210             :     {
     211        1250 :         inputBuffer[m / 2 + r / 2 + i] = -1.0f * x[l + m / 2 - 1 - i];
     212             :     }
     213             : 
     214         686 :     for ( i = 0; i < l / 2; i++ )
     215             :     {
     216         660 :         inputBuffer[m / 2 + r / 2 + m / 2 + i] = signLeft * x[i] - x[l - 1 - i];
     217             :     }
     218             : 
     219        1276 :     for ( i = 0; i < m / 2; i++ )
     220             :     {
     221        1250 :         inputBuffer[m / 2 + r / 2 - 1 - i] = -1.0f * x[l + m / 2 + i];
     222             :     }
     223             : 
     224         866 :     for ( i = 0; i < r / 2; i++ )
     225             :     {
     226         840 :         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          26 :     edxt( inputBuffer, y, l / 2 + m + r / 2, kernelType, FALSE );
     230             : 
     231          26 :     v_multc( y, sqrtf( (float) NORM_MDCT_FACTOR / ( l / 2 + m + r / 2 ) ), y, l / 2 + m + r / 2 );
     232             : 
     233          26 :     return;
     234             : }
     235             : 
     236             : 
     237      436065 : 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      436065 :     s = L_subframe;
     253      436065 :     nSubframes = ( tcxTransType == TCX_20 ? 1 : NB_DIV );
     254             : 
     255      436065 :     if ( kernelType == MDCT_IV ) /* no updates required here! */
     256             :     {
     257      435410 :         return;
     258             :     }
     259             : 
     260         655 :     if ( kernelType == MDST_IV ) /* swap real and imag. parts */
     261             :     {
     262      165416 :         for ( s = L_subframe - 1; s >= 0; s-- )
     263             :         {
     264      165120 :             const float sigTemp = -sigR[s];
     265             : 
     266      165120 :             sigR[s] = sigI[s];
     267      165120 :             sigI[s] = sigTemp;
     268             :         }
     269             : 
     270         296 :         return;
     271             :     }
     272             : 
     273             :     /* MDCT/MDST-II as real part, keep imag. part for speedup */
     274         359 :     if ( tcxTransType == TCX_20 && hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     275             :     {
     276         333 :         wtda_ext( tcxTimeSignal, windowedTimeSignal, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], s, kernelType );
     277             : 
     278         333 :         edxt( windowedTimeSignal, sigR, s, kernelType, FALSE );
     279             : 
     280         333 :         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          26 :         const float sign = ( kernelType >= MDCT_II ? 1.f : -1.f );
     285          26 :         const int16_t minWindowLen = hTcxCfg->tcx_mdct_window_min_lengthFB - 1;
     286          26 :         int16_t i, leftOverlap = 0, rightOverlap = 0;
     287             :         const float *left_win, *right_win;
     288             : 
     289          26 :         tcx_get_windows( hTcxCfg, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], &leftOverlap, &left_win, &rightOverlap, &right_win, 1 );
     290          26 :         if ( speech_TCX != NULL && tcxTransType != TCX_20 && (int16_t) windowedTimeSignal[0] == FULL_OVERLAP && s - leftOverlap > minWindowLen )
     291             :         {
     292         208 :             for ( i = minWindowLen; i >= 0; i-- ) /* outer left folding of shortened long ALDO slope */
     293             :             {
     294         200 :                 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          26 :         if ( tcxTransType == TCX_5 )
     299             :         {
     300             :             float tcx5Win[N_TCX10_MAX / 2 + L_MDCT_OVLP_MAX]; /* temporary buffer for TCX5 windowing */
     301             : 
     302          16 :             assert( L_subframe == nSubframes * hTcxCfg->tcx5SizeFB );
     303             : 
     304             :             /* Outer left folding */
     305         596 :             for ( i = 0; i < leftOverlap / 2; i++ )
     306             :             {
     307         580 :                 windowedTimeSignal[2 + leftOverlap / 2 + i] += sign * windowedTimeSignal[2 + leftOverlap / 2 - 1 - i];
     308             :             }
     309             : 
     310          16 :             s = hTcxCfg->tcx5SizeFB; /* obtain 1st TCX5 again */
     311          16 :             nSubframes *= 2;
     312          16 :             WindowSignal( hTcxCfg, leftOverlap / 2, RECTANGULAR_OVERLAP, MIN_OVERLAP, &leftOverlap, &rightOverlap, windowedTimeSignal + 2, &s, tcx5Win, 0, 1 );
     313          16 :             kernel_switch_trafo( tcx5Win, sigR, leftOverlap, s /* L_subfr. */ - ( leftOverlap + rightOverlap ) / 2, rightOverlap, kernelType );
     314             : 
     315          16 :             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          10 :             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         359 :     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         359 :     return;
     339             : }
     340             : 
     341             : 
     342      728411 : 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      728411 :     const float valI1 = *mdstSample1;
     352      728411 :     const float valI2 = *mdstSample2;
     353      728411 :     const float valR1 = *mdctSample1;
     354      728411 :     const float valR2 = *mdctSample2;
     355             :     float absR1, absR2;
     356             :     float dmxI1, dmxR1, dmxI2, dmxR2;
     357             :     float n, d;
     358             : 
     359      728411 :     absR1 = fabsf( valR1 );
     360      728411 :     absR2 = fabsf( valR2 );
     361      728411 :     dmxR1 = valR1 * factDe + sign * valR2 * factIn; /* cross */
     362      728411 :     dmxR2 = valR2 * factDe + sign * valR1 * factIn; /* -talk */
     363             : 
     364      728411 :     if ( fabsf( dmxR1 ) < absR1 + absR2 ) /* avoid destructive summations */
     365             :     {
     366        2091 :         if ( absR1 * factDe < absR2 * factIn )
     367             :         {
     368        1041 :             dmxR1 = valR2 * factIn - sign * valR1 * factDe;
     369        1041 :             dmxI1 = valI2 * factIn - sign * valI1 * factDe;
     370             :         }
     371             :         else
     372             :         {
     373        1050 :             dmxR1 = valR1 * factDe - sign * valR2 * factIn;
     374        1050 :             dmxI1 = valI1 * factDe - sign * valI2 * factIn;
     375             :         }
     376             :     }
     377             :     else
     378             :     {
     379      726320 :         dmxI1 = valI1 * factDe + sign * valI2 * factIn;
     380             :     }
     381             : 
     382      728411 :     if ( fabsf( dmxR2 ) < absR1 + absR2 ) /* avoid destructive summations */
     383             :     {
     384        1735 :         if ( absR1 * factIn < absR2 * factDe )
     385             :         {
     386         848 :             dmxR2 = valR2 * factDe - sign * valR1 * factIn;
     387         848 :             dmxI2 = valI2 * factDe - sign * valI1 * factIn;
     388             :         }
     389             :         else
     390             :         {
     391         887 :             dmxR2 = valR1 * factIn - sign * valR2 * factDe;
     392         887 :             dmxI2 = valI1 * factIn - sign * valI2 * factDe;
     393             :         }
     394             :     }
     395             :     else
     396             :     {
     397      726676 :         dmxI2 = valI2 * factDe + sign * valI1 * factIn;
     398             :     }
     399             : 
     400      728411 :     n = valR1 * valR1 + valI1 * valI1; /* maintain spectral magnitude (1) */
     401      728411 :     d = dmxR1 * dmxR1 + dmxI1 * dmxI1;
     402      728411 :     d = sqrtf( n / max( 1.f, d ) );
     403      728411 :     *mdctSample1 = dmxR1 * d;
     404      728411 :     *mdstSample1 = dmxI1 * d;
     405             : 
     406      728411 :     n = valR2 * valR2 + valI2 * valI2; /* maintain spectral magnitude (2) */
     407      728411 :     d = dmxR2 * dmxR2 + dmxI2 * dmxI2;
     408      728411 :     d = sqrtf( n / max( 1.f, d ) );
     409      728411 :     *mdctSample2 = dmxR2 * d;
     410      728411 :     *mdstSample2 = dmxI2 * d;
     411             : 
     412      728411 :     return;
     413             : }
     414             : 
     415             : 
     416             : /*--------------------------------------------------------------*
     417             :  * enc_ste_pre_mdct()
     418             :  *
     419             :  * encoder-side complex-valued stereo pre-processing (crosstalk)
     420             :  *---------------------------------------------------------------*/
     421             : 
     422       14781 : 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       14781 :     fadeInLen = ( nSamplesMax < 512 ? ( nSamplesMax < 256 ? 10 : 20 ) : 40 );            /* 1000Hz transition */
     441       14781 :     fadeInOff = ( ( bitRateMode * 3 ) >> 19 ) & ( nSamplesMax < 512 ? 0xFFFF : 0xFFFE ); /* offset */
     442       14781 :     nSampProc = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     443       14781 :     preproLen = nSampProc - fadeInOff;
     444       14781 :     maxSqrValue = 16777216.f * preproLen * preproLen;
     445       14781 :     stepWeightI = ( limitWeight > 0 ? 1.f : 2.f ); /* crosstalk weight */
     446       14781 :     stepWeightD = 4.f - stepWeightI;               /* decrement, i.e., 1 - crosstalk */
     447       14781 :     corr = 0.f, sumL = 0.f, sumR = 0.f;
     448       14781 :     sumMagnL = 0.f, sumMagnR = 0.f, sumPrdLR = 0.f, sumPrdLL = 0.f, sumPrdRR = 0.f;
     449             : 
     450       14781 :     if ( fadeInOff + fadeInLen + MIN_STE_PRE_LEN >= nSampProc )
     451             :     {
     452         608 :         return 0;
     453             :     }
     454             : 
     455     2782789 :     for ( s = fadeInOff; s < nSampProc; s++ )
     456             :     {
     457             :         float absMagnL, absMagnR;
     458     2768616 :         absMagnL = sqrtf( sigR0[s] * sigR0[s] + sigI0[s] * sigI0[s] );
     459     2768616 :         absMagnR = sqrtf( sigR1[s] * sigR1[s] + sigI1[s] * sigI1[s] );
     460             : 
     461     2768616 :         corr += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     462     2768616 :         sumL += sigR0[s] + sigI0[s];
     463     2768616 :         sumR += sigR1[s] + sigI1[s];
     464             : 
     465     2768616 :         sumMagnL += absMagnL;
     466     2768616 :         sumMagnR += absMagnR;
     467     2768616 :         sumPrdLR += absMagnL * absMagnR;
     468     2768616 :         sumPrdLL += absMagnL * absMagnL;
     469     2768616 :         sumPrdRR += absMagnR * absMagnR;
     470             :     }
     471       14173 :     corr *= 2.f * preproLen;
     472       14173 :     corr -= sumL * sumR;
     473       14173 :     chanCorrSign = ( corr < -maxSqrValue ) ? -1.f : 1.f;
     474             : 
     475       14173 :     sumL = sumMagnL / (float) preproLen; /* inter-channel correlation of magnitude */
     476       14173 :     sumR = sumMagnR / (float) preproLen;
     477       14173 :     corr = sumPrdLR + sumL * sumR * preproLen - sumMagnL * sumR - sumMagnR * sumL;
     478       14173 :     sumL = sumPrdLL + sumL * sumL * preproLen - sumMagnL * sumL - sumMagnL * sumL;
     479       14173 :     sumR = sumPrdRR + sumR * sumR * preproLen - sumMagnR * sumR - sumMagnR * sumR;
     480       14173 :     corr = ( ( corr <= 0.f ) || ( sumL * sumR <= 0.f ) ? 0.f : ( corr * corr ) / ( sumL * sumR ) );
     481             : 
     482       14173 :     if ( ( corr > 0.75f && corrIdxPrev == 0 ) || /* processing weight fade-in/-out */
     483       11562 :          ( corr <= 0.75f && corrIdxPrev > 0 ) )
     484             :     {
     485        1449 :         stepWeightI = 1.f;
     486        1449 :         stepWeightD = 4.f - stepWeightI;
     487             :     }
     488             : 
     489       14173 :     if ( corr <= 0.75f && corrIdxPrev == 0 ) /* bypass all processing, just return */
     490             :     {
     491       10966 :         return 0;
     492             :     }
     493             : 
     494        3207 :     sigR0 += fadeInOff + 1; /* stereo pre-processing starts with an offset of one! */
     495        3207 :     sigR1 += fadeInOff + 1;
     496        3207 :     sigI0 += fadeInOff + 1;
     497        3207 :     sigI1 += fadeInOff + 1;
     498        3207 :     xTalkI = stepWeightI;
     499        3207 :     xTalkD = stepWeightD * ( 2 * fadeInLen - 1 );
     500             : 
     501      112400 :     for ( s = fadeInLen - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     502             :     {
     503      109193 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     504      109193 :         xTalkI += stepWeightI;
     505      109193 :         xTalkD -= stepWeightD;
     506             :     }
     507             : 
     508      518545 :     for ( s = preproLen - fadeInLen; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     509             :     {
     510      515338 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     511             :     }
     512             : 
     513        3207 :     if ( nSampProc < nSamplesMax ) /* fade-out of processing at start of IGF range */
     514             :     {
     515      106800 :         for ( s = min( fadeInLen, nSamplesMax - nSampProc ) - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     516             :         {
     517      103880 :             xTalkI -= stepWeightI;
     518      103880 :             xTalkD += stepWeightD;
     519      103880 :             applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     520             :         }
     521             :     }
     522             : 
     523        3207 :     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      220330 : 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      220330 :     nbits_start = hBstr->nb_bits_tot;
     541             : 
     542      220330 :     if ( st->core == TCX_20_CORE )
     543             :     {
     544      214740 :         st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     545             : 
     546      214740 :         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        5590 :         IGFEncWriteConcatenatedBitstream( st->hIGFEnc, hBstr );
     551             :     }
     552             : 
     553      220330 :     total_nbbits = hBstr->nb_bits_tot - nbits_start;
     554      220330 :     st->side_bits_frame_channel += total_nbbits;
     555             : 
     556      220330 :     return;
     557             : }
     558             : 
     559             : 
     560             : /*-------------------------------------------------------------------*
     561             :  * ivas_mdct_core_whitening_enc()
     562             :  *
     563             :  * MCT preprocessing up to whitening the core spectrum
     564             :  *-------------------------------------------------------------------*/
     565             : 
     566      229569 : 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      229569 :     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      229569 :     push_wmops( "mdct_core_whitening" );
     607             : 
     608             :     /*--------------------------------------------------------------*
     609             :      * Initialization
     610             :      *---------------------------------------------------------------*/
     611             : 
     612      229569 :     sts = hCPE->hCoreCoder;
     613             : 
     614      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     615             :     {
     616      459138 :         stereo_tcx_init_enc( sts[ch] );
     617             : 
     618      459138 :         set_s( tnsSize[ch], 0, 2 );
     619      459138 :         set_s( tnsBits[ch], 0, 2 );
     620      459138 :         ltpBits[ch] = 0;
     621             : 
     622     1836552 :         for ( i = 0; i < 3; i++ )
     623             :         {
     624     1377414 :             T_op[ch][i] = sts[ch]->pitch[i];
     625             : 
     626             :             /* check minimum pitch for quantization */
     627     1377414 :             if ( T_op[ch][i] < PIT_MIN_SHORTER )
     628             :             {
     629      228590 :                 T_op[ch][i] *= 2;
     630             :             }
     631             :         }
     632             :     }
     633             : 
     634      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     635             :     {
     636      459138 :         orig_spectrum[ch][0] = orig_spectrum_long[ch];
     637      459138 :         orig_spectrum[ch][1] = orig_spectrum_long[ch] + N_TCX10_MAX;
     638      459138 :         mdst_spectrum[ch][0] = mdst_spectrum_long[ch];
     639      459138 :         mdst_spectrum[ch][1] = mdst_spectrum_long[ch] + N_TCX10_MAX;
     640             :     }
     641             : 
     642      229569 :     windowedSignal[0] = orig_spectrum_long[0]; /* NOTE temporarily available */
     643      229569 :     windowedSignal[1] = temp_buffer;           /* orig_spectrum_long isn't long enough */
     644             : 
     645             :     /*--------------------------------------------------------------*
     646             :      * TCX20/TCX10 switching decision
     647             :      *---------------------------------------------------------------*/
     648             : 
     649      229569 :     if ( mct_on )
     650             :     {
     651      182718 :         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      182718 :         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      548154 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     654             :         {
     655      365436 :             st = sts[ch];
     656      365436 :             SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     657             :         }
     658             :     }
     659             : 
     660      229569 :     sts[0]->core = sts[0]->hTcxEnc->tcxMode;
     661      229569 :     sts[1]->core = sts[1]->hTcxEnc->tcxMode;
     662             : 
     663             :     /*--------------------------------------------------------------*
     664             :      * Core Signal Analysis: MDCT, TNS
     665             :      *---------------------------------------------------------------*/
     666             : 
     667      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     668             :     {
     669      459138 :         st = sts[ch];
     670      459138 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     671             :         {
     672       32569 :             continue;
     673             :         }
     674      426569 :         SetCurrentPsychParams( st->core, 0, st->hTcxCfg );
     675             : 
     676             :         /* tcx ltp analysis on the 12.8kHz weighted speech, saves preproc resampling to sr_core */
     677      426569 :         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      426569 :         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      426569 :         if ( st->hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     683             :         {
     684      425826 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     685             : 
     686      861148 :             for ( n = 0; n < nSubframes; n++ )
     687             :             {
     688      435322 :                 bw_detect( st, NULL, st->hTcxEnc->spectrum[n], NULL, MC_FORMAT /*just cannot be ISM_FORMAT*/, mct_on );
     689             : 
     690      435322 :                 if ( nSubframes == NB_DIV && n == 0 )
     691             :                 {
     692        9496 :                     st->last_input_bwidth = st->input_bwidth;
     693             :                 }
     694             :             }
     695             :         }
     696             : 
     697      426569 :         if ( st->last_core == ACELP_CORE ) /* reset past kernel info */
     698             :         {
     699         743 :             st->hTcxEnc->kernel_switch_corr_past = 0.f;
     700         743 :             st->hTcxEnc->kernel_symmetry_past = 0;
     701             :         }
     702             :     }
     703             : 
     704             :     /*--------------------------------------------------------------*
     705             :      * Transform Kernel Switching, Stereo Pre-Processing, and TNS
     706             :      *---------------------------------------------------------------*/
     707             : 
     708      229569 :     sts[0]->hTcxEnc->fUseTns[1] = 0;
     709             : 
     710      229569 :     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       14054 :     {
     712             :         int16_t nSampCore;
     713             :         int32_t totalRate;
     714             :         TCX_ENC_HANDLE hTcxEnc0, hTcxEnc1;
     715             : 
     716       14054 :         nSampCore = ( sts[0]->igf ) ? max( sts[0]->hIGFEnc->infoStartLine, sts[1]->hIGFEnc->infoStartLine ) : 0;
     717       14054 :         totalRate = sts[0]->element_brate;
     718       14054 :         hTcxEnc0 = sts[0]->hTcxEnc;
     719       14054 :         hTcxEnc1 = sts[1]->hTcxEnc;
     720             : 
     721       14054 :         init_tcx_enc_info( sts[0], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     722             : 
     723       14054 :         if ( nSampCore == 0 )
     724             :         {
     725        3583 :             nSampCore = tcx_subframe_coded_lines;
     726             :         }
     727             : 
     728       14054 :         nrg = 0.25f * ( hTcxEnc0->tcxltp_norm_corr_past + hTcxEnc0->tcxltp_norm_corr_mem +  /* tcxltp_norm_corr_past already contains the */
     729       14054 :                         hTcxEnc1->tcxltp_norm_corr_past + hTcxEnc1->tcxltp_norm_corr_mem ); /* normalized correlation of the current frame */
     730             : 
     731       14054 :         L_subframe = max( 512, L_subframe );
     732       14054 :         nSubframes = ( hTcxEnc0->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     733             : 
     734       28575 :         for ( n = 0; n < nSubframes; n++ )
     735             :         {
     736       14521 :             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       14515 :                     kernel_switch_detect( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     740       14515 :                                           L_subframeTCX / nSubframes, hTcxEnc0->transform_type[n], &hTcxEnc0->kernel_switch_corr_past, ( totalRate * L_subframe ) / nSubframes );
     741       14515 :                 if ( switchKernel ) /* apply MDST-IV coding in one of the channels */
     742             :                 {
     743         474 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 1 ) - max( 0, switchKernel );
     744         474 :                     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       14041 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 2 : 0 );
     749       14041 :                     hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 2 : 0 );
     750             :                 }
     751             :             }
     752             :             else
     753             :             {
     754           6 :                 hTcxEnc0->kernel_switch_corr_past = 0.f; /* don't update the kernel switching state, postpone it to when data is available */
     755           6 :                 hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 0 );
     756           6 :                 hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 3 : 0 );
     757             :             }
     758       14521 :             hTcxEnc0->kernel_symmetry_past = hTcxEnc0->kernel_type[n] & 1;
     759       14521 :             hTcxEnc1->kernel_symmetry_past = hTcxEnc1->kernel_type[n] & 1;
     760             : 
     761       14521 :             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       29042 :                                              hTcxEnc0->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc0->speech_TCX ), windowedSignal[0] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     763       14521 :             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       29042 :                                              hTcxEnc1->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc1->speech_TCX ), windowedSignal[1] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     765             :         }
     766       28575 :         for ( n = 0; n < nSubframes; n++ )
     767             :         {
     768       14521 :             if ( hTcxEnc0->transform_type[n] == hTcxEnc1->transform_type[n] && /* do not combine TCX-5 and TCX-10 as this causes artifacts */
     769       14515 :                  sts[0]->hTcxCfg->tcx_curr_overlap_mode == sts[1]->hTcxCfg->tcx_curr_overlap_mode &&
     770       14401 :                  sts[0]->hTcxCfg->tcx_last_overlap_mode == sts[1]->hTcxCfg->tcx_last_overlap_mode )
     771             :             {
     772       14305 :                 if ( hTcxEnc0->transform_type[n] == TCX_5 )
     773             :                 {
     774         476 :                     const int16_t tcx5SizeFB = sts[1]->hTcxCfg->tcx5SizeFB;
     775             : 
     776         476 :                     hTcxEnc0->enc_ste_pre_corr_past =
     777         476 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / ( 2 * NB_DIV ),
     778         476 :                                           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         476 :                     hTcxEnc0->enc_ste_pre_corr_past =
     781         476 :                         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         476 :                                           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       13829 :                     hTcxEnc0->enc_ste_pre_corr_past =
     787       13829 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     788       13829 :                                           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         216 :                 hTcxEnc0->enc_ste_pre_corr_past = 0;
     794             :             }
     795             :         }
     796             :     }
     797             :     else
     798             :     {
     799      215515 :         sts[0]->hTcxEnc->enc_ste_pre_corr_past = 0;
     800      215515 :         sts[0]->hTcxEnc->kernel_switch_corr_past = 0.f;
     801             : 
     802      646545 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     803             :         {
     804      431030 :             TCX_ENC_HANDLE hTcxEncCh = sts[ch]->hTcxEnc;
     805             : 
     806      431030 :             if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     807             :             {
     808       32569 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[0] = 0;
     809             : 
     810       32569 :                 continue;
     811             :             }
     812             : 
     813      398461 :             init_tcx_enc_info( sts[ch], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     814      398461 :             nSubframes = ( hTcxEncCh->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     815             : 
     816      805484 :             for ( n = 0; n < nSubframes; n++ )
     817             :             {
     818      407023 :                 hTcxEncCh->kernel_type[n] = ( hTcxEncCh->kernel_symmetry_past && sts[ch]->element_mode == IVAS_CPE_MDCT ? 3 - mct_on : 0 );
     819      407023 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[n] & 1;
     820             : 
     821      407023 :                 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      814046 :                                                  hTcxEncCh->new_speech_TCX, ( n /*1*/ ? NULL : hTcxEncCh->speech_TCX ), windowedSignal[ch] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     823             :             }
     824             :         }
     825             :     }
     826             : 
     827      229569 :     TNSAnalysisStereo( sts, mdst_spectrum, 0, tnsSize, tnsBits, param_core, mct_on );
     828             : 
     829             :     /*--------------------------------------------------------------*
     830             :      * Envelope Quantization and FDNS
     831             :      *---------------------------------------------------------------*/
     832             : 
     833      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     834             :     {
     835      459138 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     836             :         {
     837       32569 :             continue;
     838             :         }
     839             : 
     840      426569 :         st = sts[ch];
     841      426569 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     842             : 
     843      426569 :         if ( mct_on )
     844             :         {
     845      332867 :             set_zero( chE, NB_DIV );
     846             :         }
     847             : 
     848      426569 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     849             : 
     850      426569 :         L_subframe = L_subframe / nSubframes;
     851      426569 :         L_subframeTCX = ( mct_on ? L_subframeTCX / nSubframes : L_subframe );
     852      426569 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     853             : 
     854      862634 :         for ( n = 0; n < nSubframes; n++ )
     855             :         {
     856      436065 :             if ( st->hTcxEnc->fUseTns[n] )
     857             :             {
     858     6047769 :                 for ( i = 0; i < L_subframeTCX; i++ )
     859             :                 {
     860     6036256 :                     powerSpec[i] = ( st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i] );
     861             :                 }
     862             :             }
     863             :             else
     864             :             {
     865   328559912 :                 for ( i = 0; i < L_subframeTCX; i++ )
     866             :                 {
     867   328135360 :                     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      436065 :             if ( mct_on )
     872             :             {
     873      339876 :                 chE[n] = sum_f( powerSpec, L_subframeTCX );
     874             :             }
     875             : 
     876      436065 :             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      426569 :         if ( mct_on )
     881             :         {
     882      332867 :             chE_tot = sum_f( chE, NB_DIV );
     883             : 
     884      332867 :             if ( chE_tot < SILENT_CHANNEL_THRES && nSubframes == 1 )
     885             :             {
     886        7335 :                 st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     887        7335 :                 st->bits_frame_channel = 0;
     888             :             }
     889             :             else
     890             :             {
     891      325532 :                 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      229569 :     if ( hCPE->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
     898             :     {
     899         525 :         sns_low_br_mode = !sts[0]->sp_aud_decision0;
     900             :     }
     901             :     else
     902             :     {
     903      229044 :         sns_low_br_mode = 0;
     904             :     }
     905             : 
     906      229569 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
     907             :     {
     908       23317 :         quantize_sns( scf, scf_q, sts, sns_vq_indices, zero_side_flag, sns_stereo_mode );
     909             :     }
     910             :     else
     911             :     {
     912      206252 :         if ( sts[0]->hTcxEnc->tcxMode == TCX_20 && sts[1]->hTcxEnc->tcxMode == TCX_20 &&
     913      200131 :              sts[0]->mct_chan_mode == MCT_CHAN_MODE_REGULAR && sts[1]->mct_chan_mode == MCT_CHAN_MODE_REGULAR )
     914             :         {
     915      164018 :             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      126702 :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     928             :             {
     929       84468 :                 param_lpc[ch][0] = ch;
     930       84468 :                 if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     931             :                 {
     932       39904 :                     continue;
     933             :                 }
     934       44564 :                 st = sts[ch];
     935             : 
     936       44564 :                 if ( st->hTcxEnc->tcxMode == TCX_20 )
     937             :                 {
     938       36262 :                     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        8302 :                     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      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     949             :     {
     950      459138 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     951             :         {
     952       39904 :             continue;
     953             :         }
     954      419234 :         st = sts[ch];
     955      419234 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     956      419234 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     957             : 
     958      419234 :         L_subframe = L_subframe / nSubframes;
     959      419234 :         L_subframeTCX = L_subframeTCX / nSubframes;
     960      419234 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     961             : 
     962      847964 :         for ( n = 0; n < nSubframes; n++ )
     963             :         {
     964      428730 :             mvr2r( st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], L_subframeTCX );
     965             : 
     966             :             /* Shape spectrum */
     967      428730 :             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      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     977             :     {
     978      459138 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     979             :         {
     980       39904 :             continue;
     981             :         }
     982      419234 :         st = sts[ch];
     983      419234 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     984             : 
     985      847964 :         for ( n = 0; n < nSubframes; n++ )
     986             :         {
     987      428730 :             if ( st->hTcxEnc->transform_type[n] == TCX_5 )
     988             :             {
     989        9794 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, st->hTcxEnc->spectrum[n] );
     990        9794 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, mdst_spectrum[ch][n] );
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995      229569 :     TNSAnalysisStereo( sts, mdst_spectrum, 1, tnsSize, tnsBits, param_core, mct_on );
     996             : 
     997      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     998             :     {
     999      459138 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1000             :         {
    1001       39904 :             continue;
    1002             :         }
    1003             : 
    1004      419234 :         st = sts[ch];
    1005      419234 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1006             : 
    1007      419234 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
    1008             : 
    1009      419234 :         L_subframe = L_subframe / nSubframes;
    1010      419234 :         L_subframeTCX = L_subframeTCX / nSubframes;
    1011      419234 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
    1012             : 
    1013      847964 :         for ( n = 0; n < nSubframes; n++ )
    1014             :         {
    1015      428730 :             if ( !st->hTcxEnc->fUseTns[n] )
    1016             :             {
    1017      384459 :                 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      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1022             :     {
    1023             :         /*no need to write last channel bit in case of odd channels*/
    1024      459138 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
    1025             :         {
    1026       32569 :             continue;
    1027             :         }
    1028             : 
    1029      426569 :         st = sts[ch];
    1030             : 
    1031      426569 :         if ( mct_on ) /* signal bits should be written only for MCT*/
    1032             :         {
    1033      332867 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1034             :             {
    1035        7335 :                 push_next_indice( hBstr, 1, 1 );
    1036             :             }
    1037             :             else
    1038             :             {
    1039      325532 :                 assert( st->mct_chan_mode == MCT_CHAN_MODE_REGULAR );
    1040      325532 :                 push_next_indice( hBstr, 0, 1 );
    1041             :             }
    1042             :         }
    1043             :     }
    1044             : 
    1045             :     /* write bitstream with info up to here */
    1046      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1047             :     {
    1048      459138 :         st = sts[ch];
    1049             : 
    1050      459138 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1051             :         {
    1052       39904 :             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       39904 :             *p_param[ch] = 1 + NOISE_FILL_RANGES + LTPSIZE + tnsSize[ch][0] + NPRM_CTX_HM;
    1055       39904 :             continue;
    1056             :         }
    1057             : 
    1058      419234 :         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      419234 :         if ( ch > 0 && sts[0]->hTcxEnc->fUseTns[0] + sts[0]->hTcxEnc->fUseTns[1] > 0 && !mct_on )
    1061             :         {
    1062        5000 :             if ( st->hTcxEnc->tnsData[0].nFilters < 0 )
    1063             :             {
    1064        1211 :                 tnsBits[ch][0] = 1;
    1065             :             }
    1066             :             else
    1067             :             {
    1068        3789 :                 tnsBits[ch][0]++;
    1069             :             }
    1070             : 
    1071        5000 :             if ( st->core == TCX_10_CORE )
    1072             :             {
    1073         907 :                 if ( st->hTcxEnc->tnsData[1].nFilters < 0 )
    1074             :                 {
    1075         100 :                     tnsBits[ch][1] = 1;
    1076             :                 }
    1077             :                 else
    1078             :                 {
    1079         807 :                     tnsBits[ch][1]++;
    1080             :                 }
    1081             :             }
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*--------------------------------------------------------------------------------*
    1086             :      * SNS parameters
    1087             :      *--------------------------------------------------------------------------------*/
    1088             : 
    1089      229569 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
    1090             :     {
    1091       23317 :         idx = 0;
    1092             : 
    1093       23317 :         if ( sts[0]->core == sts[1]->core )
    1094             :         {
    1095       23143 :             nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1096             : 
    1097             :             /* push all stereo mode bits first */
    1098       46796 :             for ( n = 0; n < nSubframes; ++n )
    1099             :             {
    1100       23653 :                 push_next_indice( hBstr, sns_stereo_mode[n], 1 );
    1101       23653 :                 sts[0]->side_bits_frame_channel++;
    1102             :             }
    1103             : 
    1104             :             /* zero side flags only get transmitted if needed */
    1105       46796 :             for ( n = 0; n < nSubframes; ++n )
    1106             :             {
    1107       23653 :                 if ( sns_stereo_mode[n] == SNS_STEREO_MODE_MS )
    1108             :                 {
    1109       21591 :                     push_next_indice( hBstr, zero_side_flag[n], 1 );
    1110       21591 :                     sts[0]->side_bits_frame_channel++;
    1111             :                 }
    1112             :             }
    1113             :         }
    1114             : 
    1115       69951 :         for ( ch = 0; ch < CPE_CHANNELS; ++ch )
    1116             :         {
    1117       46634 :             st = sts[ch];
    1118       46634 :             nbits_start_sns = hBstr->nb_bits_tot;
    1119       46634 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1120       94462 :             for ( n = 0; n < nSubframes; ++n )
    1121             :             {
    1122       47828 :                 const int16_t is_side = ch == 1 && sns_stereo_mode[n] == SNS_STEREO_MODE_MS;
    1123       47828 :                 const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
    1124       47828 :                 int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
    1125             : 
    1126       47828 :                 if ( is_side )
    1127             :                 {
    1128       21591 :                     if ( zero_side_flag[n] )
    1129             :                     {
    1130        4187 :                         continue;
    1131             :                     }
    1132       17404 :                     nStages = SNS_MSVQ_NSTAGES_SIDE;
    1133       17404 :                     bits = ( sts[ch]->core == TCX_20_CORE ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
    1134             :                 }
    1135      181921 :                 for ( int16_t j = 0; j < nStages; ++j, ++idx )
    1136             :                 {
    1137      138280 :                     push_next_indice( hBstr, sns_vq_indices[idx], bits[j] );
    1138             :                 }
    1139             :             }
    1140       46634 :             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      206252 :         skipped_first_channel = 0;
    1147      618756 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1148             :         {
    1149             : #ifdef DEBUG_PLOT_BITS
    1150             :             int16_t tmp = hBstr->nb_bits_tot;
    1151             : #endif
    1152      412504 :             st = sts[ch];
    1153             : 
    1154      412504 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1155             :             {
    1156       39904 :                 skipped_first_channel = 1;
    1157       39904 :                 continue;
    1158             :             }
    1159             : 
    1160      372600 :             nbits_start_sns = hBstr->nb_bits_tot;
    1161             : 
    1162      372600 :             num_sns = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1163             : 
    1164      372600 :             if ( ch == 0 || skipped_first_channel )
    1165             :             {
    1166      203267 :                 push_next_indice( hBstr, param_lpc[0][0] >> 1, 1 );
    1167             : 
    1168      203267 :                 if ( st->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
    1169             :                 {
    1170             :                     /* write classifier decision to signal low br mode for SNS encoding, for all other configs, low_br mode is not possible */
    1171          72 :                     push_next_indice( hBstr, sns_low_br_mode, 1 );
    1172             :                 }
    1173             :             }
    1174      372600 :             encode_lpc_avq( hBstr, num_sns, param_lpc[ch], st->core, st->element_mode );
    1175             : 
    1176             : #ifdef DEBUG_PLOT_BITS
    1177             :             tmp = hBstr->nb_bits_tot - tmp;
    1178             :             dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_SNS" );
    1179             : #endif
    1180             : 
    1181      372600 :             st->side_bits_frame_channel += hBstr->nb_bits_tot - nbits_start_sns;
    1182             :         }
    1183             :     }
    1184             : 
    1185             : 
    1186             :     /*update pitch buffer*/
    1187      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1188             :     {
    1189      459138 :         st = sts[ch];
    1190      459138 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1191             :         {
    1192       39904 :             continue;
    1193             :         }
    1194      419234 :         if ( param_core[ch][1 + NOISE_FILL_RANGES] != 0 )
    1195             :         {
    1196      305503 :             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 );
    1197             :         }
    1198             :         else
    1199             :         {
    1200      113731 :             set_f( pitch_buf[ch], L_SUBFR, NB_SUBFR16k );
    1201             :         }
    1202             :     }
    1203             : 
    1204      229569 :     pop_wmops();
    1205      229569 :     return;
    1206             : }
    1207             : 
    1208             : 
    1209             : /*--------------------------------------------------------------*
    1210             :  * ivas_mdct_quant_coder()
    1211             :  *
    1212             :  * Spectrum quantization and coding
    1213             :  *---------------------------------------------------------------*/
    1214             : 
    1215      229569 : void ivas_mdct_quant_coder(
    1216             :     CPE_ENC_HANDLE hCPE,                   /* i/o: Encoder CPE handle                   */
    1217             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV], /* i  : bits needed for TNS parameters       */
    1218             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV], /* i  : size of TNS                          */
    1219             :     int16_t p_param[CPE_CHANNELS][NB_DIV], /* i  : pointer to parameter array           */
    1220             :     const int16_t MCT_flag                 /* i  : hMCT handle allocated (1) or not (0) */
    1221             : )
    1222             : {
    1223             :     Encoder_State *st, **sts;
    1224             :     int16_t bitsAvailable, target_bits, nSubframes, ch, n;
    1225             :     int16_t L_frameTCX[CPE_CHANNELS][NB_DIV];           /* full frame length */
    1226             :     int16_t L_frame[CPE_CHANNELS][NB_DIV];              /* frame length */
    1227             :     int16_t L_spec[CPE_CHANNELS][NB_DIV];               /* length of the coded spectrum */
    1228             :     int16_t tcx_offset[CPE_CHANNELS][NB_DIV];           /* folding point offset relative to the end of the previous frame */
    1229             :     int16_t noiseFillingBorder[CPE_CHANNELS][NB_DIV];   /* noise filling border */
    1230             :     float fac_ns[CPE_CHANNELS][NB_DIV];                 /* noise filling level */
    1231             :     int16_t nf_seed[CPE_CHANNELS][NB_DIV];              /* noise filling random seed */
    1232             :     int16_t hm_active[CPE_CHANNELS][NB_DIV];            /* flag indicating if the harmonic model is active */
    1233             :     float ener[CPE_CHANNELS][NB_DIV];                   /* energy of the quantized spectrum */
    1234             :     float gain_tcx[CPE_CHANNELS][NB_DIV];               /* global gain */
    1235             :     float quantized_spectrum_long[CPE_CHANNELS][N_MAX]; /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
    1236             :     float *quantized_spectrum[CPE_CHANNELS][NB_DIV];
    1237             :     int16_t param_core[CPE_CHANNELS][2 * NPRM_DIV];
    1238             :     int16_t ignore_chan[CPE_CHANNELS];
    1239             :     int16_t target_bitsTCX10[CPE_CHANNELS][NB_DIV];
    1240             :     int16_t nbits_start, total_nbbits;
    1241             : 
    1242      229569 :     push_wmops( "mdct_core_Q" );
    1243      229569 :     sts = hCPE->hCoreCoder;
    1244             : 
    1245      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1246             :     {
    1247      459138 :         set_f( fac_ns[ch], 0.0f, NB_DIV );
    1248      459138 :         set_s( nf_seed[ch], 0, NB_DIV );
    1249      459138 :         set_s( hm_active[ch], 0, NB_DIV );
    1250      459138 :         set_f( ener[ch], 0.0f, NB_DIV );
    1251      459138 :         set_f( gain_tcx[ch], 0.0f, NB_DIV );
    1252             :     }
    1253             : 
    1254      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1255             :     {
    1256      459138 :         quantized_spectrum[ch][0] = quantized_spectrum_long[ch];
    1257      459138 :         quantized_spectrum[ch][1] = quantized_spectrum_long[ch] + N_TCX10_MAX;
    1258             : 
    1259      459138 :         st = sts[ch];
    1260             : 
    1261      459138 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1262             :         {
    1263       39828 :             ignore_chan[ch] = 1;
    1264       39828 :             continue;
    1265             :         }
    1266             : 
    1267      419310 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1268      419310 :         ignore_chan[ch] = 0;
    1269             : 
    1270      419310 :         bitsAvailable = st->bits_frame_channel + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ) + tnsBits[ch][0] + tnsBits[ch][1];
    1271             : 
    1272             :         /* calculate TCX10 target bits before to assure minimum amount is distributed between subframes */
    1273      419310 :         if ( st->hTcxEnc->tcxMode == TCX_10 )
    1274             :         {
    1275             :             int16_t nTnsBitsTCX10Tmp[2];
    1276        9496 :             nTnsBitsTCX10Tmp[0] = tnsBits[ch][0];
    1277        9496 :             nTnsBitsTCX10Tmp[1] = tnsBits[ch][1];
    1278             : 
    1279        9496 :             ivas_mdct_tcx10_bit_distribution( target_bitsTCX10[ch], bitsAvailable, nTnsBitsTCX10Tmp );
    1280             :         }
    1281             : #ifdef DEBUG_PLOT_BITS
    1282             :         if ( st->hTcxEnc->tcxMode == TCX_20 )
    1283             :         {
    1284             :             set_s( &target_bitsTCX10[ch][0], 0, NB_DIV );
    1285             :         }
    1286             :         for ( n = 0; n < NB_DIV; n++ )
    1287             :         {
    1288             :             dbgwrite( &target_bitsTCX10[ch][n], sizeof( int16_t ), 1, 1, "./res/bits_tarrget_TCX10" );
    1289             :         }
    1290             : #endif
    1291             : 
    1292      848116 :         for ( n = 0; n < nSubframes; n++ )
    1293             :         {
    1294      428806 :             if ( nSubframes == 2 )
    1295             :             {
    1296       18992 :                 target_bits = target_bitsTCX10[ch][n];
    1297             :             }
    1298             :             else
    1299             :             {
    1300      409814 :                 target_bits = bitsAvailable / nSubframes - tnsBits[ch][n];
    1301             :             }
    1302             : 
    1303      428806 :             assert( target_bits >= NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL + SMDCT_MINIMUM_ARITH_BITS );
    1304             : 
    1305             :             /*-----------------------------------------------------------*
    1306             :              * Quantize the MDCT spectrum                                *
    1307             :              *-----------------------------------------------------------*/
    1308             : 
    1309      428806 :             QuantizeTCXSpectrum( st, n, st->hTcxEnc->spectrum[n], NULL, NULL, tnsSize[ch][n], target_bits, 0, &L_frameTCX[ch][n],
    1310      428806 :                                  &L_frame[ch][n], &L_spec[ch][n], &tcx_offset[ch][n], &noiseFillingBorder[ch][n], quantized_spectrum[ch][n], NULL,
    1311      428806 :                                  &hm_active[ch][n], NULL, &nf_seed[ch][n], &ener[ch][n], &gain_tcx[ch][n], param_core[ch] + n * NPRM_DIV );
    1312             :         }
    1313             :     }
    1314             : 
    1315      229569 :     EstimateStereoTCXNoiseLevel( sts, quantized_spectrum, gain_tcx, L_frame, noiseFillingBorder, hm_active, ignore_chan, fac_ns, param_core, MCT_flag );
    1316             : 
    1317      688707 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1318             :     {
    1319      459138 :         st = sts[ch];
    1320             : 
    1321             :         /* update the pointer to the buffer of indices of the second channel */
    1322      459138 :         if ( ch > 0 )
    1323             :         {
    1324      229569 :             st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
    1325             :         }
    1326      459138 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1327             :         {
    1328             :             /*Enable appropriate upadte of tcx_curr_overlap_mode even for uncoded channel index 1*/
    1329       39828 :             L_frameTCX[ch][0] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1330       39828 :             L_frameTCX[ch][1] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1331             : 
    1332       39828 :             L_frame[ch][0] = st->L_frame;
    1333       39828 :             L_frame[ch][1] = st->L_frame;
    1334             :         }
    1335      459138 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1336             : 
    1337      927776 :         for ( n = 0; n < nSubframes; n++ )
    1338             :         {
    1339             :             /* Update L_frame_past */
    1340      468638 :             st->L_frame_past = L_frame[ch][n];
    1341             : 
    1342             :             /* Update overlap */
    1343      468638 :             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 ) )
    1344             :             {
    1345        7137 :                 st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
    1346             :             }
    1347             :         }
    1348             : 
    1349             :         /*--------------------------------------------------------------*
    1350             :          * Generate Bitstream
    1351             :          *---------------------------------------------------------------*/
    1352             : 
    1353      459138 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1354             :         {
    1355       39828 :             continue;
    1356             :         }
    1357      419310 :         nbits_start = st->hBstr->nb_bits_tot;
    1358             : 
    1359      419310 :         writeTCXparam( st, st->hBstr, NULL, param_core[ch], 0, 0, 0, NULL, p_param[ch], target_bitsTCX10[ch], 1 );
    1360             : 
    1361      419310 :         total_nbbits = st->hBstr->nb_bits_tot - nbits_start - nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
    1362             : 
    1363      419310 :         assert( st->bits_frame_channel == total_nbbits );
    1364             :     }
    1365             : 
    1366      229569 :     pop_wmops();
    1367      229569 :     return;
    1368             : }
    1369             : 
    1370             : 
    1371             : /*--------------------------------------------------------------*
    1372             :  * init_tcx_enc_info()
    1373             :  *
    1374             :  * Initialize TCX parameters
    1375             :  *---------------------------------------------------------------*/
    1376             : 
    1377     1677552 : void init_tcx_enc_info(
    1378             :     Encoder_State *st, /* i/o: coder memory state      */
    1379             :     int16_t *L_frame,
    1380             :     int16_t *L_frameTCX,
    1381             :     int16_t *L_spec )
    1382             : {
    1383     1677552 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
    1384     1677552 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1385             :     int16_t tcx_offset, tcx_offsetFB;
    1386             : 
    1387             :     /* Init lengths */
    1388     1677552 :     tcx_offset = hTcxCfg->tcx_offset;
    1389     1677552 :     tcx_offsetFB = hTcxCfg->tcx_offsetFB;
    1390     1677552 :     *L_frame = st->L_frame;
    1391     1677552 :     *L_frameTCX = hTcxEnc->L_frameTCX;
    1392     1677552 :     *L_spec = st->hTcxCfg->tcx_coded_lines;
    1393             : 
    1394     1677552 :     if ( st->last_core == ACELP_CORE )
    1395             :     {
    1396             :         /* if past frame is ACELP */
    1397        2775 :         *L_frame += tcx_offset;
    1398        2775 :         *L_frameTCX += tcx_offsetFB;
    1399        2775 :         *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
    1400             : 
    1401        2775 :         assert( hTcxCfg->lfacNext <= 0 );
    1402        2775 :         *L_frame -= hTcxCfg->lfacNext;
    1403        2775 :         *L_frameTCX -= hTcxCfg->lfacNextFB;
    1404             :     }
    1405             : 
    1406     1677552 :     return;
    1407             : }

Generated by: LCOV version 1.14