blob: 81d1bfa5c80a16d6a4fd3d05ce3253dbd74558cf [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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_Vq3.c
16
17******************************************************************/
18
19#include "vq3.h"
20#include "constants.h"
21
22/*----------------------------------------------------------------*
23 * vector quantization
24 *---------------------------------------------------------------*/
25
26void WebRtcIlbcfix_Vq3(
27 WebRtc_Word16 *Xq, /* quantized vector (Q13) */
28 WebRtc_Word16 *index,
29 WebRtc_Word16 *CB, /* codebook in Q13 */
30 WebRtc_Word16 *X, /* vector to quantize (Q13) */
31 WebRtc_Word16 n_cb
32 ){
33 WebRtc_Word16 i, j;
34 WebRtc_Word16 pos, minindex=0;
35 WebRtc_Word16 tmp;
36 WebRtc_Word32 dist, mindist;
37
38 pos = 0;
39 mindist = WEBRTC_SPL_WORD32_MAX; /* start value */
40
41 /* Find the codebook with the lowest square distance */
42 for (j = 0; j < n_cb; j++) {
43 tmp = X[0] - CB[pos];
44 dist = WEBRTC_SPL_MUL_16_16(tmp, tmp);
45 for (i = 1; i < 3; i++) {
46 tmp = X[i] - CB[pos + i];
47 dist += WEBRTC_SPL_MUL_16_16(tmp, tmp);
48 }
49
50 if (dist < mindist) {
51 mindist = dist;
52 minindex = j;
53 }
54 pos += 3;
55 }
56
57 /* Store the quantized codebook and the index */
58 for (i = 0; i < 3; i++) {
59 Xq[i] = CB[minindex*3 + i];
60 }
61 *index = minindex;
62
63}