LCOV - code coverage report
Current view: top level - lib_enc - ari_hm_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 187 187 100.0 %
Date: 2025-05-23 08:37:30 Functions: 9 9 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             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include <math.h>
      41             : #include "cnst.h"
      42             : #include "rom_enc.h"
      43             : #include "stl.h"
      44             : #include "basop_util.h"
      45             : #include "prot.h"
      46             : #include "rom_com.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * EncodeIndex()
      51             :  *
      52             :  *
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55       19810 : int16_t EncodeIndex(
      56             :     const int16_t Bandwidth,
      57             :     int16_t PeriodicityIndex,
      58             :     BSTR_ENC_HANDLE hBstr )
      59             : {
      60       19810 :     if ( PeriodicityIndex & kLtpHmFlag )
      61             :     {
      62       10940 :         int16_t LtpPitchIndex = PeriodicityIndex >> 9;
      63       10940 :         assert( 0 <= LtpPitchIndex && LtpPitchIndex <= 16 );
      64       10940 :         --PeriodicityIndex;
      65       10940 :         assert( ( PeriodicityIndex & 0xff ) < ( 1 << NumRatioBits[Bandwidth][LtpPitchIndex] ) );
      66             : 
      67       10940 :         push_next_indice( hBstr, PeriodicityIndex & 0xff, NumRatioBits[Bandwidth][LtpPitchIndex] );
      68       10940 :         return NumRatioBits[Bandwidth][LtpPitchIndex];
      69             :     }
      70             :     else
      71             :     {
      72        8870 :         push_next_indice( hBstr, PeriodicityIndex, 8 );
      73        8870 :         return 8;
      74             :     }
      75             : }
      76             : 
      77             : 
      78             : /*-------------------------------------------------------------------*
      79             :  * GetWeight()
      80             :  *
      81             :  *
      82             :  *-------------------------------------------------------------------*/
      83             : 
      84   735522356 : static float GetWeight( int16_t i )
      85             : {
      86   735522356 :     i = 3 * i - 2;
      87             : 
      88   735522356 :     return (float) ( pow( i, 0.3 ) / pow( 256 - 1, 0.3 ) );
      89             : }
      90             : 
      91             : 
      92             : /*-------------------------------------------------------------------*
      93             :  * SearchPeriodicityIndex_Single()
      94             :  *
      95             :  *
      96             :  *-------------------------------------------------------------------*/
      97             : 
      98    17398178 : static float SearchPeriodicityIndex_Single(
      99             :     const float AbsMdct3[],
     100             :     const int16_t NumToConsider,
     101             :     const int32_t Lag,
     102             :     const int16_t FractionalResolution )
     103             : {
     104             :     int16_t HighestMultiplier;
     105             :     float AbsMeanCurrent3; /* Mean for BucketWidth == 3 */
     106             :     int32_t Limit, OldIndex, i;
     107             : 
     108    17398178 :     Limit = ( NumToConsider - 1 ) << FractionalResolution;
     109    17398178 :     AbsMeanCurrent3 = 0;
     110    17398178 :     HighestMultiplier = 1;
     111             : 
     112   752920534 :     for ( i = Lag; i < Limit; i += Lag )
     113             :     {
     114   735522356 :         OldIndex = i >> FractionalResolution;
     115   735522356 :         AbsMeanCurrent3 += AbsMdct3[OldIndex] * GetWeight( HighestMultiplier );
     116   735522356 :         ++HighestMultiplier;
     117             :     }
     118             : 
     119    17398178 :     return AbsMeanCurrent3 / ( HighestMultiplier - 1 + 0.00001f );
     120             : }
     121             : 
     122             : 
     123             : /*-------------------------------------------------------------------*
     124             :  * SearchPeriodicityIndex_Range()
     125             :  *
     126             :  *
     127             :  *-------------------------------------------------------------------*/
     128             : 
     129      735361 : static void SearchPeriodicityIndex_Range(
     130             :     const float AbsMdct3[],
     131             :     const int16_t NumToConsider,
     132             :     const int16_t Lo,
     133             :     const int16_t Hi,
     134             :     const int16_t FractionalResolution,
     135             :     const int16_t Adj,
     136             :     const int16_t Spacing,
     137             :     int16_t *PeriodicityIndex,
     138             :     float *Score )
     139             : {
     140             :     int16_t Index, BestIndex, B;
     141             :     float CurrentScore, BestScore;
     142             : 
     143      735361 :     BestScore = -1e30f;
     144      735361 :     BestIndex = 0;
     145             : 
     146    15534729 :     for ( Index = Lo; Index < Hi; Index += Spacing )
     147             :     {
     148    14799368 :         CurrentScore = SearchPeriodicityIndex_Single( AbsMdct3, NumToConsider, Index + Adj, FractionalResolution );
     149             : 
     150    14799368 :         if ( CurrentScore > BestScore )
     151             :         {
     152     2334613 :             BestScore = CurrentScore;
     153     2334613 :             BestIndex = Index;
     154             :         }
     155             :     }
     156             : 
     157      735361 :     if ( BestScore > *Score )
     158             :     {
     159      194153 :         *Score = BestScore;
     160      194153 :         *PeriodicityIndex = BestIndex;
     161             :     }
     162             : 
     163             : 
     164      735361 :     B = BestIndex - ( Spacing >> 1 );
     165      735361 :     B = max( Lo, B );
     166             : 
     167     1392985 :     for ( Index = B; Index < BestIndex; ++Index )
     168             :     {
     169      657624 :         CurrentScore = SearchPeriodicityIndex_Single( AbsMdct3, NumToConsider, Index + Adj, FractionalResolution );
     170             : 
     171      657624 :         if ( CurrentScore > *Score )
     172             :         {
     173       43857 :             *Score = CurrentScore;
     174       43857 :             *PeriodicityIndex = Index;
     175             :         }
     176             :     }
     177             : 
     178      735361 :     B = BestIndex + ( Spacing >> 1 );
     179             : 
     180     1623493 :     for ( Index = BestIndex + 1; Index <= B; ++Index )
     181             :     {
     182      888132 :         CurrentScore = SearchPeriodicityIndex_Single( AbsMdct3, NumToConsider, Index + Adj, FractionalResolution );
     183             : 
     184      888132 :         if ( CurrentScore > *Score )
     185             :         {
     186       96982 :             *Score = CurrentScore;
     187       96982 :             *PeriodicityIndex = Index;
     188             :         }
     189             :     }
     190             : 
     191      735361 :     return;
     192             : }
     193             : 
     194             : 
     195             : /*-------------------------------------------------------------------*
     196             :  * SearchPeriodicityIndex()
     197             :  *
     198             :  *
     199             :  *-------------------------------------------------------------------*/
     200             : 
     201             : /*! r: PeriodicityIndex */
     202      194385 : int16_t SearchPeriodicityIndex(
     203             :     const float Mdct[],           /* i  : Coefficients, Mdct[0..NumCoeffs-1]                      */
     204             :     const float UnfilteredMdct[], /* i  : Unfiltered coefficients, UnfilteredMdct[0..NumCoeffs-1] */
     205             :     const int16_t NumCoeffs,      /* i  : Number of coefficients                                  */
     206             :     const int16_t TargetBits,     /* i  : Target bit budget (excl. Done flag)                     */
     207             :     const int16_t LtpPitchLag,    /* i  : TCX-LTP pitch                                           */
     208             :     const float LtpGain,          /* i  : LTP gain                                                */
     209             :     float *RelativeScore          /* o  : Energy concentration factor                             */
     210             : )
     211             : {
     212      194385 :     float AbsMdct3[MAX_LENGTH], A, B, C = 0.f;
     213             :     int16_t i;
     214             :     int16_t MaxAt;
     215             :     float Score;
     216             :     int16_t PeriodicityIndex;
     217             :     int16_t NumToConsider;
     218             :     float AbsTotal;
     219             : 
     220      194385 :     PeriodicityIndex = 0;
     221      194385 :     NumToConsider = NumCoeffs;
     222      194385 :     Score = -1e30f;
     223             : 
     224      194385 :     A = (float) fabs( Mdct[0] );
     225      194385 :     B = (float) fabs( Mdct[1] );
     226             : 
     227    53450135 :     for ( i = 1; i < NumToConsider - 3; i += 3 )
     228             :     {
     229    53255750 :         C = (float) fabs( Mdct[i + 1] );
     230    53255750 :         AbsMdct3[i] = A + B + C;
     231             : 
     232    53255750 :         A = (float) fabs( Mdct[i + 2] );
     233    53255750 :         AbsMdct3[i + 1] = A + B + C;
     234             : 
     235    53255750 :         B = (float) fabs( Mdct[i + 3] );
     236    53255750 :         AbsMdct3[i + 2] = A + B + C;
     237             :     }
     238             : 
     239      194385 :     if ( i < NumToConsider - 1 )
     240             :     {
     241      186335 :         C = (float) fabs( Mdct[i + 1] );
     242      186335 :         AbsMdct3[i] = A + B + C;
     243             :     }
     244             : 
     245             : 
     246      194385 :     if ( i + 1 < NumToConsider - 1 )
     247             :     {
     248       61485 :         A = (float) fabs( Mdct[i + 2] );
     249       61485 :         AbsMdct3[i + 1] = A + B + C;
     250             :     }
     251             : 
     252      194385 :     AbsTotal = 0.0f;
     253             : 
     254      194385 :     if ( UnfilteredMdct != NULL )
     255             :     {
     256     8365610 :         for ( i = 0; i < NumToConsider; ++i )
     257             :         {
     258     8352000 :             AbsTotal += (float) fabs( UnfilteredMdct[i] );
     259             :         }
     260             :     }
     261             :     else
     262             :     {
     263    50843770 :         for ( i = 1; i < NumToConsider - 1; i += 3 )
     264             :         {
     265    50662995 :             AbsTotal += AbsMdct3[i];
     266             :         }
     267             :     }
     268             : 
     269             : 
     270      194385 :     if ( ( LtpPitchLag > 0 ) && ( LtpGain > kLtpHmGainThr ) )
     271       46363 :     {
     272       46363 :         int16_t FractionalResolution = kLtpHmFractionalResolution;
     273             :         int16_t Multiplier, LtpPitchIndex, Bandwidth;
     274             : 
     275       46363 :         Bandwidth = NumCoeffs >= 256;
     276       46363 :         LtpPitchIndex = ( ( LtpPitchLag + ( 1 << ( kLtpHmFractionalResolution - 1 ) ) ) >> kLtpHmFractionalResolution ) - 2;
     277       46363 :         assert( 0 <= LtpPitchIndex && LtpPitchIndex <= 16 );
     278             : 
     279     1107131 :         for ( Multiplier = 1; Multiplier <= ( 1 << NumRatioBits[Bandwidth][LtpPitchIndex] ); ++Multiplier )
     280             :         {
     281             :             float CurrentScore;
     282             :             int32_t Lag;
     283             : 
     284     1060768 :             Lag = ( LtpPitchLag * (int16_t) ( 4 * Ratios[Bandwidth][LtpPitchIndex][Multiplier - 1] ) ) >> 2;
     285             : 
     286     1060768 :             if ( Lag >= ( 4 << FractionalResolution ) && ( Lag <= ( ( NumToConsider - 2 ) << FractionalResolution ) ) )
     287             :             {
     288     1053054 :                 CurrentScore = SearchPeriodicityIndex_Single( AbsMdct3, NumToConsider, Lag, FractionalResolution );
     289             : 
     290     1053054 :                 if ( CurrentScore > Score )
     291             :                 {
     292       74259 :                     Score = CurrentScore;
     293       74259 :                     PeriodicityIndex = Multiplier | kLtpHmFlag;
     294             :                 }
     295             :             }
     296             :         }
     297       46363 :         PeriodicityIndex |= LtpPitchIndex << 9;
     298             :     }
     299             :     else
     300             :     {
     301      148022 :         if ( UnfilteredMdct != NULL )
     302             :         {
     303        7970 :             MaxAt = 1;
     304        7970 :             A = AbsMdct3[1];
     305             : 
     306     1621854 :             for ( i = 4; i < NumToConsider - 1; i += 3 )
     307             :             {
     308             : 
     309     1613884 :                 if ( AbsMdct3[i] > AbsMdct3[MaxAt] )
     310             :                 {
     311        9414 :                     MaxAt = i;
     312             :                 }
     313     1613884 :                 A += AbsMdct3[i];
     314             :             }
     315             : 
     316        7970 :             if ( AbsMdct3[MaxAt] > A * 0.7f )
     317             :             {
     318          59 :                 NumToConsider = min( NumToConsider, MaxAt + 4 );
     319             :             }
     320             :         }
     321             : 
     322      148022 :         SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 0, 16, 3, GET_ADJ2( 0, 6, 3 ), 4, &PeriodicityIndex, &Score );
     323             : 
     324      148022 :         SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 16, 80, 4, GET_ADJ2( 16, 8, 4 ), 4, &PeriodicityIndex, &Score );
     325             : 
     326      148022 :         SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 80, 208, 3, GET_ADJ2( 80, 12, 3 ), 4, &PeriodicityIndex, &Score );
     327             : 
     328      148022 :         if ( NumToConsider <= 128 )
     329             :         {
     330             :             /* no long lags for band-limited MDCTs */
     331             : 
     332          59 :             SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 208, 88 + NumToConsider, 0, GET_ADJ2( 224, 188, 0 ), 1, &PeriodicityIndex, &Score );
     333             :         }
     334             :         else
     335             :         {
     336             : 
     337      147963 :             if ( TargetBits > kSmallerLagsTargetBitsThreshold && NumCoeffs >= 256 )
     338             :             {
     339      143273 :                 SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 208, 224, 1, GET_ADJ2( 208, 28, 1 ), 1, &PeriodicityIndex, &Score );
     340             : 
     341      143273 :                 SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 224, 256, 0, GET_ADJ2( 224, 188, 0 ), 1, &PeriodicityIndex, &Score );
     342             :             }
     343             :             else
     344             :             {
     345        4690 :                 SearchPeriodicityIndex_Range( AbsMdct3, NumToConsider, 208, 256, 1, GET_ADJ2( 208, 28, 1 ), 1, &PeriodicityIndex, &Score );
     346             :             }
     347             :         }
     348             :     }
     349             : 
     350      194385 :     if ( AbsTotal > 0 )
     351             :     {
     352      194365 :         *RelativeScore = Score / AbsTotal * (float) NumCoeffs;
     353             :     }
     354             :     else
     355             :     {
     356          20 :         *RelativeScore = 0;
     357             :     }
     358             : 
     359      194385 :     return PeriodicityIndex;
     360             : }
     361             : 
     362             : 
     363             : /*-------------------------------------------------------------------*
     364             :  * PeakFilter()
     365             :  *
     366             :  *
     367             :  *-------------------------------------------------------------------*/
     368             : 
     369             : #define kPeakElevationThreshold 1.0f
     370             : 
     371       13610 : static void PeakFilter(
     372             :     const float x[],      /* (I) absolute spectrum                              */
     373             :     float y[],            /* (O) filtered absolute spectrum, must not alias x[] */
     374             :     const int16_t L_frame /* (I) number of spectral lines                       */
     375             : )
     376             : {
     377             :     int16_t flen, i;
     378             :     float a, m;
     379             : 
     380       13610 :     flen = ( L_frame >> 4 );
     381       13610 :     m = kPeakElevationThreshold / (float) ( 2 * flen + 1 );
     382             : 
     383       13610 :     a = 0.0f;
     384      535610 :     for ( i = 0; i < flen; ++i )
     385             :     {
     386      522000 :         a += x[i];
     387             :     }
     388             : 
     389      535610 :     for ( i = 0; i < flen; ++i )
     390             :     {
     391      522000 :         y[i] = max( 0.0f, x[i] - a * m );
     392      522000 :         a += x[i + flen];
     393             :     }
     394     7321610 :     for ( ; i < L_frame - flen; ++i )
     395             :     {
     396     7308000 :         y[i] = max( 0.0f, x[i] - a * m );
     397     7308000 :         a -= x[i - flen];
     398     7308000 :         a += x[i + flen];
     399             :     }
     400             : 
     401      535610 :     for ( ; i < L_frame; ++i )
     402             :     {
     403      522000 :         y[i] = max( 0.0f, x[i] - a * m );
     404      522000 :         a -= x[i - flen];
     405             :     }
     406             : 
     407       13610 :     return;
     408             : }
     409             : 
     410             : 
     411             : /*-------------------------------------------------------------------*
     412             :  * tcx_hm_get_re()
     413             :  *
     414             :  *
     415             :  *-------------------------------------------------------------------*/
     416             : 
     417             : /*! r: RE error */
     418       29323 : static float tcx_hm_get_re(
     419             :     const float x[],   /* i  : absolute spectrum               */
     420             :     const Word16 gain, /* i  : HM gain (Q11)                   */
     421             :     const int32_t lag,
     422             :     const int16_t fract_res,
     423             :     const Word16 p[],     /* i  : harmonic model (Q13)            */
     424             :     const Word32 env[],   /* i  : envelope (Q16)                  */
     425             :     const int16_t L_frame /* i  : number of spectral lines        */
     426             : )
     427             : {
     428             :     Word32 ne[N_MAX_ARI];
     429             :     float G, e;
     430             :     int16_t i;
     431             : 
     432             :     /* Calculate new envelope with "gain" harmonic gain */
     433    18071563 :     for ( i = 0; i < L_frame; ++i )
     434             :     {
     435    18042240 :         ne[i] = env[i];
     436             :     }
     437             : 
     438       29323 :     tcx_hm_modify_envelope( gain, lag, fract_res, p, ne, L_frame );
     439             : 
     440             :     /* Normalize */
     441       29323 :     G = 0;
     442             : 
     443    18071563 :     for ( i = 0; i < L_frame; ++i )
     444             :     {
     445    18042240 :         G += x[i] * ne[i];
     446             :     }
     447       29323 :     G = 1.0f / G;
     448             : 
     449             :     /* Calculate error */
     450       29323 :     e = 0;
     451             : 
     452    18071563 :     for ( i = 0; i < L_frame; ++i )
     453             :     {
     454    18042240 :         e += (float) pow( x[i] * ( ne[i] * G ), 4 );
     455             :     }
     456             : 
     457       29323 :     return e;
     458             : }
     459             : 
     460             : 
     461             : /*-------------------------------------------------------------------*
     462             :  * tcx_hm_quantize_gain()
     463             :  *
     464             :  *
     465             :  *-------------------------------------------------------------------*/
     466             : 
     467       13610 : static void tcx_hm_quantize_gain(
     468             :     const float x[],    /* i  : absolute spectrum                     */
     469             :     const Word32 env[], /* i  : envelope (Q16)                        */
     470             :     const int32_t lag,
     471             :     const int16_t fract_res,
     472             :     Word16 p[],               /* i  : harmonic model (Q13)                  */
     473             :     const int16_t L_frame,    /* i  : number of spectral lines              */
     474             :     const int16_t coder_type, /* i  : GC/VC coder type                      */
     475             :     float relative_score,     /* i  : periodicity score                     */
     476             :     int16_t *gain_idx,        /* o  : quantization index                    */
     477             :     Word16 *gain              /* o  : quantized harmonic model gain (Q11)   */
     478             : )
     479             : {
     480             :     int16_t g, s;
     481             :     float be, e, pe;
     482       13610 :     const float kLowPeriodicityThr[2] = { 0.5f, 0.2f };
     483             : 
     484       13610 :     assert( coder_type == VOICED || coder_type == GENERIC );
     485             : 
     486       13610 :     s = 0;
     487       13610 :     if ( coder_type == VOICED )
     488             :     {
     489        2735 :         s = 1;
     490             :     }
     491             : 
     492       13610 :     *gain = 0;
     493             : 
     494             :     /* Disable the harmonic model if periodicity is very low */
     495       13610 :     if ( relative_score < kLowPeriodicityThr[s] )
     496             :     {
     497        3051 :         return;
     498             :     }
     499             : 
     500       10559 :     be = tcx_hm_get_re( x, *gain, lag, fract_res, p, env, L_frame );
     501             : 
     502       10559 :     if ( coder_type == GENERIC )
     503             :     {
     504        7824 :         e = tcx_hm_get_re( x, qGains[s][0], lag, fract_res, p, env, L_frame );
     505        7824 :         pe = 1.05f;
     506             : 
     507        7824 :         if ( e * pe < be )
     508             :         {
     509        4492 :             *gain_idx = 0;
     510        4492 :             *gain = qGains[s][0];
     511             :         }
     512             :     }
     513             :     else
     514             :     {
     515             :         /* Iterate over all possible gain values */
     516       13675 :         for ( g = 0; g < ( 1 << kTcxHmNumGainBits ); ++g )
     517             :         {
     518             : 
     519       10940 :             e = tcx_hm_get_re( x, qGains[s][g], lag, fract_res, p, env, L_frame );
     520             : 
     521             :             /* Add bit penalty */
     522       10940 :             pe = 1.0f;
     523       10940 :             if ( *gain == 0.0f )
     524             :             {
     525        3927 :                 pe = 1.05f;
     526             :             }
     527             : 
     528             :             /*  Minimum selection */
     529       10940 :             if ( e * pe < be )
     530             :             {
     531        4964 :                 be = e;
     532        4964 :                 *gain_idx = g;
     533        4964 :                 *gain = qGains[s][g];
     534             :             }
     535             :         }
     536             :     }
     537             : 
     538       10559 :     return;
     539             : }
     540             : 
     541             : 
     542             : /*-------------------------------------------------------------------*
     543             :  * tcx_hm_analyse()
     544             :  *
     545             :  *
     546             :  *-------------------------------------------------------------------*/
     547             : 
     548       15771 : void tcx_hm_analyse(
     549             :     const float abs_spectrum[], /* i  : absolute spectrum             */
     550             :     const int16_t L_frame,      /* i  : number of spectral lines      */
     551             :     Word32 env[],               /* i/o: envelope shape (Q16)          */
     552             :     const int16_t targetBits,   /* i  : target bit budget             */
     553             :     const int16_t coder_type,   /* i  : GC/VC coder type              */
     554             :     int16_t prm_hm[],           /* o  : HM parameters                 */
     555             :     int16_t LtpPitchLag,        /* i  : LTP pitch lag or -1 if none   */
     556             :     const float LtpGain,        /* i  : LTP gain                      */
     557             :     int16_t *hm_bits            /* o  : bit consumption               */
     558             : )
     559             : {
     560             :     int32_t lag;
     561             :     int32_t tmpL;
     562             :     int16_t fract_res;
     563             :     float fspec[N_MAX_ARI], RelativeScore;
     564             :     Word16 p[2 * kTcxHmParabolaHalfWidth + 1], gain;
     565             : 
     566             :     /* Disable HM for non-GC,VC coder types */
     567       15771 :     if ( ( coder_type != VOICED ) && ( coder_type != GENERIC ) )
     568             :     {
     569        2161 :         *hm_bits = 0;
     570        2161 :         prm_hm[0] = 0;
     571             : 
     572        2161 :         return;
     573             :     }
     574             : 
     575             :     /* Bit consumption for the HM off case: 1 bit flag */
     576       13610 :     *hm_bits = 1;
     577             : 
     578             :     /* Filter out noise and keep the peaks */
     579       13610 :     PeakFilter( abs_spectrum, fspec, L_frame );
     580             : 
     581             :     /* Get the best lag index */
     582       13610 :     prm_hm[1] = SearchPeriodicityIndex( fspec, abs_spectrum, L_frame, targetBits - *hm_bits, LtpPitchLag, LtpGain, &RelativeScore );
     583             : 
     584             :     /* Convert the index to lag */
     585       13610 :     UnmapIndex( prm_hm[1], L_frame >= 256, LtpPitchLag, ( targetBits - *hm_bits <= kSmallerLagsTargetBitsThreshold ) || ( L_frame < 256 ), &fract_res, &tmpL );
     586       13610 :     lag = tmpL;
     587             : 
     588             :     /* Render harmonic model */
     589       13610 :     tcx_hm_render( lag, fract_res, p );
     590             : 
     591             :     /* Calculate and quantize gain */
     592       13610 :     gain = 0;
     593             : 
     594       13610 :     tcx_hm_quantize_gain( abs_spectrum, env, lag, fract_res, p, L_frame, coder_type, RelativeScore, &prm_hm[2], &gain );
     595             : 
     596             :     /* Decision */
     597       13610 :     if ( gain > 0 )
     598             :     {
     599        6868 :         prm_hm[0] = 1; /* flag: on */
     600             : 
     601        6868 :         *hm_bits += CountIndexBits( L_frame >= 256, prm_hm[1] );
     602             : 
     603        6868 :         if ( coder_type == VOICED )
     604             :         {
     605        2376 :             *hm_bits += kTcxHmNumGainBits;
     606             :         }
     607             : 
     608        6868 :         tcx_hm_modify_envelope( gain, lag, fract_res, p, env, L_frame );
     609             :     }
     610             :     else
     611             :     {
     612        6742 :         prm_hm[0] = 0;  /* flag: off   */
     613        6742 :         prm_hm[1] = -1; /* pitch index */
     614        6742 :         prm_hm[2] = 0;  /* gain index  */
     615             :     }
     616             : 
     617       13610 :     return;
     618             : }

Generated by: LCOV version 1.14