LCOV - code coverage report
Current view: top level - lib_enc - hq_classifier_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 211 219 96.3 %
Date: 2025-10-27 07:01:45 Functions: 4 4 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-----------------------------------------------------------------*
      49             :  * Local constants
      50             :  *-----------------------------------------------------------------*/
      51             : 
      52             : #define SHARP_DIST_THRES    22.2f
      53             : #define HALF_WIN_LENGTH     10
      54             : #define L_SPEC_HB           320
      55             : #define PEAK_THRESHOLD      0.1f
      56             : #define LOW_COUNT_THRESHOLD 220
      57             : 
      58             : 
      59             : /*-----------------------------------------------------------------*
      60             :  * Local function prototypes
      61             :  *-----------------------------------------------------------------*/
      62             : 
      63             : static void hvq_classifier( const float *input, int16_t *prev_Npeaks, int16_t *prev_peaks, int16_t *hqswb_clas, int16_t *Npeaks, int16_t *peaks, const int32_t core_brate, const int16_t last_core, float *nf_gains, int16_t *hvq_hangover, float *pe_gains );
      64             : static int16_t hf_spectrum_sparseness( Encoder_State *st, const float *coefs );
      65             : 
      66             : /*--------------------------------------------------------------------------*
      67             :  * hq_classifier_enc()
      68             :  *
      69             :  * HQ mode selector (decision_matrix)
      70             :  *--------------------------------------------------------------------------*/
      71             : 
      72             : /*! r: Consumed bits */
      73       13005 : int16_t hq_classifier_enc(
      74             :     Encoder_State *st,          /* i/o: encoder state structure         */
      75             :     const int16_t length,       /* i  : Frame length                    */
      76             :     const float *coefs,         /* i  : Spectral coefficients           */
      77             :     const int16_t is_transient, /* i  : Transient flag                  */
      78             :     int16_t *Npeaks,            /* o  : Number of identified peaks      */
      79             :     int16_t *peaks,             /* o  : Peak indices                    */
      80             :     float *pe_gains,            /* o  : Peak gains                      */
      81             :     float *nf_gains,            /* o  : Noise-fill gains                */
      82             :     int16_t *hqswb_clas         /* o  : HQ class                        */
      83             : )
      84             : {
      85             :     int16_t bits;
      86             :     int32_t max_brate;
      87             :     int16_t harmonic_decision;
      88             : 
      89       13005 :     HQ_ENC_HANDLE hHQ_core = st->hHQ_core;
      90             : 
      91       13005 :     max_brate = HQ_32k;
      92       13005 :     if ( st->element_mode > EVS_MONO )
      93             :     {
      94       12386 :         max_brate = HQ_48k;
      95             :     }
      96             : 
      97       13005 :     *hqswb_clas = HQ_NORMAL;
      98       13005 :     bits = 1;
      99       13005 :     if ( is_transient )
     100             :     {
     101        1764 :         *hqswb_clas = HQ_TRANSIENT;
     102             :     }
     103             : 
     104       13005 :     if ( length == L_SPEC32k || length == L_SPEC48k )
     105             :     {
     106       12144 :         if ( st->core_brate <= max_brate )
     107             :         {
     108             : 
     109       11813 :             if ( !is_transient && st->bwidth == st->last_bwidth )
     110             :             {
     111             :                 /* Detect HQ_HARMONIC mode */
     112       10085 :                 *hqswb_clas = peak_avrg_ratio( st->total_brate, coefs, NUMC_N + 96, &hHQ_core->mode_count, &hHQ_core->mode_count1 );
     113             : 
     114       10085 :                 harmonic_decision = hf_spectrum_sparseness( st, coefs );
     115             : 
     116       10085 :                 if ( *hqswb_clas == HQ_HARMONIC && !harmonic_decision )
     117             :                 {
     118           1 :                     *hqswb_clas = HQ_NORMAL;
     119             :                 }
     120             :                 else
     121             :                 {
     122             :                     /* Detect HQ_HVQ mode */
     123       10084 :                     hvq_classifier( coefs, &hHQ_core->prev_Npeaks, hHQ_core->prev_peaks, hqswb_clas, Npeaks, peaks, st->core_brate, st->last_core, nf_gains, &hHQ_core->hvq_hangover, pe_gains );
     124             :                 }
     125             :             }
     126       11813 :             bits = 2;
     127             :         }
     128             :     }
     129         861 :     else if ( length == L_SPEC16k_EXT || length == L_SPEC48k_EXT )
     130             :     {
     131         155 :         bits = 0; /* HQ_NORMAL only -- no signaling needed */
     132             :     }
     133             : 
     134             :     /* write signaling info to the bitstream */
     135       13005 :     push_indice( st->hBstr, IND_HQ_SWB_CLAS, *hqswb_clas, bits );
     136             : 
     137       13005 :     if ( st->core_brate <= HQ_32k && *hqswb_clas == HQ_NORMAL )
     138             :     {
     139        5656 :         if ( length == L_SPEC32k )
     140             :         {
     141         941 :             *hqswb_clas = HQ_GEN_SWB;
     142             :         }
     143        4715 :         else if ( length == L_SPEC48k )
     144             :         {
     145        4020 :             *hqswb_clas = HQ_GEN_FB;
     146             :         }
     147             :     }
     148             : 
     149       13005 :     return bits;
     150             : }
     151             : 
     152             : /*--------------------------------------------------------------------------*
     153             :  * peak_avrg_ratio()
     154             :  *
     155             :  * Classify the input signal and decide if it has a harmonic structure
     156             :  *--------------------------------------------------------------------------*/
     157             : 
     158             : /*! r: hqswb_clas */
     159       10131 : int16_t peak_avrg_ratio(
     160             :     const int32_t total_brate, /* i  : total bitrate              */
     161             :     const float *input_hi,     /* i  : input signal               */
     162             :     const int16_t N,           /* i  : number of coefficients     */
     163             :     int16_t *mode_count,       /* i/o: HQ_HARMONIC mode count     */
     164             :     int16_t *mode_count1       /* i/o: HQ_NORMAL mode count       */
     165             : )
     166             : {
     167             :     float mean, peak, sharp;
     168             :     int16_t i, j, q, k, k1, hqswb_clas;
     169             :     float input_abs[L_FRAME32k];
     170             : 
     171     5516979 :     for ( i = 96; i < N; i++ )
     172             :     {
     173     5506848 :         input_abs[i] = (float) fabs( input_hi[i] );
     174             :     }
     175             : 
     176       10131 :     hqswb_clas = HQ_NORMAL;
     177             : 
     178       10131 :     k = 0;
     179       10131 :     k1 = 0;
     180       10131 :     q = 96;
     181      151965 :     for ( i = 3; i < 17; i++ )
     182             :     {
     183      141834 :         peak = 0.0f;
     184      141834 :         mean = EPSILON;
     185     4680522 :         for ( j = 0; j < 32; j++, q++ )
     186             :         {
     187     4538688 :             mean += input_abs[q];
     188             : 
     189     4538688 :             if ( input_abs[q] > peak )
     190             :             {
     191      612285 :                 peak = input_abs[q];
     192             :             }
     193             :         }
     194             : 
     195      141834 :         sharp = 32 * peak / mean;
     196             : 
     197      141834 :         if ( i < 8 )
     198             :         {
     199       50655 :             if ( sharp > 4.5 )
     200             :             {
     201       33080 :                 k += 1;
     202             :             }
     203             :         }
     204             :         else
     205             :         {
     206       91179 :             if ( sharp > 3.6 && peak > 10 )
     207             :             {
     208       26795 :                 k1 += 1;
     209             :             }
     210             :         }
     211             :     }
     212             : 
     213       10131 :     if ( k + k1 >= 10 && k1 > 5 )
     214             :     {
     215        1770 :         if ( *mode_count < 8 )
     216             :         {
     217        1770 :             ( *mode_count )++;
     218             :         }
     219             : 
     220        1770 :         if ( *mode_count1 > 0 )
     221             :         {
     222           0 :             ( *mode_count1 )--;
     223             :         }
     224             :     }
     225             :     else
     226             :     {
     227        8361 :         if ( *mode_count > 0 )
     228             :         {
     229           0 :             ( *mode_count )--;
     230             :         }
     231             : 
     232        8361 :         if ( *mode_count1 < 8 )
     233             :         {
     234        8318 :             ( *mode_count1 )++;
     235             :         }
     236             :     }
     237       10131 :     if ( ( k + k1 >= 5 && k1 > 2 && total_brate < HQ_BWE_CROSSOVER_BRATE && total_brate > HQ_16k40 ) || ( ( ( k + k1 >= 10 && k1 > 5 ) || *mode_count >= 5 ) && *mode_count1 < 5 ) )
     238             :     {
     239        2625 :         hqswb_clas = HQ_HARMONIC;
     240             :     }
     241             : 
     242       10131 :     return hqswb_clas;
     243             : }
     244             : 
     245             : 
     246             : /*--------------------------------------------------------------------------*
     247             :  * hvq_classifier()
     248             :  *
     249             :  * Classification of spectral content for HQ_HVQ mode
     250             :  *--------------------------------------------------------------------------*/
     251             : 
     252       10084 : static void hvq_classifier(
     253             :     const float *input,       /* i  : input signal                */
     254             :     int16_t *prev_Npeaks,     /* i/o: Peak number memory          */
     255             :     int16_t *prev_peaks,      /* i/o: Peak indices memory         */
     256             :     int16_t *hqswb_clas,      /* i/o: HQ class                    */
     257             :     int16_t *Npeaks,          /* o  : Number of peaks             */
     258             :     int16_t *peaks,           /* o  : Peak indices                */
     259             :     const int32_t core_brate, /* i  : Core bitrate                */
     260             :     const int16_t last_core,  /* i  : Last core used              */
     261             :     float *nf_gains,          /* o  : Noisefloor gains            */
     262             :     int16_t *hvq_hangover,    /* i/o: Mode-switch hangover        */
     263             :     float *pe_gains           /* o  : peak gains                  */
     264             : )
     265             : {
     266             :     const float *p_adj;
     267             :     float sharp_dist;
     268             :     float nf, pe, d, peak, thr_tmp, m;
     269             :     float input_abs[L_FRAME32k], thr[L_FRAME16k];
     270             :     float pe_mean[HVQ_NSUB_32k], nf_mean[HVQ_NSUB_32k];
     271             :     float sharp[HVQ_NSUB_32k];
     272             : 
     273             :     int16_t num_sharp_bands, i, j, k, q, peak_th, nsub, pindx, N, offset;
     274             :     int16_t num_peak_cands, high, low;
     275             :     int16_t peak_cand_idx[HVQ_THRES_BIN_32k], avail_peaks[HVQ_NSUB_32k];
     276             : 
     277       10084 :     if ( *hqswb_clas == HQ_HARMONIC && last_core != ACELP_CORE && last_core != AMR_WB_CORE )
     278             :     {
     279        2594 :         set_f( thr, 0.0f, L_FRAME16k );
     280             : 
     281        2594 :         if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
     282             :         {
     283        1627 :             nsub = HVQ_NSUB_24k;
     284             :         }
     285             :         else
     286             :         {
     287         967 :             nsub = HVQ_NSUB_32k;
     288             :         }
     289             : 
     290        2594 :         N = nsub * HVQ_BW;
     291             : 
     292      676482 :         for ( i = 0; i < N; i++ )
     293             :         {
     294      673888 :             input_abs[i] = (float) fabs( input[i] );
     295             :         }
     296             : 
     297        2594 :         *Npeaks = 0;
     298        2594 :         nf = 800;
     299        2594 :         pe = 800;
     300        2594 :         num_sharp_bands = 0;
     301        2594 :         k = 0;
     302        2594 :         q = 0;
     303        2594 :         sharp_dist = 0;
     304             : 
     305             :         /* Find peak threshold */
     306       23653 :         for ( i = 0; i < nsub; i++ )
     307             :         {
     308       21059 :             peak = 0.0f;
     309       21059 :             nf_mean[i] = EPSILON;
     310       21059 :             pe_mean[i] = EPSILON;
     311      694947 :             for ( j = 0; j < HVQ_BW; j++, q++ )
     312             :             {
     313      673888 :                 d = input_abs[q];
     314             : 
     315      673888 :                 if ( d > nf )
     316             :                 {
     317      366638 :                     nf = HVQ_NF_WEIGHT1 * nf + ( 1 - HVQ_NF_WEIGHT1 ) * d;
     318             :                 }
     319             :                 else
     320             :                 {
     321      307250 :                     nf = HVQ_NF_WEIGHT2 * nf + ( 1 - HVQ_NF_WEIGHT2 ) * d;
     322             :                 }
     323             : 
     324      673888 :                 if ( d > pe )
     325             :                 {
     326      137610 :                     pe = HVQ_PE_WEIGHT1 * pe + ( 1 - HVQ_PE_WEIGHT1 ) * d;
     327             :                 }
     328             :                 else
     329             :                 {
     330      536278 :                     pe = HVQ_PE_WEIGHT2 * pe + ( 1 - HVQ_PE_WEIGHT2 ) * d;
     331             :                 }
     332             : 
     333      673888 :                 nf_mean[i] += nf;
     334      673888 :                 pe_mean[i] += pe;
     335             : 
     336      673888 :                 if ( d > peak )
     337             :                 {
     338      100213 :                     peak = d;
     339             :                 }
     340             :             }
     341             : 
     342       21059 :             nf_mean[i] /= HVQ_BW;
     343       21059 :             pe_mean[i] /= HVQ_BW;
     344             : 
     345             : 
     346       21059 :             thr_tmp = (float) pow( pe_mean[i] / nf_mean[i], HVQ_THR_POW ) * nf_mean[i];
     347       21059 :             set_f( &thr[k], thr_tmp, HVQ_BW );
     348       21059 :             k += HVQ_BW;
     349             : 
     350       21059 :             sharp[i] = peak / nf_mean[i];
     351       21059 :             sharp_dist += sharp[i] - HVQ_SHARP_THRES;
     352             : 
     353       21059 :             if ( sharp[i] > HVQ_SHARP_THRES )
     354             :             {
     355       17710 :                 num_sharp_bands++;
     356             :             }
     357             :         }
     358             : 
     359             :         /* Estimate noise floor gains */
     360        2594 :         offset = nsub % 2;
     361       22026 :         for ( i = 0; i < 2 * ( nsub / 2 ); i++ )
     362             :         {
     363       19432 :             nf_gains[( 2 * i + 1 ) / nsub] += nf_mean[i + offset];
     364       19432 :             pe_gains[( 2 * i + 1 ) / nsub] += pe_mean[i + offset];
     365             :         }
     366             : 
     367        7782 :         for ( i = 0; i < HVQ_NF_GROUPS; i++ )
     368             :         {
     369        5188 :             nf_gains[i] /= nsub / HVQ_NF_GROUPS;
     370        5188 :             pe_gains[i] /= nsub / HVQ_NF_GROUPS;
     371             :         }
     372             : 
     373             :         /* Allocate available peaks */
     374       23653 :         for ( i = 0; i < nsub; i++ )
     375             :         {
     376       21059 :             avail_peaks[i] = HVQ_PA_PEAKS_SHARP1;
     377       21059 :             if ( nf_mean[i] < nf_gains[( 2 * i + 1 ) / nsub] * HVQ_PA_FAC )
     378             :             {
     379        7259 :                 if ( sharp[i] < HVQ_PA_SHARP_THRES3 )
     380             :                 {
     381        1931 :                     avail_peaks[i] = HVQ_PA_PEAKS_SHARP3;
     382             :                 }
     383        5328 :                 else if ( sharp[i] < HVQ_PA_SHARP_THRES2 )
     384             :                 {
     385         765 :                     avail_peaks[i] = HVQ_PA_PEAKS_SHARP2;
     386             :                 }
     387             :             }
     388             :         }
     389             : 
     390             :         /* Adjust threshold around previous peaks */
     391       29438 :         for ( i = 0; i < *prev_Npeaks; i++ )
     392             :         {
     393       26844 :             j = prev_peaks[i] - 2;
     394       26844 :             k = prev_peaks[i] + 2;
     395       26844 :             p_adj = hvq_thr_adj;
     396             : 
     397      134220 :             for ( q = j; q < k; q++ )
     398             :             {
     399      107376 :                 thr[q] *= *p_adj++;
     400             :             }
     401             :         }
     402             : 
     403        2594 :         num_peak_cands = 0;
     404             : 
     405             :         /* Remove everything below threshold for peak search */
     406        2594 :         input_abs[0] = 0;
     407        2594 :         input_abs[1] = 0;
     408        2594 :         input_abs[N - 2] = 0;
     409        2594 :         input_abs[N - 1] = 0;
     410      671294 :         for ( i = 0; i < N - 2; i++ )
     411             :         {
     412      668700 :             if ( input_abs[i] < thr[i] )
     413             :             {
     414      542204 :                 input_abs[i] = 0;
     415             :             }
     416             :             else
     417             :             {
     418      126496 :                 input_abs[num_peak_cands] = input_abs[i];
     419      126496 :                 peak_cand_idx[num_peak_cands] = i;
     420      126496 :                 num_peak_cands++;
     421             :             }
     422             :         }
     423             : 
     424             :         /* maximum 27 (5+9+13) bits for additional peak */
     425        2594 :         peak_th = (int16_t) ( ( core_brate * HVQ_PEAKS_PER_DELTA_THR + HVQ_PEAKS_PER_DELTA_THR_OFFS ) / HVQ_PEAKS_BPS_DELTA );
     426             : 
     427             :         /* Find peaks */
     428        2594 :         pindx = maximum( input_abs, num_peak_cands, &m );
     429        2594 :         i = 0;
     430             : 
     431       54325 :         while ( m > 0 && i < peak_th + 1 )
     432             :         {
     433       51731 :             if ( avail_peaks[peak_cand_idx[pindx] / HVQ_BW] > 0 )
     434             :             {
     435       50272 :                 peaks[i++] = peak_cand_idx[pindx];
     436       50272 :                 avail_peaks[peak_cand_idx[pindx] / HVQ_BW]--;
     437             :             }
     438             : 
     439       51731 :             j = pindx - 2;
     440       51731 :             k = pindx + 2;
     441             : 
     442       51731 :             if ( j < 0 )
     443             :             {
     444        2514 :                 j = 0;
     445             :             }
     446             : 
     447       51731 :             if ( k > num_peak_cands - 1 )
     448             :             {
     449        1711 :                 k = num_peak_cands - 1;
     450             :             }
     451             : 
     452       51731 :             low = peak_cand_idx[pindx] - 2;
     453       51731 :             high = peak_cand_idx[pindx] + 2;
     454             : 
     455       51731 :             if ( low < 0 )
     456             :             {
     457           0 :                 low = 0;
     458             :             }
     459             : 
     460       51731 :             if ( high > N - 1 )
     461             :             {
     462           0 :                 high = N - 1;
     463             :             }
     464             : 
     465      202915 :             for ( q = j; q <= pindx; q++ )
     466             :             {
     467      151184 :                 if ( peak_cand_idx[q] >= low )
     468             :                 {
     469       84447 :                     peak_cand_idx[q] = 0;
     470       84447 :                     input_abs[q] = 0;
     471             :                 }
     472             :             }
     473             : 
     474      152609 :             for ( q = pindx + 1; q <= k; q++ )
     475             :             {
     476      100878 :                 if ( peak_cand_idx[q] <= high )
     477             :                 {
     478       68268 :                     peak_cand_idx[q] = 0;
     479       68268 :                     input_abs[q] = 0;
     480             :                 }
     481             :             }
     482             : 
     483       51731 :             pindx = maximum( input_abs, num_peak_cands, &m );
     484             :         }
     485             : 
     486        2594 :         *Npeaks = i;
     487             : 
     488             :         /* decision about HQ_HVQ mode */
     489        2594 :         if ( *Npeaks > HVQ_MIN_PEAKS )
     490             :         {
     491        2594 :             if ( num_sharp_bands > nsub - 3 && *Npeaks <= peak_th )
     492             :             {
     493        1274 :                 sharp_dist /= nsub;
     494        1274 :                 if ( sharp_dist <= SHARP_DIST_THRES && *hvq_hangover < 0 )
     495             :                 {
     496          54 :                     ( *hvq_hangover )++;
     497             :                 }
     498             :                 else
     499             :                 {
     500        1220 :                     *hqswb_clas = HQ_HVQ;
     501        1220 :                     *hvq_hangover = 2;
     502             :                 }
     503             : 
     504             :                 /* update memory */
     505        1274 :                 *prev_Npeaks = *Npeaks;
     506        1274 :                 mvs2s( peaks, prev_peaks, *Npeaks );
     507             :             }
     508             :             else
     509             :             {
     510        1320 :                 if ( *hvq_hangover > 0 )
     511             :                 {
     512         308 :                     *hqswb_clas = HQ_HVQ;
     513         308 :                     ( *hvq_hangover )--;
     514             :                 }
     515             :                 else
     516             :                 {
     517        1012 :                     *hvq_hangover = -1;
     518             :                 }
     519             :             }
     520             :         }
     521             :         else
     522             :         {
     523           0 :             *hvq_hangover = -1;
     524             :         }
     525             : 
     526        2594 :         *Npeaks = (int16_t) ( min( ( core_brate * HVQ_PEAKS_PER_DELTA + HVQ_PEAKS_PER_DELTA_OFFS ) / HVQ_PEAKS_BPS_DELTA, *Npeaks ) );
     527             :     }
     528             :     else
     529             :     {
     530        7490 :         *prev_Npeaks = 0;
     531        7490 :         *hvq_hangover = 0;
     532             :     }
     533             : 
     534       10084 :     return;
     535             : }
     536             : /*--------------------------------------------------------------------------*
     537             :  * hf_spectrum_sparseness()
     538             :  *
     539             :  * Detection of sparse spectrum in high band for activation of harmonic
     540             :  * modes HQ_HARMONIC and HQ_HVQ
     541             :  *--------------------------------------------------------------------------*/
     542             : /*! r: Harmonic decision for high band */
     543       10085 : static int16_t hf_spectrum_sparseness(
     544             :     Encoder_State *st, /* i/o: encoder state structure         */
     545             :     const float *coefs /* i  : MDCT spectrum                   */
     546             : )
     547             : {
     548             :     int16_t i;
     549             :     float thr;
     550             :     int16_t low_count;
     551             :     float A[L_SPEC_HB];
     552             :     float Amax;
     553             :     float movmean;
     554             :     float inv_rms;
     555             :     float crest;
     556             :     float crest_mod;
     557             :     const float *p_num;
     558             :     float *crest_lp;
     559             :     float *crest_mod_lp;
     560             :     int16_t result;
     561             : 
     562       10085 :     crest_lp = &st->hHQ_core->crest_lp;
     563       10085 :     crest_mod_lp = &st->hHQ_core->crest_mod_lp;
     564             : 
     565       10085 :     result = TRUE;
     566       10085 :     if ( st->element_mode != EVS_MONO )
     567             :     {
     568     3148689 :         for ( i = 0; i < L_SPEC_HB; i++ )
     569             :         {
     570     3138880 :             A[i] = (float) fabsf( coefs[i + L_SPEC_HB] );
     571             :         }
     572        9809 :         low_count = 0;
     573        9809 :         inv_rms = 0.0f;
     574        9809 :         crest_mod = 0.0f;
     575        9809 :         maximum( A, L_SPEC_HB, &Amax );
     576        9809 :         if ( Amax == 0 )
     577             :         {
     578             :             /* For all-zero input the crest is 1.0 */
     579           0 :             crest = 1.0f;
     580           0 :             crest_mod = 1.0f;
     581           0 :             low_count = 0;
     582             :         }
     583             :         else
     584             :         {
     585        9809 :             thr = Amax * PEAK_THRESHOLD;
     586        9809 :             movmean = 0.0f;                        /* avoid uninitialized warning */
     587        9809 :             p_num = &inv_tbl[HALF_WIN_LENGTH + 1]; /* Table for division 1./(11:21) */
     588     3148689 :             for ( i = 0; i < L_SPEC_HB; i++ )
     589             :             {
     590     3138880 :                 inv_rms += A[i] * A[i];
     591     3138880 :                 if ( A[i] < thr )
     592             :                 {
     593     1819727 :                     low_count += 1;
     594             :                 }
     595     3138880 :                 if ( i <= HALF_WIN_LENGTH )
     596             :                 {
     597      107899 :                     if ( i == 0 )
     598             :                     {
     599        9809 :                         movmean = sum_f( &A[0], i + HALF_WIN_LENGTH + 1 ) * ( *p_num );
     600             :                     }
     601             :                     else
     602             :                     {
     603       98090 :                         p_num++;
     604       98090 :                         movmean = movmean + ( A[i + HALF_WIN_LENGTH] - movmean ) * ( *p_num );
     605             :                     }
     606             :                 }
     607             :                 else
     608             :                 {
     609     3030981 :                     if ( L_SPEC_HB <= i + HALF_WIN_LENGTH )
     610             :                     {
     611       98090 :                         p_num--;
     612       98090 :                         movmean = movmean + ( movmean - A[i - HALF_WIN_LENGTH - 1] ) * ( *p_num );
     613             :                     }
     614             :                     else
     615             :                     {
     616     2932891 :                         movmean = movmean + ( A[i + HALF_WIN_LENGTH] - A[i - HALF_WIN_LENGTH - 1] ) * ( *p_num );
     617             :                     }
     618             :                 }
     619     3138880 :                 if ( crest_mod < movmean )
     620             :                 {
     621      122954 :                     crest_mod = movmean;
     622             :                 }
     623             :             }
     624        9809 :             inv_rms = 1.0f / (float) sqrtf( inv_rms / L_SPEC_HB );
     625        9809 :             crest = Amax * inv_rms;
     626        9809 :             crest_mod = crest_mod * inv_rms;
     627             :         }
     628        9809 :         *crest_lp = HQ_CREST_FAC_SM * ( *crest_lp ) + ( 1.0f - HQ_CREST_FAC_SM ) * crest;
     629        9809 :         *crest_mod_lp = HQ_CREST_FAC_SM * ( *crest_mod_lp ) + ( 1.0f - HQ_CREST_FAC_SM ) * crest_mod;
     630             : 
     631        9809 :         if ( ( ( *crest_lp ) > HQ_CREST_THRESHOLD ) && ( ( *crest_mod_lp ) > HQ_CREST_MOD_THRESHOLD ) && ( low_count > LOW_COUNT_THRESHOLD ) )
     632             :         {
     633         126 :             result = FALSE;
     634             :         }
     635             :     }
     636             : 
     637       10085 :     return result;
     638             : }

Generated by: LCOV version 1.14