Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / ilbc / cb_update_best_index.c
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 ((CritNew >> shNew) > (*CritMax >> shOld)) {
55
56     tmp16 = (int16_t)WebRtcSpl_NormW32(cDotNew);
57     tmp16 = 16 - tmp16;
58
59     /* Calculate the gain in Q14
60        Compensate for inverseEnergyshift in Q29 and that the energy
61        value was stored in a int16_t (shifted down 16 steps)
62        => 29-14+16 = 31 */
63
64     scaleTmp = -energyShiftNew-tmp16+31;
65     scaleTmp = WEBRTC_SPL_MIN(31, scaleTmp);
66
67     gainW32 = WEBRTC_SPL_MUL_16_16_RSFT(
68         ((int16_t)WEBRTC_SPL_SHIFT_W32(cDotNew, -tmp16)), invEnergyNew, scaleTmp);
69
70     /* Check if criteria satisfies Gain criteria (max 1.3)
71        if it is larger set the gain to 1.3
72        (slightly different from FLP version)
73     */
74     if (gainW32>21299) {
75       *bestGain=21299;
76     } else if (gainW32<-21299) {
77       *bestGain=-21299;
78     } else {
79       *bestGain=(int16_t)gainW32;
80     }
81
82     *CritMax=CritNew;
83     *shTotMax=CritNewSh;
84     *bestIndex = IndexNew;
85   }
86
87   return;
88 }