LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_lr.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 425 785 54.1 %
Date: 2025-05-23 08:37:30 Functions: 17 24 70.8 %

          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             :  * hf_parinitiz()
      50             :  *
      51             :  *
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54         124 : void hf_parinitiz(
      55             :     const int32_t total_brate,
      56             :     const int16_t hqswb_clas,
      57             :     int16_t lowlength,
      58             :     int16_t highlength,
      59             :     int16_t wBands[],
      60             :     const int16_t **subband_search_offset,
      61             :     const int16_t **subband_offsets,
      62             :     int16_t *nBands,
      63             :     int16_t *nBands_search,
      64             :     int16_t *swb_lowband,
      65             :     int16_t *swb_highband )
      66             : {
      67         124 :     *swb_lowband = lowlength;
      68         124 :     *swb_highband = highlength;
      69             : 
      70         124 :     if ( hqswb_clas == HQ_HARMONIC )
      71             :     {
      72             :         /* Mode dependent initializations (performed every frame in case mode-switching implemented) */
      73           0 :         *nBands = NB_SWB_SUBBANDS_HAR;
      74           0 :         *nBands_search = NB_SWB_SUBBANDS_HAR_SEARCH_SB;
      75             : 
      76           0 :         if ( total_brate == HQ_13k20 )
      77             :         {
      78           0 :             wBands[0] = SWB_SB_BW_LEN0_12KBPS_HAR;
      79           0 :             wBands[1] = SWB_SB_BW_LEN1_12KBPS_HAR;
      80           0 :             wBands[2] = SWB_SB_BW_LEN2_12KBPS_HAR;
      81           0 :             wBands[3] = SWB_SB_BW_LEN3_12KBPS_HAR;
      82           0 :             *subband_offsets = subband_offsets_sub5_13p2kbps_Har;
      83           0 :             *subband_search_offset = subband_search_offsets_13p2kbps_Har;
      84             :         }
      85             :         else
      86             :         {
      87           0 :             wBands[0] = SWB_SB_BW_LEN0_16KBPS_HAR;
      88           0 :             wBands[1] = SWB_SB_BW_LEN1_16KBPS_HAR;
      89           0 :             wBands[2] = SWB_SB_BW_LEN2_16KBPS_HAR;
      90           0 :             wBands[3] = SWB_SB_BW_LEN3_16KBPS_HAR;
      91           0 :             *subband_offsets = subband_offsets_sub5_16p4kbps_Har;
      92           0 :             *subband_search_offset = subband_search_offsets_16p4kbps_Har;
      93             :         }
      94             :     }
      95             :     else
      96             :     {
      97             :         /* Mode-dependent initializations (performed every frame in case mode-switching implemented) */
      98         124 :         *nBands = NB_SWB_SUBBANDS;
      99         124 :         *nBands_search = NB_SWB_SUBBANDS;
     100             : 
     101         124 :         if ( total_brate == HQ_13k20 )
     102             :         {
     103         124 :             wBands[0] = SWB_SB_LEN0_12KBPS;
     104         124 :             wBands[1] = SWB_SB_LEN1_12KBPS;
     105         124 :             wBands[2] = SWB_SB_LEN2_12KBPS;
     106         124 :             wBands[3] = SWB_SB_LEN3_12KBPS;
     107         124 :             *subband_offsets = subband_offsets_12KBPS;
     108             :         }
     109             :         else
     110             :         {
     111           0 :             wBands[0] = SWB_SB_LEN0_16KBPS;
     112           0 :             wBands[1] = SWB_SB_LEN1_16KBPS;
     113           0 :             wBands[2] = SWB_SB_LEN2_16KBPS;
     114           0 :             wBands[3] = SWB_SB_LEN3_16KBPS;
     115           0 :             *subband_offsets = subband_offsets_16KBPS;
     116             :         }
     117             :     }
     118         124 :     return;
     119             : }
     120             : 
     121             : /*-------------------------------------------------------------------*
     122             :  * GetPredictedSignal()
     123             :  *
     124             :  * Routine for calculating the predicted signal
     125             :  *-------------------------------------------------------------------*/
     126         992 : void GetPredictedSignal(
     127             :     const float *predBuf, /* i  : prediction buffer        */
     128             :     float *outBuf,        /* o  : output buffer            */
     129             :     const int16_t lag,    /* i  : prediction buffer offset */
     130             :     const int16_t fLen,   /* i  : length of loop (output)  */
     131             :     const float gain      /* i  : gain to be applied       */
     132             : )
     133             : {
     134             :     int16_t i;
     135             : 
     136         992 :     predBuf += lag;
     137             : 
     138       78368 :     for ( i = 0; i < fLen; i++ )
     139             :     {
     140       77376 :         *outBuf++ = *predBuf++ * gain;
     141             :     }
     142             : 
     143         992 :     return;
     144             : }
     145             : /*-------------------------------------------------------------------*
     146             :  * est_freq_har_decis()
     147             :  *
     148             :  * Harmonic frequency decision matrix
     149             :  *-------------------------------------------------------------------*/
     150             : 
     151        8352 : static void est_freq_har_decis(
     152             :     int16_t *har_freq_est1,  /* o  : harmonic analysis 1*/
     153             :     int16_t *har_freq_est2,  /* o  : harmonic analysis 2*/
     154             :     int16_t sharp,           /* i  : pka-avg for group 1 */
     155             :     int16_t sharp1,          /* i  : pka-avg for group 2 */
     156             :     int16_t hfe_est_countk1, /* i  : group pks count 1*/
     157             :     int16_t hfe_est_countk2, /* i  : group pks count 2*/
     158             :     int16_t k,               /* i  : group count */
     159             :     int16_t k1,
     160             :     int16_t k2,
     161             :     int16_t *prev_frm_hfe2 /* i  : harmonic estimation  */
     162             : )
     163             : {
     164        8352 :     int16_t temp_hfe2 = 0;
     165             : 
     166        8352 :     if ( k != 0 )
     167             :     {
     168        8352 :         *har_freq_est1 = (int16_t) sharp / k;
     169             :     }
     170        8352 :     if ( k1 > 1 )
     171             :     {
     172        3342 :         *har_freq_est2 = (int16_t) sharp1 / k1;
     173             :     }
     174        5010 :     else if ( ( k1 < 2 && ( k2 != 0 || k > 1 ) ) )
     175             :     {
     176        4877 :         *har_freq_est2 = *har_freq_est1;
     177             :     }
     178             :     else
     179             :     {
     180         133 :         if ( ( hfe_est_countk1 != 0 || hfe_est_countk2 != 0 ) && ( k1 == 0 && k2 == 0 ) )
     181             :         {
     182         117 :             *har_freq_est2 = ( *har_freq_est1 );
     183             :         }
     184             :         else
     185             :         {
     186          16 :             *har_freq_est2 = 2 * ( *har_freq_est1 );
     187             :         }
     188             :     }
     189             : 
     190             :     /* Consider Estimation Error upto 200Hz */
     191        8352 :     if ( *prev_frm_hfe2 != 0 && ( (int16_t) abs( *prev_frm_hfe2 - *har_freq_est2 ) < 10 || abs( *prev_frm_hfe2 - 2 * ( *har_freq_est2 ) ) < 10 ) )
     192             :     {
     193           0 :         *har_freq_est2 = *prev_frm_hfe2;
     194             :     }
     195        8352 :     else if ( *prev_frm_hfe2 != 0 && abs( *har_freq_est2 - 2 * ( *prev_frm_hfe2 ) ) < 10 )
     196             :     {
     197           0 :         *har_freq_est2 = 2 * ( *prev_frm_hfe2 );
     198             :     }
     199             :     else
     200             :     {
     201        8352 :         temp_hfe2 = (int16_t) ( *prev_frm_hfe2 + *har_freq_est2 ) / 2;
     202             : 
     203        8352 :         if ( abs( temp_hfe2 - *prev_frm_hfe2 ) < 2 )
     204             :         {
     205           0 :             temp_hfe2 = *prev_frm_hfe2;
     206           0 :             *har_freq_est2 = temp_hfe2;
     207             :         }
     208             :     }
     209        8352 :     if ( *har_freq_est2 < *har_freq_est1 && ( k > 1 && k1 < 2 ) )
     210             :     {
     211           0 :         *har_freq_est2 = *har_freq_est1;
     212             :     }
     213        8352 :     return;
     214             : }
     215             : 
     216             : /*--------------------------------------------------------------------------*
     217             :  * har_est()
     218             :  *
     219             :  * Harmonic Structure analysis using LF spectrum
     220             :  *--------------------------------------------------------------------------*/
     221             : 
     222        8352 : int16_t har_est(
     223             :     float spectra[],                       /* i  : coded spectrum                 */
     224             :     const int16_t N,                       /* i  : length of the desired spectrum */
     225             :     int16_t *har_freq_est1,                /* i/o: Estimation harmonics 1         */
     226             :     int16_t *har_freq_est2,                /* o  : Estimation harmonics 2         */
     227             :     int16_t *flag_dis,                     /* i/o: flag for BWE reconstruction    */
     228             :     int16_t *prev_frm_hfe2,                /* i/o: Estimated harmonic update      */
     229             :     const int16_t subband_search_offset[], /* i  : Subband Search range           */
     230             :     const int16_t sbWidth[],               /* i  : Subband Search range           */
     231             :     int16_t *prev_stab_hfe2                /* i/o: Estimated harmonic position    */
     232             : )
     233             : {
     234             :     float peak;
     235             :     int16_t i, j, q, k, k1, k2;
     236             :     float input_abs[L_FRAME32k], blk_peak[30];
     237             :     int16_t blk_end, blk_st;
     238             :     int16_t peak_pos, blk_peak_pos[30], diff_peak_pos[30], sharp, sharp1;
     239             :     int16_t min_har_pos;
     240             :     int16_t blk_peak_pos_te[30];
     241             :     float blk_peak_te[30];
     242             :     int16_t temp;
     243             :     int16_t hfe_est_countk, hfe_est_countk1, hfe_est_countk2;
     244             :     int16_t r1, r2, r3;
     245             :     int16_t start_pos;
     246             :     float blk_peak_max;
     247             :     int16_t blk_peak_pos_max;
     248        8352 :     int16_t nlags, ct_hfsb2, sum_diff = 0;
     249             :     int16_t blk_peak_pos_hfsb2[30], diff_peak_pos_hfsb2[30];
     250        8352 :     int16_t rem_hfe2, q_diffpos_hfe2 = 0, diff_posmax_hfe2, q_diffpos_prevhfe2;
     251             :     int16_t blk_peak_max_idx, blk_peak_pos_max_diff, diff_peak_pos_te[30];
     252             : 
     253        8352 :     rem_hfe2 = 0;
     254        8352 :     q_diffpos_hfe2 = 0;
     255        8352 :     diff_posmax_hfe2 = 0;
     256        8352 :     q_diffpos_prevhfe2 = 0;
     257             : 
     258        8352 :     set_s( blk_peak_pos_te, 0, 30 );
     259        8352 :     set_s( diff_peak_pos, 0, 30 );
     260             : 
     261        8352 :     r1 = SWB_HAR_RAN1;
     262        8352 :     r2 = SWB_HAR_RAN2;
     263        8352 :     r3 = SWB_HAR_RAN3;
     264        8352 :     start_pos = r1;
     265             : 
     266             :     /* Copy the abs values of LF spectrum*/
     267     1845792 :     for ( i = start_pos; i < N; i++ )
     268             :     {
     269     1837440 :         input_abs[i] = (float) fabs( spectra[i] );
     270             :     }
     271             : 
     272        8352 :     blk_end = (int16_t) N / LR_BLK_LEN;
     273        8352 :     blk_st = (int16_t) start_pos / LR_BLK_LEN;
     274             : 
     275        8352 :     if ( (float) N / (LR_BLK_LEN) -blk_end > 0.0f )
     276             :     {
     277        8352 :         blk_end++;
     278             :     }
     279             : 
     280             :     /* initialization of over buffer for fractional point */
     281       41760 :     for ( i = N; i < ( blk_end * LR_BLK_LEN ); i++ )
     282             :     {
     283       33408 :         input_abs[i] = 0.0f;
     284             :     }
     285             : 
     286        8352 :     q = start_pos;
     287             : 
     288             :     /* Block Processing, to detect the spectral peaks*/
     289      125280 :     for ( i = blk_st; i < blk_end; i++ )
     290             :     {
     291      116928 :         peak = 0.0f;
     292      116928 :         peak_pos = 0;
     293             : 
     294     1987776 :         for ( j = 0; j < LR_BLK_LEN; j++, q++ )
     295             :         {
     296     1870848 :             if ( input_abs[q] > peak )
     297             :             {
     298      289589 :                 peak = input_abs[q];
     299      289589 :                 peak_pos = q;
     300             :             }
     301             : 
     302     1870848 :             if ( i > blk_st && input_abs[q] != 0 && input_abs[q] == peak && ( peak_pos - blk_peak_pos[i - 1] ) < LR_HLF_PK_BLK_LEN )
     303             :             {
     304       55727 :                 peak = input_abs[q];
     305       55727 :                 peak_pos = q;
     306             :             }
     307             :         }
     308             : 
     309      116928 :         blk_peak[i] = peak;
     310      116928 :         blk_peak_pos[i] = peak_pos;
     311             :     }
     312             : 
     313      125280 :     for ( i = blk_st; i < blk_end; i++ )
     314             :     {
     315      116928 :         if ( i > blk_st )
     316             :         {
     317      108576 :             if ( blk_peak_pos[i] != 0 && blk_peak_pos[i - 1] != 0 )
     318             :             {
     319       79738 :                 if ( ( blk_peak_pos[i] - blk_peak_pos[i - 1] ) < LR_LOWBAND_DIF_PK_LEN )
     320             :                 {
     321       13195 :                     if ( blk_peak[i] > blk_peak[i - 1] )
     322             :                     {
     323        8319 :                         blk_peak[i - 1] = 0.0f;
     324        8319 :                         blk_peak_pos[i - 1] = 0;
     325             :                     }
     326             :                     else
     327             :                     {
     328        4876 :                         blk_peak[i] = blk_peak[i - 1];
     329        4876 :                         blk_peak_pos[i] = blk_peak_pos[i - 1];
     330        4876 :                         blk_peak[i - 1] = 0.0f;
     331        4876 :                         blk_peak_pos[i - 1] = 0;
     332             :                     }
     333             :                 }
     334             :             }
     335             :         }
     336             :     }
     337             : 
     338             :     /* peak counts in each group */
     339        8352 :     j = 0;
     340        8352 :     hfe_est_countk = 0;
     341        8352 :     hfe_est_countk1 = 0;
     342        8352 :     hfe_est_countk2 = 0;
     343      125280 :     for ( i = blk_st; i < blk_end; i++ )
     344             :     {
     345      116928 :         if ( blk_peak_pos[i] != 0 )
     346             :         {
     347       78538 :             blk_peak_pos_te[j] = blk_peak_pos[i];
     348       78538 :             if ( blk_peak_pos[i] < r2 )
     349             :             {
     350       26377 :                 hfe_est_countk++;
     351             :             }
     352       52161 :             else if ( blk_peak_pos[i] < r3 )
     353             :             {
     354       24038 :                 hfe_est_countk1++;
     355             :             }
     356             :             else
     357             :             {
     358       28123 :                 hfe_est_countk2++;
     359             :             }
     360             : 
     361       78538 :             blk_peak_te[j] = blk_peak[i];
     362       78538 :             j++;
     363             :         }
     364             :     }
     365             : 
     366        8352 :     min_har_pos = SWB_HAR_RAN1;
     367        8352 :     temp = 0;
     368        8352 :     blk_peak_max = blk_peak_te[0];
     369        8352 :     blk_peak_pos_max = blk_peak_pos_te[0];
     370        8352 :     blk_peak_max_idx = 0;
     371       78538 :     for ( i = 1; i < j; i++ )
     372             :     {
     373       70186 :         diff_peak_pos[i - 1] = blk_peak_pos_te[i] - blk_peak_pos_te[i - 1];
     374       70186 :         if ( diff_peak_pos[i - 1] <= min_har_pos )
     375             :         {
     376       25054 :             min_har_pos = diff_peak_pos[i - 1];
     377             :         }
     378             : 
     379       70186 :         if ( blk_peak_te[i - 1] > blk_peak_max )
     380             :         {
     381       13518 :             blk_peak_max = blk_peak_te[i - 1];
     382       13518 :             blk_peak_pos_max = blk_peak_pos_te[i - 1];
     383       13518 :             blk_peak_max_idx = i - 1;
     384             :         }
     385             : 
     386       70186 :         temp++;
     387             :     }
     388        8352 :     blk_peak_pos_max_diff = diff_peak_pos[blk_peak_max_idx];
     389             : 
     390             :     /* Decision for BWE reconstruction */
     391        8352 :     if ( ( hfe_est_countk < 2 && hfe_est_countk1 < 2 && hfe_est_countk2 < 2 ) || min_har_pos >= SWB_HAR_RAN1 )
     392             :     {
     393           0 :         *flag_dis = 0;
     394           0 :         if ( ( hfe_est_countk == 1 && hfe_est_countk1 == 1 ) && ( hfe_est_countk2 == 1 || hfe_est_countk2 == 0 ) )
     395             :         {
     396           0 :             *flag_dis = 1;
     397             :         }
     398             :     }
     399       78538 :     for ( i = 0; i < temp; i++ )
     400             :     {
     401       70186 :         if ( blk_peak_pos_max_diff + LR_LOWBAND_DIF_PK_LEN < diff_peak_pos[i] )
     402             :         {
     403        7529 :             diff_peak_pos[i] = 0;
     404             :         }
     405             :     }
     406        8352 :     mvs2s( diff_peak_pos, diff_peak_pos_te, temp );
     407        8352 :     set_s( diff_peak_pos, -1, temp );
     408        8352 :     j = 0;
     409       78538 :     for ( i = 0; i < temp; i++ )
     410             :     {
     411       70186 :         if ( diff_peak_pos_te[i] != 0 )
     412             :         {
     413       62657 :             diff_peak_pos[j] = diff_peak_pos_te[i];
     414       62657 :             j++;
     415             :         }
     416             :     }
     417        8352 :     temp = j;
     418             :     /* harmonic estimation analysis to perform BWE Reconstruction */
     419        8352 :     if ( *flag_dis )
     420             :     {
     421        8352 :         sharp = 0;
     422        8352 :         k = 0;
     423        8352 :         k1 = 0;
     424        8352 :         sharp1 = 0;
     425        8352 :         k2 = 0;
     426             : 
     427       71009 :         for ( i = 0, q = 1; i < temp; i++, q++ )
     428             :         {
     429       62657 :             if ( ( diff_peak_pos[i] <= ( min_har_pos + LR_LOWBAND_DIF_PK_LEN ) ) && diff_peak_pos[i] > 0 )
     430             :             {
     431       49837 :                 sharp += diff_peak_pos[i];
     432       49837 :                 k++;
     433             :             }
     434       12820 :             else if ( ( diff_peak_pos[i] <= ( min_har_pos + 2 * LR_LOWBAND_DIF_PK_LEN ) ) && diff_peak_pos[i] > 0 )
     435             :             {
     436       11479 :                 sharp1 += diff_peak_pos[i];
     437       11479 :                 k1++;
     438             :             }
     439        1341 :             else if ( diff_peak_pos[i] > 0 )
     440             :             {
     441        1341 :                 k2++;
     442             :             }
     443             :         }
     444             : 
     445        8352 :         est_freq_har_decis( har_freq_est1, har_freq_est2, sharp, sharp1, hfe_est_countk1, hfe_est_countk2, k, k1, k2, prev_frm_hfe2 );
     446             : 
     447        8352 :         blk_peak_pos_max = blk_peak_pos_te[temp - 1];
     448             : 
     449        8352 :         if ( ( *prev_stab_hfe2 ) > 0 && ( *prev_frm_hfe2 ) > 0 && *prev_stab_hfe2 < N )
     450             :         {
     451           0 :             rem_hfe2 = *har_freq_est2 % ( *prev_frm_hfe2 );
     452           0 :             diff_posmax_hfe2 = (int16_t) abs( blk_peak_pos_max - *prev_stab_hfe2 );
     453           0 :             if ( rem_hfe2 == 0 )
     454             :             {
     455           0 :                 if ( diff_posmax_hfe2 < 9 || *har_freq_est2 == 0 )
     456             :                 {
     457           0 :                     blk_peak_pos_max = *prev_stab_hfe2;
     458             :                 }
     459             :                 else
     460             :                 {
     461           0 :                     q_diffpos_hfe2 = diff_posmax_hfe2 / ( *har_freq_est2 );
     462           0 :                     q_diffpos_prevhfe2 = diff_posmax_hfe2 / ( *prev_frm_hfe2 );
     463           0 :                     if ( q_diffpos_hfe2 < 10 || q_diffpos_prevhfe2 < 10 )
     464             :                     {
     465           0 :                         blk_peak_pos_max = *prev_stab_hfe2;
     466             :                     }
     467             :                     else
     468             :                     {
     469           0 :                         *prev_stab_hfe2 = blk_peak_pos_max;
     470             :                     }
     471             :                 }
     472             :             }
     473             :             else
     474             :             {
     475           0 :                 *prev_stab_hfe2 = blk_peak_pos_max;
     476             :             }
     477             :         }
     478             :         else
     479             :         {
     480        8352 :             *prev_stab_hfe2 = blk_peak_pos_max;
     481             :         }
     482             : 
     483        8352 :         if ( *har_freq_est1 == 0 || *har_freq_est2 == 0 )
     484             :         {
     485           0 :             *flag_dis = 0;
     486             :         }
     487             :     }
     488             : 
     489        8352 :     if ( *flag_dis == 0 )
     490             :     {
     491           0 :         if ( *prev_frm_hfe2 != 0 )
     492             :         {
     493           0 :             *har_freq_est2 = *prev_frm_hfe2;
     494             :         }
     495             :         else
     496             :         {
     497           0 :             nlags = (int16_t) pow( 2, bits_lagIndices_mode0_Har[0] );
     498           0 :             ct_hfsb2 = 0;
     499           0 :             for ( i = 0; i < j; i++ )
     500             :             {
     501           0 :                 if ( blk_peak_pos_te[i] >= ( subband_search_offset[0] - nlags / 2 ) && blk_peak_pos_te[i] < ( subband_search_offset[0] + sbWidth[0] + nlags / 2 ) )
     502             :                 {
     503           0 :                     blk_peak_pos_hfsb2[ct_hfsb2] = blk_peak_pos_te[i];
     504           0 :                     ct_hfsb2++;
     505             :                 }
     506             :             }
     507             : 
     508           0 :             if ( ct_hfsb2 > 1 )
     509             :             {
     510           0 :                 for ( i = 1; i < ct_hfsb2; i++ )
     511             :                 {
     512           0 :                     diff_peak_pos_hfsb2[i - 1] = blk_peak_pos_hfsb2[i] - blk_peak_pos_hfsb2[i - 1];
     513           0 :                     sum_diff += diff_peak_pos_hfsb2[i - 1];
     514             :                 }
     515           0 :                 *har_freq_est2 = sum_diff / ct_hfsb2;
     516             :             }
     517             :             else
     518             :             {
     519           0 :                 *har_freq_est2 = min_har_pos;
     520             :             }
     521             :         }
     522             :     }
     523             : 
     524             : 
     525        8352 :     return blk_peak_pos_max;
     526             : }
     527             : 
     528             : 
     529             : /*-------------------------------------------------------------------*
     530             :  * genhf_noise()
     531             :  *
     532             :  *
     533             :  *-------------------------------------------------------------------*/
     534             : 
     535           0 : void genhf_noise(
     536             :     float noise_flr[],                    /* i  : smoothed non tonal                           */
     537             :     float xSynth_har[],                   /* o  : hf non tonal components                      */
     538             :     float *predBuf,                       /* i  : smoothed tonal compone                       */
     539             :     int16_t bands,                        /* i  : total number of subbands in a frame          */
     540             :     int16_t harmonic_band,                /* i  : Number of LF harmonic frames                 */
     541             :     int16_t har_freq_est2,                /* i  : harmonic signal parameter                    */
     542             :     int16_t pos_max_hfe2,                 /* i  : last pulse in core coder                     */
     543             :     int16_t *pul_res,                     /* o  : pulse resolution                             */
     544             :     GainItem pk_sf[],                     /* o  : representative region                        */
     545             :     const int16_t fLenLow,                /* i  : low frequency length                         */
     546             :     const int16_t fLenHigh,               /* i  : high frequency length                        */
     547             :     const int16_t sbWidth[],              /* i  : bandwidth for high bands                     */
     548             :     const int16_t lagIndices[],           /* i  : correlation indices for most representative  */
     549             :     const int16_t subband_offsets[],      /* i  : band offsets for HF reconstruction           */
     550             :     const int16_t subband_search_offset[] /* i  : most representative regions offsets in LF    */
     551             : )
     552             : {
     553             :     int16_t k, j, ii, st_pos, dst_pos;
     554             :     int16_t nlags[NB_SWB_SUBBANDS_HAR_SEARCH_SB];
     555             :     float tmpbuf[L_FRAME32k];
     556             :     int16_t hfband_end[NB_SWB_SUBBANDS];
     557             :     int16_t rem_hfe, temp_last_peakpos, i, l, pos, res;
     558             :     float hf_pulse_peaks[160], pulse_peak_sb[320];
     559             :     int16_t st_last_peakpos;
     560             : 
     561           0 :     set_f( tmpbuf, 0.0f, L_FRAME32k );
     562           0 :     for ( k = 0; k < 3; k++ )
     563             :     {
     564           0 :         hfband_end[k] = fLenLow + subband_offsets[k + 1];
     565             :     }
     566           0 :     hfband_end[3] = fLenLow + fLenHigh;
     567             : 
     568           0 :     rem_hfe = (int16_t) ( fLenLow - pos_max_hfe2 - 1 ) / har_freq_est2;
     569           0 :     st_last_peakpos = pos_max_hfe2 + ( rem_hfe * har_freq_est2 );
     570           0 :     temp_last_peakpos = st_last_peakpos;
     571           0 :     i = 0;
     572             : 
     573           0 :     for ( k = 0; k < 2; k++ )
     574             :     {
     575           0 :         nlags[k] = (int16_t) pow( 2, bits_lagIndices_mode0_Har[k] );
     576             : 
     577           0 :         l = 0;
     578           0 :         while ( st_last_peakpos < ( fLenLow + subband_offsets[k] ) )
     579             :         {
     580           0 :             st_last_peakpos += har_freq_est2;
     581             :         }
     582           0 :         st_last_peakpos -= har_freq_est2;
     583             : 
     584           0 :         if ( k == 0 )
     585             :         {
     586           0 :             st_pos = subband_search_offset[k] - nlags[k] / 2 + lagIndices[k];
     587             : 
     588             :             /*Copy the LF Smoothed Noise to the HF*/
     589           0 :             for ( j = 0; j < sbWidth[k]; j++ )
     590             :             {
     591           0 :                 xSynth_har[j] = noise_flr[st_pos + j];
     592           0 :                 tmpbuf[j] = xSynth_har[j];
     593           0 :                 if ( predBuf[st_pos + j] != 0.0f )
     594             :                 {
     595           0 :                     hf_pulse_peaks[l] = predBuf[st_pos + j];
     596           0 :                     l++;
     597             :                 }
     598             :             }
     599             :         }
     600             :         else
     601             :         {
     602           0 :             st_pos = subband_search_offset[k] + nlags[k] / 2 - lagIndices[k];
     603           0 :             dst_pos = st_pos - sbWidth[k];
     604           0 :             ii = sbWidth[k - 1];
     605             :             /*Copy the LF Smoothed Noise floor to the HF*/
     606           0 :             for ( j = st_pos; j > ( dst_pos ) && ii < ( sbWidth[k] + sbWidth[k - 1] ); j-- )
     607             :             {
     608           0 :                 xSynth_har[ii] = noise_flr[j];
     609           0 :                 tmpbuf[ii] = xSynth_har[ii];
     610           0 :                 if ( predBuf[j] != 0.0f )
     611             :                 {
     612           0 :                     hf_pulse_peaks[l] = predBuf[j];
     613           0 :                     l++;
     614             :                 }
     615           0 :                 ii++;
     616             :             }
     617             :         }
     618           0 :         pos = 0;
     619           0 :         for ( j = 0; j < l; j++ )
     620             :         {
     621           0 :             st_last_peakpos += har_freq_est2;
     622           0 :             if ( st_last_peakpos < hfband_end[k] )
     623             :             {
     624           0 :                 pk_sf[k * 8 + pos].nmrValue = hf_pulse_peaks[j];
     625           0 :                 pk_sf[k * 8 + pos].gainIndex = st_last_peakpos - fLenLow;
     626           0 :                 pul_res[k]++;
     627           0 :                 pulse_peak_sb[i] = hf_pulse_peaks[j];
     628           0 :                 i++;
     629           0 :                 pos++;
     630             :             }
     631             :         }
     632           0 :         st_last_peakpos = temp_last_peakpos;
     633             :     }
     634           0 :     res = i - 1;
     635           0 :     l = 1;
     636           0 :     ii = hfband_end[k - 1] - fLenLow - 1;
     637           0 :     for ( ; k < ( bands - harmonic_band ); k++ )
     638             :     {
     639           0 :         for ( j = hfband_end[k - 1] - fLenLow; j < ( hfband_end[k] - fLenLow ); j++ )
     640             :         {
     641           0 :             xSynth_har[j] = tmpbuf[ii];
     642           0 :             tmpbuf[j] = xSynth_har[j];
     643           0 :             ii--;
     644             :         }
     645           0 :         pos = 0;
     646           0 :         while ( st_last_peakpos < hfband_end[k - 1] )
     647             :         {
     648           0 :             st_last_peakpos += har_freq_est2;
     649             :         }
     650           0 :         while ( st_last_peakpos < hfband_end[k] && pul_res[k] < pul_res[2 - l] && l <= 2 )
     651             :         {
     652           0 :             pk_sf[k * 8 + pos].nmrValue = pulse_peak_sb[res];
     653           0 :             pk_sf[k * 8 + pos].gainIndex = st_last_peakpos - fLenLow;
     654           0 :             pul_res[k]++;
     655           0 :             res--;
     656           0 :             pos++;
     657           0 :             st_last_peakpos += har_freq_est2;
     658             :         }
     659           0 :         l++;
     660             :     }
     661             : 
     662           0 :     return;
     663             : }
     664             : 
     665             : /*-------------------------------------------------------------------*
     666             :  * SmoothSpec()
     667             :  *
     668             :  * Smoothes specified samples using moving average method. The number
     669             :  * of points in the average is given by 'span'. Note that current
     670             :  * implementation does not accept 'span' to be smaller than 'fLen'.
     671             :  *-------------------------------------------------------------------*/
     672             : 
     673           0 : static void SmoothSpec(
     674             :     const float *inBuf, /* i  : input               */
     675             :     float *outBuf,      /* o  : output              */
     676             :     const int16_t fLen, /* i  : length              */
     677             :     int16_t span        /* i  : averaging length    */
     678             : )
     679             : {
     680             :     int16_t i, span1, nItems;
     681             :     float sum, ispan;
     682             :     const float *oldPtr, *newPtr;
     683             : 
     684             :     /* not accepted */
     685           0 :     if ( span > fLen )
     686             :     {
     687           0 :         mvr2r( inBuf, outBuf, fLen );
     688           0 :         return;
     689             :     }
     690             : 
     691             :     /* span must be odd */
     692           0 :     if ( ( span & 0x1 ) == 0 )
     693             :     {
     694           0 :         span--;
     695             :     }
     696             : 
     697           0 :     span1 = span >> 1;
     698             : 
     699             :     /* first sample */
     700           0 :     sum = *inBuf;
     701           0 :     *outBuf++ = sum;
     702             : 
     703           0 :     oldPtr = inBuf;
     704           0 :     newPtr = inBuf + 2;
     705             : 
     706             :     /* handle start */
     707           0 :     inBuf++;
     708           0 :     sum += *inBuf;
     709             : 
     710           0 :     for ( i = 1, nItems = 3; i < span1; i++, inBuf++ )
     711             :     {
     712           0 :         sum += *newPtr++;
     713           0 :         *outBuf++ = sum / nItems;
     714             : 
     715           0 :         sum += *newPtr++;
     716           0 :         nItems += 2;
     717             :     }
     718             : 
     719           0 :     ispan = 1.0f / span;
     720           0 :     inBuf++;
     721           0 :     i++;
     722           0 :     sum += *newPtr++;
     723           0 :     *outBuf++ = sum * ispan;
     724             : 
     725             :     /* moving average */
     726           0 :     for ( ; i < fLen - span1; i++, inBuf++ )
     727             :     {
     728           0 :         sum += *newPtr++;
     729           0 :         sum -= *oldPtr++;
     730           0 :         *outBuf++ = sum * ispan;
     731             :     }
     732             : 
     733             :     /* handle end */
     734           0 :     nItems = span - 2;
     735           0 :     sum -= *oldPtr++;
     736             : 
     737           0 :     for ( ; i < fLen - 1; i++, inBuf++ )
     738             :     {
     739           0 :         sum -= *oldPtr++;
     740           0 :         *outBuf++ = sum / nItems;
     741           0 :         nItems -= 2;
     742           0 :         sum -= *oldPtr++;
     743             :     }
     744             : 
     745             :     /* last sample */
     746           0 :     *outBuf = *inBuf;
     747             : 
     748           0 :     return;
     749             : }
     750             : 
     751             : /*-------------------------------------------------------------------*
     752             :  * SpectrumSmoothing()
     753             :  *
     754             :  * Smoothing of the low-frequency envelope
     755             :  *-------------------------------------------------------------------*/
     756             : 
     757         248 : void SpectrumSmoothing(
     758             :     float *inBuf,       /* i  : input     */
     759             :     float *outBuf,      /* o  : output    */
     760             :     const int16_t fLen, /* i  : length    */
     761             :     const float th_cut  /* i  : threshold of cut */
     762             : )
     763             : {
     764             :     int16_t i, k;
     765             :     float inBuf_pss[L_FRAME32k];
     766             :     float outBuf_pss[L_FRAME32k];
     767             :     float max_val[L_FRAME32k / L_SB];
     768             :     float max_val_norm;
     769             :     float inBuf_abs;
     770             :     int16_t j;
     771             :     int16_t num_subband_smooth;
     772             :     int16_t m, n;
     773             :     int16_t cnt_zero_cont;
     774             :     int16_t n_list[BANDS_MAX];
     775             :     int16_t reset_flag;
     776             : 
     777         248 :     num_subband_smooth = (int16_t) fLen / L_SB;
     778         248 :     if ( (float) fLen / L_SB - num_subband_smooth > 0.0f )
     779             :     {
     780         248 :         num_subband_smooth++;
     781             :     }
     782             : 
     783       63736 :     for ( i = 0; i < fLen; i++ )
     784             :     {
     785       63488 :         inBuf_pss[i] = inBuf[i];
     786       63488 :         outBuf_pss[i] = 0.0f;
     787             :     }
     788             : 
     789        2232 :     for ( i = fLen; i < fLen + ( num_subband_smooth * L_SB - fLen ); i++ )
     790             :     {
     791        1984 :         inBuf_pss[i] = 0.0f;
     792        1984 :         outBuf_pss[i] = 0.0f;
     793             :     }
     794             : 
     795         248 :     j = 0;
     796        5704 :     for ( i = 0; i < num_subband_smooth; i++ )
     797             :     {
     798        5456 :         max_val[i] = 0;
     799       70928 :         for ( k = 0; k < L_SB; k++ )
     800             :         {
     801       65472 :             inBuf_abs = (float) fabs( inBuf_pss[j] );
     802       65472 :             if ( max_val[i] < inBuf_abs )
     803             :             {
     804        9588 :                 max_val[i] = inBuf_abs;
     805             :             }
     806             : 
     807       65472 :             j++;
     808             :         }
     809             :     }
     810             : 
     811             :     /* convert to maximum amplitude frequency log scale envelope */
     812         248 :     j = 0;
     813        5704 :     for ( i = 0; i < num_subband_smooth; i++ )
     814             :     {
     815        5456 :         max_val_norm = 0.0f;
     816        5456 :         if ( max_val[i] != 0.0f )
     817             :         {
     818        3944 :             max_val_norm = 10.0f / max_val[i];
     819             :         }
     820             : 
     821       70928 :         for ( k = 0; k < L_SB; k++ )
     822             :         {
     823       65472 :             if ( inBuf_pss[j] == 0.0f )
     824             :             {
     825       36620 :                 outBuf_pss[j] = 0.0f;
     826             :             }
     827       28852 :             else if ( fabs( inBuf_pss[j] ) < max_val[i] )
     828             :             {
     829       24432 :                 outBuf_pss[j] = inBuf_pss[j] * max_val_norm;
     830             :             }
     831             :             else
     832             :             {
     833             :                 /* CLIP , for avoiding computational difference */
     834        4420 :                 outBuf_pss[j] = 10.0f;
     835        4420 :                 if ( inBuf_pss[j] < 0.0f )
     836             :                 {
     837        2204 :                     outBuf_pss[j] = -10.0f;
     838             :                 }
     839             :             }
     840       65472 :             j++;
     841             :         }
     842             :     }
     843         248 :     k = 0;
     844         248 :     m = 0;
     845         248 :     n = 0;
     846         248 :     reset_flag = 0;
     847         248 :     n_list[0] = 0;
     848        5704 :     for ( j = 0; j < num_subband_smooth; j++ )
     849             :     {
     850        5456 :         cnt_zero_cont = 0;
     851       70928 :         for ( i = 0; i < L_SB; i++ )
     852             :         {
     853       65472 :             if ( outBuf_pss[k] == 0.0f )
     854             :             {
     855       36620 :                 cnt_zero_cont++;
     856             :             }
     857             :             else
     858             :             {
     859       28852 :                 cnt_zero_cont = 0;
     860             :             }
     861       65472 :             k++;
     862             :         }
     863             : 
     864        5456 :         if ( cnt_zero_cont != 0 )
     865             :         {
     866        3128 :             if ( j > subband_search_offsets[0] / L_SB && reset_flag == 0 )
     867             :             {
     868         248 :                 n = 0;
     869         248 :                 reset_flag = 1;
     870             :             }
     871        3128 :             n_list[n] = j;
     872        3128 :             n++;
     873             :         }
     874             : 
     875        5456 :         if ( reset_flag == 1 && n == 1 )
     876             :         {
     877         508 :             m = 0;
     878             :         }
     879             : 
     880        5456 :         if ( cnt_zero_cont > 3 * L_SB / 4 )
     881             :         {
     882       21476 :             for ( i = 0; i < L_SB; i++ )
     883             :             {
     884       19824 :                 if ( outBuf_pss[k - L_SB + i] == 0.0f )
     885             :                 {
     886       19664 :                     outBuf_pss[k - L_SB + i] = outBuf_pss[n_list[m] * L_SB + i] * 0.5f;
     887             :                 }
     888             :             }
     889        1652 :             m++;
     890             :         }
     891             :     }
     892             : 
     893       63736 :     for ( i = 0; i < fLen; i++ )
     894             :     {
     895       63488 :         outBuf[i] = 0.0f;
     896       63488 :         if ( fabs( outBuf_pss[i] ) > th_cut )
     897             :         {
     898       31436 :             outBuf[i] = outBuf_pss[i];
     899             :         }
     900             :     }
     901             : 
     902         248 :     return;
     903             : }
     904             : 
     905             : /*-------------------------------------------------------------------*
     906             :  * Get20Log10Spec()
     907             :  *
     908             :  * Calculates 20*log10() for the specified samples. Input and output buffers can be the same.
     909             :  *-------------------------------------------------------------------*/
     910             : 
     911           0 : static void Get20Log10Spec(
     912             :     const float *inBuf, /* i  : input           */
     913             :     float *outBuf,      /* o  : output          */
     914             :     const int16_t fLen  /* i  : loop length     */
     915             : )
     916             : {
     917             :     int16_t i;
     918             : 
     919           0 :     for ( i = 0; i < fLen; i++, inBuf++ )
     920             :     {
     921           0 :         *outBuf++ = (float) ( 20.0f * log10( fabs( *inBuf + 1.0 ) ) );
     922             :     }
     923             : 
     924           0 :     return;
     925             : }
     926             : 
     927             : /*-------------------------------------------------------------------*
     928             :  * convert_lagIndices_pls2smp()
     929             :  *
     930             :  *
     931             :  *-------------------------------------------------------------------*/
     932             : 
     933         124 : void convert_lagIndices_pls2smp(
     934             :     int16_t lagIndices_in[],
     935             :     int16_t nBands_search,
     936             :     int16_t lagIndices_out[],
     937             :     const float sspectra[],
     938             :     const int16_t sbWidth[],
     939             :     const int16_t fLenLow )
     940             : {
     941             :     int16_t sb;
     942             :     int16_t i, cnt;
     943             : 
     944         620 :     for ( sb = 0; sb < nBands_search; sb++ )
     945             :     {
     946         496 :         cnt = 0;
     947         496 :         i = 0;
     948        5124 :         while ( cnt <= lagIndices_in[sb] )
     949             :         {
     950        4628 :             if ( sspectra[subband_search_offsets[sb] + i] != 0.0f )
     951             :             {
     952        1152 :                 cnt++;
     953             :             }
     954             : 
     955        4628 :             i++;
     956             : 
     957        4628 :             if ( subband_search_offsets[sb] + i + sbWidth[sb] >= fLenLow )
     958             :             {
     959             :                 /* over fLenLow, no need for more search */
     960           0 :                 break;
     961             :             }
     962             :         }
     963             : 
     964         496 :         lagIndices_out[sb] = i - 1 + subband_search_offsets[sb];
     965             :     }
     966             : 
     967         124 :     return;
     968             : }
     969             : 
     970             : 
     971             : /*-------------------------------------------------------------------*
     972             :  * get_usebit_npswb()
     973             :  *
     974             :  *
     975             :  *-------------------------------------------------------------------*/
     976             : 
     977         124 : int16_t get_usebit_npswb(
     978             :     const int16_t hqswb_clas )
     979             : {
     980             :     int16_t i;
     981             :     int16_t bits;
     982             :     int16_t up_lmt;
     983             :     const int16_t *bits_req;
     984             : 
     985         124 :     up_lmt = 0;
     986         124 :     bits_req = bits_lagIndices_modeNormal;
     987         124 :     bits = 0;
     988         124 :     if ( hqswb_clas == HQ_NORMAL )
     989             :     {
     990         124 :         up_lmt = NB_SWB_SUBBANDS;
     991         124 :         bits_req = bits_lagIndices_modeNormal;
     992             :     }
     993           0 :     else if ( hqswb_clas == HQ_HARMONIC )
     994             :     {
     995           0 :         up_lmt = NB_SWB_SUBBANDS_HAR_SEARCH_SB;
     996           0 :         bits_req = bits_lagIndices_mode0_Har;
     997           0 :         bits = 2; /*noise gain*/
     998             :     }
     999         620 :     for ( i = 0; i < up_lmt; i++ )
    1000             :     {
    1001         496 :         bits += bits_req[i];
    1002             :     }
    1003             : 
    1004         124 :     return bits;
    1005             : }
    1006             : 
    1007             : 
    1008             : /*-------------------------------------------------------------------*
    1009             :  * SpectrumSmoothing_nss()
    1010             :  *
    1011             :  *
    1012             :  *-------------------------------------------------------------------*/
    1013             : 
    1014           0 : static void SpectrumSmoothing_nss(
    1015             :     const float *inBuf,
    1016             :     float *outBuf,
    1017             :     const int16_t fLen )
    1018             : {
    1019             :     int16_t i, k;
    1020             :     float inBufw[L_FRAME32k + L_SB_NSS];
    1021             :     float outBufw[L_FRAME32k + L_SB_NSS];
    1022             :     float temp_sum_1[NUM_SUBBAND_SMOOTH_MAX];
    1023             :     float temp_sum_2[NUM_SUBBAND_SMOOTH_MAX];
    1024             :     float temp_sum_3[NUM_SUBBAND_SMOOTH_MAX];
    1025             :     float temp_sum_log[NUM_SUBBAND_SMOOTH_MAX];
    1026             :     float temp_sum_smooth[NUM_SUBBAND_SMOOTH_MAX];
    1027             :     float temp_sum_div[NUM_SUBBAND_SMOOTH_MAX];
    1028             :     int16_t num_subband_smooth;
    1029             :     float smr, avg_val, r0;
    1030             :     float clip_cof, avg_val2, thre;
    1031             :     float thre_min;
    1032             :     float max_peak;
    1033             : 
    1034             :     /* calculate subband number */
    1035           0 :     num_subband_smooth = (int16_t) ( fLen / L_SB_NSS );
    1036           0 :     if ( fLen / ( L_SB_NSS + 0.0f ) - num_subband_smooth > 0.0f )
    1037             :     {
    1038           0 :         num_subband_smooth++;
    1039             :     }
    1040             : 
    1041             :     /* buffer copy for fractional point */
    1042           0 :     for ( i = 0; i < fLen; i++ )
    1043             :     {
    1044           0 :         inBufw[i] = inBuf[i];
    1045           0 :         outBufw[i] = 0.0f;
    1046             :     }
    1047             : 
    1048             :     /* initialization of over buffer for fractional point */
    1049           0 :     for ( i = fLen; i < fLen + L_SB_NSS; i++ )
    1050             :     {
    1051           0 :         inBufw[i] = 0.0f;
    1052           0 :         outBufw[i] = 0.0f;
    1053             :     }
    1054             : 
    1055           0 :     avg_val = 0.0f;
    1056           0 :     for ( i = 0; i < fLen; i++ )
    1057             :     {
    1058           0 :         r0 = (float) fabs( inBufw[i] );
    1059           0 :         avg_val += r0;
    1060             :     }
    1061           0 :     avg_val /= (float) fLen;
    1062             : 
    1063           0 :     max_peak = 0.0f;
    1064           0 :     for ( i = 0; i < fLen; i++ )
    1065             :     {
    1066           0 :         r0 = (float) fabs( inBufw[i] );
    1067           0 :         if ( max_peak < r0 )
    1068             :         {
    1069           0 :             max_peak = r0;
    1070             :         }
    1071             :     }
    1072             : 
    1073           0 :     smr = 10.0f * (float) log10( max_peak / ( avg_val + 1.0e-20 ) + 1.0e-20 );
    1074             : 
    1075           0 :     for ( i = 0; i < num_subband_smooth; i++ )
    1076             :     {
    1077           0 :         temp_sum_1[i] = 0.0;
    1078           0 :         temp_sum_2[i] = 0.0;
    1079             : 
    1080           0 :         for ( k = 0; k < L_SB_NSS_HALF; k++ )
    1081             :         {
    1082           0 :             temp_sum_1[i] += (float) ( fabs( inBufw[k + L_SB_NSS * i] ) );
    1083             :         }
    1084             : 
    1085           0 :         for ( k = L_SB_NSS_HALF; k < L_SB_NSS; k++ )
    1086             :         {
    1087           0 :             temp_sum_2[i] += (float) ( fabs( inBufw[k + L_SB_NSS * i] ) );
    1088             :         }
    1089             : 
    1090           0 :         temp_sum_1[i] *= 0.25f;
    1091           0 :         temp_sum_2[i] *= 0.25f;
    1092           0 :         temp_sum_3[i] = temp_sum_1[i] * temp_sum_2[i];
    1093             : 
    1094           0 :         if ( temp_sum_3[i] == 0.0f )
    1095             :         {
    1096           0 :             temp_sum_3[i] = temp_sum_1[i] + temp_sum_2[i];
    1097             :         }
    1098             :     }
    1099             : 
    1100           0 :     Get20Log10Spec( temp_sum_3, temp_sum_log, num_subband_smooth );
    1101             : 
    1102           0 :     for ( i = 0; i < num_subband_smooth; i++ )
    1103             :     {
    1104           0 :         temp_sum_log[i] *= 0.5f;
    1105             :     }
    1106             : 
    1107           0 :     SmoothSpec( temp_sum_log, temp_sum_smooth, num_subband_smooth, MA_LEN );
    1108             : 
    1109           0 :     for ( i = 0; i < num_subband_smooth; i++ )
    1110             :     {
    1111           0 :         temp_sum_div[i] = (float) pow( 10, -1.0f * temp_sum_smooth[i] / 20 );
    1112             :     }
    1113             : 
    1114           0 :     for ( i = 0; i < num_subband_smooth; i++ )
    1115             :     {
    1116           0 :         for ( k = 0; k < L_SB_NSS; k++ )
    1117             :         {
    1118           0 :             outBufw[k + L_SB_NSS * i] = inBufw[k + L_SB_NSS * i] * temp_sum_div[i];
    1119             :         }
    1120             :     }
    1121             : 
    1122           0 :     avg_val2 = 0.0f;
    1123           0 :     for ( i = 0; i < fLen; i++ )
    1124             :     {
    1125           0 :         r0 = (float) fabs( outBufw[i] );
    1126           0 :         avg_val2 += r0;
    1127             :     }
    1128           0 :     avg_val2 /= (float) fLen;
    1129             : 
    1130           0 :     clip_cof = smr - 16.0f;
    1131           0 :     if ( clip_cof < 0.0f )
    1132             :     {
    1133           0 :         clip_cof = 0.0f;
    1134             :     }
    1135           0 :     clip_cof += 2.5f;
    1136             : 
    1137           0 :     thre = avg_val2 * clip_cof;
    1138           0 :     thre_min = avg_val2 * 0.25f;
    1139           0 :     for ( i = 0; i < fLen; i++ )
    1140             :     {
    1141           0 :         if ( fabs( outBufw[i] ) > thre )
    1142             :         {
    1143           0 :             if ( outBufw[i] < 0.0f )
    1144             :             {
    1145           0 :                 outBufw[i] = -1.0f * thre;
    1146             :             }
    1147             :             else
    1148             :             {
    1149           0 :                 outBufw[i] = thre;
    1150             :             }
    1151             :         }
    1152             : 
    1153           0 :         if ( fabs( outBufw[i] ) < thre_min )
    1154             :         {
    1155           0 :             outBufw[i] = 0.0f;
    1156             :         }
    1157             :     }
    1158             : 
    1159           0 :     for ( i = 0; i < fLen; i++ )
    1160             :     {
    1161           0 :         outBuf[i] = outBufw[i];
    1162             :     }
    1163             : 
    1164           0 :     return;
    1165             : }
    1166             : 
    1167             : 
    1168             : /*-------------------------------------------------------------------*
    1169             :  * get_sigma()
    1170             :  *
    1171             :  *
    1172             :  *-------------------------------------------------------------------*/
    1173             : 
    1174         124 : static float get_sigma(
    1175             :     const float x_abs[],
    1176             :     const float avg,
    1177             :     const int16_t length )
    1178             : {
    1179             :     int16_t i;
    1180             :     float d;
    1181             :     float sigma;
    1182             : 
    1183         124 :     d = 0;
    1184        9796 :     for ( i = 0; i < length; i++ )
    1185             :     {
    1186        9672 :         d += x_abs[i] * x_abs[i];
    1187             :     }
    1188             : 
    1189         124 :     d /= ( length - 1 );
    1190         124 :     d -= avg * avg;
    1191             : 
    1192         124 :     sigma = (float) sqrt( d );
    1193             : 
    1194         124 :     return sigma;
    1195             : }
    1196             : 
    1197             : /*--------------------------------------------------------------------------*
    1198             :  * FindNBiggest2_simple()
    1199             :  *
    1200             :  * Finds N biggest components from input
    1201             :  * Maximum value allowed for nIdx is currently 140 and the maximum value of n is currently 20
    1202             :  *--------------------------------------------------------------------------*/
    1203             : 
    1204         124 : void FindNBiggest2_simple(
    1205             :     const float *inBuf, /* i  : input buffer (searched)                     */
    1206             :     GainItem *g,        /* o  : N biggest components found                  */
    1207             :     const int16_t nIdx, /* i  : search length                               */
    1208             :     int16_t *n,         /* i  : number of components searched (N biggest)   */
    1209             :     const int16_t N_NBIGGESTSEARCH )
    1210             : {
    1211             :     int16_t j;
    1212             :     float abs_in[400];
    1213             :     float avg_in;
    1214             :     float max_in;
    1215             :     float thr;
    1216             :     int16_t peak_cnt;
    1217             :     float sigma;
    1218             : 
    1219         124 :     max_in = 0;
    1220         124 :     avg_in = 0;
    1221        9796 :     for ( j = 0; j < nIdx; j++ )
    1222             :     {
    1223        9672 :         abs_in[j] = (float) fabs( inBuf[j] );
    1224             : 
    1225        9672 :         if ( max_in < abs_in[j] )
    1226             :         {
    1227         651 :             max_in = abs_in[j];
    1228             :         }
    1229             : 
    1230        9672 :         avg_in += abs_in[j];
    1231             :     }
    1232             : 
    1233         124 :     avg_in /= (float) nIdx;
    1234             : 
    1235         124 :     peak_cnt = 0;
    1236         124 :     if ( max_in <= 0.0001f )
    1237             :     {
    1238           0 :         for ( j = 0; j < N_NBIGGESTSEARCH; j++ )
    1239             :         {
    1240           0 :             g[peak_cnt].nmrValue = 0.0f;
    1241           0 :             g[peak_cnt].gainIndex = j;
    1242           0 :             peak_cnt++;
    1243             :         }
    1244             :     }
    1245             : 
    1246         124 :     sigma = get_sigma( abs_in, avg_in, nIdx );
    1247         124 :     thr = avg_in + sigma * 1.15f;
    1248             : 
    1249         124 :     if ( peak_cnt < N_NBIGGESTSEARCH )
    1250             :     {
    1251        9770 :         for ( j = 0; j < nIdx; j++ )
    1252             :         {
    1253        9647 :             if ( abs_in[j] > thr )
    1254             :             {
    1255        1204 :                 g[peak_cnt].nmrValue = abs_in[j];
    1256        1204 :                 g[peak_cnt].gainIndex = j;
    1257        1204 :                 abs_in[j] = 0.0f;
    1258        1204 :                 peak_cnt++;
    1259             :             }
    1260             : 
    1261        9647 :             if ( peak_cnt == N_NBIGGESTSEARCH )
    1262             :             {
    1263           1 :                 break;
    1264             :             }
    1265             :         }
    1266             :     }
    1267             : 
    1268         124 :     thr *= ( 0.3f / N_NBIGGESTSEARCH ) * peak_cnt + 0.7f;
    1269             : 
    1270         124 :     if ( peak_cnt < N_NBIGGESTSEARCH )
    1271             :     {
    1272        9398 :         for ( j = 0; j < nIdx; j++ )
    1273             :         {
    1274        9285 :             if ( abs_in[j] > thr )
    1275             :             {
    1276         432 :                 g[peak_cnt].nmrValue = abs_in[j];
    1277         432 :                 g[peak_cnt].gainIndex = j;
    1278         432 :                 abs_in[j] = 0.0f;
    1279         432 :                 peak_cnt++;
    1280             :             }
    1281             : 
    1282        9285 :             if ( peak_cnt == N_NBIGGESTSEARCH )
    1283             :             {
    1284          10 :                 break;
    1285             :             }
    1286             :         }
    1287             :     }
    1288             : 
    1289         124 :     thr *= ( 0.6f / N_NBIGGESTSEARCH ) * peak_cnt + 0.3f;
    1290         124 :     if ( peak_cnt < N_NBIGGESTSEARCH )
    1291             :     {
    1292        4164 :         for ( j = 0; j < nIdx; j++ )
    1293             :         {
    1294        4157 :             if ( abs_in[j] > thr )
    1295             :             {
    1296         584 :                 g[peak_cnt].nmrValue = abs_in[j];
    1297         584 :                 g[peak_cnt].gainIndex = j;
    1298         584 :                 abs_in[j] = 0.0f;
    1299         584 :                 peak_cnt++;
    1300             :             }
    1301             : 
    1302        4157 :             if ( peak_cnt == N_NBIGGESTSEARCH )
    1303             :             {
    1304         106 :                 break;
    1305             :             }
    1306             :         }
    1307             :     }
    1308             : 
    1309         124 :     *n = peak_cnt;
    1310             : 
    1311         124 :     return;
    1312             : }
    1313             : 
    1314             : /*--------------------------------------------------------------------------*
    1315             :  * spectrumsmooth_noiseton()
    1316             :  *
    1317             :  * Spectrum normalization for the the core coder
    1318             :  *--------------------------------------------------------------------------*/
    1319             : 
    1320         124 : float spectrumsmooth_noiseton(
    1321             :     float spectra[],          /* i  : core coder                                  */
    1322             :     const float spectra_ni[], /* i  : core coder with sparse filling              */
    1323             :     float sspectra[],         /* o  : Smoothed tonal information from core coder  */
    1324             :     float sspectra_diff[],    /* o  : non tonal infomration for gap filling       */
    1325             :     float sspectra_ni[],      /* o  : smoothed core coder                         */
    1326             :     const int16_t fLenLow,    /* i  : low frequency boundaries                    */
    1327             :     int16_t *ni_seed          /* io : random seed                                 */
    1328             : )
    1329             : {
    1330             :     float spectra_diff[L_FRAME32k];
    1331             :     float ni_ratio, ss_min, cut_sig_th, cut_ni_th;
    1332             :     int16_t i, pcnt, sign;
    1333             :     float spectra_rm[L_FRAME32k];
    1334         124 :     float cut_input = 0.1f;
    1335             :     float rand_a[L_FRAME32k];
    1336             : 
    1337             :     /* pre-prepare random array for float-fix interoperability */
    1338       31868 :     for ( i = 0; i < fLenLow; i++ )
    1339             :     {
    1340       31744 :         rand_a[i] = own_random( ni_seed ) / PCM16_TO_FLT_FAC;
    1341             :     }
    1342             : 
    1343             :     /*Get the pulse resolution for the core coder*/
    1344         124 :     pcnt = 0;
    1345       31868 :     for ( i = 0; i < fLenLow; i++ )
    1346             :     {
    1347       31744 :         if ( spectra[i] != 0.0 )
    1348             :         {
    1349        3424 :             pcnt++;
    1350             :         }
    1351             :     }
    1352             : 
    1353         124 :     ni_ratio = 4.0f * ( pcnt ) / ( fLenLow + 0.0f );
    1354         124 :     ni_ratio = min( 0.9f, ni_ratio );
    1355             : 
    1356         124 :     ss_min = ni_ratio * 10.0f;
    1357         124 :     cut_sig_th = ss_min / 4.0f;
    1358         124 :     cut_sig_th = max( 0.95f, cut_sig_th );
    1359             :     /*core coder normalization for gap filling*/
    1360       31868 :     for ( i = 0; i < fLenLow; i++ )
    1361             :     {
    1362       31744 :         spectra_rm[i] = 0.0f;
    1363       31744 :         if ( fabs( spectra[i] ) > cut_input )
    1364             :         {
    1365        3424 :             spectra_rm[i] = spectra[i];
    1366             :         }
    1367             :     }
    1368         124 :     SpectrumSmoothing( spectra_rm, sspectra, fLenLow, cut_sig_th );
    1369             :     /*Extract noise informaton from the core coder*/
    1370         124 :     mvr2r( sspectra, sspectra_ni, fLenLow );
    1371       31868 :     for ( i = 0; i < fLenLow; i++ )
    1372             :     {
    1373       31744 :         spectra_diff[i] = spectra_ni[i] - spectra[i];
    1374             :     }
    1375         124 :     cut_ni_th = 0.0f;
    1376             :     /*normalize sparse filled components*/
    1377       31868 :     for ( i = 0; i < fLenLow; i++ )
    1378             :     {
    1379       31744 :         spectra_rm[i] = 0.0f;
    1380       31744 :         if ( fabs( spectra_diff[i] ) > cut_input )
    1381             :         {
    1382       25428 :             spectra_rm[i] = spectra_diff[i];
    1383             :         }
    1384             :     }
    1385         124 :     SpectrumSmoothing( spectra_rm, sspectra_diff, fLenLow, cut_ni_th );
    1386             :     /*Normalized corecoder for Gap filling */
    1387       31868 :     for ( i = 0; i < fLenLow; i++ )
    1388             :     {
    1389       31744 :         sign = 1;
    1390       31744 :         if ( sspectra[i] < 0 )
    1391             :         {
    1392        2828 :             sign = -1;
    1393             :         }
    1394       31744 :         if ( fabs( sspectra[i] ) > ss_min )
    1395             :         {
    1396        3552 :             sspectra[i] = sign * ( ( 10 - ss_min ) / 10.0f * (float) fabs( sspectra[i] ) + ss_min );
    1397             :         }
    1398       31744 :         if ( sspectra[i] != 0.0 )
    1399             :         {
    1400        5832 :             sspectra_ni[i] = sspectra[i];
    1401             :         }
    1402             :         else
    1403             :         {
    1404       25912 :             sspectra_ni[i] = sspectra_diff[i] * ni_ratio;
    1405             :         }
    1406       31744 :         if ( sspectra_ni[i] == 0.0f )
    1407             :         {
    1408        2812 :             sspectra_ni[i] = 0.5f * 10.0f * ni_ratio * rand_a[i];
    1409             :         }
    1410             :     }
    1411             : 
    1412         124 :     return ( ss_min );
    1413             : }
    1414             : 
    1415             : /*--------------------------------------------------------------------------*
    1416             :  * noiseinj_hf()
    1417             :  *
    1418             :  * level adjustments for the missing bands in the core coder
    1419             :  *--------------------------------------------------------------------------*/
    1420             : 
    1421         124 : void noiseinj_hf(
    1422             :     float xSynth_har[],         /* o  : gap filled information            */
    1423             :     const float th_g[],         /* i  : level adjustment information      */
    1424             :     const float band_energy[],  /* i  : subband energies                  */
    1425             :     float *prev_En_sb,          /* i/o: band Energies                     */
    1426             :     const int16_t p2a_flags[],  /* i  : Missing bands in the core coder   */
    1427             :     const int16_t BANDS,        /* i  : total bands                       */
    1428             :     const int16_t band_start[], /* i  : band start indices                */
    1429             :     const int16_t band_end[],   /* i  : band end indices                  */
    1430             :     const int16_t fLenLow       /* i  : low frequency bandwidth           */
    1431             : )
    1432             : {
    1433             : 
    1434             :     float *p_En, ni_scale, *p_Enn_sm_sb, En[NB_SWB_SUBBANDS], Enn_sm_sb[NB_SWB_SUBBANDS];
    1435             :     int16_t k, i;
    1436             : 
    1437             :     int16_t map_pulse_t[L_FRAME32k];
    1438             :     int16_t map_pulse[L_FRAME32k];
    1439             : 
    1440         124 :     set_s( map_pulse_t, 0, band_end[BANDS - 1] + 1 );
    1441         124 :     set_s( map_pulse, 0, band_end[BANDS - 1] + 1 );
    1442             :     /*level adjust the missing bands in the core coder */
    1443         124 :     p_En = En;
    1444         620 :     for ( k = BANDS - NB_SWB_SUBBANDS; k < BANDS; k++ )
    1445             :     {
    1446         496 :         *p_En = 0.0f;
    1447         496 :         if ( p2a_flags[k] == 0 )
    1448             :         {
    1449       38420 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
    1450             :             {
    1451       37932 :                 if ( fabs( xSynth_har[i - fLenLow] ) <= th_g[k - ( BANDS - NB_SWB_SUBBANDS )] )
    1452             :                 {
    1453       32792 :                     *p_En += xSynth_har[i - fLenLow] * xSynth_har[i - fLenLow];
    1454             :                 }
    1455             :                 else
    1456             :                 {
    1457        5140 :                     map_pulse_t[i] = 1;
    1458             :                 }
    1459             :             }
    1460         488 :             *p_En = (float) sqrt( *p_En );
    1461             :         }
    1462         496 :         p_En++;
    1463             :     }
    1464             : 
    1465         124 :     p_En = En;
    1466         124 :     p_Enn_sm_sb = Enn_sm_sb;
    1467         620 :     for ( k = BANDS - NB_SWB_SUBBANDS; k < BANDS; k++ )
    1468             :     {
    1469         496 :         *p_Enn_sm_sb = prev_En_sb[k - ( BANDS - NB_SWB_SUBBANDS )];
    1470         496 :         if ( p2a_flags[k] == 0 )
    1471             :         {
    1472         488 :             if ( prev_En_sb[k - ( BANDS - NB_SWB_SUBBANDS )] < 0.8f * band_energy[k] )
    1473             :             {
    1474          60 :                 *p_Enn_sm_sb = ( 0.15f * ( *p_En ) ) + ( 0.85f * prev_En_sb[k - ( BANDS - NB_SWB_SUBBANDS )] );
    1475             :             }
    1476             :             else
    1477             :             {
    1478         428 :                 *p_Enn_sm_sb = ( 0.8f * ( *p_En ) ) + ( 0.2f * prev_En_sb[k - ( BANDS - NB_SWB_SUBBANDS )] );
    1479             :             }
    1480             :         }
    1481             : 
    1482         496 :         p_Enn_sm_sb++;
    1483         496 :         p_En++;
    1484             :     }
    1485             : 
    1486         124 :     p_En = En;
    1487         124 :     p_Enn_sm_sb = Enn_sm_sb;
    1488         124 :     map_pulse[fLenLow] = ( map_pulse_t[fLenLow] | map_pulse_t[fLenLow + 1] );
    1489       38564 :     for ( i = fLenLow + 1; i < band_end[BANDS - 1]; i++ )
    1490             :     {
    1491       38440 :         map_pulse[i] = ( map_pulse_t[i - 1] | map_pulse_t[i] | map_pulse_t[i + 1] );
    1492             :     }
    1493         124 :     map_pulse[i] = ( map_pulse_t[i - 1] | map_pulse_t[i] );
    1494         620 :     for ( k = BANDS - NB_SWB_SUBBANDS; k < BANDS; k++ )
    1495             :     {
    1496         496 :         if ( p2a_flags[k] == 0 && *p_En != 0.0f )
    1497             :         {
    1498         488 :             ni_scale = (float) sqrt( ( *p_Enn_sm_sb ) / ( *p_En ) );
    1499         488 :             ni_scale = min( 1.25f, ni_scale );
    1500         488 :             ni_scale = max( 0.75f, ni_scale );
    1501         488 :             ni_scale *= 0.8f;
    1502       38420 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
    1503             :             {
    1504       37932 :                 if ( fabs( xSynth_har[i - fLenLow] ) <= th_g[k - ( BANDS - NB_SWB_SUBBANDS )] )
    1505             :                 {
    1506       32792 :                     if ( map_pulse[i] == 0 )
    1507             :                     {
    1508       25516 :                         xSynth_har[i - fLenLow] *= ni_scale;
    1509             :                     }
    1510             :                 }
    1511             :             }
    1512         488 :             prev_En_sb[k - ( BANDS - NB_SWB_SUBBANDS )] = *p_Enn_sm_sb;
    1513             :         }
    1514         496 :         p_Enn_sm_sb++;
    1515         496 :         p_En++;
    1516             :     }
    1517             : 
    1518         124 :     return;
    1519             : }
    1520             : 
    1521             : /*--------------------------------------------------------------------------*
    1522             :  * noise_extr_corcod()
    1523             :  *
    1524             :  * Spectrum normalization for the core coder
    1525             :  *--------------------------------------------------------------------------*/
    1526             : 
    1527           0 : void noise_extr_corcod(
    1528             :     float spectra[],          /* i  : core coder                                  */
    1529             :     const float spectra_ni[], /* i  : core coder with sparse filling              */
    1530             :     float sspectra[],         /* o  : Smoothed tonal information from core coder  */
    1531             :     float sspectra_diff[],    /* o  : non tonal infomration for gap filling       */
    1532             :     float sspectra_ni[],      /* o  : smoothed core coder                         */
    1533             :     const int16_t fLenLow,    /* i  : low frequency bands width                   */
    1534             :     int16_t prev_hqswb_clas,  /* i  : classification information                  */
    1535             :     float *prev_ni_ratio      /* i/o: noise parameter                             */
    1536             : )
    1537             : {
    1538             :     int16_t i, pulse_num;
    1539             :     float spectra_diff[L_FRAME32k];
    1540             :     float ni_ratio, ni_ratio_cur, br_adj;
    1541             : 
    1542             :     /*Spectrum Smoothing for tonal signals*/
    1543           0 :     SpectrumSmoothing_nss( spectra, sspectra, fLenLow );
    1544             : 
    1545           0 :     mvr2r( sspectra, sspectra_ni, fLenLow );
    1546             :     /*noise extraction*/
    1547           0 :     for ( i = 0; i < fLenLow; i++ )
    1548             :     {
    1549           0 :         spectra_diff[i] = spectra_ni[i] - spectra[i];
    1550             :     }
    1551           0 :     SpectrumSmoothing_nss( spectra_diff, sspectra_diff, fLenLow );
    1552             :     /*Smoothing the noise components*/
    1553           0 :     br_adj = 0.9f;
    1554           0 :     pulse_num = 0;
    1555           0 :     for ( i = 0; i < fLenLow; i++ )
    1556             :     {
    1557           0 :         if ( spectra[i] != 0.0f )
    1558             :         {
    1559           0 :             pulse_num++;
    1560             :         }
    1561             :     }
    1562           0 :     ni_ratio_cur = 0.0f;
    1563           0 :     if ( pulse_num != 0 )
    1564             :     {
    1565           0 :         ni_ratio_cur = ( fLenLow - pulse_num ) / ( fLenLow + 0.0f );
    1566           0 :         ni_ratio_cur *= br_adj;
    1567             :     }
    1568           0 :     if ( prev_hqswb_clas == HQ_HARMONIC )
    1569             :     {
    1570           0 :         if ( ni_ratio_cur > ( *prev_ni_ratio ) )
    1571             :         {
    1572           0 :             ni_ratio = 0.8f * ni_ratio_cur + ( *prev_ni_ratio ) * 0.2f;
    1573             :         }
    1574             :         else
    1575             :         {
    1576           0 :             ni_ratio = 0.6f * ni_ratio_cur + ( *prev_ni_ratio ) * 0.4f;
    1577             :         }
    1578             :     }
    1579             :     else
    1580             :     {
    1581           0 :         ni_ratio = 0.7f * ni_ratio_cur;
    1582             :     }
    1583           0 :     *prev_ni_ratio = ni_ratio;
    1584             : 
    1585           0 :     for ( i = 0; i < fLenLow; i++ )
    1586             :     {
    1587           0 :         sspectra_diff[i] *= ni_ratio;
    1588           0 :         sspectra_ni[i] = sspectra[i] + sspectra_diff[i];
    1589             :     }
    1590             : 
    1591           0 :     return;
    1592             : }
    1593             : 
    1594             : /*--------------------------------------------------------------------------*
    1595             :  * ton_ene_est()
    1596             :  *
    1597             :  * band energies for missing bands in the core coder
    1598             :  *--------------------------------------------------------------------------*/
    1599             : 
    1600           0 : void ton_ene_est(
    1601             :     float xSynth_har[],     /* i  : buffer with non tonal compoents    */
    1602             :     float be_tonal[],       /* o  : tonal energy of the missing bands  */
    1603             :     float band_energy[],    /* i  : subband energies                   */
    1604             :     int16_t band_start[],   /* i  : subband start indices              */
    1605             :     int16_t band_end[],     /* i  : subband end indices                */
    1606             :     int16_t band_width[],   /* i  : subband widths                     */
    1607             :     const int16_t fLenLow,  /* i  : low frequency width                */
    1608             :     const int16_t fLenHigh, /* i  : High frequency width               */
    1609             :     int16_t bands,          /* i  : total subbands                     */
    1610             :     int16_t har_bands,      /* i  : total number of harmonics bands    */
    1611             :     float ni_lvl,           /* i  : noise enve for the hf bands        */
    1612             :     GainItem pk_sf[],       /* i  : subband widths                     */
    1613             :     int16_t *pul_res        /* i  : tonal resolution                   */
    1614             : )
    1615             : {
    1616             :     int16_t sb_ton_loc[SWB_HAR_RAN1];
    1617             :     float sb_ton[SWB_HAR_RAN1], peak[NB_SWB_SUBBANDS];
    1618             :     int16_t pos, count_pos_st, count_pos_end;
    1619             :     int16_t pul_res_bnd[NB_SWB_SUBBANDS];
    1620             :     int16_t k, i, j;
    1621           0 :     float E = 0.0f, E_r = 0.0f, fac, ni_gain[NB_SWB_SUBBANDS], avg_pe[NB_SWB_SUBBANDS];
    1622             : 
    1623           0 :     set_s( sb_ton_loc, -1, SWB_HAR_RAN1 );
    1624           0 :     set_f( ni_gain, 0.0f, NB_SWB_SUBBANDS );
    1625           0 :     set_f( avg_pe, 0.0f, NB_SWB_SUBBANDS );
    1626           0 :     set_f( sb_ton, 0.0f, NB_SWB_SUBBANDS );
    1627           0 :     set_f( peak, 0.0f, NB_SWB_SUBBANDS );
    1628             :     /*non tonal adjustments*/
    1629           0 :     for ( i = 0; i < fLenHigh; i++ )
    1630             :     {
    1631           0 :         xSynth_har[i] *= ni_lvl;
    1632             :     }
    1633             : 
    1634           0 :     pos = 0;
    1635           0 :     for ( k = 0; k < bands - har_bands; k++ )
    1636             :     {
    1637           0 :         for ( j = 0; j < pul_res[k]; j++ )
    1638             :         {
    1639           0 :             sb_ton_loc[pos] = pk_sf[k * 8 + j].gainIndex;
    1640           0 :             sb_ton[pos] = pk_sf[k * 8 + j].nmrValue;
    1641           0 :             pos++;
    1642             :         }
    1643             :     }
    1644           0 :     k = 0;
    1645           0 :     pos = 0;
    1646             :     do
    1647             :     {
    1648           0 :         count_pos_st = pos;
    1649           0 :         while ( sb_ton_loc[pos] <= ( band_end[k + har_bands] - fLenLow ) && sb_ton_loc[pos] >= 0 )
    1650             :         {
    1651           0 :             pos++;
    1652             :         }
    1653           0 :         count_pos_end = pos;
    1654           0 :         pul_res_bnd[k] = count_pos_end - count_pos_st;
    1655           0 :         if ( pul_res_bnd[k] > 0 )
    1656             :         {
    1657           0 :             peak[k] = (float) fabs( sb_ton[count_pos_st] );
    1658             :         }
    1659           0 :         k++;
    1660           0 :     } while ( k < NB_SWB_SUBBANDS );
    1661             : 
    1662           0 :     k = 0;
    1663             :     /*energy calculation for tonal components*/
    1664           0 :     for ( i = har_bands; i < bands; i++ )
    1665             :     {
    1666           0 :         E = sum2_f( &xSynth_har[band_start[i] - fLenLow], band_width[i] ); /*noise energy*/
    1667           0 :         E_r = (float) E / (float) pow( 2.0f, band_energy[i] );
    1668             : 
    1669           0 :         if ( E_r < 0.06f )
    1670             :         {
    1671           0 :             avg_pe[k] = (float) sqrt( pow( 2.0f, band_energy[i] ) / band_width[i] );
    1672           0 :             fac = 0.6f;
    1673           0 :             if ( pul_res_bnd[k] != 0 )
    1674             :             {
    1675           0 :                 fac = ( (float) sqrt( E / band_width[i] ) / peak[k] );
    1676             :             }
    1677           0 :             ni_gain[k] = fac * avg_pe[k];
    1678             : 
    1679           0 :             ni_gain[k] = max( ( ( ni_gain[k] * ni_gain[k] * E_r ) >= 0.12f ) ? 0.05f * ni_gain[k] : 1.0f * ni_gain[k], 1.4f );
    1680             : 
    1681           0 :             for ( j = band_start[i]; j <= band_end[i]; j++ )
    1682             :             {
    1683           0 :                 xSynth_har[j - fLenLow] *= ni_gain[k];
    1684             :             }
    1685           0 :             E = sum2_f( &xSynth_har[band_start[i] - fLenLow], band_width[i] ); /*noise energy*/
    1686             :         }
    1687           0 :         k++;
    1688           0 :         be_tonal[i] = (float) pow( 2.0f, band_energy[i] ) - E; /*tonal energy*/
    1689             : 
    1690           0 :         if ( be_tonal[i] < 0.0f )
    1691             :         {
    1692           0 :             E = 0;
    1693           0 :             for ( j = ( band_start[i] - fLenLow ); j <= ( band_end[i] - fLenLow ); j++ )
    1694             :             {
    1695           0 :                 xSynth_har[j] *= 0.25f;
    1696           0 :                 E += xSynth_har[j] * xSynth_har[j];
    1697             :             }
    1698           0 :             be_tonal[i] = (float) pow( 2.0f, band_energy[i] ) - E; /*tonal energy*/
    1699             :         }
    1700             :     }
    1701             : 
    1702           0 :     return;
    1703             : }
    1704             : 
    1705             : 
    1706             : /*--------------------------------------------------------------------------*
    1707             :  * Gettonl_scalfact()
    1708             :  *
    1709             :  * Gap filling for the core coder
    1710             :  *--------------------------------------------------------------------------*/
    1711           0 : void Gettonl_scalfact(
    1712             :     float *outBuf,            /* o  : synthesized spectrum                        */
    1713             :     const float *codbuf,      /* i  : core coder                                  */
    1714             :     const int16_t fLenLow,    /* i  : lowband length                              */
    1715             :     const int16_t fLenHigh,   /* i  : highband length                             */
    1716             :     int16_t harmonic_band,    /* i  : total number of Low frequency bands         */
    1717             :     int16_t bands,            /* i  : total number of subbands in a frame         */
    1718             :     float *band_energy,       /* i  : band energy of each subband                 */
    1719             :     int16_t *band_start,      /* i  : subband start indices                       */
    1720             :     int16_t *band_end,        /* i  : subband end indices                         */
    1721             :     const int16_t p2aflags[], /* i  : missing bands in the core coder             */
    1722             :     float be_tonal[],         /* i  : tonal energy                                */
    1723             :     GainItem *pk_sf,          /* i  : toanl information for Sparse filling        */
    1724             :     int16_t *pul_res_pk       /* i  : pulse resolution information                */
    1725             : )
    1726             : {
    1727             :     int16_t k, i, band_pos;
    1728             :     int16_t sb_ton_loc[SWB_HAR_RAN1];
    1729             :     int16_t pos_tmp;
    1730             :     float sb_ton[SWB_HAR_RAN1], est_ton_ene[NB_SWB_SUBBANDS], ton_sf;
    1731           0 :     float step, enrd_r = 0.9f;
    1732             :     float band_sf[SWB_HAR_RAN1];
    1733             :     int16_t pos, count_pos_st, count_pos_end, j;
    1734             : 
    1735           0 :     set_f( est_ton_ene, 0.0f, NB_SWB_SUBBANDS );
    1736           0 :     set_s( sb_ton_loc, -1, SWB_HAR_RAN1 );
    1737             :     /* Get the tonal information for sparse filling  */
    1738           0 :     pos = 0;
    1739           0 :     for ( k = 0; k < bands - harmonic_band; k++ )
    1740             :     {
    1741           0 :         for ( j = 0; j < pul_res_pk[k]; j++ )
    1742             :         {
    1743           0 :             sb_ton_loc[pos] = pk_sf[k * 8 + j].gainIndex;
    1744           0 :             sb_ton[pos] = pk_sf[k * 8 + j].nmrValue;
    1745           0 :             pos++;
    1746             :         }
    1747             :     }
    1748           0 :     k = 0;
    1749           0 :     pos = 0;
    1750           0 :     pos_tmp = 0;
    1751             : 
    1752             :     do
    1753             :     {
    1754           0 :         band_pos = k + harmonic_band;
    1755           0 :         count_pos_st = pos;
    1756           0 :         while ( sb_ton_loc[pos] <= ( band_end[band_pos] - fLenLow ) && sb_ton_loc[pos] >= 0 )
    1757             :         {
    1758           0 :             pos++;
    1759             :         }
    1760           0 :         count_pos_end = pos;
    1761           0 :         for ( i = count_pos_st; i < count_pos_end; i++ )
    1762             :         {
    1763           0 :             est_ton_ene[k] += ( sb_ton[i] * sb_ton[i] );
    1764             :         }
    1765           0 :         if ( est_ton_ene[k] <= 0.0f )
    1766             :         {
    1767           0 :             est_ton_ene[k] = 0.01f;
    1768             :         }
    1769           0 :         ton_sf = 0.0f;
    1770           0 :         if ( be_tonal[band_pos] > 0.0f )
    1771             :         {
    1772           0 :             ton_sf = (float) sqrt( be_tonal[band_pos] / est_ton_ene[k] );
    1773             :         }
    1774           0 :         for ( i = count_pos_st; i < count_pos_end; i++ )
    1775             :         {
    1776           0 :             band_sf[pos_tmp] = ton_sf;
    1777           0 :             pos_tmp++;
    1778             :         }
    1779           0 :         k++;
    1780           0 :     } while ( k < NB_SWB_SUBBANDS );
    1781             :     /* Gap filling for the core coder  */
    1782           0 :     step = 1.0f / ( 0.077f * fLenHigh );
    1783           0 :     pos_tmp = 0;
    1784           0 :     for ( k = 0; k < bands - harmonic_band; k++ )
    1785             :     {
    1786           0 :         band_pos = k + harmonic_band;
    1787           0 :         if ( be_tonal[band_pos] > 0.0f )
    1788             :         {
    1789           0 :             enrd_r *= (float) sqrt( be_tonal[band_pos] / pow( 2.0f, band_energy[band_pos] ) );
    1790             :         }
    1791             :         else
    1792             :         {
    1793           0 :             enrd_r = 0.0f;
    1794             :         }
    1795           0 :         enrd_r -= step;
    1796           0 :         if ( p2aflags[band_pos] == 1 )
    1797             :         {
    1798           0 :             for ( i = band_start[band_pos]; i <= band_end[band_pos]; i++ )
    1799             :             {
    1800           0 :                 outBuf[i - fLenLow] = codbuf[i];
    1801             :             }
    1802             :         }
    1803             :         else
    1804             :         {
    1805           0 :             pos = 0;
    1806           0 :             pos += pos_tmp;
    1807           0 :             for ( j = 0; j < pul_res_pk[k]; j++ )
    1808             :             {
    1809           0 :                 outBuf[pk_sf[k * 8 + j].gainIndex] = pk_sf[k * 8 + j].nmrValue * band_sf[pos] * enrd_r;
    1810           0 :                 pos++;
    1811             :             }
    1812             :         }
    1813           0 :         pos_tmp += pul_res_pk[k];
    1814             :     }
    1815             : 
    1816           0 :     return;
    1817             : }
    1818             : 
    1819             : 
    1820             : /*--------------------------------------------------------------------------*
    1821             :  * return_bits_normal2()
    1822             :  *
    1823             :  *
    1824             :  *--------------------------------------------------------------------------*/
    1825             : 
    1826         124 : void return_bits_normal2(
    1827             :     int16_t *bit_budget,
    1828             :     const int16_t p2a_flags[],
    1829             :     const int16_t bands,
    1830             :     const int16_t bits_lagIndices[] )
    1831             : {
    1832             :     int16_t i;
    1833             : 
    1834         620 :     for ( i = 0; i < NB_SWB_SUBBANDS; i++ )
    1835             :     {
    1836         496 :         if ( p2a_flags[bands - NB_SWB_SUBBANDS + i] == 1 )
    1837             :         {
    1838          16 :             *bit_budget += bits_lagIndices[i];
    1839             :         }
    1840             :     }
    1841             : 
    1842         124 :     return;
    1843             : }
    1844             : 
    1845             : /*--------------------------------------------------------------------------*
    1846             :  * preset_hq2_swb()
    1847             :  *
    1848             :  *
    1849             :  *--------------------------------------------------------------------------*/
    1850             : 
    1851         124 : void preset_hq2_swb(
    1852             :     const int16_t hqswb_clas,
    1853             :     const int16_t band_end[],
    1854             :     int16_t *har_bands,
    1855             :     int16_t p2a_bands,
    1856             :     const int16_t length,
    1857             :     const int16_t bands,
    1858             :     int16_t *lowlength,
    1859             :     int16_t *highlength,
    1860             :     float m[] )
    1861             : {
    1862         124 :     if ( hqswb_clas == HQ_HARMONIC )
    1863             :     {
    1864           0 :         *har_bands = bands - p2a_bands + 1;
    1865           0 :         *lowlength = band_end[*har_bands - 1] + 1;
    1866             :     }
    1867             :     else
    1868             :     {
    1869         124 :         *lowlength = band_end[bands - NB_SWB_SUBBANDS - 1] + 1;
    1870             :     }
    1871             : 
    1872         124 :     *highlength = ( length - *lowlength );
    1873         124 :     set_f( m, 0.0f, length );
    1874             : 
    1875         124 :     return;
    1876             : }
    1877             : 
    1878             : 
    1879             : /*--------------------------------------------------------------------------*
    1880             :  * post_hq2_swb()
    1881             :  *
    1882             :  *
    1883             :  *--------------------------------------------------------------------------*/
    1884             : 
    1885         124 : void post_hq2_swb(
    1886             :     const float m[],
    1887             :     const int16_t lowlength,
    1888             :     const int16_t highlength,
    1889             :     const int16_t hqswb_clas,
    1890             :     const int16_t har_bands,
    1891             :     const int16_t bands,
    1892             :     const int16_t p2a_flags[],
    1893             :     const int16_t band_start[],
    1894             :     const int16_t band_end[],
    1895             :     float y2[],
    1896             :     int16_t npulses[] )
    1897             : {
    1898             :     int16_t i, k;
    1899             : 
    1900             :     /* copy the scratch buffer to the output */
    1901         124 :     mvr2r( &m[lowlength], &y2[lowlength], highlength );
    1902             : 
    1903         124 :     if ( hqswb_clas == HQ_HARMONIC )
    1904             :     {
    1905           0 :         k = har_bands;
    1906             :     }
    1907             :     else
    1908             :     {
    1909         124 :         k = bands - NB_SWB_SUBBANDS;
    1910             :     }
    1911             : 
    1912         620 :     for ( ; k < bands; k++ )
    1913             :     {
    1914         496 :         if ( p2a_flags[k] == 0 && npulses[k] == 0 )
    1915             :         {
    1916       37300 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
    1917             :             {
    1918       36832 :                 if ( y2[i] != 0.0f )
    1919             :                 {
    1920       36832 :                     npulses[k]++;
    1921             :                 }
    1922             :             }
    1923             :         }
    1924             :     }
    1925             : 
    1926         124 :     return;
    1927             : }
    1928             : 
    1929             : 
    1930             : /*--------------------------------------------------------------------------*
    1931             :  * GetSynthesizedSpecThinOut()
    1932             :  *
    1933             :  * Synthesize the spectrum in generic subband coding
    1934             :  *--------------------------------------------------------------------------*/
    1935             : 
    1936         124 : void GetSynthesizedSpecThinOut(
    1937             :     const float *predBuf,      /* i  : prediction buffer (i.e., lowband)   */
    1938             :     float *outBuf,             /* o  : synthesized spectrum                */
    1939             :     const int16_t nBands,      /* i  : number of subbands calculated       */
    1940             :     const int16_t *sbWidth,    /* i  : subband lengths                     */
    1941             :     const int16_t *lagIndices, /* i  : lowband index for each subband      */
    1942             :     const float *lagGains,     /* i  : first gain for each subband         */
    1943             :     const int16_t predBufLen   /* i  : lowband length                      */
    1944             : )
    1945             : {
    1946             :     int16_t sb;
    1947             :     int16_t fLen, lag;
    1948             :     float *ptr_in_outBuf;
    1949             : 
    1950         124 :     ptr_in_outBuf = outBuf;
    1951             : 
    1952         620 :     for ( sb = 0; sb < nBands; sb++ )
    1953             :     {
    1954         496 :         fLen = sbWidth[sb];
    1955         496 :         lag = lagIndices[sb];
    1956             : 
    1957         496 :         if ( lag + fLen > predBufLen )
    1958             :         {
    1959             :             /* should never happen */
    1960           0 :             lag = predBufLen - fLen;
    1961             :         }
    1962             : 
    1963         496 :         GetPredictedSignal( predBuf, outBuf, lag, fLen, lagGains[sb] );
    1964         496 :         outBuf += fLen;
    1965             :     }
    1966             : 
    1967         124 :     outBuf = ptr_in_outBuf;
    1968             : 
    1969         124 :     return;
    1970             : }
    1971             : 
    1972             : 
    1973             : /*--------------------------------------------------------------------------*
    1974             :  * GetlagGains()
    1975             :  *
    1976             :  *
    1977             :  *--------------------------------------------------------------------------*/
    1978             : 
    1979         124 : void GetlagGains(
    1980             :     const float *predBuf,      /* i  : predictve buffer                */
    1981             :     const float *band_energy,  /* i  : band Energies                   */
    1982             :     const int16_t nBands,      /* i  : high frequency bands            */
    1983             :     const int16_t *sbWidth,    /* i  : high frequency band resolution  */
    1984             :     const int16_t *lagIndices, /* i  : correlation indices             */
    1985             :     const int16_t predBufLen,  /* i  : predictive buffer length        */
    1986             :     float *lagGains            /* o  : lag gains                       */
    1987             : )
    1988             : {
    1989             :     int16_t i;
    1990             :     int16_t sb, fLen, lag;
    1991             :     float outBuf[L_FRAME32k];
    1992             :     float lagEnergy;
    1993             : 
    1994             :     /* Get the gain information for the missing bands*/
    1995         620 :     for ( sb = 0; sb < nBands; sb++ )
    1996             :     {
    1997         496 :         fLen = sbWidth[sb];
    1998         496 :         lag = lagIndices[sb];
    1999             : 
    2000         496 :         if ( lag + fLen > predBufLen )
    2001             :         {
    2002             :             /* should never happen */
    2003           0 :             lag = predBufLen - fLen;
    2004             :         }
    2005             : 
    2006         496 :         GetPredictedSignal( predBuf, outBuf, lag, fLen, 1.0 );
    2007             : 
    2008         496 :         lagEnergy = 0.0f;
    2009       39184 :         for ( i = 0; i < fLen; i++ )
    2010             :         {
    2011       38688 :             lagEnergy += outBuf[i] * outBuf[i];
    2012             :         }
    2013             : 
    2014         496 :         if ( lagEnergy != 0.0f )
    2015             :         {
    2016         496 :             lagGains[sb] = (float) sqrt( pow( 2.0f, band_energy[sb] ) / lagEnergy );
    2017             :         }
    2018             :         else
    2019             :         {
    2020           0 :             lagGains[sb] = (float) sqrt( pow( 2.0f, band_energy[sb] ) / ( lagEnergy + 0.001f ) );
    2021             :         }
    2022             :     }
    2023             : 
    2024         124 :     return;
    2025             : }
    2026             : 
    2027             : 
    2028             : /*--------------------------------------------------------------------------*
    2029             :  * updat_prev_frm()
    2030             :  *
    2031             :  *
    2032             :  *--------------------------------------------------------------------------*/
    2033             : 
    2034         136 : void updat_prev_frm(
    2035             :     float y2[],                      /* i/o: core coder buffer                 */
    2036             :     float t_audio[],                 /* o  : core coder buffer                 */
    2037             :     const int32_t bwe_br,            /* i  : core bitrate                      */
    2038             :     const int16_t length,            /* i  : frame length coded bw             */
    2039             :     const int16_t inner_frame,       /* i  : input frame length                */
    2040             :     const int16_t bands,             /* i  : sub band resolution               */
    2041             :     const int16_t bwidth,            /* i  : audio bandwidth                   */
    2042             :     const int16_t is_transient,      /* i  : signal class information          */
    2043             :     const int16_t hqswb_clas,        /* i  : signal class information          */
    2044             :     int16_t *prev_hqswb_clas,        /* o  : update signal class information   */
    2045             :     int16_t prev_SWB_peak_pos[],     /* o  : update core coder last coded peaks*/
    2046             :     int16_t prev_SWB_peak_pos_tmp[], /* o  : update core coder last coded peaks*/
    2047             :     int16_t *prev_frm_hfe2,          /* o  : update harmonics                  */
    2048             :     int16_t *prev_stab_hfe2,         /* o  : update harmonics                  */
    2049             :     const int16_t bws_cnt            /* i  : band width detector counter       */
    2050             : )
    2051             : {
    2052             :     int16_t i, k, k1, k2, j;
    2053             : 
    2054             :     /* Copy the coded MDCT coefficient to the output buffer */
    2055         136 :     if ( !is_transient )
    2056             :     {
    2057             :         /* Copy the scratch buffer to the output */
    2058         124 :         mvr2r( y2, t_audio, length );
    2059             : 
    2060             :         /* If the input frame is larger than coded bandwidth, zero out uncoded MDCT coefficients */
    2061         124 :         if ( inner_frame > length )
    2062             :         {
    2063         124 :             set_f( t_audio + length, 0.0f, inner_frame - length );
    2064             :         }
    2065             :     }
    2066             :     else /* transient frame */
    2067             :     {
    2068          12 :         if ( inner_frame == length || bws_cnt > 0 )
    2069             :         {
    2070             :             /* Copy the scratch buffer to the output */
    2071           0 :             mvr2r( y2, t_audio, length );
    2072             :         }
    2073             :         else
    2074             :         {
    2075             :             /* un-collapse transient frame and interleave zeros */
    2076          60 :             for ( i = 0; i < NUM_TIME_SWITCHING_BLOCKS; i++ )
    2077             :             {
    2078          48 :                 k1 = i * length / NUM_TIME_SWITCHING_BLOCKS;
    2079          48 :                 k2 = i * inner_frame / NUM_TIME_SWITCHING_BLOCKS;
    2080             : 
    2081          48 :                 mvr2r( y2 + k1, t_audio + k2, length / NUM_TIME_SWITCHING_BLOCKS );
    2082          48 :                 set_f( t_audio + k2 + length / NUM_TIME_SWITCHING_BLOCKS, 0.0f, ( inner_frame - length ) / NUM_TIME_SWITCHING_BLOCKS );
    2083             :             }
    2084             :         }
    2085             :     }
    2086             : 
    2087             :     /* update */
    2088         136 :     if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && bwidth == SWB )
    2089             :     {
    2090         136 :         *prev_hqswb_clas = hqswb_clas;
    2091         136 :         if ( hqswb_clas != HQ_HARMONIC )
    2092             :         {
    2093         136 :             *prev_frm_hfe2 = 0;
    2094         136 :             *prev_stab_hfe2 = 0;
    2095             :         }
    2096             :     }
    2097             :     else
    2098             :     {
    2099           0 :         *prev_hqswb_clas = is_transient;
    2100             :     }
    2101             : 
    2102         136 :     if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && bwidth == SWB && hqswb_clas == HQ_NORMAL )
    2103             :     {
    2104         124 :         j = 0;
    2105         620 :         for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
    2106             :         {
    2107         496 :             prev_SWB_peak_pos[j] = prev_SWB_peak_pos_tmp[j];
    2108         496 :             j++;
    2109             :         }
    2110             :     }
    2111             : 
    2112         136 :     return;
    2113             : }

Generated by: LCOV version 1.14