Home | History | Annotate | Download | only in ilbc
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /******************************************************************
     12 
     13  iLBC Speech Coder ANSI-C Source Code
     14 
     15  WebRtcIlbcfix_CbUpdateBestIndex.c
     16 
     17 ******************************************************************/
     18 
     19 #include "defines.h"
     20 #include "cb_update_best_index.h"
     21 #include "constants.h"
     22 
     23 void WebRtcIlbcfix_CbUpdateBestIndex(
     24     int32_t CritNew,    /* (i) New Potentially best Criteria */
     25     int16_t CritNewSh,   /* (i) Shift value of above Criteria */
     26     int16_t IndexNew,   /* (i) Index of new Criteria */
     27     int32_t cDotNew,    /* (i) Cross dot of new index */
     28     int16_t invEnergyNew,  /* (i) Inversed energy new index */
     29     int16_t energyShiftNew,  /* (i) Energy shifts of new index */
     30     int32_t *CritMax,   /* (i/o) Maximum Criteria (so far) */
     31     int16_t *shTotMax,   /* (i/o) Shifts of maximum criteria */
     32     int16_t *bestIndex,   /* (i/o) Index that corresponds to
     33                                                    maximum criteria */
     34     int16_t *bestGain)   /* (i/o) Gain in Q14 that corresponds
     35                                                    to maximum criteria */
     36 {
     37   int16_t shOld, shNew, tmp16;
     38   int16_t scaleTmp;
     39   int32_t gainW32;
     40 
     41   /* Normalize the new and old Criteria to the same domain */
     42   if (CritNewSh>(*shTotMax)) {
     43     shOld=WEBRTC_SPL_MIN(31,CritNewSh-(*shTotMax));
     44     shNew=0;
     45   } else {
     46     shOld=0;
     47     shNew=WEBRTC_SPL_MIN(31,(*shTotMax)-CritNewSh);
     48   }
     49 
     50   /* Compare the two criterias. If the new one is better,
     51      calculate the gain and store this index as the new best one
     52   */
     53 
     54   if (WEBRTC_SPL_RSHIFT_W32(CritNew, shNew)>
     55       WEBRTC_SPL_RSHIFT_W32((*CritMax),shOld)) {
     56 
     57     tmp16 = (int16_t)WebRtcSpl_NormW32(cDotNew);
     58     tmp16 = 16 - tmp16;
     59 
     60     /* Calculate the gain in Q14
     61        Compensate for inverseEnergyshift in Q29 and that the energy
     62        value was stored in a int16_t (shifted down 16 steps)
     63        => 29-14+16 = 31 */
     64 
     65     scaleTmp = -energyShiftNew-tmp16+31;
     66     scaleTmp = WEBRTC_SPL_MIN(31, scaleTmp);
     67 
     68     gainW32 = WEBRTC_SPL_MUL_16_16_RSFT(
     69         ((int16_t)WEBRTC_SPL_SHIFT_W32(cDotNew, -tmp16)), invEnergyNew, scaleTmp);
     70 
     71     /* Check if criteria satisfies Gain criteria (max 1.3)
     72        if it is larger set the gain to 1.3
     73        (slightly different from FLP version)
     74     */
     75     if (gainW32>21299) {
     76       *bestGain=21299;
     77     } else if (gainW32<-21299) {
     78       *bestGain=-21299;
     79     } else {
     80       *bestGain=(int16_t)gainW32;
     81     }
     82 
     83     *CritMax=CritNew;
     84     *shTotMax=CritNewSh;
     85     *bestIndex = IndexNew;
     86   }
     87 
     88   return;
     89 }
     90