LCOV - code coverage report
Current view: top level - lib_enc - ivas_mct_core_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 198 200 99.0 %
Date: 2025-05-23 08:37:30 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "prot.h"
      38             : #include "ivas_prot.h"
      39             : #include "rom_com.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "wmc_auto.h"
      44             : 
      45             : 
      46             : /*----------------------------------------------------------*
      47             :  * FindChannelRatio()
      48             :  *
      49             :  * find ratio of channel energy to total energy for
      50             :  * bit distribution
      51             :  *----------------------------------------------------------*/
      52             : 
      53      100939 : static void FindChannelRatio(
      54             :     Encoder_State **sts,                   /* i/o: encoder state structure          */
      55             :     int16_t chBitRatios[MCT_MAX_CHANNELS], /* o  : bit-disctribution channel ratios */
      56             :     const int16_t nChannels                /* i  : number of channels to be coded   */
      57             : )
      58             : {
      59             :     float sum_nrg, chRatio;
      60             :     int16_t i;
      61             :     float nrg[MCT_MAX_CHANNELS];
      62             : 
      63      100939 :     set_f( nrg, 0, MCT_MAX_CHANNELS );
      64             : 
      65      100939 :     getChannelEnergies( sts, nrg, nChannels );
      66             : 
      67             :     /*calculate total energy without LFE*/
      68      100939 :     sum_nrg = 0;
      69      582726 :     for ( i = 0; i < nChannels; i++ )
      70             :     {
      71      481787 :         if ( sts[i]->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
      72             :         {
      73      473222 :             sum_nrg += nrg[i];
      74             :         }
      75             :     }
      76      100939 :     sum_nrg = 1.0f / max( sum_nrg, EPSILON );
      77             : 
      78      582726 :     for ( i = 0; i < nChannels; i++ )
      79             :     {
      80      481787 :         if ( sts[i]->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
      81             :         {
      82      473222 :             chRatio = nrg[i] * sum_nrg;
      83      473222 :             chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * chRatio + 0.5f ) ) );
      84             :         }
      85        8565 :         else if ( sts[i]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
      86             :         {
      87        8565 :             chBitRatios[i] = 0;
      88             :         }
      89             :     }
      90             : 
      91             : #ifdef DEBUG_MODE_MDCT
      92             :     for ( i = 0; i < nChannels; i++ )
      93             :     {
      94             :         dbgwrite( &chBitRatios[i], sizeof( int16_t ), 1, 1, "./res/chBitRatio" );
      95             :         /* dbgwrite(&chRatio[i], sizeof(float),1,1,"./res/chRatio");*/
      96             :     }
      97             : #endif
      98             : 
      99      100939 :     return;
     100             : }
     101             : 
     102             : 
     103             : /*----------------------------------------------------------*
     104             :  * AdjustChannelRatios()
     105             :  *
     106             :  * adjust ratio of channels for bit distribution
     107             :  *----------------------------------------------------------*/
     108             : 
     109       24650 : static void AdjustChannelRatios(
     110             :     int16_t chBitRatios[MCT_MAX_CHANNELS], /* o  : bit-disctribution channel ratios */
     111             :     const int16_t nChannels,               /* i/o: number of channels               */
     112             :     const int32_t ivas_total_brate,        /* i  : IVAS total bitrate               */
     113             :     const int16_t nAvailBits,              /* i  : number of available bits         */
     114             :     const int16_t sba_order                /* i  : Ambisonic (SBA) order            */
     115             : )
     116             : {
     117             :     int16_t force_ch_bit_ratios[IVAS_SPAR_MAX_DMX_CHS];
     118             :     int32_t temp_brs[IVAS_SPAR_MAX_DMX_CHS];
     119             :     float cur_ratio, tar_ratio, sum_ratio, sum_tar_ratio;
     120             :     int16_t ratio_diff, i;
     121             : 
     122       24650 :     ivas_spar_bitrate_dist( temp_brs, nAvailBits, ivas_total_brate, sba_order, (int16_t) FB );
     123             : 
     124       24650 :     sum_ratio = 0.0f;
     125      123250 :     for ( i = 0; i < nChannels; i++ )
     126             :     {
     127       98600 :         sum_ratio += (float) temp_brs[i];
     128             :     }
     129      123250 :     for ( i = 0; i < nChannels; i++ )
     130             :     {
     131       98600 :         cur_ratio = temp_brs[i] / sum_ratio;
     132       98600 :         force_ch_bit_ratios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * cur_ratio + 0.5f ) ) );
     133             :     }
     134             : 
     135             :     /* adjust the ratios further based on received chBitRatios[]*/
     136       24650 :     ratio_diff = 0;
     137       24650 :     sum_ratio = 0.0f;
     138       24650 :     sum_tar_ratio = 0.0f;
     139      123250 :     for ( i = 0; i < nChannels; i++ )
     140             :     {
     141       98600 :         sum_ratio += (float) chBitRatios[i];
     142       98600 :         sum_tar_ratio += (float) force_ch_bit_ratios[i];
     143             :     }
     144       49300 :     for ( i = 3; i < nChannels; i++ )
     145             :     {
     146       24650 :         cur_ratio = (float) chBitRatios[i] / ( sum_ratio + FLT_MIN );
     147       24650 :         tar_ratio = (float) force_ch_bit_ratios[i] / ( sum_tar_ratio + FLT_MIN );
     148             : 
     149       24650 :         tar_ratio = min( tar_ratio, cur_ratio );
     150       24650 :         chBitRatios[i] = (int16_t) ( tar_ratio * sum_tar_ratio );
     151       24650 :         chBitRatios[i] = max( 1, chBitRatios[i] );
     152       24650 :         assert( chBitRatios[i] < ( BITRATE_MCT_RATIO_RANGE - 1 ) );
     153       24650 :         ratio_diff += force_ch_bit_ratios[i] - chBitRatios[i];
     154             :     }
     155             : 
     156       98600 :     for ( i = 0; i < min( 3, nChannels ); i++ )
     157             :     {
     158       73950 :         assert( force_ch_bit_ratios[i] >= 0 );
     159       73950 :         chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, force_ch_bit_ratios[i] );
     160             :     }
     161       24650 :     chBitRatios[1] += ratio_diff;
     162             : 
     163             :     /* make sure final ratios are within range*/
     164       24650 :     sum_ratio = 0.0f;
     165      123250 :     for ( i = 0; i < nChannels; i++ )
     166             :     {
     167       98600 :         sum_ratio += (float) chBitRatios[i];
     168             :     }
     169      123250 :     for ( i = 0; i < nChannels; i++ )
     170             :     {
     171       98600 :         cur_ratio = chBitRatios[i] / sum_ratio;
     172       98600 :         chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * cur_ratio + 0.5f ) ) );
     173             :     }
     174             : 
     175       24650 :     return;
     176             : }
     177             : 
     178             : 
     179             : /*-------------------------------------------------------------------*
     180             :  * ivas_mct_core_enc()
     181             :  *
     182             :  * joint mct encoding
     183             :  *-------------------------------------------------------------------*/
     184             : 
     185      100939 : void ivas_mct_core_enc(
     186             :     const IVAS_FORMAT ivas_format,       /* i  : IVAS format                     */
     187             :     MCT_ENC_HANDLE hMCT,                 /* i/o: MCT encoder structure           */
     188             :     CPE_ENC_HANDLE hCPE[MCT_MAX_BLOCKS], /* i/o: CPE encoder structures          */
     189             :     const int16_t nChannels,             /* i  : number of channels to be coded  */
     190             :     const int32_t ivas_total_brate,      /* i  : IVAS total bitrate              */
     191             :     const int16_t switch_bw,             /* i  : flag bandwidth switch occurance */
     192             :     const int16_t lfe_bits,              /* i  : bits spent for LFE              */
     193             :     const int16_t sba_order              /* i  : Ambisonic (SBA) order           */
     194             : )
     195             : {
     196             :     int16_t ch, ch_core, nSubframes, L_subframeTCX;
     197             :     int16_t i, cpe_id, n, nAvailBits;
     198             :     int16_t nCPE;
     199             :     float *orig_spectrum[MCT_MAX_CHANNELS][2]; /* Pointers to MDCT output for a short block (L/R) */
     200             :     float powerSpecMsInv_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
     201             :     float inv_spectrum_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
     202             :     float powerSpec_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
     203             :     float powerSpec_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k];
     204             :     float inv_spectrum_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k];   /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
     205             :     float powerSpecMsInv_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k]; /* MS inv power spectrum, also inverse MDST spectrum */
     206             :     float *powerSpec[MCT_MAX_CHANNELS];
     207             :     float *powerSpecMsInv[MCT_MAX_CHANNELS][2];
     208             :     float *inv_mdst_spectrum[MCT_MAX_CHANNELS][2];
     209             :     float *inv_spectrum[MCT_MAX_CHANNELS][2];
     210             :     float *mdst_spectrum[MCT_MAX_CHANNELS][2];
     211             :     int16_t total_side_bits;
     212             :     int16_t chBitRatios[MCT_MAX_CHANNELS];
     213             :     Encoder_State *sts[MCT_MAX_CHANNELS];
     214             :     Encoder_State *st;
     215             :     int16_t sp_aud_decision0[MCT_MAX_CHANNELS];
     216             :     BSTR_ENC_HANDLE hBstr;
     217             :     float mdst;
     218             : #ifdef DEBUGGING
     219             :     int32_t total_brate = 0;
     220             :     int16_t sba_meta = 0;
     221             :     int16_t format_bits = 0;
     222             :     int16_t mct_bits = 0;
     223             : #endif
     224             : 
     225             : #if defined( DEBUG_MODE_MDCT ) && defined( DEBUG_PLOT_BITS )
     226             :     static FILE *f_bit_split = 0;
     227             : #endif
     228             : 
     229      100939 :     push_wmops( "mct_encoding" );
     230             : 
     231             :     /*--------------------------------------------------------------*
     232             :      * Initialization
     233             :      *---------------------------------------------------------------*/
     234             : 
     235      100939 :     total_side_bits = 0;
     236             : 
     237      100939 :     nCPE = nChannels / CPE_CHANNELS;
     238             : 
     239             :     /*in case of odd number of channels*/
     240      100939 :     if ( ( nCPE * CPE_CHANNELS ) != nChannels )
     241             :     {
     242       58829 :         nCPE++;
     243             :     }
     244             : 
     245             :     /* point first CPE channels to longer buffers where switching from ACELP to TCX may occur in SBA with DTX (total memory saving)*/
     246      302817 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     247             :     {
     248      201878 :         inv_mdst_spectrum[ch][0] = powerSpecMsInv[ch][0] = powerSpecMsInv_long_cpe0[ch];
     249      201878 :         inv_mdst_spectrum[ch][1] = powerSpecMsInv[ch][1] = powerSpecMsInv_long_cpe0[ch] + N_TCX10_MAX;
     250      201878 :         inv_spectrum[ch][0] = inv_spectrum_long_cpe0[ch];
     251      201878 :         inv_spectrum[ch][1] = inv_spectrum_long_cpe0[ch] + N_TCX10_MAX;
     252      201878 :         powerSpec[ch] = powerSpec_long_cpe0[ch];
     253             :     }
     254             : 
     255      380848 :     for ( ch = CPE_CHANNELS; ch < nChannels; ch++ )
     256             :     {
     257      279909 :         inv_mdst_spectrum[ch][0] = powerSpecMsInv[ch][0] = powerSpecMsInv_long[ch - CPE_CHANNELS];
     258      279909 :         inv_mdst_spectrum[ch][1] = powerSpecMsInv[ch][1] = powerSpecMsInv_long[ch - CPE_CHANNELS] + N_TCX10_MAX;
     259      279909 :         inv_spectrum[ch][0] = inv_spectrum_long[ch - CPE_CHANNELS];
     260      279909 :         inv_spectrum[ch][1] = inv_spectrum_long[ch - CPE_CHANNELS] + N_TCX10_MAX;
     261      279909 :         powerSpec[ch] = powerSpec_long[ch - CPE_CHANNELS];
     262             :     }
     263             : 
     264      371247 :     for ( cpe_id = 0, i = 0; cpe_id < nCPE; cpe_id++ )
     265             :     {
     266      810924 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     267             :         {
     268      540616 :             if ( cpe_id * CPE_CHANNELS + ch < nChannels )
     269             :             {
     270      481787 :                 sts[i] = hCPE[cpe_id]->hCoreCoder[ch];
     271             :             }
     272             : 
     273      540616 :             if ( hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     274             :             {
     275       67394 :                 i++;
     276       67394 :                 continue;
     277             :             }
     278             : 
     279      473222 :             mdst_spectrum[i][0] = hMCT->p_mdst_spectrum_long[cpe_id][ch];
     280      473222 :             mdst_spectrum[i][1] = hMCT->p_mdst_spectrum_long[cpe_id][ch] + N_TCX10_MAX;
     281      473222 :             orig_spectrum[i][0] = hMCT->p_orig_spectrum_long[cpe_id][ch];
     282      473222 :             orig_spectrum[i][1] = hMCT->p_orig_spectrum_long[cpe_id][ch] + N_TCX10_MAX;
     283             : 
     284      473222 :             sp_aud_decision0[i] = hCPE[cpe_id]->hCoreCoder[0]->sp_aud_decision0;
     285             : 
     286      473222 :             sts[i]->hTcxEnc->tns_ms_flag[0] = 0;
     287      473222 :             sts[i]->hTcxEnc->tns_ms_flag[1] = 0;
     288             : 
     289      473222 :             i++;
     290             :         }
     291             :     }
     292             : 
     293      100939 :     hBstr = sts[0]->hBstr;
     294             : 
     295      312418 :     for ( ch = 0; ch < (int16_t) ( hMCT->nchan_out_woLFE * 0.5 ); ch++ )
     296             :     {
     297      211479 :         ch_core = ch * CPE_CHANNELS;
     298             : 
     299      211479 :         if ( switch_bw )
     300             :         {
     301             :             H_IGF_GRID igf_grid;
     302             : 
     303         401 :             igf_grid = sts[ch_core]->igf ? sts[ch_core]->hIGFEnc->igfData.igfInfo.grid : NULL;
     304         401 :             initMdctStereoEncData( hMCT->hBlockData[ch]->hStereoMdct, ivas_format, sts[ch_core]->element_mode, sts[ch_core]->element_brate, sts[ch_core]->bwidth, sts[ch_core]->igf, igf_grid, 0 );
     305             :         }
     306             : 
     307      211479 :         if ( sts[ch_core]->igf )
     308             :         {
     309             :             /* calculate the igf start band from the igf start line */
     310      102679 :             stereo_mdct_init_igf_start_band( &hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20, 1.0f, sts[ch_core]->bwidth, sts[ch_core]->total_brate );
     311             : 
     312      102679 :             stereo_mdct_init_igf_start_band( &hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10, 0.5f, sts[ch_core]->bwidth, sts[ch_core]->total_brate );
     313             :         }
     314             :         else
     315             :         {
     316      108800 :             hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.sfbIgfStart = -1;
     317      108800 :             hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.sfbIgfStart = -1;
     318      108800 :             hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.nBandsStereoCore = hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.sfbCnt;
     319      108800 :             hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.nBandsStereoCore = hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.sfbCnt;
     320             :         }
     321             :     }
     322             : 
     323      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     324             :     {
     325      481787 :         st = sts[ch];
     326             : 
     327      481787 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     328             :         {
     329        8565 :             continue;
     330             :         }
     331             : 
     332      473222 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     333             : 
     334      957090 :         for ( n = 0; n < nSubframes; n++ )
     335             :         {
     336      483868 :             st->side_bits_frame_channel += NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
     337             :         }
     338             : 
     339             :         /* Add estimated stereo bits */
     340      473222 :         st->side_bits_frame_channel += NBBITS_MCT_RATIO;
     341             :     }
     342             : 
     343             :     /*--------------------------------------------------------------*
     344             :      * MCT algorithm
     345             :      *---------------------------------------------------------------*/
     346             : 
     347      100939 :     apply_MCT_enc( hMCT, sts, mdst_spectrum, inv_spectrum, inv_mdst_spectrum, nChannels );
     348             : 
     349             :     /*--------------------------------------------------------------*
     350             :      * Power spectrum calculation for Noise Estimation
     351             :      *---------------------------------------------------------------*/
     352             : 
     353      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     354             :     {
     355      481787 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     356             :         {
     357        8565 :             continue;
     358             :         }
     359      473222 :         nSubframes = ( sts[ch]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     360      473222 :         L_subframeTCX = sts[ch]->hTcxEnc->L_frameTCX / nSubframes;
     361             :         /* in MCT only relevant for bitrate switching from non-MCT bitrates */
     362      473222 :         if ( sts[ch]->last_core == ACELP_CORE )
     363             :         {
     364           0 :             L_subframeTCX += L_subframeTCX / 4;
     365             :         }
     366             : 
     367      957090 :         for ( n = 0; n < nSubframes; n++ )
     368             :         {
     369      483868 :             if ( sts[ch]->hTcxEnc->tns_ms_flag[n] )
     370             :             {
     371             :                 /* power spectrum: MDCT^2 + MDST^2 */
     372   139014247 :                 for ( i = 0; i < L_subframeTCX; i++ )
     373             :                 {
     374   138859520 :                     powerSpec[ch][i + n * L_subframeTCX] = mdst_spectrum[ch][n][i] * mdst_spectrum[ch][n][i] + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
     375   138859520 :                     powerSpecMsInv[ch][n][i] = inv_mdst_spectrum[ch][n][i] * inv_mdst_spectrum[ch][n][i] + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
     376             :                 }
     377             :             }
     378             :             else
     379             :             {
     380      329141 :                 if ( hMCT->currBlockDataCnt > 0 )
     381             :                 {
     382             :                     /* power spectrum: MDCT^2 + MDST^2 */
     383      143731 :                     powerSpecMsInv[ch][n][0] = inv_spectrum[ch][n][0] * inv_spectrum[ch][n][0];
     384             : 
     385   122014989 :                     for ( i = 1; i < L_subframeTCX - 1; i++ )
     386             :                     {
     387   121871258 :                         mdst = ( inv_spectrum[ch][n][i + 1] - inv_spectrum[ch][n][i - 1] ); /* An MDST estimate */
     388   121871258 :                         powerSpecMsInv[ch][n][i] = mdst * mdst + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
     389             :                     }
     390             : 
     391      143731 :                     powerSpecMsInv[ch][n][L_subframeTCX - 1] = inv_spectrum[ch][n][L_subframeTCX - 1] * inv_spectrum[ch][n][L_subframeTCX - 1];
     392             :                 }
     393             : 
     394             :                 /* power spectrum: MDCT^2 + MDST^2 */
     395      329141 :                 powerSpec[ch][n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][0] * sts[ch]->hTcxEnc->spectrum[n][0];
     396             : 
     397   270995659 :                 for ( i = 1; i < L_subframeTCX - 1; i++ )
     398             :                 {
     399   270666518 :                     mdst = ( sts[ch]->hTcxEnc->spectrum[n][i + 1] - sts[ch]->hTcxEnc->spectrum[n][i - 1] ); /* An MDST estimate */
     400   270666518 :                     powerSpec[ch][i + n * L_subframeTCX] = mdst * mdst + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
     401             :                 }
     402      329141 :                 powerSpec[ch][L_subframeTCX - 1 + n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1] * sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1];
     403             :             }
     404             :         }
     405             :     }
     406             : 
     407      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     408             :     {
     409      481787 :         st = sts[ch];
     410      481787 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     411             :         {
     412        8565 :             continue;
     413             :         }
     414      473222 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     415      473222 :         L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
     416             :         /* in MCT only relevant for bitrate switching from non-MCT bitrates */
     417      473222 :         if ( st->last_core == ACELP_CORE )
     418             :         {
     419           0 :             L_subframeTCX += L_subframeTCX / 4;
     420             :         }
     421             : 
     422      473222 :         if ( ( st->hTcxEnc->tcxMode == TCX_20 ) && ( st->total_brate < HQ_96k || st->igf ) )
     423             :         {
     424             :             /*-----------------------------------------------------------*
     425             :              * Compute noise-measure flags for spectrum filling          *
     426             :              * and quantization (0: tonal, 1: noise-like).               *
     427             :              * Detect low pass if present.                               *
     428             :              *-----------------------------------------------------------*/
     429             : 
     430      244819 :             ComputeSpectrumNoiseMeasure( powerSpec[ch], st->hTcxEnc->L_frameTCX, st->hTcxEnc->nmStartLine, ( st->L_frame * st->last_sr_core != st->L_frame_past * st->sr_core ) || ( st->last_core != TCX_20_CORE ), st->hTcxEnc->memQuantZeros, st->hTcxEnc->L_frameTCX );
     431             :         }
     432             : 
     433      473222 :         st->hTcxEnc->measuredBwRatio = 1.f; /* No bandwidth limit for the noise filling */
     434             :     }
     435             : 
     436      100939 :     if ( sts[0]->igf )
     437             :     {
     438       54884 :         if ( hMCT->currBlockDataCnt > 0 )
     439             :         {
     440       35766 :             mctStereoIGF_enc( hMCT, sts, orig_spectrum, powerSpec, powerSpecMsInv, inv_spectrum, sp_aud_decision0 );
     441             :         }
     442             :         else
     443             :         {
     444       92153 :             for ( ch = 0; ch < nChannels; ch++ )
     445             :             {
     446       73035 :                 st = sts[ch];
     447             : 
     448             :                 /* update the pointer to the buffer of indices of the second channel */
     449       73035 :                 if ( ch > 0 )
     450             :                 {
     451       53917 :                     st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
     452             :                 }
     453       73035 :                 if (
     454       73035 :                     sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     455             :                 {
     456        2690 :                     continue;
     457             :                 }
     458       70345 :                 nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     459       70345 :                 L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
     460             : 
     461       70345 :                 if ( st->igf )
     462             :                 {
     463      142573 :                     for ( n = 0; n < nSubframes; n++ )
     464             :                     {
     465       72228 :                         ProcessIGF( st, st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], &powerSpec[ch][n * L_subframeTCX], st->core == TCX_20_CORE, n, sp_aud_decision0[ch], 0 );
     466             :                     }
     467             :                 }
     468             :             }
     469             :         }
     470             : 
     471             :         /*write IGF data to bitstream*/
     472      302766 :         for ( ch = 0; ch < nChannels; ch++ )
     473             :         {
     474      247882 :             st = sts[ch];
     475      247882 :             if (
     476      247882 :                 sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     477             :             {
     478        3943 :                 continue;
     479             :             }
     480      243939 :             enc_prm_igf_mdct( st, hBstr );
     481             :         }
     482             :     }
     483             : 
     484             :     /* correct side bits per channel*/
     485      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     486             :     {
     487      481787 :         st = sts[ch];
     488             : 
     489      481787 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     490             :         {
     491        8565 :             continue;
     492             :         }
     493             : 
     494      473222 :         st->side_bits_frame_channel -= NBBITS_MCT_RATIO; /* Subtract estimated stereo bits */
     495      473222 :         total_side_bits += st->side_bits_frame_channel;
     496             :     }
     497             : 
     498             :     /*--------------------------------------------------------------*
     499             :      * Bitrate distribution among channels and MCT bitstream writing
     500             :      *---------------------------------------------------------------*/
     501             : 
     502      100939 :     write_mct_bitstream( sts, hMCT, nChannels );
     503             : 
     504      100939 :     FindChannelRatio( sts, chBitRatios, nChannels );
     505             : 
     506      100939 :     nAvailBits = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - NBITS_BWIDTH - hMCT->nBitsMCT - lfe_bits );
     507             : 
     508             : 
     509             :     /* subtract IVAS format signaling bits */
     510      100939 :     if ( ivas_format == MC_FORMAT )
     511             :     {
     512       27030 :         nAvailBits -= IVAS_FORMAT_SIGNALING_NBITS;
     513       27030 :         nAvailBits -= MC_LS_SETUP_BITS;
     514             :     }
     515       73909 :     else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
     516             :     {
     517       73909 :         nAvailBits -= IVAS_FORMAT_SIGNALING_NBITS_EXTENDED;
     518       73909 :         nAvailBits -= SBA_ORDER_BITS + SBA_PLANAR_BITS;
     519             : 
     520             :         /*MCT is used at bitrates > 80 kbps and additional 1 bit is present at these bitrates*/
     521       73909 :         if ( ivas_format == SBA_ISM_FORMAT )
     522             :         {
     523       23670 :             nAvailBits -= IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
     524             :         }
     525             :     }
     526             : 
     527      371247 :     for ( cpe_id = 0, i = 0; cpe_id < nCPE; cpe_id++ )
     528             :     {
     529      270308 :         if ( hCPE[cpe_id]->hMetaData != NULL )
     530             :         {
     531      100939 :             nAvailBits -= hCPE[cpe_id]->hMetaData->nb_bits_tot;
     532             :         }
     533             :     }
     534             : 
     535             :     /*substract bits needed for the bitrate ratios */
     536      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     537             :     {
     538      481787 :         if (
     539      481787 :             sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     540             :         {
     541        8565 :             continue;
     542             :         }
     543      473222 :         nAvailBits -= NBBITS_MCT_RATIO;
     544             :     }
     545      100939 :     nAvailBits -= total_side_bits + hMCT->nchan_out_woLFE; /* if MC 1 extra bit that was initially send to signal LFE_off  */
     546             : 
     547             : #ifdef DEBUG_MODE_MDCT
     548             :     dbgwrite( &nAvailBits, sizeof( int16_t ), 1, 1, "./res/availBits" );
     549             : #endif
     550             : 
     551      100939 :     if ( hMCT->hbr_mct )
     552             :     {
     553       24650 :         assert( ivas_total_brate >= IVAS_256k );
     554       24650 :         AdjustChannelRatios( chBitRatios, nChannels, ivas_total_brate, nAvailBits, sba_order );
     555             :     }
     556             : 
     557      582726 :     for ( ch = 0; ch < nChannels; ch++ )
     558             :     {
     559      481787 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     560             :         {
     561        8565 :             continue;
     562             :         }
     563             : 
     564      473222 :         push_next_indice( hBstr, chBitRatios[ch], NBBITS_MCT_RATIO );
     565             : #ifdef DEBUGGING
     566             :         mct_bits += NBBITS_MCT_RATIO;
     567             : #endif
     568             :     }
     569             : 
     570             :     /*distribute bits amongst channels*/
     571      100939 :     splitAvailableBitsMCT( (void **) sts, nAvailBits, chBitRatios, ENC, nChannels );
     572             : 
     573      371247 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     574             :     {
     575      810924 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     576             :         {
     577      540616 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     578      540616 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     579             :             {
     580       67249 :                 continue;
     581             :             }
     582      473367 :             st->total_brate = ( st->bits_frame_channel + st->side_bits_frame_channel ) * FRAMES_PER_SEC;
     583             : 
     584             : #ifdef DEBUGGING
     585             :             total_brate += st->total_brate;
     586             : #endif
     587             :         }
     588             : #ifdef DEBUGGING
     589             :         if ( hCPE[cpe_id]->hMetaData != NULL )
     590             :         {
     591             :             sba_meta += hCPE[cpe_id]->hMetaData->nb_bits_tot;
     592             :         }
     593             : #endif
     594             :     }
     595             : 
     596             : #ifdef DEBUGGING
     597             :     format_bits = ( ivas_format == MC_FORMAT ? IVAS_FORMAT_SIGNALING_NBITS + MC_LS_SETUP_BITS : IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + SBA_ORDER_BITS + SBA_PLANAR_BITS );
     598             : 
     599             :     format_bits += ( ivas_format == SBA_ISM_FORMAT );
     600             : 
     601             :     mct_bits += hMCT->nBitsMCT + hMCT->nchan_out_woLFE;
     602             :     assert( ( total_brate + ( NBITS_BWIDTH + format_bits + mct_bits + sba_meta + lfe_bits ) * FRAMES_PER_SEC ) == ivas_total_brate );
     603             : #endif
     604             : 
     605      100939 :     pop_wmops();
     606             : 
     607      100939 :     return;
     608             : }

Generated by: LCOV version 1.14