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_SortSq.c
16 
17 ******************************************************************/
18 
19 #include "defines.h"
20 
21 /*----------------------------------------------------------------*
22  *  scalar quantization
23  *---------------------------------------------------------------*/
24 
WebRtcIlbcfix_SortSq(int16_t * xq,int16_t * index,int16_t x,const int16_t * cb,int16_t cb_size)25 void WebRtcIlbcfix_SortSq(
26     int16_t *xq,   /* (o) the quantized value */
27     int16_t *index,  /* (o) the quantization index */
28     int16_t x,   /* (i) the value to quantize */
29     const int16_t *cb, /* (i) the quantization codebook */
30     int16_t cb_size  /* (i) the size of the quantization codebook */
31                           ){
32   int i;
33 
34   if (x <= cb[0]) {
35     *index = 0;
36     *xq = cb[0];
37   } else {
38     i = 0;
39     while ((x > cb[i]) && (i < (cb_size-1))) {
40       i++;
41     }
42 
43     if (x > (((int32_t)cb[i] + cb[i - 1] + 1) >> 1)) {
44       *index = i;
45       *xq = cb[i];
46     } else {
47       *index = i - 1;
48       *xq = cb[i - 1];
49     }
50   }
51 }
52