2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 /******************************************************************
13 iLBC Speech Coder ANSI-C Source Code
15 WebRtcIlbcfix_CbUpdateBestIndex.c
17 ******************************************************************/
20 #include "cb_update_best_index.h"
21 #include "constants.h"
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
34 int16_t *bestGain) /* (i/o) Gain in Q14 that corresponds
35 to maximum criteria */
37 int16_t shOld, shNew, tmp16;
41 /* Normalize the new and old Criteria to the same domain */
42 if (CritNewSh>(*shTotMax)) {
43 shOld=WEBRTC_SPL_MIN(31,CritNewSh-(*shTotMax));
47 shNew=WEBRTC_SPL_MIN(31,(*shTotMax)-CritNewSh);
50 /* Compare the two criterias. If the new one is better,
51 calculate the gain and store this index as the new best one
54 if ((CritNew >> shNew) > (*CritMax >> shOld)) {
56 tmp16 = (int16_t)WebRtcSpl_NormW32(cDotNew);
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)
64 scaleTmp = -energyShiftNew-tmp16+31;
65 scaleTmp = WEBRTC_SPL_MIN(31, scaleTmp);
67 gainW32 = WEBRTC_SPL_MUL_16_16_RSFT(
68 ((int16_t)WEBRTC_SPL_SHIFT_W32(cDotNew, -tmp16)), invEnergyNew, scaleTmp);
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)
76 } else if (gainW32<-21299) {
79 *bestGain=(int16_t)gainW32;
84 *bestIndex = IndexNew;