LCOV - code coverage report
Current view: top level - lib_enc - gain_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 361 389 92.8 %
Date: 2025-10-27 07:01:45 Functions: 8 8 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 "rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * Local constants
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52             : #define RANGE 64
      53             : 
      54             : /*---------------------------------------------------------------------*
      55             :  * Es_pred_enc()
      56             :  *
      57             :  * Calculation and quantization of average predicted innovation energy to be
      58             :  *---------------------------------------------------------------------*/
      59             : 
      60      256875 : void Es_pred_enc(
      61             :     float *Es_pred,          /* o  : predicited scaled innovation energy       */
      62             :     int16_t *Es_pred_indice, /* o  : indice corresponding to above parameter   */
      63             :     const int16_t L_frame,   /* i  : length of the frame                       */
      64             :     const int16_t L_subfr,   /* i  : length of the subframe                    */
      65             :     const float *res,        /* i  : residual signal                           */
      66             :     const float *voicing,    /* i  : normalized correlation in three 1/2frames */
      67             :     const int16_t nb_bits,   /* i  : allocated number of bits                  */
      68             :     const int16_t no_ltp     /* i  : no_ltp flag                               */
      69             : )
      70             : {
      71             :     int16_t i, i_subfr, size;
      72             :     float tmp, dist, mean_ener_code, ener;
      73             :     float weight;
      74             :     const float *qua_table;
      75             : 
      76             :     /*----------------------------------------------------------*
      77             :      * calculate the average residual signal energy
      78             :      *----------------------------------------------------------*/
      79      256875 :     if ( L_frame == L_FRAME )
      80             :     {
      81      121951 :         weight = 0.25f;
      82             :     }
      83             :     else /* L_frame == L_FRAME16k */
      84             :     {
      85      134924 :         weight = 0.2f;
      86             :     }
      87             : 
      88      256875 :     mean_ener_code = 0.0f;
      89     1419299 :     for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_subfr )
      90             :     {
      91             :         /* calculate the energy of residual signal */
      92     1162424 :         ener = sum2_f( &res[i_subfr], L_subfr ) + 0.01f;
      93     1162424 :         ener = 10 * (float) log10( ener / ( (float) L_subfr ) );
      94     1162424 :         if ( ( ener < 0 ) && !( no_ltp ) )
      95             :         {
      96       43526 :             ener = 0;
      97             :         }
      98             : 
      99             :         /* update the average energy of residual signal */
     100     1162424 :         mean_ener_code += weight * ener;
     101             :     }
     102             : 
     103      256875 :     if ( !no_ltp )
     104             :     {
     105             :         /*----------------------------------------------------------*
     106             :          * subtract an estimate of adaptive codebook contribution
     107             :          *----------------------------------------------------------*/
     108             : 
     109      254023 :         mean_ener_code -= 10.0f * ( 0.5f * voicing[0] + 0.5f * voicing[1] );
     110             : 
     111             :         /*----------------------------------------------------------*
     112             :          * quantize the average predicted innovation energy
     113             :          *----------------------------------------------------------*/
     114      254023 :         switch ( nb_bits )
     115             :         {
     116      217810 :             case 5:
     117             :             {
     118      217810 :                 qua_table = Es_pred_qua_5b;
     119      217810 :                 break;
     120             :             }
     121       30514 :             case 4:
     122             :             {
     123       30514 :                 qua_table = Es_pred_qua_4b;
     124       30514 :                 break;
     125             :             }
     126        5699 :             case 3:
     127             :             {
     128        5699 :                 qua_table = Es_pred_qua_3b;
     129        5699 :                 break;
     130             :             }
     131           0 :             default:
     132             :             {
     133           0 :                 qua_table = Es_pred_qua_5b;
     134           0 :                 break;
     135             :             }
     136             :         }
     137             :     }
     138             :     else
     139             :     {
     140        2852 :         qua_table = Es_pred_qua_4b_no_ltp;
     141             :     }
     142             : 
     143             :     /* select codebook, size and number of bits */
     144      256875 :     size = 1 << nb_bits;
     145             : 
     146             :     /* find the nearest neighbour (codevector) */
     147      256875 :     tmp = 1e30f;
     148      256875 :     *Es_pred_indice = 0;
     149     7806243 :     for ( i = 0; i < size; i++ )
     150             :     {
     151     7549368 :         dist = (float) fabs( mean_ener_code - qua_table[i] );
     152     7549368 :         if ( dist < tmp )
     153             :         {
     154     4021685 :             tmp = dist;
     155     4021685 :             *Es_pred = qua_table[i];
     156     4021685 :             *Es_pred_indice = i;
     157             :         }
     158             :     }
     159             : 
     160      256875 :     return;
     161             : }
     162             : 
     163             : 
     164             : /*-------------------------------------------------------------------*
     165             :  * gain_enc_amr_wb()
     166             :  *
     167             :  * Quantization of pitch and codebook gains (used also in AMR-WB IO mode)
     168             :  * MA prediction is performed on the innovation energy (in dB with mean removed).
     169             :  * An initial predicted gain, gcode0, is first determined and the correction
     170             :  * factor     alpha = g_code / gcode0   is quantized.
     171             :  * The pitch gain and the correction factor are vector quantized and the
     172             :  * mean-squared weighted error criterion is used in the quantizer search.
     173             :  *-------------------------------------------------------------------*/
     174             : 
     175        8160 : void gain_enc_amr_wb(
     176             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle                                        */
     177             :     const float *xn,          /* i  : target vector                                                   */
     178             :     const float *y1,          /* i  : zero-memory filtered adaptive excitation                        */
     179             :     const float *y2,          /* i  : zero-memory filtered algebraic codebook excitation              */
     180             :     const float *code,        /* i  : algebraic excitation                                            */
     181             :     const int32_t core_brate, /* i  : core bitrate                                                    */
     182             :     float *gain_pit,          /* i/o: pitch gain / Quantized pitch gain                               */
     183             :     float *gain_code,         /* o  : quantized codebook gain                                         */
     184             :     float *gain_inov,         /* o  : gain of the innovation (used for normalization)                 */
     185             :     float *norm_gain_code,    /* o  : norm. gain of the codebook excitation                           */
     186             :     float *g_corr,            /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
     187             :     const int16_t clip_gain,  /* i  : gain pitch clipping flag (1 = clipping)                         */
     188             :     float *past_qua_en        /* i/o: gain quantization memory (4 words)                              */
     189             : )
     190             : {
     191             :     int16_t index, i, j, min_ind, size, nBits;
     192             :     float dist, dist_min, g_pitch, g_code, qua_en, gcode0;
     193             :     const float *p, *t_qua_gain;
     194             : 
     195             :     /*-----------------------------------------------------------------*
     196             :      * gain computation correlations
     197             :      * find raw innovation energy
     198             :      *-----------------------------------------------------------------*/
     199             : 
     200        8160 :     E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
     201        8160 :     g_corr[2] += 0.01F;
     202        8160 :     g_corr[3] -= 0.02F;
     203        8160 :     g_corr[4] += 0.02F;
     204        8160 :     *gain_inov = 1.0f / (float) sqrt( ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR );
     205             : 
     206             :     /*-----------------------------------------------------------------*
     207             :      * find the initial quantization pitch index
     208             :      * set gains search range
     209             :      *-----------------------------------------------------------------*/
     210             : 
     211        8160 :     if ( core_brate < ACELP_12k65 )
     212             :     {
     213        1920 :         t_qua_gain = t_qua_gain6b;
     214        1920 :         nBits = 6;
     215        1920 :         min_ind = 0;
     216        1920 :         size = RANGE;
     217        1920 :         if ( clip_gain == 1 )
     218             :         {
     219           0 :             size -= 16; /* limit pitch gain  to 1.0 */
     220             :         }
     221             :     }
     222             :     else
     223             :     {
     224        6240 :         t_qua_gain = t_qua_gain7b;
     225        6240 :         nBits = 7;
     226        6240 :         p = t_qua_gain7b + RANGE; /* pt at 1/4th of table */
     227        6240 :         j = NB_QUA_GAIN7B - RANGE;
     228        6240 :         if ( clip_gain == 1 )
     229             :         {
     230           0 :             j -= 27; /* limit pitch gain to 1.0 */
     231             :         }
     232             : 
     233        6240 :         min_ind = 0;
     234        6240 :         g_pitch = *gain_pit;
     235             : 
     236             : 
     237      405600 :         for ( i = 0; i < j; i++, p += 2 )
     238             :         {
     239      399360 :             if ( g_pitch > *p )
     240             :             {
     241      228902 :                 min_ind++;
     242             :             }
     243             :         }
     244        6240 :         size = RANGE;
     245             :     }
     246             : 
     247             :     /*-----------------------------------------------------------------*
     248             :      * predicted code gain
     249             :      *-----------------------------------------------------------------*/
     250             : 
     251             :     /* start with predicting code energy in dB */
     252        8160 :     gcode0 = MEAN_ENER;
     253       40800 :     for ( i = 0; i < GAIN_PRED_ORDER; i++ )
     254             :     {
     255       32640 :         gcode0 += pred_gain[i] * past_qua_en[i];
     256             :     }
     257        8160 :     gcode0 += (float) ( 20.0 * log10( *gain_inov ) );
     258             : 
     259             :     /* convert from energy in dB to gain */
     260        8160 :     gcode0 = (float) pow( 10.0, gcode0 / 20.0 );
     261             : 
     262             :     /*-----------------------------------------------------------------*
     263             :      * search the codebook
     264             :      *-----------------------------------------------------------------*/
     265             : 
     266        8160 :     dist_min = 3.402823466e+38F;
     267        8160 :     p = t_qua_gain + min_ind * 2;
     268             : 
     269        8160 :     index = 0;
     270      530400 :     for ( i = 0; i < size; i++ )
     271             :     {
     272      522240 :         g_pitch = *p++;         /* pitch gain */
     273      522240 :         g_code = gcode0 * *p++; /* code gain */
     274      522240 :         dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
     275      522240 :         if ( dist < dist_min )
     276             :         {
     277       69718 :             dist_min = dist;
     278       69718 :             index = i;
     279             :         }
     280             :     }
     281        8160 :     index = index + min_ind;
     282        8160 :     *gain_pit = t_qua_gain[index * 2];
     283        8160 :     qua_en = t_qua_gain[index * 2 + 1];
     284        8160 :     *gain_code = qua_en * gcode0;
     285             : 
     286             :     /*-----------------------------------------------------------------*
     287             :      * update table of past quantized energies
     288             :      *-----------------------------------------------------------------*/
     289             : 
     290       32640 :     for ( i = GAIN_PRED_ORDER - 1; i > 0; i-- )
     291             :     {
     292       24480 :         past_qua_en[i] = past_qua_en[i - 1];
     293             :     }
     294        8160 :     past_qua_en[0] = (float) ( 20.0 * log10( qua_en ) );
     295             : 
     296        8160 :     push_indice( hBstr, IND_GAIN, index, nBits );
     297             : 
     298        8160 :     *norm_gain_code = *gain_code / *gain_inov;
     299             : 
     300        8160 :     return;
     301             : }
     302             : 
     303             : /*---------------------------------------------------------------------*
     304             :  * gain_enc_mless()
     305             :  *
     306             :  * Quantization of pitch and codebook gains without prediction (memory-less)
     307             :  * - an initial predicted gain, gcode0, is first determined based on
     308             :  *   the predicted average innovation energy
     309             :  * - a correction factor gamma = g_code / gcode0 is then vector quantized along with gain_pit
     310             :  * - the mean-squared weighted error criterion is used for codebook search
     311             :  *---------------------------------------------------------------------*/
     312             : 
     313      974443 : void gain_enc_mless(
     314             :     BSTR_ENC_HANDLE hBstr,      /* i/o: encoder bitstream handle                                        */
     315             :     const int16_t gains_mode[], /* i  : gain bits                                                       */
     316             :     const int16_t element_mode, /* i  : element mode                                                    */
     317             :     const int16_t L_frame,      /* i  : length of the frame                                             */
     318             :     const int16_t i_subfr,      /* i  : subframe index                                                  */
     319             :     const int16_t tc_subfr,     /* i  : TC subframe index                                               */
     320             :     const float *xn,            /* i  : target vector                                                   */
     321             :     const float *y1,            /* i  : zero-memory filtered adaptive excitation                        */
     322             :     const float *y2,            /* i  : zero-memory filtered algebraic codebook excitation              */
     323             :     const float *code,          /* i  : algebraic excitation                                            */
     324             :     const float Es_pred,        /* i  : predicted scaled innovation energy                              */
     325             :     float *gain_pit,            /* o  : quantized pitch gain                                            */
     326             :     float *gain_code,           /* o  : quantized codebook gain                                         */
     327             :     float *gain_inov,           /* o  : gain of the innovation (used for normalization)                 */
     328             :     float *norm_gain_code,      /* o  : norm. gain of the codebook excitation                           */
     329             :     float *g_corr,              /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
     330             :     const int16_t clip_gain     /* i  : gain pitch clipping flag (1 = clipping)                         */
     331             : )
     332             : {
     333             :     int16_t index, i, size, nBits;
     334             :     float dist, dist_min, g_pitch, g_code, gcode0, Ei, Ecode;
     335             :     int16_t nBits2;
     336             :     float tmp1, tmp2;
     337             :     const float *p, *qua_table;
     338             : 
     339             :     /*-----------------------------------------------------------------*
     340             :      * calculate the rest of the correlation coefficients
     341             :      * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>
     342             :      *-----------------------------------------------------------------*/
     343             : 
     344      974443 :     E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
     345      974443 :     g_corr[2] += 0.01F;
     346      974443 :     g_corr[3] -= 0.02F;
     347      974443 :     g_corr[4] += 0.02F;
     348             : 
     349             :     /*-----------------------------------------------------------------*
     350             :      * calculate the unscaled innovation energy
     351             :      * calculate the predicted gain code
     352             :      *-----------------------------------------------------------------*/
     353             : 
     354      974443 :     Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
     355      974443 :     *gain_inov = 1.0f / (float) sqrt( Ecode );
     356      974443 :     Ei = 10 * (float) log10( Ecode );
     357      974443 :     gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
     358             : 
     359             :     /*-----------------------------------------------------------------*
     360             :      * select the codebook, size and number of bits
     361             :      * set the gains searching range
     362             :      *-----------------------------------------------------------------*/
     363             : 
     364      974443 :     nBits = gains_mode[i_subfr / L_SUBFR];
     365             : 
     366      974443 :     if ( ( tc_subfr == 3 * L_SUBFR && i_subfr == 3 * L_SUBFR && L_frame == L_FRAME ) ||
     367        3879 :          ( tc_subfr == 4 * L_SUBFR && i_subfr == 4 * L_SUBFR && L_frame == L_FRAME16k ) )
     368             :     {
     369             :         /* in case of attack at the end of the frame, use scalar gain quantizers */
     370        7304 :         tmp1 = ( g_corr[0] * g_corr[2] ) - ( 0.25f * g_corr[4] * g_corr[4] );
     371        7304 :         tmp2 = -0.5f * g_corr[1] / tmp1;
     372        7304 :         tmp1 = -0.5f * g_corr[3] / tmp1;
     373             : 
     374        7304 :         *gain_pit = ( g_corr[2] * tmp2 ) - ( 0.5f * g_corr[4] * tmp1 );
     375        7304 :         *gain_code = ( g_corr[0] * tmp1 ) - ( 0.5f * g_corr[4] * tmp2 );
     376             : 
     377        7304 :         *gain_pit = max( G_PITCH_MIN_TC192, min( *gain_pit, G_PITCH_MAX_TC192 ) );
     378             : 
     379             :         /* set number of bits for two SQs */
     380        7304 :         nBits2 = ( nBits + 1 ) >> 1;
     381        7304 :         nBits = nBits >> 1;
     382             : 
     383             :         /* gain_pit Q */
     384        7304 :         tmp1 = ( G_PITCH_MAX_TC192 - G_PITCH_MIN_TC192 ) / ( ( 1 << nBits ) - 1 ); /* set quantization step */
     385        7304 :         index = usquant( *gain_pit, gain_pit, G_PITCH_MIN_TC192, tmp1, ( 1 << nBits ) );
     386        7304 :         push_indice( hBstr, IND_GAIN_PIT, index, nBits );
     387             : 
     388             :         /* gain_code Q */
     389        7304 :         *gain_code /= gcode0;
     390        7304 :         index = gain_quant( gain_code, G_CODE_MIN_TC192, G_CODE_MAX_TC192, nBits2 );
     391        7304 :         push_indice( hBstr, IND_GAIN_CODE, index, nBits2 );
     392        7304 :         *gain_code *= gcode0;
     393             :     }
     394             :     else
     395             :     {
     396      967139 :         size = 1 << nBits;
     397             : 
     398      967139 :         switch ( nBits )
     399             :         {
     400         986 :             case 7:
     401             :             {
     402         986 :                 qua_table = gain_qua_mless_7b;
     403         986 :                 if ( clip_gain == 1 )
     404             :                 {
     405           0 :                     size -= 30;
     406             :                 }
     407         986 :                 break;
     408             :             }
     409      964592 :             case 6:
     410             :             {
     411      964592 :                 qua_table = gain_qua_mless_6b;
     412             : 
     413      964592 :                 if ( element_mode > EVS_MONO )
     414             :                 {
     415      951665 :                     qua_table = gain_qua_mless_6b_stereo;
     416             :                 }
     417             : 
     418      964592 :                 if ( clip_gain == 1 )
     419             :                 {
     420        6650 :                     size -= 14;
     421             :                 }
     422      964592 :                 break;
     423             :             }
     424        1561 :             case 5:
     425             :             {
     426        1561 :                 qua_table = gain_qua_mless_5b;
     427        1561 :                 if ( clip_gain == 1 )
     428             :                 {
     429           0 :                     size -= 6;
     430             :                 }
     431        1561 :                 break;
     432             :             }
     433           0 :             default:
     434             :             {
     435           0 :                 qua_table = gain_qua_mless_6b;
     436           0 :                 size = 64;
     437           0 :                 if ( clip_gain == 1 )
     438             :                 {
     439           0 :                     size -= 14;
     440             :                 }
     441           0 :                 break;
     442             :             }
     443             :         }
     444             : 
     445             :         /* in case of AVQ inactive, limit the gain_pit to 0.65 */
     446      967139 :         if ( clip_gain == 2 && nBits == 6 )
     447             :         {
     448       26460 :             size -= 36;
     449       26460 :             nBits--;
     450             :         }
     451             : 
     452             :         /*-----------------------------------------------------------------*
     453             :          * search for the best quantizer
     454             :          *-----------------------------------------------------------------*/
     455             : 
     456      967139 :         p = qua_table;
     457      967139 :         dist_min = 3.402823466e+38F;
     458      967139 :         index = 0;
     459    61831527 :         for ( i = 0; i < size; i++ )
     460             :         {
     461    60864388 :             g_pitch = *p++;         /* pitch gain */
     462    60864388 :             g_code = gcode0 * *p++; /* code gain */
     463    60864388 :             dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
     464    60864388 :             if ( dist < dist_min )
     465             :             {
     466    11891906 :                 dist_min = dist;
     467    11891906 :                 index = i;
     468             :             }
     469             :         }
     470      967139 :         *gain_pit = qua_table[index * 2];
     471      967139 :         *gain_code = qua_table[index * 2 + 1] * gcode0;
     472             : 
     473      967139 :         push_indice( hBstr, IND_GAIN, index, nBits );
     474             :     }
     475             : 
     476      974443 :     *norm_gain_code = *gain_code / *gain_inov;
     477             : 
     478      974443 :     return;
     479             : }
     480             : 
     481             : 
     482             : /*---------------------------------------------------------------------*
     483             :  * gain_enc_SQ()
     484             :  *
     485             :  * Scalar Quantization of pitch and codebook gains without prediction
     486             :  * - an initial predicted gain, gcode0, is first determined based on
     487             :  *   the predicted scaled innovation energy
     488             :  * - a correction factor gamma = g_code / gcode0 is then vector quantized
     489             :  *   along with gain_pit
     490             :  * - the mean-squared weighted error criterion is used for codebook search
     491             :  *---------------------------------------------------------------------*/
     492             : 
     493      107867 : void gain_enc_SQ(
     494             :     BSTR_ENC_HANDLE hBstr,      /* i/o: encoder bitstream handle                                        */
     495             :     const int16_t gains_mode[], /* i  : gain bits                                                       */
     496             :     const int16_t i_subfr,      /* i  : subframe index                                                  */
     497             :     const float *xn,            /* i  : target vector                                                   */
     498             :     const float *yy1,           /* i  : zero-memory filtered adaptive excitation                        */
     499             :     const float *y2,            /* i  : zero-memory filtered algebraic codebook excitation              */
     500             :     const float *code,          /* i  : algebraic excitation                                            */
     501             :     const float Es_pred,        /* i  : predicted scaled innovation energy                              */
     502             :     float *gain_pit,            /* o  : quantized pitch gain                                            */
     503             :     float *gain_code,           /* o  : quantized codebook gain                                         */
     504             :     float *gain_inov,           /* o  : gain of the innovation (used for normalization)                 */
     505             :     float *norm_gain_code,      /* o  : norm. gain of the codebook excitation                           */
     506             :     float *g_corr,              /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
     507             :     const int16_t clip_gain     /* i  : gain pitch clipping flag (1 = clipping)                         */
     508             : )
     509             : {
     510             :     int16_t index, nBits_pitch, nBits_code;
     511             :     float g_code, gcode0, Ei, Ecode, tmp1, tmp2;
     512             :     int16_t tmp16;
     513             :     /*-----------------------------------------------------------------*
     514             :      * calculate the rest of the correlation coefficients
     515             :      * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>
     516             :      *-----------------------------------------------------------------*/
     517             : 
     518      107867 :     g_corr[1] *= -0.5;
     519      107867 :     g_corr[2] = dotp( y2, y2, L_SUBFR ) + 0.01f;
     520      107867 :     g_corr[3] = dotp( xn, y2, L_SUBFR ) - 0.02f;
     521      107867 :     g_corr[4] = dotp( yy1, y2, L_SUBFR ) + 0.02f;
     522             : 
     523             :     /*-----------------------------------------------------------------*
     524             :      * calculate the unscaled innovation energy
     525             :      * calculate the predicted gain code
     526             :      * calculate optimal gains
     527             :      *-----------------------------------------------------------------*/
     528             : 
     529      107867 :     Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
     530      107867 :     *gain_inov = 1.0f / (float) sqrt( Ecode );
     531      107867 :     Ei = 10 * (float) log10( Ecode );
     532      107867 :     gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
     533             : 
     534      107867 :     tmp1 = ( g_corr[0] * g_corr[2] ) - ( g_corr[4] * g_corr[4] );
     535      107867 :     tmp2 = g_corr[1] / tmp1;
     536      107867 :     tmp1 = g_corr[3] / tmp1;
     537             : 
     538      107867 :     *gain_pit = ( g_corr[2] * tmp2 ) - ( g_corr[4] * tmp1 );
     539      107867 :     *gain_code = ( g_corr[0] * tmp1 ) - ( g_corr[4] * tmp2 );
     540             : 
     541      107867 :     *gain_pit = max( G_PITCH_MIN, min( *gain_pit, G_PITCH_MAX ) );
     542             : 
     543             :     /*-----------------------------------------------------------------*
     544             :      * limit the pitch gain searching range (if indicated by clip_gain)
     545             :      *-----------------------------------------------------------------*/
     546             : 
     547      107867 :     if ( clip_gain == 1 && *gain_pit > 0.95f )
     548             :     {
     549          19 :         *gain_pit = 0.95f;
     550             :     }
     551      107848 :     else if ( clip_gain == 2 && *gain_pit > 0.65f )
     552             :     {
     553        1656 :         *gain_pit = 0.65f;
     554             :     }
     555             : 
     556             :     /*-----------------------------------------------------------------*
     557             :      * search for the best quantized values
     558             :      *-----------------------------------------------------------------*/
     559             : 
     560      107867 :     nBits_pitch = gains_mode[i_subfr / L_SUBFR];
     561      107867 :     nBits_code = ( nBits_pitch + 1 ) >> 1;
     562      107867 :     nBits_pitch = nBits_pitch >> 1;
     563             : 
     564      107867 :     tmp16 = div_s( 1, ( ( 1 << ( nBits_pitch ) ) - 1 ) ); /* Q15*/
     565      107867 :     tmp1 = (float) mult_r( (int16_t) ( G_PITCH_MAX * 8192.0f + 0.5f ), tmp16 ) / 8192.0f;
     566      107867 :     index = usquant( *gain_pit, gain_pit, G_PITCH_MIN, tmp1, ( 1 << nBits_pitch ) );
     567      107867 :     push_indice( hBstr, IND_GAIN_PIT, index, nBits_pitch );
     568             : 
     569      107867 :     g_code = *gain_code / gcode0;
     570      107867 :     index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits_code );
     571      107867 :     *gain_code = g_code * gcode0;
     572      107867 :     push_indice( hBstr, IND_GAIN_CODE, index, nBits_code );
     573             : 
     574      107867 :     *norm_gain_code = *gain_code / *gain_inov;
     575             : 
     576      107867 :     return;
     577             : }
     578             : 
     579             : /*-------------------------------------------------------------------*
     580             :  * gain_enc_gaus()
     581             :  *
     582             :  * Quantization of gain for Gaussian codebook
     583             :  *-------------------------------------------------------------------*/
     584             : 
     585             : /*! r: Return index of quantization */
     586         104 : int16_t gain_enc_gaus(
     587             :     float *gain,          /* i/o: Code gain to quantize         */
     588             :     const int16_t bits,   /* i  : number of bits to quantize    */
     589             :     const float lowBound, /* i  : lower bound of quantizer (dB) */
     590             :     const float topBound  /* i  : upper bound of quantizer (dB) */
     591             : )
     592             : {
     593             :     int16_t index;
     594             :     float enr, stepSize;
     595             : 
     596         104 :     enr = (float) ( 20.0 * log10( *gain + 0.001f ) ); /* codebook gain in dB  */
     597             : 
     598             :     /*-----------------------------------------------------------------*
     599             :      * quantize linearly the log E
     600             :      *-----------------------------------------------------------------*/
     601             : 
     602         104 :     stepSize = ( topBound - lowBound ) / ( (float) ( 1 << bits ) );
     603         104 :     index = (int16_t) ( ( ( enr - lowBound ) / stepSize ) + 0.5f );
     604         104 :     if ( index >= ( 1 << bits ) )
     605             :     {
     606           0 :         index = ( 1 << bits ) - 1;
     607             :     }
     608             : 
     609         104 :     if ( index < 0 )
     610             :     {
     611           2 :         index = 0;
     612             :     }
     613             : 
     614         104 :     enr = (float) index * stepSize + lowBound; /* quantized codebook gain in dB */
     615         104 :     *gain = (float) pow( 10.0f, enr / 20.0f ); /* quantized codebook gain */
     616             : 
     617         104 :     return index;
     618             : }
     619             : 
     620             : /*-----------------------------------------------------------------*
     621             :  * gain_enc_tc()
     622             :  *
     623             :  * Search and quantization of gain_code for subframes (in the
     624             :  * beginning of frame) without pulses in TC - 3b coding.
     625             :  * In this case:
     626             :  * - gain_pit = 0
     627             :  * - gain_code - scalar quantization (no prediciton history used)
     628             :  *-----------------------------------------------------------------*/
     629             : 
     630       43740 : void gain_enc_tc(
     631             :     BSTR_ENC_HANDLE hBstr,      /* i/o: encoder bitstream handle                           */
     632             :     const int16_t gains_mode[], /* i  : gain bits                                          */
     633             :     const int16_t i_subfr,      /* i  : subframe index                                     */
     634             :     const float xn[],           /* i  : target vector                                      */
     635             :     const float y2[],           /* i  : zero-memory filtered algebraic codebook excitation */
     636             :     const float code[],         /* i  : algebraic excitation                               */
     637             :     const float Es_pred,        /* i  : predicted scaled innovation energy                 */
     638             :     float *gain_pit,            /* o  : Pitch gain / Quantized pitch gain                  */
     639             :     float *gain_code,           /* o  : quantized codebook gain                            */
     640             :     float *gain_inov,           /* o  : innovation gain                                    */
     641             :     float *norm_gain_code       /* o  : norm. gain of the codebook excitation              */
     642             : )
     643             : {
     644             :     int16_t i, index, nBits;
     645             :     float Ei, g_code, gcode0, Ecode;
     646             : 
     647             :     /*----------------------------------------------------------------*
     648             :      * get number of bits for gain quantization
     649             :      *----------------------------------------------------------------*/
     650             : 
     651       43740 :     nBits = gains_mode[i_subfr / L_SUBFR];
     652             : 
     653             :     /*----------------------------------------------------------------*
     654             :      * find the code pitch (for current subframe)
     655             :      *----------------------------------------------------------------*/
     656             : 
     657       43740 :     *gain_code = dotp( xn, y2, L_SUBFR ) / ( dotp( y2, y2, L_SUBFR ) + 0.01f );
     658             : 
     659             :     /*----------------------------------------------------------------*
     660             :      * calculate the predicted gain code
     661             :      * decode codebook gain
     662             :      *----------------------------------------------------------------*/
     663             : 
     664       43740 :     *gain_pit = 0;
     665             : 
     666       43740 :     Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
     667       43740 :     *gain_inov = 1.0f / (float) sqrt( Ecode );
     668       43740 :     Ei = 10 * (float) log10( Ecode );
     669       43740 :     gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
     670             : 
     671       43740 :     if ( nBits > 3 )
     672             :     {
     673        6938 :         g_code = *gain_code / gcode0;
     674        6938 :         index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits );
     675        6938 :         *gain_code = g_code * gcode0;
     676        6938 :         push_indice( hBstr, IND_GAIN_CODE, index, nBits );
     677             :     }
     678             :     else
     679             :     {
     680       36802 :         index = N_GAIN_CODE_TC - 1;
     681      114747 :         for ( i = 0; i < N_GAIN_CODE_TC - 1; i++ )
     682             :         {
     683      112426 :             if ( *gain_code < ( ( tbl_gain_code_tc[i] + ( tbl_gain_code_tc[i + 1] - tbl_gain_code_tc[i] ) / 2 ) * gcode0 ) )
     684             :             {
     685       34481 :                 index = i;
     686       34481 :                 break;
     687             :             }
     688             :         }
     689             : 
     690             :         /*----------------------------------------------------------------*
     691             :          * 3-bit -> 2-bit encoding
     692             :          *----------------------------------------------------------------*/
     693             : 
     694       36802 :         if ( nBits == 2 )
     695             :         {
     696           0 :             index /= 2;
     697           0 :             *gain_code = tbl_gain_code_tc[index * 2] * gcode0;
     698           0 :             push_indice( hBstr, IND_GAIN_CODE, index, nBits );
     699             :         }
     700             :         else
     701             :         {
     702       36802 :             *gain_code = tbl_gain_code_tc[index] * gcode0;
     703       36802 :             push_indice( hBstr, IND_GAIN_CODE, index, nBits );
     704             :         }
     705             :     }
     706             : 
     707       43740 :     *norm_gain_code = *gain_code / *gain_inov;
     708             : 
     709       43740 :     return;
     710             : }
     711             : 
     712             : /*---------------------------------------------------------------------*
     713             :  * E_corr_xy2()
     714             :  *
     715             :  * Find the correlations between the target xn[], the filtered adaptive
     716             :  * codebook exc. y1[], and the filtered fixed codebook innovation y2[].
     717             :  *  ( <y2,y2>, -2<xn,y2> and 2<y1,y2> )   (stored in g_corr[2..4])
     718             :  *---------------------------------------------------------------------*/
     719             : 
     720     1052721 : void E_corr_xy2(
     721             :     const float xn[],     /* i  : target vector                          */
     722             :     const float y1[],     /* i  : filtered excitation components 1       */
     723             :     const float y2[],     /* i  : filtered excitation components 2       */
     724             :     float g_corr[],       /* o  : correlations between x, y1, y2, y3, y4 */
     725             :     const int16_t L_subfr /* i  : subframe size                          */
     726             : )
     727             : {
     728     1052721 :     g_corr[2] = dotp( y2, y2, L_subfr );
     729     1052721 :     g_corr[3] = -2.0f * dotp( xn, y2, L_subfr );
     730     1052721 :     g_corr[4] = 2.0f * dotp( y1, y2, L_subfr );
     731             : 
     732     1052721 :     return;
     733             : }
     734             : 
     735             : 
     736             : /*---------------------------------------------------------------------*
     737             :  * gain_enc_lbr()
     738             :  *
     739             :  * Quantization of pitch and codebook gains without prediction (memory-less)
     740             :  * in ACELP at 7.2 and 8.0 kbps
     741             :  * - the gain codebooks and gain estimation constants are different in each subframe
     742             :  * - the estimated gain, gcode0, is first determined based on
     743             :  *   classification and/or previous quantized gains (from previous subframes in the current frame)
     744             :  * - a correction factor gamma = g_code / gcode0 is then vector quantized
     745             :  *   along with gain_pit
     746             :  * - the mean-squared error criterion is used for codebook search
     747             :  *---------------------------------------------------------------------*/
     748             : 
     749       35064 : void gain_enc_lbr(
     750             :     BSTR_ENC_HANDLE hBstr,      /* i/o: encoder bitstream handle                                        */
     751             :     const int16_t gains_mode[], /* i  : gain bits                                                       */
     752             :     const int16_t coder_type,   /* i  : coding type                                                     */
     753             :     const int16_t i_subfr,      /* i  : subframe index                                                  */
     754             :     const float *xn,            /* i  : target vector                                                   */
     755             :     const float *y1,            /* i  : zero-memory filtered adaptive excitation                        */
     756             :     const float *y2,            /* i  : zero-memory filtered algebraic codebook excitation              */
     757             :     const float *code,          /* i  : algebraic excitation                                            */
     758             :     float *gain_pit,            /* o  : quantized pitch gain                                            */
     759             :     float *gain_code,           /* o  : quantized codebook gain                                         */
     760             :     float *gain_inov,           /* o  : gain of the innovation (used for normalization)                 */
     761             :     float *norm_gain_code,      /* o  : norm. gain of the codebook excitation                           */
     762             :     float *g_corr,              /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
     763             :     float gains_mem[],          /* i/o: pitch gain and code gain from previous subframes                */
     764             :     const int16_t clip_gain,    /* i  : gain pitch clipping flag (1 = clipping)                         */
     765             :     const int16_t L_subfr       /* i  : subframe length                                                 */
     766             : )
     767             : {
     768       35064 :     int16_t index = 0, i, size, nBits, n_pred, ctype;
     769             :     float dist, dist_min, g_pitch, g_code, gcode0, aux[10], Ecode;
     770       35064 :     int16_t rf_flag = 0;
     771       35064 :     const float *p, *b, *cdbk = 0;
     772             : 
     773             :     /*-----------------------------------------------------------------*
     774             :      * calculate the rest of the correlation coefficients
     775             :      * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>, c5* = <xn,xn>
     776             :      * c5* - not necessary to calculate
     777             :      *-----------------------------------------------------------------*/
     778             : 
     779       35064 :     E_corr_xy2( xn, y1, y2, g_corr, L_subfr );
     780       35064 :     g_corr[2] += 0.01F;
     781       35064 :     g_corr[3] -= 0.02F;
     782       35064 :     g_corr[4] += 0.02F;
     783             : 
     784       35064 :     Ecode = ( dotp( code, code, L_subfr ) + 0.01f ) / L_subfr;
     785       35064 :     *gain_inov = 1.0f / (float) sqrt( Ecode );
     786             : 
     787             :     /*-----------------------------------------------------------------*
     788             :      * select the codebook, size and number of bits
     789             :      * set the gains searching range
     790             :      *-----------------------------------------------------------------*/
     791             : 
     792       35064 :     nBits = gains_mode[i_subfr / L_subfr];
     793       35064 :     size = 1 << nBits;
     794             : 
     795             :     /*-----------------------------------------------------------------*
     796             :      * calculate prediction of gcode
     797             :      * search for the best codeword
     798             :      *-----------------------------------------------------------------*/
     799             : 
     800       35064 :     ctype = 2 * ( coder_type - 1 );
     801       35064 :     if ( i_subfr == 0 )
     802             :     {
     803       12054 :         b = b_1sfr;
     804       12054 :         n_pred = 2;
     805             : 
     806       12054 :         switch ( nBits )
     807             :         {
     808        3588 :             case 8:
     809             :             {
     810        3588 :                 cdbk = gp_gamma_1sfr_8b;
     811        3588 :                 if ( clip_gain == 1 )
     812             :                 {
     813           0 :                     size -= 60;
     814             :                 }
     815        3588 :                 break;
     816             :             }
     817         875 :             case 7:
     818             :             {
     819         875 :                 cdbk = gp_gamma_1sfr_7b;
     820         875 :                 if ( clip_gain == 1 )
     821             :                 {
     822           4 :                     size -= 27;
     823             :                 }
     824         875 :                 break;
     825             :             }
     826        7591 :             case 6:
     827             :             {
     828        7591 :                 cdbk = gp_gamma_1sfr_6b;
     829        7591 :                 if ( clip_gain == 1 )
     830             :                 {
     831           0 :                     size -= 10;
     832             :                 }
     833        7591 :                 break;
     834             :             }
     835             :         }
     836             : 
     837             :         /* calculate predicted gain */
     838       12054 :         aux[0] = 1.0f;
     839       12054 :         aux[1] = ctype;
     840       12054 :         gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) - 0.5f * (float) log10( Ecode ) );
     841             : 
     842             :         /* searching of codebook */
     843       12054 :         p = cdbk;
     844       12054 :         dist_min = 3.402823466e+38F;
     845       12054 :         index = 0;
     846     1528298 :         for ( i = 0; i < size; i++ )
     847             :         {
     848     1516244 :             g_pitch = *p++;
     849     1516244 :             g_code = gcode0 * *p++;
     850     1516244 :             dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
     851             : 
     852     1516244 :             if ( dist < dist_min )
     853             :             {
     854      144030 :                 dist_min = dist;
     855      144030 :                 index = i;
     856             :             }
     857             :         }
     858             : 
     859       12054 :         *gain_pit = cdbk[index * 2];
     860       12054 :         *gain_code = cdbk[index * 2 + 1] * gcode0;
     861             : 
     862       12054 :         gains_mem[0] = *gain_code;
     863       12054 :         gains_mem[3] = *gain_pit;
     864             :     }
     865       23010 :     else if ( i_subfr == L_SUBFR || ( L_subfr == 2 * L_SUBFR ) )
     866             :     {
     867       12054 :         b = b_2sfr;
     868       12054 :         n_pred = 4;
     869             : 
     870       12054 :         switch ( nBits )
     871             :         {
     872        3584 :             case 7:
     873             :             {
     874        3584 :                 cdbk = gp_gamma_2sfr_7b;
     875        3584 :                 if ( clip_gain == 1 )
     876             :                 {
     877           0 :                     size -= 30;
     878             :                 }
     879        3584 :                 break;
     880             :             }
     881        8470 :             case 6:
     882             :             {
     883        8470 :                 cdbk = gp_gamma_2sfr_6b;
     884        8470 :                 if ( clip_gain == 1 )
     885             :                 {
     886           4 :                     size -= 12;
     887             :                 }
     888        8470 :                 break;
     889             :             }
     890             :         }
     891             : 
     892             :         /* calculate predicted gain */
     893       12054 :         aux[0] = 1.0f;
     894       12054 :         aux[1] = ctype;
     895       12054 :         aux[2] = (float) log10( gains_mem[0] );
     896       12054 :         aux[3] = gains_mem[3];
     897       12054 :         gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
     898             : 
     899             :         /* searching of codebook */
     900       12054 :         p = cdbk;
     901       12054 :         dist_min = 3.402823466e+38F;
     902       12054 :         index = 0;
     903     1012838 :         for ( i = 0; i < size; i++ )
     904             :         {
     905     1000784 :             g_pitch = *p++;
     906     1000784 :             g_code = gcode0 * *p++;
     907     1000784 :             dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
     908             : 
     909     1000784 :             if ( dist < dist_min )
     910             :             {
     911      164952 :                 dist_min = dist;
     912      164952 :                 index = i;
     913             :             }
     914             :         }
     915       12054 :         *gain_pit = cdbk[index * 2];
     916       12054 :         *gain_code = cdbk[index * 2 + 1] * gcode0;
     917             : 
     918       12054 :         gains_mem[1] = *gain_code;
     919       12054 :         gains_mem[4] = *gain_pit;
     920             :     }
     921       10956 :     else if ( i_subfr == 2 * L_SUBFR )
     922             :     {
     923        5478 :         if ( rf_flag == 1 )
     924             :         {
     925           0 :             gains_mem[1] = gains_mem[0];
     926           0 :             gains_mem[4] = gains_mem[3];
     927             :         }
     928             : 
     929        5478 :         b = b_3sfr;
     930        5478 :         n_pred = 6;
     931             : 
     932        5478 :         switch ( nBits )
     933             :         {
     934           2 :             case 7:
     935             :             {
     936           2 :                 cdbk = gp_gamma_3sfr_7b;
     937           2 :                 if ( clip_gain == 1 )
     938             :                 {
     939           0 :                     size -= 28;
     940             :                 }
     941           2 :                 break;
     942             :             }
     943        5476 :             case 6:
     944             :             {
     945        5476 :                 cdbk = gp_gamma_3sfr_6b;
     946        5476 :                 if ( clip_gain == 1 )
     947             :                 {
     948           4 :                     size -= 11;
     949             :                 }
     950        5476 :                 break;
     951             :             }
     952             :         }
     953             : 
     954             :         /* calculate predicted gain */
     955        5478 :         aux[0] = 1.0f;
     956        5478 :         aux[1] = ctype;
     957        5478 :         aux[2] = (float) log10( gains_mem[0] );
     958        5478 :         aux[3] = (float) log10( gains_mem[1] );
     959        5478 :         aux[4] = gains_mem[3];
     960        5478 :         aux[5] = gains_mem[4];
     961        5478 :         gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
     962             : 
     963             :         /* searching of codebook */
     964        5478 :         p = cdbk;
     965        5478 :         dist_min = 3.402823466e+38F;
     966        5478 :         index = 0;
     967      356154 :         for ( i = 0; i < size; i++ )
     968             :         {
     969      350676 :             g_pitch = *p++;
     970      350676 :             g_code = gcode0 * *p++;
     971      350676 :             dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
     972             : 
     973      350676 :             if ( dist < dist_min )
     974             :             {
     975       82021 :                 dist_min = dist;
     976       82021 :                 index = i;
     977             :             }
     978             :         }
     979        5478 :         *gain_pit = cdbk[index * 2];
     980        5478 :         *gain_code = cdbk[index * 2 + 1] * gcode0;
     981             : 
     982        5478 :         gains_mem[2] = *gain_code;
     983        5478 :         gains_mem[5] = *gain_pit;
     984             :     }
     985        5478 :     else if ( i_subfr == 3 * L_SUBFR )
     986             :     {
     987        5478 :         b = b_4sfr;
     988        5478 :         n_pred = 8;
     989             : 
     990        5478 :         switch ( nBits )
     991             :         {
     992           0 :             case 7:
     993             :             {
     994           0 :                 cdbk = gp_gamma_4sfr_7b;
     995           0 :                 if ( clip_gain == 1 )
     996             :                 {
     997           0 :                     size -= 25;
     998             :                 }
     999           0 :                 break;
    1000             :             }
    1001        5478 :             case 6:
    1002             :             {
    1003        5478 :                 cdbk = gp_gamma_4sfr_6b;
    1004        5478 :                 if ( clip_gain == 1 )
    1005             :                 {
    1006           4 :                     size -= 11;
    1007             :                 }
    1008        5478 :                 break;
    1009             :             }
    1010             :         }
    1011             : 
    1012             :         /* calculate predicted gain */
    1013        5478 :         aux[0] = 1.0f;
    1014        5478 :         aux[1] = ctype;
    1015        5478 :         aux[2] = (float) log10( gains_mem[0] );
    1016        5478 :         aux[3] = (float) log10( gains_mem[1] );
    1017        5478 :         aux[4] = (float) log10( gains_mem[2] );
    1018        5478 :         aux[5] = gains_mem[3];
    1019        5478 :         aux[6] = gains_mem[4];
    1020        5478 :         aux[7] = gains_mem[5];
    1021        5478 :         gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
    1022             : 
    1023             :         /* searching of codebook */
    1024        5478 :         p = cdbk;
    1025        5478 :         dist_min = 3.402823466e+38F;
    1026        5478 :         index = 0;
    1027      356026 :         for ( i = 0; i < size; i++ )
    1028             :         {
    1029      350548 :             g_pitch = *p++;
    1030      350548 :             g_code = gcode0 * *p++;
    1031      350548 :             dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
    1032             : 
    1033      350548 :             if ( dist < dist_min )
    1034             :             {
    1035       88213 :                 dist_min = dist;
    1036       88213 :                 index = i;
    1037             :             }
    1038             :         }
    1039        5478 :         *gain_pit = cdbk[index * 2];
    1040        5478 :         *gain_code = cdbk[index * 2 + 1] * gcode0;
    1041             :     }
    1042             : 
    1043       35064 :     *norm_gain_code = *gain_code / *gain_inov;
    1044             : 
    1045       35064 :     push_indice( hBstr, IND_GAIN, index, nBits );
    1046             : 
    1047       35064 :     return;
    1048             : }

Generated by: LCOV version 1.14