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 "modules/audio_coding/codecs/ilbc/cb_update_best_index.h"
20
21 #include "modules/audio_coding/codecs/ilbc/constants.h"
22 #include "modules/audio_coding/codecs/ilbc/defines.h"
23
WebRtcIlbcfix_CbUpdateBestIndex(int32_t CritNew,int16_t CritNewSh,size_t IndexNew,int32_t cDotNew,int16_t invEnergyNew,int16_t energyShiftNew,int32_t * CritMax,int16_t * shTotMax,size_t * bestIndex,int16_t * bestGain)24 void WebRtcIlbcfix_CbUpdateBestIndex(
25 int32_t CritNew, /* (i) New Potentially best Criteria */
26 int16_t CritNewSh, /* (i) Shift value of above Criteria */
27 size_t IndexNew, /* (i) Index of new Criteria */
28 int32_t cDotNew, /* (i) Cross dot of new index */
29 int16_t invEnergyNew, /* (i) Inversed energy new index */
30 int16_t energyShiftNew, /* (i) Energy shifts of new index */
31 int32_t *CritMax, /* (i/o) Maximum Criteria (so far) */
32 int16_t *shTotMax, /* (i/o) Shifts of maximum criteria */
33 size_t *bestIndex, /* (i/o) Index that corresponds to
34 maximum criteria */
35 int16_t *bestGain) /* (i/o) Gain in Q14 that corresponds
36 to maximum criteria */
37 {
38 int16_t shOld, shNew, tmp16;
39 int16_t scaleTmp;
40 int32_t gainW32;
41
42 /* Normalize the new and old Criteria to the same domain */
43 if (CritNewSh>(*shTotMax)) {
44 shOld=WEBRTC_SPL_MIN(31,CritNewSh-(*shTotMax));
45 shNew=0;
46 } else {
47 shOld=0;
48 shNew=WEBRTC_SPL_MIN(31,(*shTotMax)-CritNewSh);
49 }
50
51 /* Compare the two criterias. If the new one is better,
52 calculate the gain and store this index as the new best one
53 */
54
55 if ((CritNew >> shNew) > (*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 = ((int16_t)WEBRTC_SPL_SHIFT_W32(cDotNew, -tmp16) * invEnergyNew) >>
69 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