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, int16_t *quantLenIN,
25                                int16_t *idxVecIN ) {
26  int n, 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  int16_t *quantLen  = quantLenIN;
37  int16_t *idxVec   = idxVecIN;
38
39  n=0;
40
41  for(k1=0;k1<2;k1++) {
42    for(k2=0;k2<quantLen[k1];k2++){
43
44      /* Filter to get the predicted value */
45      WebRtcSpl_FilterARFastQ12(
46          syntOut, syntOut,
47          weightDenum, LPC_FILTERORDER+1, 1);
48
49      /* the quantizer */
50      toQW32 = (int32_t)(*in_weighted) - (int32_t)(*syntOut);
51
52      toQ32 = (((int32_t)toQW32)<<2);
53
54      if (toQ32 > 32767) {
55        toQ32 = (int32_t) 32767;
56      } else if (toQ32 < -32768) {
57        toQ32 = (int32_t) -32768;
58      }
59
60      /* Quantize the state */
61      if (toQW32<(-7577)) {
62        /* To prevent negative overflow */
63        index=0;
64      } else if (toQW32>8151) {
65        /* To prevent positive overflow */
66        index=7;
67      } else {
68        /* Find the best quantization index
69           (state_sq3Tbl is in Q13 and toQ is in Q11)
70        */
71        WebRtcIlbcfix_SortSq(&xq, &index,
72                             (int16_t)toQ32,
73                             WebRtcIlbcfix_kStateSq3, 8);
74      }
75
76      /* Store selected index */
77      (*idxVec++) = index;
78
79      /* Compute decoded sample and update of the prediction filter */
80      tmp16a = ((WebRtcIlbcfix_kStateSq3[index] + 2 ) >> 2);
81
82      *syntOut     = (int16_t) (tmp16a + (int32_t)(*in_weighted) - toQW32);
83
84      n++;
85      syntOut++; in_weighted++;
86    }
87    /* Update perceptual weighting filter at subframe border */
88    weightDenum += 11;
89  }
90}
91