LCOV - code coverage report
Current view: top level - lib_dec - swb_bwe_dec_hr.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 279 285 97.9 %
Date: 2025-09-11 08:49:05 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       10941 : 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       10941 :     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       10941 :     float env = 0.0f;
      76             : 
      77       10941 :     hBWE_FD = st->hBWE_FD;
      78       10941 :     hBWE_FD_HR = st->hBWE_FD_HR;
      79             : 
      80             :     /*---------------------------------------------------------------------*
      81             :      * initializations
      82             :      *---------------------------------------------------------------------*/
      83             : 
      84       10941 :     set_f( t_audio, 0.0f, output_frame );
      85             : 
      86       10941 :     hBWE_FD_HR->bwe_highrate_seed = (int16_t) ( (int16_t) ( pitch_buf[0] * 64.0f ) * (int16_t) ( pitch_buf[3] * 64.0f ) );
      87             : 
      88       10941 :     ind2 = 0;  /* only to suppress warnings */
      89       10941 :     Nsv2 = 0;  /* only to suppress warnings */
      90       10941 :     gain2 = 0; /* only to suppress warnings */
      91             : 
      92             :     /* reset memories in case that last frame was a different technology */
      93       10941 :     if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
      94             :     {
      95         163 :         set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
      96             :     }
      97             : 
      98             :     /* calculate SWB BWE bit-budget */
      99       10941 :     nBits = (int16_t) ( st->extl_brate ) / FRAMES_PER_SEC + unbits;
     100       10941 :     nBits_total = nBits;
     101             : 
     102             :     /*---------------------------------------------------------------------*
     103             :      * calculate tilt of the core synthesis
     104             :      *---------------------------------------------------------------------*/
     105             : 
     106       10941 :     calc_tilt_bwe( syn_12k8_16k, &tilt_wb, L_FRAME16k );
     107       10941 :     pitch = sum_f( pitch_buf, NB_SUBFR16k ) + EPSILON;
     108             : 
     109             :     /*---------------------------------------------------------------------*
     110             :      * FEC, or good frame decoding
     111             :      *---------------------------------------------------------------------*/
     112             : 
     113       10941 :     if ( st->bfi )
     114             :     {
     115         134 :         is_transient = hBWE_FD_HR->old_is_transient_hr_bwe;
     116             : 
     117             :         /* Replication of the last spectrum, with an attenuation */
     118         134 :         if ( ( st->clas_dec == VOICED_CLAS || st->clas_dec == INACTIVE_CLAS ) && st->nbLostCmpt <= 3 )
     119             :         {
     120          68 :             alpha = 0.8f;
     121             :         }
     122          66 :         else if ( is_transient )
     123             :         {
     124           3 :             alpha = 0.15f;
     125             :         }
     126             :         else
     127             :         {
     128          63 :             alpha = 0.3f;
     129             :         }
     130             : 
     131         134 :         if ( is_transient )
     132             :         {
     133             :             /* set BWE spectrum length */
     134           3 :             if ( output_frame == L_FRAME32k )
     135             :             {
     136           3 :                 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          15 :             for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
     145             :             {
     146        1020 :                 for ( i = 0; i < tmpS; i++ )
     147             :                 {
     148        1008 :                     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          12 :                 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         131 :             if ( output_frame == L_FRAME32k )
     158             :             {
     159          87 :                 tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
     160             :             }
     161             :             else /* output_frame == L_FRAME48k */
     162             :             {
     163          44 :                 tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
     164             :             }
     165             : 
     166             :             /* reconstruct */
     167       51187 :             for ( i = 0; i < tmpS; i++ )
     168             :             {
     169       51056 :                 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         131 :             mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, tmpS );
     173             :         }
     174             : 
     175         134 :         hBWE_FD_HR->mem_EnergyLT *= alpha;
     176         134 :         gain = (float) ( 2.0f * sqrt( hBWE_FD_HR->mem_EnergyLT / output_frame ) );
     177         134 :         env = 1.0f;
     178             :     }
     179             :     else
     180             :     {
     181             :         /*---------------------------------------------------------------------*
     182             :          * get transient frame flag
     183             :          *---------------------------------------------------------------------*/
     184             : 
     185       10807 :         is_transient = get_next_indice( st, 1 );
     186             : 
     187       10807 :         if ( is_transient )
     188             :         {
     189         896 :             nBits = -1; /* is_transient flag */
     190         896 :             nBits_block = nBits_total / NUM_TIME_SWITCHING_BLOCKS;
     191         896 :             nBits += nBits_total % NUM_TIME_SWITCHING_BLOCKS;
     192             : 
     193             :             /* set width of noncoded (blind estimated) spectrum */
     194         896 :             if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
     195             :             {
     196         766 :                 width_noncoded = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     197         766 :                 tmpS = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF_EFF;
     198             :             }
     199             :             else /* st->extl == FB_BWE_HIGHRATE */
     200             :             {
     201         130 :                 width_noncoded = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     202         130 :                 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        4480 :             for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
     210             :             {
     211        3584 :                 nBits += nBits_block;
     212             : 
     213             :                 /*---------------------------------------------------------------------*
     214             :                  * global gain and envelope decoding
     215             :                  *---------------------------------------------------------------------*/
     216             : 
     217             :                 /* get global gain */
     218        3584 :                 ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     219        3584 :                 gain = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     220        3584 :                 nBits -= NBITS_GLOB_GAIN_BWE_HR;
     221             : 
     222             :                 /* get energy per band */
     223        3584 :                 if ( k == 0 )
     224             :                 {
     225         896 :                     ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR );
     226         896 :                     ind2 = ind1;
     227         896 :                     nBits -= NBITS_ENVELOPE_BWE_HR_TR;
     228             :                 }
     229             :                 else
     230             :                 {
     231        2688 :                     if ( ind2 < 8 )
     232             :                     {
     233         576 :                         ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 );
     234             :                     }
     235             :                     else
     236             :                     {
     237        2112 :                         ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 ) + NUM_ENVLOPE_CODE_HR_TR2;
     238             :                     }
     239        2688 :                     nBits -= ( NBITS_ENVELOPE_BWE_HR_TR - 1 );
     240             :                 }
     241             : 
     242        3584 :                 en_band[0] = swb_hr_env_code3[2 * ind1];
     243        3584 :                 en_band[1] = swb_hr_env_code3[2 * ind1 + 1];
     244        3584 :                 env = 0.5f * ( en_band[0] + en_band[1] );
     245             : 
     246             :                 /*---------------------------------------------------------------------*
     247             :                  * estimate energy of noncoded spectrum (14.4-20kHz)
     248             :                  *---------------------------------------------------------------------*/
     249             : 
     250        3584 :                 en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
     251             : 
     252        3584 :                 if ( st->extl == FB_BWE_HIGHRATE )
     253             :                 {
     254         520 :                     ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
     255         520 :                     nBits -= NBITS_HF_GAIN_BWE_HR;
     256             : 
     257         520 :                     if ( ind1 == 1 )
     258             :                     {
     259           3 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT1;
     260             :                     }
     261         517 :                     else if ( ind1 == 2 )
     262             :                     {
     263         200 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT2;
     264             :                     }
     265         317 :                     else if ( ind1 == 3 )
     266             :                     {
     267         136 :                         en_noncoded *= BWE_HR_TRANS_EN_LIMIT3;
     268             :                     }
     269             :                 }
     270             : 
     271             :                 /*---------------------------------------------------------------------*
     272             :                  * AVQ decoding (dequantize normalized spectrum)
     273             :                  *---------------------------------------------------------------------*/
     274             : 
     275        3584 :                 Nsv = ( NUM_TRANS_END_FREQ_COEF - NUM_TRANS_START_FREQ_COEF ) / WIDTH_BAND;
     276        3584 :                 AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     277      232960 :                 for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
     278             :                 {
     279      229376 :                     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        3584 :                 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       96064 :                 for ( j = 0; j < tmpS; j++ )
     291             :                 {
     292       92480 :                     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       10752 :                 for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
     297             :                 {
     298      250880 :                     for ( j = 0; j < WIDTH_TRANS_FREQ_COEF; j++ )
     299             :                     {
     300      243712 :                         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       81728 :                 for ( j = tmpS - width_noncoded; j < tmpS; j++ )
     306             :                 {
     307       78144 :                     t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF + j] *= en_noncoded;
     308             :                 }
     309             : 
     310             :                 /* overlap region */
     311        3584 :                 if ( output_frame == L_FRAME48k )
     312             :                 {
     313       14260 :                     for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
     314             :                     {
     315       11408 :                         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        3660 :                     for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
     321             :                     {
     322        2928 :                         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      325440 :                 for ( i = 0; i < WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR + width_noncoded; i++ )
     328             :                 {
     329      321856 :                     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        3584 :                 if ( output_frame == L_FRAME32k )
     334             :                 {
     335         732 :                     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        2852 :                     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        3584 :                 if ( st->bws_cnt1 > 0 )
     344             :                 {
     345          16 :                     if ( output_frame == L_FRAME32k )
     346             :                     {
     347          16 :                         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        1360 :                     for ( i = 0; i < j; i++ )
     355             :                     {
     356        1344 :                         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        9911 :             nBits--;
     365             : 
     366             :             /*---------------------------------------------------------------------*
     367             :              * global gain and envelope decoding
     368             :              *---------------------------------------------------------------------*/
     369             : 
     370             :             /* get global gain */
     371        9911 :             ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     372        9911 :             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        9911 :             ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR1 );
     376        9911 :             ind2 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR2 );
     377             : 
     378        9911 :             en_band[0] = swb_hr_env_code1[2 * ind1];
     379        9911 :             en_band[1] = swb_hr_env_code1[2 * ind1 + 1];
     380        9911 :             en_band[2] = swb_hr_env_code2[2 * ind2];
     381        9911 :             en_band[3] = swb_hr_env_code2[2 * ind2 + 1];
     382        9911 :             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        9911 :             pos = 0;
     390        9911 :             min_env = en_band[0];
     391        9911 :             max_env = en_band[0];
     392       39644 :             for ( j = 1; j < N_BANDS_BWE_HR; j++ )
     393             :             {
     394       29733 :                 if ( en_band[j] < min_env )
     395             :                 {
     396       20971 :                     pos = j;
     397       20971 :                     min_env = en_band[j];
     398             :                 }
     399       29733 :                 if ( en_band[j] > max_env )
     400             :                 {
     401        4208 :                     max_env = en_band[j];
     402             :                 }
     403             :             }
     404             : 
     405             :             /* decide the spectrum to be dequantized */
     406        9911 :             if ( nBits_total > NBITS_THRESH_BWE_HR )
     407             :             {
     408          50 :                 i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF;
     409             :             }
     410             :             else
     411             :             {
     412        9861 :                 i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - 64 - 8 * ( pos % 2 );
     413             :             }
     414             : 
     415        9911 :             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        9911 :             en_noncoded = 0.5f * min_env;
     422             : 
     423        9911 :             if ( st->extl == FB_BWE_HIGHRATE )
     424             :             {
     425         931 :                 ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
     426         931 :                 nBits -= NBITS_HF_GAIN_BWE_HR;
     427             : 
     428         931 :                 if ( ind1 == 1 )
     429             :                 {
     430         445 :                     en_noncoded *= BWE_HR_NONTRANS_EN_LIMIT1;
     431             :                 }
     432         486 :                 else if ( ind1 == 2 )
     433             :                 {
     434          84 :                     en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT2;
     435             :                 }
     436         402 :                 else if ( ind1 == 3 )
     437             :                 {
     438         186 :                     en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT3;
     439             :                 }
     440             :             }
     441             : 
     442             :             /*---------------------------------------------------------------------*
     443             :              * AVQ decoding (dequantize normalized spectrum)
     444             :              *---------------------------------------------------------------------*/
     445             : 
     446        9911 :             Nsv = i / WIDTH_BAND;
     447        9911 :             AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     448             :             /*---------------------------------------------------------------------*
     449             :              * second stage decoding
     450             :              *---------------------------------------------------------------------*/
     451             : 
     452        9911 :             if ( nBits >= 9 + NBITS_GLOB_GAIN_BWE_HR && sum_s( nq, Nsv ) > 0 )
     453             :             {
     454        9603 :                 ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
     455        9603 :                 gain2 = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     456        9603 :                 gain2 *= 0.0625f;
     457             : 
     458             :                 /* calculate the number of subbands according to the rest bits */
     459        9603 :                 if ( nBits > 396 )
     460             :                 {
     461          23 :                     Nsv2 = 33;
     462             :                 }
     463             :                 else
     464             :                 {
     465        9580 :                     Nsv2 = nBits / 12;
     466             :                 }
     467             : 
     468        9603 :                 nBits -= NBITS_GLOB_GAIN_BWE_HR;
     469        9603 :                 AVQ_demuxdec( st, x_norm1, &nBits, Nsv2, nq2, 0, Nsv2 - 1 );
     470             :             }
     471             : 
     472             :             /*---------------------------------------------------------------------*
     473             :              * dequantization
     474             :              *---------------------------------------------------------------------*/
     475             : 
     476     2006663 :             for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
     477             :             {
     478     1996752 :                 t_audio_tmp[i] = (float) ( x_norm[i] );
     479             :             }
     480             : 
     481        9911 :             mvs2s( nq, nq_tmp, Nsv );
     482        9911 :             if ( Nsv2 > Nsv )
     483             :             {
     484             :                 /* Safety check, happens rarely */
     485           0 :                 set_s( nq_tmp + Nsv, 0, Nsv2 - Nsv );
     486             :             }
     487             : 
     488        9911 :             k = 0;
     489        9911 :             incr = 0;
     490      259505 :             for ( i = 0; i < Nsv; i++ )
     491             :             {
     492      249594 :                 if ( nq[i] == 0 && incr < Nsv2 )
     493             :                 {
     494      358821 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     495             :                     {
     496      318952 :                         t_audio_tmp[i * WIDTH_BAND + j] = gain2 * x_norm1[k++];
     497             :                     }
     498       39869 :                     nq[i] += nq2[incr++];
     499             :                 }
     500             :             }
     501             : 
     502       15163 :             for ( i = 0; incr < Nsv2; i++ )
     503             :             {
     504             :                 /* safety check, happens rarely */
     505        5252 :                 if ( i >= Nsv2 )
     506             :                 {
     507           0 :                     break;
     508             :                 }
     509             : 
     510        5252 :                 if ( nq_tmp[i] != 0 )
     511             :                 {
     512       40842 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     513             :                     {
     514       36304 :                         t_audio_tmp[i * WIDTH_BAND + j] += gain2 * x_norm1[k++];
     515             :                     }
     516        4538 :                     nq[i] += nq2[incr++];
     517             :                 }
     518             :             }
     519             : 
     520             :             /*---------------------------------------------------------------------*
     521             :              * reorder the decoded spectrum
     522             :              *---------------------------------------------------------------------*/
     523             : 
     524        9911 :             if ( nBits_total > NBITS_THRESH_BWE_HR )
     525             :             {
     526          50 :                 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        9861 :                 ind1 = ( pos * 64 + pos / 2 * WIDTH_BAND );
     531        9861 :                 mvr2r( t_audio_tmp, t_audio + NUM_NONTRANS_START_FREQ_COEF, ind1 );
     532             : 
     533        9861 :                 ind2 = ( ( pos + 1 ) * 64 + ( pos + 1 ) / 2 * WIDTH_BAND );
     534        9861 :                 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        9861 :                 if ( pos == 3 )
     538             :                 {
     539        7150 :                     mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + 128, t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, 72 );
     540             : 
     541        7150 :                     mvs2s( nq + 16, nq + 25, 9 );
     542             :                 }
     543             :                 else
     544             :                 {
     545        2711 :                     pos %= 2;
     546        2711 :                     mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, t_audio + NUM_NONTRANS_START_FREQ_COEF + ind1, 64 + pos * WIDTH_BAND );
     547        2711 :                     ind1 /= WIDTH_BAND;
     548        2711 :                     ind2 /= WIDTH_BAND;
     549             : 
     550        2711 :                     j = 0;
     551       46414 :                     for ( i = Nsv - 1; i >= ind1; i-- )
     552             :                     {
     553       43703 :                         nq[33 - j++] = nq[i];
     554             :                     }
     555             : 
     556        2711 :                     mvs2s( nq + ind2, nq + ind1, WIDTH_BAND + pos );
     557             :                 }
     558             :             }
     559             : 
     560             :             /* apply noise-fill */
     561        9911 :             if ( nBits < 200 )
     562             :             {
     563        9894 :                 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        9911 :             if ( pos == 3 && nBits_total <= 400 )
     572             :             {
     573        7150 :                 tmpF = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200 - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
     574        7150 :                 tmp2 = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, WIDTH_BAND ) + EPSILON;
     575        7150 :                 tmpF = (float) sqrt( tmpF / tmp2 );
     576       64350 :                 for ( i = 0; i < WIDTH_BAND; i++ )
     577             :                 {
     578       57200 :                     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        9911 :             if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
     584             :             {
     585        8980 :                 width_noncoded = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
     586             :             }
     587             :             else /* st->extl == FB_BWE_HIGHRATE */
     588             :             {
     589         931 :                 width_noncoded = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_END_FREQ_COEF;
     590             :             }
     591        9911 :             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        9911 :             tmpF = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
     595        9911 :             tmp2 = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, WIDTH_BAND ) + EPSILON;
     596        9911 :             tmpF = (float) sqrt( tmpF / tmp2 );
     597       89199 :             for ( i = 0; i < WIDTH_BAND; i++ )
     598             :             {
     599       79288 :                 t_audio[NUM_NONTRANS_END_FREQ_COEF + i] *= tmpF;
     600             :             }
     601             : 
     602             :             /* envelope denormalization */
     603       49555 :             for ( i = 0; i < N_BANDS_BWE_HR; i++ )
     604             :             {
     605     2735436 :                 for ( j = 0; j < WIDTH_NONTRANS_FREQ_COEF; j++ )
     606             :                 {
     607     2695792 :                     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        9911 :             tmpF = max_env / min_env;
     613        9911 :             if ( st->extl == SWB_BWE_HIGHRATE || tmpF < 2.2f )
     614             :             {
     615       82773 :                 for ( j = 0; j < WIDTH_BAND; j++ )
     616             :                 {
     617       73576 :                     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      558949 :                 for ( j = WIDTH_BAND; j < width_noncoded; j++ )
     621             :                 {
     622      549752 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= en_noncoded;
     623             :                 }
     624             :             }
     625             :             else
     626             :             {
     627         714 :                 if ( output_frame == L_FRAME48k )
     628             :                 {
     629         714 :                     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         714 :                 k = 0;
     637       63546 :                 for ( j = 0; j < tmpS; j++ )
     638             :                 {
     639       62832 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 2.2f * en_noncoded * ( 1 - (float) k / (float) 160 );
     640       62832 :                     k++;
     641             :                 }
     642             : 
     643         714 :                 k = 0;
     644       97818 :                 for ( ; j < width_noncoded; j++ )
     645             :                 {
     646       97104 :                     t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 0.65f * en_noncoded * ( 1 - (float) k / (float) 320 );
     647       97104 :                     k++;
     648             :                 }
     649             :             }
     650             : 
     651             :             /* overlap region */
     652        9911 :             if ( output_frame == L_FRAME48k )
     653             :             {
     654      148784 :                 for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
     655             :                 {
     656      140032 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs_48kHz[i];
     657             :                 }
     658             :             }
     659             :             else
     660             :             {
     661       19703 :                 for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
     662             :                 {
     663       18544 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs[i];
     664             :                 }
     665             :             }
     666             : 
     667             :             /* apply global gain */
     668        9911 :             if ( nBits_total <= NBITS_THRESH_BWE_HR )
     669             :             {
     670        9861 :                 gain *= 0.85f;
     671             :             }
     672             : 
     673     3488967 :             for ( i = 0; i < WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR + width_noncoded; i++ )
     674             :             {
     675     3479056 :                 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        9911 :             if ( output_frame == L_FRAME32k )
     680             :             {
     681        1159 :                 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        8752 :                 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        9911 :             if ( st->bws_cnt1 > 0 )
     690             :             {
     691          16 :                 if ( output_frame == L_FRAME32k )
     692             :                 {
     693          16 :                     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        5392 :                 for ( i = 0; i < j; i++ )
     701             :                 {
     702        5376 :                     t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
     703             :                 }
     704             :             }
     705             :         }
     706             :     }
     707             : 
     708       10941 :     st->prev_ener_shb = gain * env;
     709      164115 :     for ( i = 0; i < SWB_FENV; i++ )
     710             :     {
     711      153174 :         hBWE_FD->prev_SWB_fenv[i] = gain * env;
     712             :     }
     713             : 
     714             :     /*---------------------------------------------------------------------*
     715             :      * iOLA and iMDCT
     716             :      *---------------------------------------------------------------------*/
     717             : 
     718       10941 :     inverse_transform( t_audio, t_audio_tmp, is_transient, output_frame, output_frame, st->element_mode );
     719             : 
     720       10941 :     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       10941 :     if ( !st->bfi )
     727             :     {
     728       10807 :         IsTransient = 0;
     729       10807 :         EnergyLT = hBWE_FD_HR->mem_EnergyLT;
     730       10807 :         pos = 0;
     731       54035 :         for ( j = 0; j < 4; j++ )
     732             :         {
     733       43228 :             Energy = sum2_f( hb_synth + j * ( output_frame / 4 ), output_frame / 4 ) + EPSILON;
     734       43228 :             if ( Energy > 12.5f * EnergyLT )
     735             :             {
     736         489 :                 IsTransient = 1;
     737         489 :                 pos = j;
     738             :             }
     739             : 
     740       43228 :             EnergyLT = 0.75f * EnergyLT + 0.25f * Energy;
     741             :         }
     742             : 
     743       10807 :         if ( IsTransient == 1 && pos > 0 && tilt_wb < 3.0f && pitch > 500 )
     744             :         {
     745          41 :             Nsv = pos * ( output_frame / 4 );
     746          41 :             Energy = sum2_f( hb_synth, Nsv ) + EPSILON;
     747          41 :             if ( st->last_extl != st->extl )
     748             :             {
     749           8 :                 hBWE_FD_HR->mem_EnergyLT = Energy;
     750             :             }
     751          41 :             gain = (float) sqrt( pos * hBWE_FD_HR->mem_EnergyLT / Energy );
     752             : 
     753          41 :             gain *= 0.2f;
     754       16841 :             for ( i = 0; i < Nsv; i++ )
     755             :             {
     756       16800 :                 hb_synth[i] *= gain;
     757             :             }
     758             : 
     759          41 :             alpha = (float) WIDTH_BAND / output_frame;
     760        4841 :             for ( i = 0; i < output_frame / WIDTH_BAND; i++ )
     761             :             {
     762        4800 :                 hb_synth[i + Nsv] *= ( ( 1.0f - i * alpha ) * gain + i * alpha );
     763             :             }
     764             :         }
     765             : 
     766       10807 :         hBWE_FD_HR->mem_EnergyLT = EnergyLT;
     767       10807 :         hBWE_FD_HR->old_is_transient_hr_bwe = is_transient;
     768             :     }
     769             : 
     770             :     /* post-processing in case of TD/FD switching */
     771       10941 :     if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
     772             :     {
     773         163 :         if ( tilt_wb < 3.0f )
     774             :         {
     775         143 :             gain = td_postprocess( hb_synth, output_frame, st->last_extl );
     776             : 
     777      123983 :             for ( i = 0; i < output_frame; i++ )
     778             :             {
     779      123840 :                 hBWE_FD->old_wtda_swb[i] *= gain;
     780             :             }
     781             : 
     782         143 :             tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
     783         143 :             if ( output_frame == L_FRAME48k )
     784             :             {
     785         101 :                 tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
     786             :             }
     787             : 
     788       64351 :             for ( i = 0; i < tmpS; i++ )
     789             :             {
     790       64208 :                 hBWE_FD_HR->t_audio_prev[i] *= gain;
     791             :             }
     792             :         }
     793             :     }
     794             : 
     795       10941 :     return;
     796             : }
     797             : 
     798             : /*-------------------------------------------------------------------*
     799             :  * hr_bwe_dec_init()
     800             :  *
     801             :  * Initialize HR BWE state structure at the decoder
     802             :  *-------------------------------------------------------------------*/
     803             : 
     804         547 : void hr_bwe_dec_init(
     805             :     HR_BWE_DEC_HANDLE hBWE_FD_HR /* i/o: HR BWE data handle  */
     806             : )
     807             : {
     808         547 :     set_f( hBWE_FD_HR->t_audio_prev, 0, 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF );
     809         547 :     hBWE_FD_HR->old_is_transient_hr_bwe = 0;
     810         547 :     hBWE_FD_HR->bwe_highrate_seed = 12345;
     811         547 :     hBWE_FD_HR->mem_EnergyLT = 0.0f;
     812             : 
     813         547 :     return;
     814             : }

Generated by: LCOV version 1.14