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