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

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "basop_util.h"
      47             : #include "basop_proto_func.h"
      48             : #include "wmc_auto.h"
      49             : 
      50             : /*-------------------------------------------------------------------*
      51             :  * para_pred_bws()
      52             :  *
      53             :  * predict SWB parameters for bandwidth switching
      54             :  *-------------------------------------------------------------------*/
      55             : 
      56        9696 : static int16_t para_pred_bws(
      57             :     Decoder_State *st, /* i/o: decoder state structure       */
      58             :     float *signal_wb,  /* i  : wideband frequency signal     */
      59             :     float *SWB_fenv    /* o  : frequency-domain BWE envelope */
      60             : )
      61             : {
      62             :     int16_t i, j, k;
      63             :     int16_t mode;
      64             :     FD_BWE_DEC_HANDLE hBWE_FD;
      65             :     float *input_hi;
      66             :     float peak, mean[7], mag, min_val;
      67             :     float avrg1, avrg2;
      68             :     float att;
      69             : 
      70        9696 :     hBWE_FD = st->hBWE_FD;
      71             : 
      72        9696 :     mode = NORMAL;
      73             : 
      74        9696 :     k = 0;
      75        9696 :     input_hi = &signal_wb[SHARP_WIDTH];
      76       77568 :     for ( i = 0; i < 7; i++ )
      77             :     {
      78       67872 :         peak = 0.0f;
      79       67872 :         mean[i] = 0;
      80     2239776 :         for ( j = 0; j < SHARP_WIDTH; j++ )
      81             :         {
      82     2171904 :             mag = (float) fabs( *input_hi );
      83     2171904 :             if ( mag > peak )
      84             :             {
      85      333228 :                 peak = mag;
      86             :             }
      87     2171904 :             mean[i] += mag;
      88     2171904 :             input_hi++;
      89             :         }
      90             : 
      91       67872 :         if ( peak * ( SHARP_WIDTH + 3.5f ) > 4.5f * mean[i] && peak > 8.0f )
      92             :         {
      93        1614 :             k += 1;
      94             :         }
      95             :     }
      96             : 
      97        9696 :     avrg1 = 0.0f;
      98        9696 :     avrg2 = 0.0f;
      99       38784 :     for ( i = 1; i < 4; i++ )
     100             :     {
     101       29088 :         avrg1 += mean[i];
     102       29088 :         avrg2 += mean[i + 3];
     103             :     }
     104        9696 :     avrg1 /= 3;
     105        9696 :     avrg2 /= 3;
     106             : 
     107        9696 :     min_val = FLT_MAX;
     108        9696 :     peak = 0.0f;
     109       38784 :     for ( i = 4; i < 7; i++ )
     110             :     {
     111       29088 :         if ( mean[i] > 2.0f * avrg2 )
     112             :         {
     113        4335 :             mean[i] *= 2 * avrg2 / mean[i];
     114             :         }
     115       29088 :         if ( mean[i] < min_val )
     116             :         {
     117       16293 :             min_val = mean[i];
     118             :         }
     119       29088 :         if ( mean[i] > peak )
     120             :         {
     121       21012 :             peak = mean[i];
     122             :         }
     123             :     }
     124             : 
     125        9696 :     if ( st->tilt_wb > 8 )
     126             :     {
     127         264 :         min_val = min( st->tilt_wb / 15.0f, 1.0f ) * peak;
     128             :     }
     129             : 
     130        9696 :     if ( peak == 0 || min_val == 0 )
     131             :     {
     132           0 :         set_f( SWB_fenv, 0, SWB_FENV );
     133             :     }
     134             :     else
     135             :     {
     136      145440 :         for ( i = 0; i < SWB_FENV; i++ )
     137             :         {
     138      135744 :             SWB_fenv[i] = min_val * mean[i / 5 + 4] / ( 64 * peak );
     139             :         }
     140             :     }
     141             : 
     142       77568 :     for ( j = 0, i = SWB_FENV / 2; i < SWB_FENV; i++ )
     143             :     {
     144       67872 :         SWB_fenv[i] *= ( 1.0f - (float) j++ / SWB_FENV );
     145             :     }
     146             : 
     147        9696 :     if ( avrg1 > 8.0f * avrg2 )
     148             :     {
     149           0 :         for ( i = 0; i < SWB_FENV; i++ )
     150             :         {
     151           0 :             SWB_fenv[i] *= 0.5f;
     152             :         }
     153             :     }
     154        9696 :     if ( st->last_core != HQ_CORE && st->last_codec_mode == MODE1 &&
     155        9696 :          ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) &&
     156           0 :          ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) )
     157             :     {
     158           0 :         for ( i = 0; i < SWB_FENV; i++ )
     159             :         {
     160           0 :             if ( st->prev_coder_type != st->coder_type && SWB_fenv[i] > 2.0f * hBWE_FD->prev_SWB_fenv[i] )
     161             :             {
     162           0 :                 SWB_fenv[i] = 0.1f * SWB_fenv[i] + 0.9f * hBWE_FD->prev_SWB_fenv[i];
     163             :             }
     164             :             else
     165             :             {
     166           0 :                 SWB_fenv[i] = st->attenu1 * SWB_fenv[i] + ( 1.0f - st->attenu1 ) * hBWE_FD->prev_SWB_fenv[i];
     167             :             }
     168             :         }
     169             : 
     170           0 :         if ( st->attenu1 < 0.9f )
     171             :         {
     172           0 :             st->attenu1 += 0.05f;
     173             :         }
     174             :     }
     175             :     else
     176             :     {
     177        9696 :         if ( st->core_brate != st->last_core_brate || ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) ||
     178        8151 :              ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) )
     179             :         {
     180       23175 :             for ( i = 0; i < SWB_FENV; i++ )
     181             :             {
     182       21630 :                 if ( SWB_fenv[i] > 2.0f * hBWE_FD->prev_SWB_fenv[i] )
     183             :                 {
     184        8670 :                     SWB_fenv[i] = hBWE_FD->prev_SWB_fenv[i];
     185             :                 }
     186             :             }
     187             :         }
     188             : 
     189      145440 :         for ( i = 0; i < SWB_FENV; i++ )
     190             :         {
     191      135744 :             SWB_fenv[i] = 0.9f * SWB_fenv[i] + 0.1f * hBWE_FD->prev_SWB_fenv[i];
     192             :         }
     193             : 
     194        9696 :         st->attenu1 = 0.1f;
     195             :     }
     196             : 
     197        9696 :     if ( k > 3 )
     198             :     {
     199           0 :         mode = HARMONIC;
     200             :     }
     201             : 
     202        9696 :     att = ( (float) N_WS2N_FRAMES - (float) st->bws_cnt ) / (float) N_WS2N_FRAMES;
     203        9696 :     if ( st->L_frame == L_FRAME16k )
     204             :     {
     205         240 :         for ( i = 0; i < 4; i++ )
     206             :         {
     207         192 :             SWB_fenv[i] *= att;
     208             :         }
     209             :     }
     210             : 
     211      106656 :     for ( i = 4; i < SWB_FENV; i++ )
     212             :     {
     213       96960 :         SWB_fenv[i] *= att;
     214             :     }
     215             : 
     216        9696 :     return mode;
     217             : }
     218             : 
     219             : /*-------------------------------------------------------------------*
     220             :  * WB_BWE_gain_deq()
     221             :  *
     222             :  * Decoding of WB parameters
     223             :  *-------------------------------------------------------------------*/
     224             : 
     225       12246 : static int16_t WB_BWE_gain_deq(
     226             :     Decoder_State *st, /* i/o: decoder state structure   */
     227             :     float *WB_fenv )
     228             : {
     229             :     int16_t mode;
     230             :     int16_t index;
     231             : 
     232       12246 :     index = get_next_indice( st, 5 );
     233       12246 :     mode = get_next_indice( st, 1 ) + 2;
     234             : 
     235       12246 :     WB_fenv[0] = (float) pow( 2, 0.5f * F_2_5[2 * index] );
     236       12246 :     WB_fenv[1] = (float) pow( 2, 0.5f * F_2_5[2 * index + 1] );
     237             : 
     238       12246 :     return ( mode );
     239             : }
     240             : 
     241             : /*-------------------------------------------------------------------*
     242             :  * wb_bwe_dec()
     243             :  *
     244             :  * WB BWE decoder (only for 16kHz signals)
     245             :  *-------------------------------------------------------------------*/
     246             : 
     247       27591 : void wb_bwe_dec(
     248             :     Decoder_State *st,               /* i/o: decoder state structure                 */
     249             :     const float output[],            /* i  : synthesis @internal Fs                  */
     250             :     float *synth,                    /* i/o: ACELP core synthesis/final synthesis    */
     251             :     float *hb_synth,                 /* o  : SHB synthesis/final synthesis           */
     252             :     const int16_t use_cldfb_for_dft, /* i  : flag to use of CLDFB for DFT Stereo     */
     253             :     const int16_t output_frame,      /* i  : frame length                            */
     254             :     const float voice_factors[],     /* i  : voicing factors                         */
     255             :     const float pitch_buf[]          /* i  : pitch buffer                            */
     256             : )
     257             : {
     258             :     int16_t i, mode;
     259             :     FD_BWE_DEC_HANDLE hBWE_FD;
     260             :     float ysynth[L_FRAME48k]; /* MDCT spectrum of core synthesis              */
     261             :     float yerror[L_FRAME48k]; /* MDCT spectrum of error                       */
     262             :     float wtda_synth[2 * L_FRAME48k];
     263             :     float WB_fenv[SWB_FENV];
     264             : 
     265       27591 :     hBWE_FD = st->hBWE_FD;
     266             : 
     267             :     /* MDCT of the core synthesis signal */
     268       27591 :     if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
     269             :     {
     270             :         /* IVAS_fmToDo: wtda() does not support L_FRAME length; thus temporarily resample the signal */
     271             :         /* IVAS_fmToDo: delay output[] by 1.25ms ? */
     272         678 :         lerp( output, ysynth, L_FRAME16k, st->L_frame );
     273             : 
     274         678 :         wtda( ysynth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k );
     275         678 :         direct_transform( wtda_synth, ysynth, 0, /*st->L_frame*/ L_FRAME16k, st->element_mode );
     276             :     }
     277             :     else
     278             :     {
     279       26913 :         wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
     280       26913 :         direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
     281             :     }
     282             : 
     283       27591 :     if ( !st->bfi )
     284             :     {
     285       27435 :         if ( st->extl_brate > 0 )
     286             :         {
     287             :             /* de-quantization */
     288       12246 :             mode = WB_BWE_gain_deq( st, WB_fenv );
     289       12246 :             hBWE_FD->last_wb_bwe_ener = 0.5f * ( WB_fenv[0] + WB_fenv[1] );
     290             :         }
     291             :         else
     292             :         {
     293             :             int32_t tmp_brate;
     294             : 
     295       15189 :             tmp_brate = st->last_core_brate;
     296       15189 :             if ( st->last_total_brate == ACELP_9k60 && st->last_extl == SWB_TBE )
     297             :             {
     298           0 :                 tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */
     299             :             }
     300             : 
     301       15189 :             if ( st->last_extl != WB_BWE )
     302             :             {
     303         123 :                 hBWE_FD->prev_SWB_fenv[0] = 0.0f;
     304             :             }
     305             : 
     306       15189 :             mode = WB_BWE_gain_pred( WB_fenv, ysynth, st->coder_type, st->prev_coder_type, hBWE_FD->prev_SWB_fenv[0], voice_factors, pitch_buf, tmp_brate, hBWE_FD->last_wb_bwe_ener, st->last_extl, st->tilt_wb );
     307             :         }
     308             :     }
     309             :     else
     310             :     {
     311             :         /* FEC */
     312         156 :         mode = NORMAL;
     313         468 :         for ( i = 0; i < 2; i++ )
     314             :         {
     315         312 :             WB_fenv[i] = 0.75f * hBWE_FD->prev_SWB_fenv[i];
     316             :         }
     317             :     }
     318             : 
     319       27591 :     if ( st->last_extl != WB_BWE || st->bfi )
     320             :     {
     321         597 :         mvr2r( WB_fenv, hBWE_FD->prev_SWB_fenv, 2 );
     322             :     }
     323             : 
     324             :     /* reconstruction of MDCT spectrum of the error signal */
     325       27591 :     WB_BWE_decoding( ysynth, WB_fenv, yerror, L_FRAME16k, mode, st->last_extl, &hBWE_FD->prev_Energy_wb, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->extl, st->coder_type, st->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, st->prev_coder_type );
     326             : 
     327       27591 :     if ( st->output_Fs == 32000 )
     328             :     {
     329        1428 :         set_f( &yerror[L_FRAME16k], 0, L_FRAME16k );
     330             :     }
     331       26163 :     else if ( st->output_Fs == 48000 )
     332             :     {
     333       23754 :         set_f( &yerror[L_FRAME16k], 0, L_FRAME32k );
     334             :     }
     335             : 
     336       27591 :     inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
     337             : 
     338       27591 :     window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
     339             : 
     340       27591 :     if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
     341             :     {
     342             :         /* add HB synth from hf_synth() */
     343         678 :         v_add( hb_synth, synth, hb_synth, output_frame );
     344             :     }
     345             : 
     346       27591 :     st->hBWE_FD->prev_mode = mode;
     347             : 
     348       27591 :     return;
     349             : }
     350             : 
     351             : /*-------------------------------------------------------------------*
     352             :  * swb_bwe_gain_deq()
     353             :  *
     354             :  * Decoding of SWB parameters
     355             :  *-------------------------------------------------------------------*/
     356             : 
     357             : /*! r: BWE class */
     358       44022 : int16_t swb_bwe_gain_deq(
     359             :     Decoder_State *st,       /* i/o: decoder state structure         */
     360             :     const int16_t core,      /* i  : core                            */
     361             :     float *SWB_tenv,         /* o  : time-domain BWE envelope        */
     362             :     float *SWB_fenv,         /* o  : frequency-domain BWE envelope   */
     363             :     const int16_t hr_flag,   /* i  : high rate flag                  */
     364             :     const int16_t hqswb_clas /* i  : HQ BWE class                    */
     365             : )
     366             : {
     367             :     int16_t index, mode, n_band;
     368             :     int16_t indice[6];
     369             :     float quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2];
     370             :     int16_t nb_bits[6];
     371             :     int16_t nenv;
     372             : 
     373       44022 :     if ( hqswb_clas > 0 )
     374             :     {
     375        9003 :         mode = get_next_indice( st, 1 );
     376        9003 :         if ( mode == 0 )
     377             :         {
     378        8577 :             mode = get_next_indice( st, 1 );
     379             :         }
     380             :         else
     381             :         {
     382         426 :             mode = HQ_GENERIC_SP_EXC;
     383             :         }
     384             :     }
     385             :     else
     386             :     {
     387       35019 :         mode = get_next_indice( st, 2 );
     388             :     }
     389             : 
     390       44022 :     if ( mode == 1 && core == ACELP_CORE )
     391             :     {
     392        1200 :         for ( n_band = 0; n_band < SWB_TENV; n_band++ )
     393             :         {
     394         960 :             index = get_next_indice( st, 4 );
     395         960 :             SWB_tenv[n_band] = (float) ( 1 << index );
     396             :         }
     397             : 
     398         240 :         indice[0] = get_next_indice( st, 7 );
     399         240 :         indice[1] = get_next_indice( st, 6 );
     400             : 
     401         720 :         for ( n_band = 0; n_band < DIM_TR1; n_band++ )
     402             :         {
     403         480 :             quant_tmp[2 * n_band] = Env_TR_Cdbk1[indice[0] * DIM_TR1 + n_band];
     404             :         }
     405             : 
     406         240 :         quant_tmp[1] = ( quant_tmp[0] + quant_tmp[2] ) * 0.5f + Env_TR_Cdbk2[indice[1] * DIM_TR2];
     407         240 :         quant_tmp[3] = quant_tmp[2] + Env_TR_Cdbk2[indice[1] * DIM_TR2 + 1];
     408             : 
     409        1200 :         for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
     410             :         {
     411         960 :             SWB_fenv[n_band] = (float) pow( 10, 0.025f * ( quant_tmp[n_band] + Mean_env_tr[n_band] ) );
     412             :         }
     413             : 
     414             :         /* in case of band-width switching, attenuate frame gain */
     415         240 :         if ( st->bws_cnt1 > 0 )
     416             :         {
     417          30 :             for ( n_band = 0; n_band < SWB_TENV; n_band++ )
     418             :             {
     419          24 :                 SWB_tenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
     420             :             }
     421             : 
     422          30 :             for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
     423             :             {
     424          24 :                 SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
     425             :             }
     426             :         }
     427             :     }
     428             :     else
     429             :     {
     430       43782 :         nb_bits[0] = 5;
     431       43782 :         nb_bits[1] = 7;
     432       43782 :         nb_bits[2] = 6;
     433       43782 :         nb_bits[3] = 5;
     434             : 
     435       43782 :         if ( hr_flag == 1 )
     436             :         {
     437           0 :             nb_bits[4] = 5;
     438           0 :             nenv = SWB_FENV - 2;
     439             :         }
     440             :         else
     441             :         {
     442       43782 :             nb_bits[4] = 6;
     443       43782 :             nenv = SWB_FENV;
     444             :         }
     445             : 
     446      262692 :         for ( n_band = 0; n_band < 5; n_band++ )
     447             :         {
     448      218910 :             indice[n_band] = get_next_indice( st, nb_bits[n_band] );
     449             :         }
     450             : 
     451       43782 :         if ( hqswb_clas == HQ_GEN_FB )
     452             :         {
     453        6765 :             indice[n_band] = get_next_indice( st, 5 );
     454             :         }
     455             : 
     456       43782 :         mvr2r( &EnvCdbk11[indice[0] * DIM11], quant_tmp, DIM11 );
     457       43782 :         mvr2r( &EnvCdbk1st[indice[1] * DIM1ST], quant_tmp2, DIM1ST );
     458       43782 :         mvr2r( &EnvCdbk2nd[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND );
     459             : 
     460      306474 :         for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
     461             :         {
     462      262692 :             quant_tmp[n_band] += quant_tmp2[n_band];
     463      262692 :             SWB_fenv[n_band * 2] = quant_tmp[n_band];
     464             :         }
     465             : 
     466       43782 :         if ( hr_flag == 1 )
     467             :         {
     468           0 :             quant_tmp[6] += quant_tmp2[6];
     469           0 :             SWB_fenv[11] = quant_tmp[6];
     470             : 
     471             : 
     472           0 :             mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
     473           0 :             mvr2r( &EnvCdbk3rd[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD );
     474             : 
     475           0 :             for ( n_band = 0; n_band < 5; n_band++ )
     476             :             {
     477           0 :                 SWB_fenv[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band + 1];
     478             :             }
     479             : 
     480           0 :             SWB_fenv[0] += quant_tmp2[0];
     481             :         }
     482             :         else
     483             :         {
     484       43782 :             quant_tmp[DIM11 - 1] += quant_tmp2[DIM11 - 1];
     485       43782 :             SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1];
     486             : 
     487       43782 :             mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
     488       43782 :             mvr2r( &EnvCdbk4th[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH );
     489             : 
     490      306474 :             for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
     491             :             {
     492      262692 :                 SWB_fenv[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
     493             :             }
     494             : 
     495       43782 :             SWB_fenv[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
     496             :         }
     497             : 
     498      656730 :         for ( n_band = 0; n_band < nenv; n_band++ )
     499             :         {
     500             :             Word16 tmp, frac, exp;
     501             :             Word32 L_tmp;
     502             : #ifdef BASOP_NOGLOB
     503             :             Flag Overflow;
     504             : #endif
     505      612948 :             tmp = add( (int16_t) ( SWB_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
     506             : 
     507      612948 :             L_tmp = L_mult( tmp, 21771 );       /* 0.166096 in Q17 -> Q26 */
     508      612948 :             L_tmp = L_shr( L_tmp, 10 );         /* From Q26 to Q16 */
     509      612948 :             frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
     510             : 
     511      612948 :             tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
     512             :             /* output of Pow2() will be: */
     513             :             /* 16384 < Pow2() <= 32767 */
     514      612948 :             exp = sub( exp, 13 );
     515             : #ifdef BASOP_NOGLOB
     516      612948 :             tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
     517             : #else
     518             :             tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
     519             : #endif
     520      612948 :             SWB_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
     521             :         }
     522             : 
     523       43782 :         if ( hqswb_clas == HQ_GEN_FB )
     524             :         {
     525        6765 :             mvr2r( &EnvCdbkFB[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB );
     526       27060 :             for ( n_band = 0; n_band < DIM_FB; n_band++ )
     527             :             {
     528             :                 Word16 tmp, frac, exp;
     529             :                 Word32 L_tmp;
     530             : 
     531       20295 :                 tmp = add( (int16_t) ( SWB_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
     532       20295 :                 L_tmp = L_mult( tmp, 21771 );                                                                      /* 0.166096 in Q17 -> Q25 */
     533       20295 :                 L_tmp = L_shr( L_tmp, 9 );                                                                         /* From Q25 to Q16 */
     534       20295 :                 frac = L_Extract_lc( L_tmp, &exp );                                                                /* Extract exponent of L_tmp */
     535             : 
     536       20295 :                 tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
     537             :                 /* output of Pow2() will be: */
     538             :                 /* 16384 < Pow2() <= 32767 */
     539       20295 :                 exp = sub( exp, 13 );
     540       20295 :                 tmp = shl( tmp, add( exp, 1 ) );
     541       20295 :                 move16();
     542       20295 :                 SWB_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
     543             :             }
     544             :         }
     545             :     }
     546             : 
     547       44022 :     return mode;
     548             : }
     549             : 
     550             : /*-------------------------------------------------------------------*
     551             :  * swb_bwe_dec()
     552             :  *
     553             :  * SWB BWE decoder
     554             :  *-------------------------------------------------------------------*/
     555             : 
     556       45324 : void swb_bwe_dec(
     557             :     Decoder_State *st,               /* i/o: decoder state structure                 */
     558             :     const float output[],            /* i  : synthesis @internal Fs                  */
     559             :     const float *synth,              /* i  : ACELP core synthesis/final synthesis    */
     560             :     float *hb_synth,                 /* o  : SHB synthesis/final synthesis           */
     561             :     const int16_t use_cldfb_for_dft, /* i  : flag to use of CLDFB for DFT Stereo     */
     562             :     const int16_t output_frame       /* i  : frame length                            */
     563             : )
     564             : {
     565             :     int16_t i, l_subfr;
     566             :     FD_BWE_DEC_HANDLE hBWE_FD;
     567             :     float ysynth[L_FRAME48k];
     568             :     float yerror[L_FRAME48k];
     569             :     float wtda_synth[2 * L_FRAME48k];
     570             :     float SWB_tenv[SWB_TENV];
     571             :     float SWB_fenv[SWB_FENV];
     572             :     int16_t L;
     573             :     int16_t mode;
     574       45324 :     int16_t frica_flag = 0;
     575       45324 :     float fb_ener_adjust = 0.0f;
     576       45324 :     int16_t j = 0;
     577             :     float ener_adjust_quan;
     578             :     int16_t idxGain;
     579             :     int16_t fb_band_begin;
     580             : 
     581       45324 :     hBWE_FD = st->hBWE_FD;
     582             : 
     583             :     /*---------------------------------------------------------------------*
     584             :      * SWB BWE decoding
     585             :      *---------------------------------------------------------------------*/
     586             : 
     587       45324 :     if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
     588             :     {
     589             :         /* todo - wtda() does not support L_FRAME length; thus temporarily resample the signal */
     590             :         /* todo - delay output[] by 1.25ms ? */
     591        2865 :         lerp( output, ysynth, L_FRAME16k, st->L_frame );
     592             : 
     593             :         /* windowing of the ACELP core synthesis */
     594        2865 :         wtda( ysynth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k );
     595             : 
     596             :         /* DCT of the ACELP core synthesis */
     597        2865 :         direct_transform( wtda_synth, ysynth, 0, /*st->L_frame*/ L_FRAME16k, st->element_mode );
     598             :     }
     599             :     else
     600             :     {
     601             :         /* windowing of the ACELP core synthesis */
     602       42459 :         wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
     603             : 
     604             :         /* DCT of the ACELP core synthesis */
     605       42459 :         direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
     606             :     }
     607             : 
     608       45324 :     if ( !st->bfi )
     609             :     {
     610       44715 :         if ( st->bws_cnt > 0 )
     611             :         {
     612             :             /* estimate parameters */
     613        9696 :             mode = para_pred_bws( st, ysynth, SWB_fenv );
     614             :         }
     615             :         else
     616             :         {
     617             :             /* de-quantization */
     618       35019 :             mode = swb_bwe_gain_deq( st, ACELP_CORE, SWB_tenv, SWB_fenv, 0, -1 );
     619             :         }
     620             : 
     621       44715 :         L = mode == TRANSIENT ? SWB_FENV_TRANS : SWB_FENV;
     622       44715 :         st->prev_ener_shb = 0.0f;
     623      668325 :         for ( i = 0; i < L; i++ )
     624             :         {
     625      623610 :             st->prev_ener_shb += SWB_fenv[i];
     626             :         }
     627       44715 :         st->prev_ener_shb /= L;
     628             :     }
     629             :     else
     630             :     {
     631             :         /* SHB FEC */
     632         609 :         if ( hBWE_FD->prev_mode != TRANSIENT )
     633             :         {
     634         609 :             mode = hBWE_FD->prev_mode;
     635             :         }
     636             :         else
     637             :         {
     638           0 :             mode = NORMAL;
     639             :         }
     640             : 
     641         609 :         mvr2r( hBWE_FD->prev_SWB_fenv, SWB_fenv, SWB_FENV );
     642             :     }
     643             : 
     644             :     /* reconstruction of MDCT spectrum of the error signal */
     645       45324 :     set_f( yerror, 0, output_frame );
     646             : 
     647       45324 :     if ( st->L_frame == L_FRAME16k )
     648             :     {
     649       13392 :         SWB_BWE_decoding( ysynth, SWB_fenv, yerror, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->tilt_wb, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight, st->extl, st->last_extl );
     650             :     }
     651             :     else
     652             :     {
     653       31932 :         SWB_BWE_decoding( ysynth, SWB_fenv, yerror, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->tilt_wb, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight, st->extl, st->last_extl );
     654             :     }
     655             : 
     656       45324 :     if ( hBWE_FD->prev_frica_flag == 1 && frica_flag == 0 )
     657             :     {
     658        9750 :         for ( i = 0; i < L_SUBFR; i++ )
     659             :         {
     660        9600 :             hBWE_FD->mem_imdct[i] *= 1.0f - i * 0.015625f;
     661             :         }
     662             : 
     663      129750 :         for ( ; i < output_frame; i++ )
     664             :         {
     665      129600 :             hBWE_FD->mem_imdct[i] = 0.0f;
     666             :         }
     667             :     }
     668             : 
     669             :     /* decode information */
     670       45324 :     if ( st->extl == FB_BWE )
     671             :     {
     672       14211 :         if ( !st->bfi )
     673             :         {
     674       14079 :             idxGain = get_next_indice( st, NUM_BITS_FB_FRAMEGAIN );
     675       14079 :             fb_ener_adjust = usdequant( idxGain, FB_GAIN_QLOW, FB_GAIN_QDELTA );
     676             :         }
     677         132 :         else if ( st->bfi )
     678             :         {
     679         132 :             fb_ener_adjust = hBWE_FD->prev_fb_ener_adjust;
     680             :         }
     681             : 
     682       14211 :         hBWE_FD->prev_fb_ener_adjust = fb_ener_adjust;
     683       14211 :         if ( mode == TRANSIENT )
     684             :         {
     685           3 :             ener_adjust_quan = fb_ener_adjust;
     686             :         }
     687             :         else
     688             :         {
     689       14208 :             if ( SWB_fenv[7] < 0.01f )
     690             :             {
     691         114 :                 ener_adjust_quan = 0.0f;
     692             :             }
     693             :             else
     694             :             {
     695       14094 :                 ener_adjust_quan = min( SWB_fenv[13] / SWB_fenv[7], 4.0f );
     696             :             }
     697             :         }
     698             : 
     699       14211 :         fb_band_begin = FB_BAND_BEGIN;
     700       14211 :         if ( st->L_frame == L_FRAME )
     701             :         {
     702        2229 :             fb_band_begin = FB_BAND_BEGIN_12k8;
     703             :         }
     704             : 
     705      468963 :         for ( i = fb_band_begin; i < fb_band_begin + DE_OFFSET1; i++ )
     706             :         {
     707      454752 :             yerror[i] = yerror[i - FB_BAND_WIDTH] * ( ( 1.0f - j * FB_GAIN_QDELTA ) * ener_adjust_quan + j * FB_GAIN_QDELTA * fb_ener_adjust );
     708      454752 :             j++;
     709             :         }
     710     2251179 :         for ( ; i < FB_BAND_END; i++ )
     711             :         {
     712     2236968 :             yerror[i] = yerror[i - FB_BAND_WIDTH] * fb_ener_adjust;
     713             :         }
     714             :     }
     715             : 
     716             :     /* iDCT of the error signal */
     717       45324 :     inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
     718             : 
     719             :     /* inverse windowing of the error signal */
     720       45324 :     window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
     721       45324 :     l_subfr = output_frame / 4;
     722             : 
     723       45324 :     if ( mode == TRANSIENT )
     724             :     {
     725        1200 :         for ( i = 0; i < SWB_TENV; i++ )
     726             :         {
     727         960 :             SWB_tenv[i] *= 0.8f;
     728             :         }
     729             : 
     730             :         /* time envelope shaping when the current frame is TRANSIENT frame */
     731         240 :         time_envelop_shaping( hb_synth, SWB_tenv, output_frame );
     732             : 
     733         240 :         hBWE_FD->prev_td_energy = SWB_tenv[3];
     734             :     }
     735       45084 :     else if ( frica_flag == 1 && hBWE_FD->prev_frica_flag == 0 )
     736             :     {
     737             :         /* IVAS_fmToDo: synth[] is @internal_Fs!!! */
     738         423 :         time_reduce_pre_echo( synth, hb_synth, hBWE_FD->prev_td_energy, l_subfr );
     739             :     }
     740             :     else
     741             :     {
     742       44661 :         hBWE_FD->prev_td_energy = 0.0f;
     743    10269141 :         for ( i = 0; i < l_subfr; i++ )
     744             :         {
     745    10224480 :             hBWE_FD->prev_td_energy += hb_synth[3 * l_subfr + i] * hb_synth[3 * l_subfr + i];
     746             :         }
     747       44661 :         hBWE_FD->prev_td_energy = (float) sqrt( hBWE_FD->prev_td_energy / l_subfr );
     748             :     }
     749             : 
     750       45324 :     if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
     751             :     {
     752             :         /* add HB synth from hf_synth() */
     753        2865 :         v_add( hb_synth, synth, hb_synth, output_frame );
     754             :     }
     755             : 
     756       45324 :     hBWE_FD->prev_frica_flag = frica_flag;
     757       45324 :     hBWE_FD->prev_mode = mode;
     758             : 
     759       45324 :     return;
     760             : }
     761             : 
     762             : /*-------------------------------------------------------------------*
     763             :  * fd_bwe_dec_init()
     764             :  *
     765             :  * Initialize FD BWE state structure at the decoder
     766             :  *-------------------------------------------------------------------*/
     767             : 
     768       11040 : void fd_bwe_dec_init(
     769             :     FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle  */
     770             : )
     771             : {
     772       11040 :     set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
     773       11040 :     set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     774       11040 :     hBWE_FD->prev_mode = NORMAL;
     775       11040 :     set_f( hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
     776       11040 :     hBWE_FD->prev_Energy = 0.0f;
     777       11040 :     hBWE_FD->prev_L_swb_norm = 8;
     778       11040 :     hBWE_FD->Seed = 21211;
     779       11040 :     hBWE_FD->prev_frica_flag = 0;
     780       11040 :     set_f( hBWE_FD->mem_imdct, 0, L_FRAME48k );
     781       11040 :     hBWE_FD->prev_td_energy = 0.0f;
     782       11040 :     hBWE_FD->prev_weight = 0.2f;
     783       11040 :     hBWE_FD->prev_flag = 0;
     784       11040 :     hBWE_FD->last_wb_bwe_ener = 0.0f;
     785       11040 :     hBWE_FD->prev_Energy_wb = 0.0f;
     786             : 
     787       11040 :     hBWE_FD->mem_deemph_old_syn = 0.0f;
     788             : 
     789       11040 :     hBWE_FD->prev_fb_ener_adjust = 0.0f;
     790             : 
     791       11040 :     return;
     792             : }

Generated by: LCOV version 1.14