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_AbsQuantLoop.c
16
17******************************************************************/
18
19#include "defines.h"
20#include "constants.h"
21#include "sort_sq.h"
22
23void WebRtcIlbcfix_AbsQuantLoop(int16_t *syntOutIN, int16_t *in_weightedIN,
24                                int16_t *weightDenumIN, size_t *quantLenIN,
25                                int16_t *idxVecIN ) {
26  size_t k1, k2;
27  int16_t index;
28  int32_t toQW32;
29  int32_t toQ32;
30  int16_t tmp16a;
31  int16_t xq;
32
33  int16_t *syntOut   = syntOutIN;
34  int16_t *in_weighted  = in_weightedIN;
35  int16_t *weightDenum  = weightDenumIN;
36  size_t *quantLen  = quantLenIN;
37  int16_t *idxVec   = idxVecIN;
38
39  for(k1=0;k1<2;k1++) {
40    for(k2=0;k2<quantLen[k1];k2++){
41
42      /* Filter to get the predicted value */
43      WebRtcSpl_FilterARFastQ12(
44          syntOut, syntOut,
45          weightDenum, LPC_FILTERORDER+1, 1);
46
47      /* the quantizer */
48      toQW32 = (int32_t)(*in_weighted) - (int32_t)(*syntOut);
49
50      toQ32 = (((int32_t)toQW32)<<2);
51
52      if (toQ32 > 32767) {
53        toQ32 = (int32_t) 32767;
54      } else if (toQ32 < -32768) {
55        toQ32 = (int32_t) -32768;
56      }
57
58      /* Quantize the state */
59      if (toQW32<(-7577)) {
60        /* To prevent negative overflow */
61        index=0;
62      } else if (toQW32>8151) {
63        /* To prevent positive overflow */
64        index=7;
65      } else {
66        /* Find the best quantization index
67           (state_sq3Tbl is in Q13 and toQ is in Q11)
68        */
69        WebRtcIlbcfix_SortSq(&xq, &index,
70                             (int16_t)toQ32,
71                             WebRtcIlbcfix_kStateSq3, 8);
72      }
73
74      /* Store selected index */
75      (*idxVec++) = index;
76
77      /* Compute decoded sample and update of the prediction filter */
78      tmp16a = ((WebRtcIlbcfix_kStateSq3[index] + 2 ) >> 2);
79
80      *syntOut     = (int16_t) (tmp16a + (int32_t)(*in_weighted) - toQW32);
81
82      syntOut++; in_weighted++;
83    }
84    /* Update perceptual weighting filter at subframe border */
85    weightDenum += 11;
86  }
87}
88