LCOV - code coverage report
Current view: top level - lib_dec - swb_bwe_dec_hr.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 211 285 74.0 %
Date: 2025-05-23 08:37:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "prot.h"
      44             : #include "rom_com.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * swb_bwe_dec_hr()
      49             :  *
      50             :  * HR SWB BWE decoder
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53        2007 : void swb_bwe_dec_hr(
      54             :     Decoder_State *st,          /* i/o: decoder state structure     */
      55             :     const float *syn_12k8_16k,  /* i  : ACELP core synthesis @16kHz */
      56             :     float *hb_synth,            /* o  : SHB synthesis               */
      57             :     const int16_t output_frame, /* i  : frame length                */
      58             :     const int16_t unbits,       /* i  : number of core unused bits  */
      59             :     const float pitch_buf[]     /* i  : pitch buffer                */
      60             : )
      61             : {
      62             :     int16_t i, j, k, nBits, nBits_total, nBits_block, Nsv, Nsv2, width_noncoded;
      63             :     int16_t is_transient, tmpS, incr, IsTransient, pos;
      64             :     FD_BWE_DEC_HANDLE hBWE_FD;
      65             :     HR_BWE_DEC_HANDLE hBWE_FD_HR;
      66             :     int16_t x_norm[NSV_MAX * ( WIDTH_BAND + 1 )], x_norm1[NSV_MAX * ( WIDTH_BAND + 1 )];
      67             :     float t_audio[L_FRAME48k], t_audio_tmp[L_FRAME48k];
      68        2007 :     float gain = 0.0f, gain2, en_band[N_BANDS_BWE_HR];
      69             :     int16_t ind1, ind2;
      70             :     float EnergyLT, Energy;
      71             :     int16_t nq[NSV_MAX], nq2[NSV_MAX], nq_tmp[NSV_MAX];
      72             :     float tilt_wb, min_env, max_env;
      73             :     float tmpF, tmp2, pitch, alpha;
      74             :     float en_noncoded;
      75        2007 :     float env = 0.0f;
      76             : 
      77        2007 :     hBWE_FD = st->hBWE_FD;
      78        2007 :     hBWE_FD_HR = st->hBWE_FD_HR;
      79             : 
      80             :     /*---------------------------------------------------------------------*
      81             :      * initializations
      82             :      *---------------------------------------------------------------------*/
      83             : 
      84        2007 :     set_f( t_audio, 0.0f, output_frame );
      85             : 
      86        2007 :     hBWE_FD_HR->bwe_highrate_seed = (int16_t) ( (int16_t) ( pitch_buf[0] * 64.0f ) * (int16_t) ( pitch_buf[3] * 64.0f ) );
      87             : 
      88        2007 :     ind2 = 0;  /* only to suppress warnings */
      89        2007 :     Nsv2 = 0;  /* only to suppress warnings */
      90        2007 :     gain2 = 0; /* only to suppress warnings */
      91             : 
      92             :     /* reset memories in case that last frame was a different technology */
      93        2007 :     if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
      94             :     {
      95          33 :         set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
      96             :     }
      97             : 
      98             :     /* calculate SWB BWE bit-budget */
      99        2007 :     nBits = (int16_t) ( st->extl_brate ) / FRAMES_PER_SEC + unbits;
     100        2007 :     nBits_total = nBits;
     101             : 
     102             :     /*---------------------------------------------------------------------*
     103             :      * calculate tilt of the core synthesis
     104             :      *---------------------------------------------------------------------*/
     105             : 
     106        2007 :     calc_tilt_bwe( syn_12k8_16k, &tilt_wb, L_FRAME16k );
     107        2007 :     pitch = sum_f( pitch_buf, NB_SUBFR16k ) + EPSILON;
     108             : 
     109             :     /*---------------------------------------------------------------------*
     110             :      * FEC, or good frame decoding
     111             :      *---------------------------------------------------------------------*/
     112             : 
     113        2007 :     if ( st->bfi )
     114             :     {
     115           0 :         is_transient = hBWE_FD_HR->old_is_transient_hr_bwe;
     116             : 
     117             :         /* Replication of the last spectrum, with an attenuation */
     118           0 :         if ( ( st->clas_dec == VOICED_CLAS || st->clas_dec == INACTIVE_CLAS ) && st->nbLostCmpt <= 3 )
     119             :         {
     120           0 :             alpha = 0.8f;
     121             :         }
     122           0 :         else if ( is_transient )
     123             :         {
     124           0 :             alpha = 0.15f;
     125             :         }
     126             :         else
     127             :         {
     128           0 :             alpha = 0.3f;
     129             :         }
     130             : 
     131           0 :         if ( is_transient )
     132             :         {
     133             :             /* set BWE spectrum length */
     134           0 :             if ( output_frame == L_FRAME32k )
     135             :             {
     136           0 :                 tmpS = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
     137             :             }
     138             :             else /* output_frame == L_FRAME48k */
     139             :             {
     140           0 :                 tmpS = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
     141             :             }
     142             : 
     143             :             /* reconstruct */
     144           0 :             for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
     145             :             {
     146           0 :                 for ( i = 0; i < tmpS; i++ )
     147             :                 {
     148           0 :                     t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] = alpha * hBWE_FD_HR->t_audio_prev[i + k * tmpS];
     149             :                 }
     150             :                 /* save transform coefficients for the next frame (needed in case of frame erasures) */
     151           0 :                 mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * tmpS, tmpS );
     152             :             }
     153             :         }
     154             :         else
     155             :         {
     156             :             /* set BWE spectrum length */
     157           0 :             if ( output_frame == L_FRAME32k )
     158             :             {
     159           0 :                 tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
     160             :             }
     161             :             else /* output_frame == L_FRAME48k */
     162             :             {
     163           0 :                 tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
     164             :             }
     165             : 
     166             :             /* reconstruct */
     167           0 :             for ( i = 0; i < tmpS; i++ )
     168             :             {
     169           0 :                 t_audio[NUM_NONTRANS_START_FREQ_COEF + i] = alpha * hBWE_FD_HR->t_audio_prev[i];
     170             :             }
     171             :             /* Save transform coefficients for the next frame (needed in case of frame erasures) */
     172           0 :             mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, tmpS );
     173             :         }
     174             : 
     175           0 :         hBWE_FD_HR->mem_EnergyLT *= alpha;
     176           0 :         gain = (float) ( 2.0f * sqrt( hBWE_FD_HR->mem_EnergyLT / output_frame ) );
     177           0 :         env = 1.0f;
     178             :     }
     179             :     else
     180             :     {
     181             :         /*---------------------------------------------------------------------*
     182             :          * get transient frame flag
     183             :          *---------------------------------------------------------------------*/
     184             : 
     185        2007 :         is_transient = get_next_indice( st, 1 );
     186             : 
     187        2007 :         if ( is_transient )
     188             :         {
     189         192 :             nBits = -1; /* is_transient flag */
     190         192 :             nBits_block = nBits_total / NUM_TIME_SWITCHING_BLOCKS;
     191         192 :             nBits += nBits_total % NUM_TIME_SWITCHING_BLOCKS;
     192             : 
     193             :             /* set width of noncoded (blind estimated) spectrum */
     194         192 :             if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
     195             :             {
     196         192 :                 width_noncoded = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     197         192 :                 tmpS = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF_EFF;
     198             :             }
     199             :             else /* st->extl == FB_BWE_HIGHRATE */
     200             :             {
     201           0 :                 width_noncoded = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     202           0 :                 tmpS = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF_EFF;
     203             :             }
     204             : 
     205             :             /*---------------------------------------------------------------------*
     206             :              * transient frames: processing in blocks (subframes)
     207             :              *---------------------------------------------------------------------*/
     208             : 
     209         960 :             for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
     210             :             {
     211         768 :                 nBits += nBits_block;
     212             : 
     213             :                 /*---------------------------------------------------------------------*
     214             :                  * global gain and envelope decoding
     215             :                  *---------------------------------------------------------------------*/
     216             : 
     217             :                 /* get global gain */
     218         768 :                 ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     219         768 :                 gain = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     220         768 :                 nBits -= NBITS_GLOB_GAIN_BWE_HR;
     221             : 
     222             :                 /* get energy per band */
     223         768 :                 if ( k == 0 )
     224             :                 {
     225         192 :                     ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR );
     226         192 :                     ind2 = ind1;
     227         192 :                     nBits -= NBITS_ENVELOPE_BWE_HR_TR;
     228             :                 }
     229             :                 else
     230             :                 {
     231         576 :                     if ( ind2 < 8 )
     232             :                     {
     233         126 :                         ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 );
     234             :                     }
     235             :                     else
     236             :                     {
     237         450 :                         ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 ) + NUM_ENVLOPE_CODE_HR_TR2;
     238             :                     }
     239         576 :                     nBits -= ( NBITS_ENVELOPE_BWE_HR_TR - 1 );
     240             :                 }
     241             : 
     242         768 :                 en_band[0] = swb_hr_env_code3[2 * ind1];
     243         768 :                 en_band[1] = swb_hr_env_code3[2 * ind1 + 1];
     244         768 :                 env = 0.5f * ( en_band[0] + en_band[1] );
     245             : 
     246             :                 /*---------------------------------------------------------------------*
     247             :                  * estimate energy of noncoded spectrum (14.4-20kHz)
     248             :                  *---------------------------------------------------------------------*/
     249             : 
     250         768 :                 en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
     251             : 
     252         768 :                 if ( st->extl == FB_BWE_HIGHRATE )
     253             :                 {
     254           0 :                     ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
     255           0 :                     nBits -= NBITS_HF_GAIN_BWE_HR;
     256             : 
     257           0 :                     if ( ind1 == 1 )
     258             :                     {
     259           0 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT1;
     260             :                     }
     261           0 :                     else if ( ind1 == 2 )
     262             :                     {
     263           0 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT2;
     264             :                     }
     265           0 :                     else if ( ind1 == 3 )
     266             :                     {
     267           0 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT3;
     268             :                     }
     269             :                 }
     270             : 
     271             :                 /*---------------------------------------------------------------------*
     272             :                  * AVQ decoding (dequantize normalized spectrum)
     273             :                  *---------------------------------------------------------------------*/
     274             : 
     275         768 :                 Nsv = ( NUM_TRANS_END_FREQ_COEF - NUM_TRANS_START_FREQ_COEF ) / WIDTH_BAND;
     276         768 :                 AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     277       49920 :                 for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
     278             :                 {
     279       49152 :                     t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] = (float) ( x_norm[i] );
     280             :                 }
     281             : 
     282             :                 /* apply noise-fill */
     283         768 :                 swb_hr_noise_fill( is_transient, NUM_TRANS_START_FREQ_COEF, NUM_TRANS_END_FREQ_COEF, tilt_wb, pitch, nq, Nsv, &hBWE_FD_HR->bwe_highrate_seed, t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS );
     284             : 
     285             :                 /*---------------------------------------------------------------------*
     286             :                  * reconstruction
     287             :                  *---------------------------------------------------------------------*/
     288             : 
     289             :                 /* reconstruct 14-16(20) kHz spectrum */
     290       16128 :                 for ( j = 0; j < tmpS; j++ )
     291             :                 {
     292       15360 :                     t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF + j] = 0.5f * t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF - tmpS + j];
     293             :                 }
     294             : 
     295             :                 /* envelope denormalization */
     296        2304 :                 for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
     297             :                 {
     298       53760 :                     for ( j = 0; j < WIDTH_TRANS_FREQ_COEF; j++ )
     299             :                     {
     300       52224 :                         t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF + j] *= en_band[i];
     301             :                     }
     302             :                 }
     303             : 
     304             :                 /* envelope denormalization of 14.4-16(20) kHz spectrum */
     305       13056 :                 for ( j = tmpS - width_noncoded; j < tmpS; j++ )
     306             :                 {
     307       12288 :                     t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF + j] *= en_noncoded;
     308             :                 }
     309             : 
     310             :                 /* overlap region */
     311         768 :                 if ( output_frame == L_FRAME48k )
     312             :                 {
     313        3840 :                     for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
     314             :                     {
     315        3072 :                         t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] *= overlap_coefs_48kHz[i * 4];
     316             :                     }
     317             :                 }
     318             :                 else
     319             :                 {
     320           0 :                     for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
     321             :                     {
     322           0 :                         t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] *= overlap_coefs[i * 4];
     323             :                     }
     324             :                 }
     325             : 
     326             :                 /* apply global gain */
     327       65280 :                 for ( i = 0; i < WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR + width_noncoded; i++ )
     328             :                 {
     329       64512 :                     t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= gain;
     330             :                 }
     331             : 
     332             :                 /* save transform coefficients for the next frame (needed in case of frame erasures) */
     333         768 :                 if ( output_frame == L_FRAME32k )
     334             :                 {
     335           0 :                     mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * ( L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF ), L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF );
     336             :                 }
     337             :                 else /* output_frame == L_FRAME48k */
     338             :                 {
     339         768 :                     mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * ( ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF ), ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF );
     340             :                 }
     341             : 
     342             :                 /* attenuate HFs in case of band-width switching */
     343         768 :                 if ( st->bws_cnt1 > 0 )
     344             :                 {
     345           0 :                     if ( output_frame == L_FRAME32k )
     346             :                     {
     347           0 :                         j = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
     348             :                     }
     349             :                     else /* output_frame == L_FRAME48k */
     350             :                     {
     351           0 :                         j = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
     352             :                     }
     353             : 
     354           0 :                     for ( i = 0; i < j; i++ )
     355             :                     {
     356           0 :                         t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
     357             :                     }
     358             :                 }
     359             :             }
     360             :         }
     361             :         else /* !is_transient */
     362             :         {
     363             :             /* subtract one bit for is_transient flag */
     364        1815 :             nBits--;
     365             : 
     366             :             /*---------------------------------------------------------------------*
     367             :              * global gain and envelope decoding
     368             :              *---------------------------------------------------------------------*/
     369             : 
     370             :             /* get global gain */
     371        1815 :             ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     372        1815 :             gain = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     373             : 
     374             :             /* get energy per band */
     375        1815 :             ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR1 );
     376        1815 :             ind2 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR2 );
     377             : 
     378        1815 :             en_band[0] = swb_hr_env_code1[2 * ind1];
     379        1815 :             en_band[1] = swb_hr_env_code1[2 * ind1 + 1];
     380        1815 :             en_band[2] = swb_hr_env_code2[2 * ind2];
     381        1815 :             en_band[3] = swb_hr_env_code2[2 * ind2 + 1];
     382        1815 :             env = 0.25f * ( en_band[0] + en_band[1] + en_band[2] + en_band[3] );
     383             : 
     384             :             /*---------------------------------------------------------------------*
     385             :              * choose sub-bands to be dequantized
     386             :              *---------------------------------------------------------------------*/
     387             : 
     388             :             /* find the subband with the min envelope */
     389        1815 :             pos = 0;
     390        1815 :             min_env = en_band[0];
     391        1815 :             max_env = en_band[0];
     392        7260 :             for ( j = 1; j < N_BANDS_BWE_HR; j++ )
     393             :             {
     394        5445 :                 if ( en_band[j] < min_env )
     395             :                 {
     396        4167 :                     pos = j;
     397        4167 :                     min_env = en_band[j];
     398             :                 }
     399        5445 :                 if ( en_band[j] > max_env )
     400             :                 {
     401         624 :                     max_env = en_band[j];
     402             :                 }
     403             :             }
     404             : 
     405             :             /* decide the spectrum to be dequantized */
     406        1815 :             if ( nBits_total > NBITS_THRESH_BWE_HR )
     407             :             {
     408           0 :                 i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF;
     409             :             }
     410             :             else
     411             :             {
     412        1815 :                 i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - 64 - 8 * ( pos % 2 );
     413             :             }
     414             : 
     415        1815 :             nBits -= NBITS_GLOB_GAIN_BWE_HR + NBITS_ENVELOPE_BWE_HR1 + NBITS_ENVELOPE_BWE_HR2;
     416             : 
     417             :             /*---------------------------------------------------------------------*
     418             :              * estimate energy of noncoded spectrum (14.4-20kHz)
     419             :              *---------------------------------------------------------------------*/
     420             : 
     421        1815 :             en_noncoded = 0.5f * min_env;
     422             : 
     423        1815 :             if ( st->extl == FB_BWE_HIGHRATE )
     424             :             {
     425           0 :                 ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
     426           0 :                 nBits -= NBITS_HF_GAIN_BWE_HR;
     427             : 
     428           0 :                 if ( ind1 == 1 )
     429             :                 {
     430           0 :                     en_noncoded *= BWE_HR_NONTRANS_EN_LIMIT1;
     431             :                 }
     432           0 :                 else if ( ind1 == 2 )
     433             :                 {
     434           0 :                     en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT2;
     435             :                 }
     436           0 :                 else if ( ind1 == 3 )
     437             :                 {
     438           0 :                     en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT3;
     439             :                 }
     440             :             }
     441             : 
     442             :             /*---------------------------------------------------------------------*
     443             :              * AVQ decoding (dequantize normalized spectrum)
     444             :              *---------------------------------------------------------------------*/
     445             : 
     446        1815 :             Nsv = i / WIDTH_BAND;
     447        1815 :             AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     448             :             /*---------------------------------------------------------------------*
     449             :              * second stage decoding
     450             :              *---------------------------------------------------------------------*/
     451             : 
     452        1815 :             if ( nBits >= 9 + NBITS_GLOB_GAIN_BWE_HR && sum_s( nq, Nsv ) > 0 )
     453             :             {
     454        1776 :                 ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     455        1776 :                 gain2 = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     456        1776 :                 gain2 *= 0.0625f;
     457             : 
     458             :                 /* calculate the number of subbands according to the rest bits */
     459        1776 :                 if ( nBits > 396 )
     460             :                 {
     461           0 :                     Nsv2 = 33;
     462             :                 }
     463             :                 else
     464             :                 {
     465        1776 :                     Nsv2 = nBits / 12;
     466             :                 }
     467             : 
     468        1776 :                 nBits -= NBITS_GLOB_GAIN_BWE_HR;
     469        1776 :                 AVQ_demuxdec( st, x_norm1, &nBits, Nsv2, nq2, 0, Nsv2 - 1 );
     470             :             }
     471             : 
     472             :             /*---------------------------------------------------------------------*
     473             :              * dequantization
     474             :              *---------------------------------------------------------------------*/
     475             : 
     476      366591 :             for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
     477             :             {
     478      364776 :                 t_audio_tmp[i] = (float) ( x_norm[i] );
     479             :             }
     480             : 
     481        1815 :             mvs2s( nq, nq_tmp, Nsv );
     482        1815 :             if ( Nsv2 > Nsv )
     483             :             {
     484             :                 /* Safety check, happens rarely */
     485           0 :                 set_s( nq_tmp + Nsv, 0, Nsv2 - Nsv );
     486             :             }
     487             : 
     488        1815 :             k = 0;
     489        1815 :             incr = 0;
     490       47412 :             for ( i = 0; i < Nsv; i++ )
     491             :             {
     492       45597 :                 if ( nq[i] == 0 && incr < Nsv2 )
     493             :                 {
     494       64665 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     495             :                     {
     496       57480 :                         t_audio_tmp[i * WIDTH_BAND + j] = gain2 * x_norm1[k++];
     497             :                     }
     498        7185 :                     nq[i] += nq2[incr++];
     499             :                 }
     500             :             }
     501             : 
     502        2565 :             for ( i = 0; incr < Nsv2; i++ )
     503             :             {
     504             :                 /* safety check, happens rarely */
     505         750 :                 if ( i >= Nsv2 )
     506             :                 {
     507           0 :                     break;
     508             :                 }
     509             : 
     510         750 :                 if ( nq_tmp[i] != 0 )
     511             :                 {
     512        6318 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     513             :                     {
     514        5616 :                         t_audio_tmp[i * WIDTH_BAND + j] += gain2 * x_norm1[k++];
     515             :                     }
     516         702 :                     nq[i] += nq2[incr++];
     517             :                 }
     518             :             }
     519             : 
     520             :             /*---------------------------------------------------------------------*
     521             :              * reorder the decoded spectrum
     522             :              *---------------------------------------------------------------------*/
     523             : 
     524        1815 :             if ( nBits_total > NBITS_THRESH_BWE_HR )
     525             :             {
     526           0 :                 mvr2r( t_audio_tmp, t_audio + NUM_NONTRANS_START_FREQ_COEF, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF );
     527             :             }
     528             :             else
     529             :             {
     530        1815 :                 ind1 = ( pos * 64 + pos / 2 * WIDTH_BAND );
     531        1815 :                 mvr2r( t_audio_tmp, t_audio + NUM_NONTRANS_START_FREQ_COEF, ind1 );
     532             : 
     533        1815 :                 ind2 = ( ( pos + 1 ) * 64 + ( pos + 1 ) / 2 * WIDTH_BAND );
     534        1815 :                 mvr2r( t_audio_tmp + ind1, t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2 );
     535             : 
     536             :                 /* reconstruct non-encoded subband */
     537        1815 :                 if ( pos == 3 )
     538             :                 {
     539        1428 :                     mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + 128, t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, 72 );
     540             : 
     541        1428 :                     mvs2s( nq + 16, nq + 25, 9 );
     542             :                 }
     543             :                 else
     544             :                 {
     545         387 :                     pos %= 2;
     546         387 :                     mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, t_audio + NUM_NONTRANS_START_FREQ_COEF + ind1, 64 + pos * WIDTH_BAND );
     547         387 :                     ind1 /= WIDTH_BAND;
     548         387 :                     ind2 /= WIDTH_BAND;
     549             : 
     550         387 :                     j = 0;
     551        5598 :                     for ( i = Nsv - 1; i >= ind1; i-- )
     552             :                     {
     553        5211 :                         nq[33 - j++] = nq[i];
     554             :                     }
     555             : 
     556         387 :                     mvs2s( nq + ind2, nq + ind1, WIDTH_BAND + pos );
     557             :                 }
     558             :             }
     559             : 
     560             :             /* apply noise-fill */
     561        1815 :             if ( nBits < 200 )
     562             :             {
     563        1815 :                 swb_hr_noise_fill( is_transient, NUM_NONTRANS_START_FREQ_COEF, NUM_NONTRANS_END_FREQ_COEF, tilt_wb, pitch, nq, Nsv, &hBWE_FD_HR->bwe_highrate_seed, t_audio + NUM_NONTRANS_START_FREQ_COEF );
     564             :             }
     565             : 
     566             :             /*---------------------------------------------------------------------*
     567             :              * reconstruction
     568             :              *---------------------------------------------------------------------*/
     569             : 
     570             :             /* smoothing 12.6-12.8kHz */
     571        1815 :             if ( pos == 3 && nBits_total <= 400 )
     572             :             {
     573        1428 :                 tmpF = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200 - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
     574        1428 :                 tmp2 = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, WIDTH_BAND ) + EPSILON;
     575        1428 :                 tmpF = (float) sqrt( tmpF / tmp2 );
     576       12852 :                 for ( i = 0; i < WIDTH_BAND; i++ )
     577             :                 {
     578       11424 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + 200 + i] *= ( ( 1.0f - i / (float) WIDTH_BAND ) * tmpF + i / (float) WIDTH_BAND );
     579             :                 }
     580             :             }
     581             : 
     582             :             /* reconstruct 14.4-16(20) kHz spectrum */
     583        1815 :             if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
     584             :             {
     585        1815 :                 width_noncoded = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
     586             :             }
     587             :             else /* st->extl == FB_BWE_HIGHRATE */
     588             :             {
     589           0 :                 width_noncoded = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_END_FREQ_COEF;
     590             :             }
     591        1815 :             mvr2r( t_audio + NUM_NONTRANS_END_FREQ_COEF - width_noncoded, t_audio + NUM_NONTRANS_END_FREQ_COEF, width_noncoded );
     592             : 
     593             :             /* smoothing 14.4-14.8kHz */
     594        1815 :             tmpF = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
     595        1815 :             tmp2 = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, WIDTH_BAND ) + EPSILON;
     596        1815 :             tmpF = (float) sqrt( tmpF / tmp2 );
     597       16335 :             for ( i = 0; i < WIDTH_BAND; i++ )
     598             :             {
     599       14520 :                 t_audio[NUM_NONTRANS_END_FREQ_COEF + i] *= tmpF;
     600             :             }
     601             : 
     602             :             /* envelope denormalization */
     603        9075 :             for ( i = 0; i < N_BANDS_BWE_HR; i++ )
     604             :             {
     605      500940 :                 for ( j = 0; j < WIDTH_NONTRANS_FREQ_COEF; j++ )
     606             :                 {
     607      493680 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF + j] *= en_band[i];
     608             :                 }
     609             :             }
     610             : 
     611             :             /* equalize 14.4-16(20) kHz spectrum */
     612        1815 :             tmpF = max_env / min_env;
     613        1815 :             if ( st->extl == SWB_BWE_HIGHRATE || tmpF < 2.2f )
     614             :             {
     615       16335 :                 for ( j = 0; j < WIDTH_BAND; j++ )
     616             :                 {
     617       14520 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= ( ( 1.0f - j / (float) WIDTH_BAND ) * en_band[3] + ( j / (float) WIDTH_BAND ) * en_noncoded );
     618             :                 }
     619             : 
     620      103455 :                 for ( j = WIDTH_BAND; j < width_noncoded; j++ )
     621             :                 {
     622      101640 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= en_noncoded;
     623             :                 }
     624             :             }
     625             :             else
     626             :             {
     627           0 :                 if ( output_frame == L_FRAME48k )
     628             :                 {
     629           0 :                     tmpS = width_noncoded - 2 * WIDTH_NONTRANS_FREQ_COEF;
     630             :                 }
     631             :                 else
     632             :                 {
     633           0 :                     tmpS = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
     634             :                 }
     635             : 
     636           0 :                 k = 0;
     637           0 :                 for ( j = 0; j < tmpS; j++ )
     638             :                 {
     639           0 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 2.2f * en_noncoded * ( 1 - (float) k / (float) 160 );
     640           0 :                     k++;
     641             :                 }
     642             : 
     643           0 :                 k = 0;
     644           0 :                 for ( ; j < width_noncoded; j++ )
     645             :                 {
     646           0 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 0.65f * en_noncoded * ( 1 - (float) k / (float) 320 );
     647           0 :                     k++;
     648             :                 }
     649             :             }
     650             : 
     651             :             /* overlap region */
     652        1815 :             if ( output_frame == L_FRAME48k )
     653             :             {
     654       30855 :                 for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
     655             :                 {
     656       29040 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs_48kHz[i];
     657             :                 }
     658             :             }
     659             :             else
     660             :             {
     661           0 :                 for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
     662             :                 {
     663           0 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs[i];
     664             :                 }
     665             :             }
     666             : 
     667             :             /* apply global gain */
     668        1815 :             if ( nBits_total <= NBITS_THRESH_BWE_HR )
     669             :             {
     670        1815 :                 gain *= 0.85f;
     671             :             }
     672             : 
     673      611655 :             for ( i = 0; i < WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR + width_noncoded; i++ )
     674             :             {
     675      609840 :                 t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= gain;
     676             :             }
     677             : 
     678             :             /* save transform coefficients for the next frame (needed in case of frame erasures) */
     679        1815 :             if ( output_frame == L_FRAME32k )
     680             :             {
     681           0 :                 mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF );
     682             :             }
     683             :             else /* output_frame == L_FRAME48k */
     684             :             {
     685        1815 :                 mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF );
     686             :             }
     687             : 
     688             :             /* attenuate HFs in case of band-width switching */
     689        1815 :             if ( st->bws_cnt1 > 0 )
     690             :             {
     691           0 :                 if ( output_frame == L_FRAME32k )
     692             :                 {
     693           0 :                     j = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
     694             :                 }
     695             :                 else /* output_frame == L_FRAME48k */
     696             :                 {
     697           0 :                     j = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
     698             :                 }
     699             : 
     700           0 :                 for ( i = 0; i < j; i++ )
     701             :                 {
     702           0 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
     703             :                 }
     704             :             }
     705             :         }
     706             :     }
     707             : 
     708        2007 :     st->prev_ener_shb = gain * env;
     709       30105 :     for ( i = 0; i < SWB_FENV; i++ )
     710             :     {
     711       28098 :         hBWE_FD->prev_SWB_fenv[i] = gain * env;
     712             :     }
     713             : 
     714             :     /*---------------------------------------------------------------------*
     715             :      * iOLA and iMDCT
     716             :      *---------------------------------------------------------------------*/
     717             : 
     718        2007 :     inverse_transform( t_audio, t_audio_tmp, is_transient, output_frame, output_frame, st->element_mode );
     719             : 
     720        2007 :     window_ola( t_audio_tmp, hb_synth, hBWE_FD->old_wtda_swb, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
     721             : 
     722             :     /*---------------------------------------------------------------------*
     723             :      * final adjustments
     724             :      *---------------------------------------------------------------------*/
     725             : 
     726        2007 :     if ( !st->bfi )
     727             :     {
     728        2007 :         IsTransient = 0;
     729        2007 :         EnergyLT = hBWE_FD_HR->mem_EnergyLT;
     730        2007 :         pos = 0;
     731       10035 :         for ( j = 0; j < 4; j++ )
     732             :         {
     733        8028 :             Energy = sum2_f( hb_synth + j * ( output_frame / 4 ), output_frame / 4 ) + EPSILON;
     734        8028 :             if ( Energy > 12.5f * EnergyLT )
     735             :             {
     736          84 :                 IsTransient = 1;
     737          84 :                 pos = j;
     738             :             }
     739             : 
     740        8028 :             EnergyLT = 0.75f * EnergyLT + 0.25f * Energy;
     741             :         }
     742             : 
     743        2007 :         if ( IsTransient == 1 && pos > 0 && tilt_wb < 3.0f && pitch > 500 )
     744             :         {
     745           9 :             Nsv = pos * ( output_frame / 4 );
     746           9 :             Energy = sum2_f( hb_synth, Nsv ) + EPSILON;
     747           9 :             if ( st->last_extl != st->extl )
     748             :             {
     749           3 :                 hBWE_FD_HR->mem_EnergyLT = Energy;
     750             :             }
     751           9 :             gain = (float) sqrt( pos * hBWE_FD_HR->mem_EnergyLT / Energy );
     752             : 
     753           9 :             gain *= 0.2f;
     754        2169 :             for ( i = 0; i < Nsv; i++ )
     755             :             {
     756        2160 :                 hb_synth[i] *= gain;
     757             :             }
     758             : 
     759           9 :             alpha = (float) WIDTH_BAND / output_frame;
     760        1089 :             for ( i = 0; i < output_frame / WIDTH_BAND; i++ )
     761             :             {
     762        1080 :                 hb_synth[i + Nsv] *= ( ( 1.0f - i * alpha ) * gain + i * alpha );
     763             :             }
     764             :         }
     765             : 
     766        2007 :         hBWE_FD_HR->mem_EnergyLT = EnergyLT;
     767        2007 :         hBWE_FD_HR->old_is_transient_hr_bwe = is_transient;
     768             :     }
     769             : 
     770             :     /* post-processing in case of TD/FD switching */
     771        2007 :     if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
     772             :     {
     773          33 :         if ( tilt_wb < 3.0f )
     774             :         {
     775          30 :             gain = td_postprocess( hb_synth, output_frame, st->last_extl );
     776             : 
     777       28830 :             for ( i = 0; i < output_frame; i++ )
     778             :             {
     779       28800 :                 hBWE_FD->old_wtda_swb[i] *= gain;
     780             :             }
     781             : 
     782          30 :             tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
     783          30 :             if ( output_frame == L_FRAME48k )
     784             :             {
     785          30 :                 tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
     786             :             }
     787             : 
     788       14910 :             for ( i = 0; i < tmpS; i++ )
     789             :             {
     790       14880 :                 hBWE_FD_HR->t_audio_prev[i] *= gain;
     791             :             }
     792             :         }
     793             :     }
     794             : 
     795        2007 :     return;
     796             : }
     797             : 
     798             : /*-------------------------------------------------------------------*
     799             :  * hr_bwe_dec_init()
     800             :  *
     801             :  * Initialize HR BWE state structure at the decoder
     802             :  *-------------------------------------------------------------------*/
     803             : 
     804           9 : void hr_bwe_dec_init(
     805             :     HR_BWE_DEC_HANDLE hBWE_FD_HR /* i/o: HR BWE data handle  */
     806             : )
     807             : {
     808           9 :     set_f( hBWE_FD_HR->t_audio_prev, 0, 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF );
     809           9 :     hBWE_FD_HR->old_is_transient_hr_bwe = 0;
     810           9 :     hBWE_FD_HR->bwe_highrate_seed = 12345;
     811           9 :     hBWE_FD_HR->mem_EnergyLT = 0.0f;
     812             : 
     813           9 :     return;
     814             : }

Generated by: LCOV version 1.14