LCOV - code coverage report
Current view: top level - lib_enc - swb_bwe_enc_hr.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 172 172 100.0 %
Date: 2025-05-28 04:28:20 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             :  * en_band_quant()
      49             :  *
      50             :  * Quantize the band envelop
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : /*! r: quantization index */
      54       19520 : static int16_t en_band_quant(
      55             :     float *en_band,        /* i/o: (un)quantized envelope value    */
      56             :     const float *env_code, /* i  : envelope codebook               */
      57             :     const int16_t N        /* i  : codebook dimension              */
      58             : )
      59             : {
      60             :     float maxerr, err;
      61             :     int16_t i, j, ind;
      62             : 
      63       19520 :     maxerr = FLT_MAX;
      64       19520 :     ind = 0;
      65             : 
      66      851904 :     for ( i = 0; i < N; i++ )
      67             :     {
      68      832384 :         err = FLT_MIN;
      69     2497152 :         for ( j = 0; j < 2; j++ )
      70             :         {
      71     1664768 :             err += ( en_band[j] - env_code[i * 2 + j] ) * ( en_band[j] - env_code[i * 2 + j] );
      72             :         }
      73      832384 :         if ( err < maxerr )
      74             :         {
      75      221713 :             maxerr = err;
      76      221713 :             ind = i;
      77             :         }
      78             :     }
      79             : 
      80       19520 :     en_band[0] = env_code[2 * ind];
      81       19520 :     en_band[1] = env_code[2 * ind + 1];
      82             : 
      83       19520 :     return ( ind );
      84             : }
      85             : 
      86             : /*-------------------------------------------------------------------*
      87             :  * swb_bwe_enc_hr()
      88             :  *
      89             :  * HR SWB BWE encoder
      90             :  *-------------------------------------------------------------------*/
      91             : 
      92        9072 : void swb_bwe_enc_hr(
      93             :     Encoder_State *st,         /* i/o: encoder state structure     */
      94             :     const float *new_input,    /* i  : input signal                */
      95             :     const int16_t input_frame, /* i  : frame length                */
      96             :     const int16_t unbits       /* i  : number of core unused bits  */
      97             : )
      98             : {
      99             :     int16_t i, j, k, nBits, nBits_total, nBits_block, Nsv, Nsv2, width_noncoded;
     100             :     int16_t is_transient, pos;
     101             :     int16_t x_norm[NSV_MAX * ( WIDTH_BAND + 1 )], x_norm1[NSV_MAX * ( WIDTH_BAND + 1 )];
     102             :     float t_audio[L_FRAME48k], t_audio_tmp[L_FRAME48k];
     103             :     float gain, gain2, en_band[N_BANDS_BWE_HR];
     104             :     int16_t ind1, ind2;
     105             :     int16_t nq[NSV_MAX], nq2[NSV_MAX];
     106             :     float tmpF, min_env;
     107             :     float en_noncoded;
     108        9072 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     109             : 
     110             :     /*---------------------------------------------------------------------*
     111             :      * initializations
     112             :      *---------------------------------------------------------------------*/
     113             : 
     114        9072 :     ind2 = 0;        /* only to suppress warnings */
     115        9072 :     Nsv2 = 0;        /* only to suppress warnings */
     116        9072 :     gain2 = 0;       /* only to suppress warnings */
     117        9072 :     en_noncoded = 0; /* only to suppress warnings */
     118             : 
     119             :     /* reset memories in case that last frame was a different technology */
     120        9072 :     if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
     121             :     {
     122         132 :         set_f( st->hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
     123             :     }
     124             : 
     125             :     /* calculate SWB BWE bit-budget (extension layer bitrate + AVQ unused bits from the core layer) */
     126        9072 :     nBits = (int16_t) ( st->extl_brate ) / FRAMES_PER_SEC + unbits;
     127        9072 :     nBits_total = nBits;
     128             : 
     129             : 
     130             :     /*---------------------------------------------------------------------*
     131             :      * detect transient frames
     132             :      *---------------------------------------------------------------------*/
     133             : 
     134        9072 :     is_transient = detect_transient( st, new_input, input_frame );
     135        9072 :     push_indice( hBstr, IND_HR_IS_TRANSIENT, is_transient, 1 );
     136             : 
     137             :     /*---------------------------------------------------------------------*
     138             :      * OLA and MDCT
     139             :      *---------------------------------------------------------------------*/
     140             : 
     141        9072 :     wtda( new_input, t_audio_tmp, st->hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, input_frame );
     142             : 
     143        9072 :     direct_transform( t_audio_tmp, t_audio, is_transient, input_frame, st->element_mode );
     144             : 
     145        9072 :     if ( is_transient )
     146             :     {
     147         688 :         nBits = -1; /* is_transient flag */
     148         688 :         nBits_block = nBits_total / NUM_TIME_SWITCHING_BLOCKS;
     149         688 :         nBits += nBits_total % NUM_TIME_SWITCHING_BLOCKS;
     150             : 
     151             :         /* set width of noncoded (blind estimated) spectrum */
     152         688 :         if ( st->extl == SWB_BWE_HIGHRATE )
     153             :         {
     154         590 :             width_noncoded = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     155             :         }
     156             :         else /* st->extl == FB_BWE_HIGHRATE */
     157             :         {
     158          98 :             width_noncoded = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
     159             :         }
     160             : 
     161             :         /*---------------------------------------------------------------------*
     162             :          * transient frames: processing in blocks (subframes)
     163             :          *---------------------------------------------------------------------*/
     164             : 
     165        3440 :         for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
     166             :         {
     167        2752 :             nBits += nBits_block;
     168             : 
     169             :             /* compute energy of noncoded (14.4-20kHz) spectrum */
     170        2752 :             if ( st->extl == FB_BWE_HIGHRATE )
     171             :             {
     172         392 :                 en_noncoded = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF, width_noncoded ) + 0.001f;
     173         392 :                 en_noncoded = (float) sqrt( en_noncoded / width_noncoded );
     174             :             }
     175             : 
     176             :             /* keep only frequencies in interest */
     177        2752 :             set_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS, 0, NUM_TRANS_START_FREQ_COEF );
     178        2752 :             set_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS, 0, ( input_frame - L_FRAME32k ) / NUM_TIME_SWITCHING_BLOCKS );
     179             : 
     180             :             /*---------------------------------------------------------------------*
     181             :              * global gain coding
     182             :              *---------------------------------------------------------------------*/
     183             : 
     184             :             /* compute and quantize global energy */
     185        2752 :             gain = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF, WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR ) + 0.001f;
     186        2752 :             gain = (float) sqrt( gain ) / ( WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR );
     187        2752 :             ind1 = gain_quant( &gain, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     188             : 
     189        2752 :             push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
     190        2752 :             nBits -= NBITS_GLOB_GAIN_BWE_HR;
     191             : 
     192             :             /* normalization with global gain  */
     193        2752 :             tmpF = 1 / gain;
     194      189888 :             for ( i = 0; i < WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR; i++ )
     195             :             {
     196      187136 :                 t_audio[NUM_TRANS_START_FREQ_COEF + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= tmpF;
     197             :             }
     198             : 
     199             :             /*---------------------------------------------------------------------*
     200             :              * envelope coding
     201             :              *---------------------------------------------------------------------*/
     202             : 
     203             :             /* compute energy per band */
     204        8256 :             for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
     205             :             {
     206        5504 :                 en_band[i] = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF, WIDTH_TRANS_FREQ_COEF ) + 0.001f;
     207        5504 :                 en_band[i] = (float) sqrt( en_band[i] / ( WIDTH_TRANS_FREQ_COEF ) );
     208             :             }
     209             : 
     210             :             /* Q energy per band */
     211        2752 :             if ( k == 0 )
     212             :             {
     213         688 :                 ind1 = en_band_quant( en_band, swb_hr_env_code3, NUM_ENVLOPE_CODE_HR_TR );
     214         688 :                 push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR_TR );
     215         688 :                 nBits -= NBITS_ENVELOPE_BWE_HR_TR;
     216         688 :                 ind2 = ind1;
     217             :             }
     218             :             else
     219             :             {
     220        2064 :                 if ( ind2 < NUM_ENVLOPE_CODE_HR_TR2 )
     221             :                 {
     222         444 :                     ind1 = en_band_quant( en_band, swb_hr_env_code3, NUM_ENVLOPE_CODE_HR_TR2 );
     223             :                 }
     224             :                 else
     225             :                 {
     226        1620 :                     ind1 = en_band_quant( en_band, swb_hr_env_code3 + ( NUM_ENVLOPE_CODE_HR_TR2 * 2 ), NUM_ENVLOPE_CODE_HR_TR2 );
     227             :                 }
     228             : 
     229        2064 :                 push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR_TR - 1 );
     230        2064 :                 nBits -= ( NBITS_ENVELOPE_BWE_HR_TR - 1 );
     231             :             }
     232             : 
     233             :             /* normalize spectrum per bands */
     234        8256 :             for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
     235             :             {
     236        5504 :                 tmpF = 1 / en_band[i];
     237      192640 :                 for ( j = 0; j < WIDTH_TRANS_FREQ_COEF; j++ )
     238             :                 {
     239      187136 :                     t_audio[k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF + j] *= tmpF;
     240             :                 }
     241             :             }
     242             : 
     243             :             /*---------------------------------------------------------------------*
     244             :              * estimate energy of noncoded spectrum (14.4-20kHz)
     245             :              *---------------------------------------------------------------------*/
     246             : 
     247        2752 :             if ( st->extl == SWB_BWE_HIGHRATE )
     248             :             {
     249        2360 :                 en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
     250             :             }
     251             :             else /* st->extl == FB_BWE_HIGHRATE */
     252             :             {
     253         392 :                 en_noncoded /= ( gain * en_band[N_BANDS_TRANS_BWE_HR - 1] );
     254             : 
     255         392 :                 ind1 = 0;
     256         392 :                 if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT1 )
     257             :                 {
     258           3 :                     ind1 = 1;
     259           3 :                     en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT1;
     260             :                 }
     261         389 :                 else if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT2 )
     262             :                 {
     263         138 :                     ind1 = 2;
     264         138 :                     en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT2;
     265             :                 }
     266         251 :                 else if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT3 )
     267             :                 {
     268          94 :                     ind1 = 3;
     269          94 :                     en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT3;
     270             :                 }
     271             :                 else
     272             :                 {
     273         157 :                     en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
     274             :                 }
     275         392 :                 push_indice( hBstr, IND_HR_HF_GAIN, ind1, NBITS_HF_GAIN_BWE_HR );
     276         392 :                 nBits -= NBITS_HF_GAIN_BWE_HR;
     277             :             }
     278             : 
     279             :             /*---------------------------------------------------------------------*
     280             :              * AVQ coding (quantize normalized spectrum)
     281             :              *---------------------------------------------------------------------*/
     282             : 
     283        2752 :             Nsv = ( NUM_TRANS_END_FREQ_COEF - NUM_TRANS_START_FREQ_COEF ) / WIDTH_BAND;
     284        2752 :             AVQ_cod( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF, x_norm, nBits, Nsv );
     285        2752 :             AVQ_encmux( hBstr, st->extl, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     286             :         }
     287             :     }
     288             :     else /* !is_transient */
     289             :     {
     290        8384 :         nBits--; /* is_transient flag */
     291             : 
     292             :         /*---------------------------------------------------------------------*
     293             :          * processing of normal (non-transient) frames
     294             :          *---------------------------------------------------------------------*/
     295             : 
     296             :         /* set width of noncoded (blind estimated) spectrum */
     297        8384 :         if ( st->extl == SWB_BWE_HIGHRATE )
     298             :         {
     299        7593 :             width_noncoded = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
     300             :         }
     301             :         else /* st->extl == FB_BWE_HIGHRATE */
     302             :         {
     303         791 :             width_noncoded = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_END_FREQ_COEF;
     304             :         }
     305             : 
     306             :         /* compute energy of noncoded (14.4-20kHz) spectrum */
     307        8384 :         if ( st->extl == FB_BWE_HIGHRATE )
     308             :         {
     309         791 :             en_noncoded = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, width_noncoded ) + 0.001f;
     310         791 :             en_noncoded = (float) sqrt( en_noncoded / width_noncoded );
     311             :         }
     312             : 
     313             :         /* keep only frequencies in interest */
     314        8384 :         set_f( t_audio, 0, NUM_NONTRANS_START_FREQ_COEF );
     315        8384 :         set_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, 0, input_frame - NUM_NONTRANS_END_FREQ_COEF );
     316             : 
     317             :         /*---------------------------------------------------------------------*
     318             :          * global gain coding
     319             :          *---------------------------------------------------------------------*/
     320             : 
     321             :         /* compute and quantize global gain */
     322        8384 :         gain = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF, WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR ) + 0.001f;
     323        8384 :         gain = (float) sqrt( gain ) / ( WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR );
     324        8384 :         ind1 = gain_quant( &gain, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     325             : 
     326        8384 :         push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
     327        8384 :         nBits -= NBITS_GLOB_GAIN_BWE_HR;
     328             : 
     329             :         /* normalization with global gain */
     330        8384 :         tmpF = 1 / gain;
     331     2288832 :         for ( i = 0; i < WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR; i++ )
     332             :         {
     333     2280448 :             t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= tmpF;
     334             :         }
     335             : 
     336             :         /*---------------------------------------------------------------------*
     337             :          * envelope coding
     338             :          *---------------------------------------------------------------------*/
     339             : 
     340             :         /* compute energy per band */
     341       41920 :         for ( i = 0; i < N_BANDS_BWE_HR; i++ )
     342             :         {
     343       33536 :             en_band[i] = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF, WIDTH_NONTRANS_FREQ_COEF ) + 0.001f;
     344       33536 :             en_band[i] = (float) sqrt( en_band[i] / WIDTH_NONTRANS_FREQ_COEF );
     345             :         }
     346             : 
     347             :         /* Q energy per band */
     348        8384 :         ind1 = en_band_quant( en_band, swb_hr_env_code1, NUM_ENVLOPE_CODE_HR1 );
     349        8384 :         ind2 = en_band_quant( en_band + 2, swb_hr_env_code2, NUM_ENVLOPE_CODE_HR2 );
     350             : 
     351        8384 :         push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR1 );
     352        8384 :         push_indice( hBstr, IND_HR_ENVELOPE, ind2, NBITS_ENVELOPE_BWE_HR2 );
     353             : 
     354        8384 :         nBits -= NBITS_ENVELOPE_BWE_HR1 + NBITS_ENVELOPE_BWE_HR2;
     355             : 
     356             :         /* normalize spectrum per bands */
     357       41920 :         for ( i = 0; i < N_BANDS_BWE_HR; i++ )
     358             :         {
     359       33536 :             tmpF = 1 / en_band[i];
     360     2313984 :             for ( j = 0; j < WIDTH_NONTRANS_FREQ_COEF; j++ )
     361             :             {
     362     2280448 :                 t_audio[NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF + j] *= tmpF;
     363             :             }
     364             :         }
     365             : 
     366             :         /*---------------------------------------------------------------------*
     367             :          * choose sub-bands to be quantized
     368             :          *---------------------------------------------------------------------*/
     369             : 
     370             :         /* find the subband with the min envelope */
     371        8384 :         pos = minimum( en_band, N_BANDS_BWE_HR, &min_env );
     372             : 
     373             :         /* decide the spectrum to be quantized */
     374        8384 :         if ( nBits_total > NBITS_THRESH_BWE_HR )
     375             :         {
     376          50 :             i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF;
     377          50 :             mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, t_audio_tmp, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF );
     378             :         }
     379             :         else
     380             :         {
     381             :             /* reorder the spectrum */
     382        8334 :             ind1 = ( pos * 64 + pos / 2 * WIDTH_BAND );
     383        8334 :             mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, t_audio_tmp, ind1 );
     384             : 
     385        8334 :             ind2 = ( ( pos + 1 ) * 64 + ( pos + 1 ) / 2 * WIDTH_BAND );
     386        8334 :             mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, t_audio_tmp + ind1, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2 );
     387             : 
     388        8334 :             i = ind1 + NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2;
     389             :         }
     390             : 
     391             :         /*---------------------------------------------------------------------*
     392             :          * estimate energy of noncoded spectrum (14.4-20kHz)
     393             :          *---------------------------------------------------------------------*/
     394             : 
     395        8384 :         if ( st->extl == SWB_BWE_HIGHRATE )
     396             :         {
     397        7593 :             en_noncoded = 0.5f * min_env;
     398             :         }
     399             :         else /* st->extl == FB_BWE_HIGHRATE */
     400             :         {
     401         791 :             en_noncoded /= ( gain * min_env );
     402             : 
     403         791 :             ind1 = 0;
     404         791 :             if ( en_noncoded < BWE_HR_NONTRANS_EN_LIMIT1 )
     405             :             {
     406         347 :                 ind1 = 1;
     407         347 :                 en_noncoded = 0.5f * min_env * BWE_HR_NONTRANS_EN_LIMIT1;
     408             :             }
     409         444 :             else if ( en_noncoded > BWE_HR_NONTRANS_EN_LIMIT2 )
     410             :             {
     411          82 :                 ind1 = 2;
     412          82 :                 en_noncoded = min_env * BWE_HR_NONTRANS_EN_LIMIT2;
     413             :             }
     414         362 :             else if ( en_noncoded > BWE_HR_NONTRANS_EN_LIMIT3 )
     415             :             {
     416         182 :                 ind1 = 3;
     417         182 :                 en_noncoded = min_env * BWE_HR_NONTRANS_EN_LIMIT3;
     418             :             }
     419             :             else
     420             :             {
     421         180 :                 en_noncoded = 0.5f * min_env;
     422             :             }
     423             : 
     424         791 :             push_indice( hBstr, IND_HR_HF_GAIN, ind1, NBITS_HF_GAIN_BWE_HR );
     425         791 :             nBits -= NBITS_HF_GAIN_BWE_HR;
     426             :         }
     427             : 
     428             :         /*---------------------------------------------------------------------*
     429             :          * AVQ coding (quantize normalized spectrum)
     430             :          *---------------------------------------------------------------------*/
     431             : 
     432        8384 :         Nsv = i / WIDTH_BAND;
     433        8384 :         AVQ_cod( t_audio_tmp, x_norm, nBits, Nsv );
     434        8384 :         AVQ_encmux( hBstr, st->extl, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
     435             :         /*---------------------------------------------------------------------*
     436             :          * second stage coding
     437             :          *---------------------------------------------------------------------*/
     438             : 
     439        8384 :         if ( nBits >= 9 + NBITS_GLOB_GAIN_BWE_HR && sum_s( nq, Nsv ) > 0 )
     440             :         {
     441             :             /* select spectrum of the second stage coding */
     442        8109 :             k = 0;
     443      212425 :             for ( i = 0; i < Nsv; i++ )
     444             :             {
     445      204316 :                 if ( nq[i] == 0 )
     446             :                 {
     447      365949 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     448             :                     {
     449      325288 :                         t_audio[k++] = t_audio_tmp[i * WIDTH_BAND + j];
     450             :                     }
     451             :                 }
     452             :             }
     453             : 
     454      212425 :             for ( i = 0; i < Nsv; i++ )
     455             :             {
     456      204316 :                 if ( nq[i] != 0 )
     457             :                 {
     458     1472895 :                     for ( j = 0; j < WIDTH_BAND; j++ )
     459             :                     {
     460     1309240 :                         t_audio[k++] = t_audio_tmp[i * WIDTH_BAND + j] - x_norm[i * WIDTH_BAND + j];
     461             :                     }
     462             :                 }
     463             :             }
     464             : 
     465             :             /* calculate the number of subbands according to the rest bits */
     466        8109 :             if ( nBits > 396 )
     467             :             {
     468          23 :                 Nsv2 = 33;
     469             :             }
     470             :             else
     471             :             {
     472        8086 :                 Nsv2 = nBits / 12;
     473             :             }
     474             : 
     475             :             /* second stage global gain estimation and coding */
     476        8109 :             gain2 = sum2_f( t_audio, Nsv2 * WIDTH_BAND ) + 0.001f;
     477        8109 :             gain2 = (float) ( 16 * sqrt( gain2 / ( Nsv2 * WIDTH_BAND ) ) );
     478        8109 :             ind1 = gain_quant( &gain2, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
     479             : 
     480        8109 :             push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
     481        8109 :             nBits -= NBITS_GLOB_GAIN_BWE_HR;
     482             : 
     483             :             /* normalize with global gain */
     484        8109 :             gain2 *= 0.0625f; /* 1/16 */
     485        8109 :             tmpF = 1 / gain2;
     486      311445 :             for ( i = 0; i < Nsv2 * WIDTH_BAND; i++ )
     487             :             {
     488      303336 :                 t_audio[i] *= tmpF;
     489             :             }
     490             : 
     491        8109 :             set_s( nq2, 0, Nsv );
     492             : 
     493        8109 :             AVQ_cod( t_audio, x_norm1, nBits, Nsv2 );
     494        8109 :             AVQ_encmux( hBstr, st->extl, x_norm1, &nBits, Nsv2, nq2, 0, Nsv2 - 1 );
     495             :         }
     496             :     }
     497             : 
     498             :     /* write unused bits */
     499       19259 :     while ( nBits > 0 )
     500             :     {
     501       10187 :         i = min( nBits, 16 );
     502       10187 :         push_indice( hBstr, IND_UNUSED, 0, i );
     503       10187 :         nBits -= i;
     504             :     }
     505             : 
     506             : 
     507        9072 :     return;
     508             : }

Generated by: LCOV version 1.14