Welcome to mirror list, hosted at ThFree Co, Russian Federation.

quant_lsp.c « libspeex - github.com/mumble-voip/speex.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c994cbc6b5803bdba24b93a1cf07efae7f973fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Copyright (C) 2002 Jean-Marc Valin 
   File: quant_lsp.c
   LSP vector quantization

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "quant_lsp.h"
#include <math.h>
#include <stdio.h>

static float quant_weight[MAX_LSP_SIZE];

/* Note: x is modified*/
static int lsp_quant(float *x, float *cdbk, int nbVec, int nbDim)
{
   int i,j;
   float dist, tmp;
   float best_dist=0;
   int best_id=0;
   float *ptr=cdbk;
   for (i=0;i<nbVec;i++)
   {
      dist=0;
      for (j=0;j<nbDim;j++)
      {
         tmp=(x[j]-*ptr++);
         dist+=tmp*tmp;
      }
      if (dist<best_dist || i==0)
      {
         best_dist=dist;
         best_id=i;
      }
   }

   for (j=0;j<nbDim;j++)
      x[j] -= cdbk[best_id*nbDim+j];
    
   return best_id;
}

/* Note: x is modified*/
static int lsp_weight_quant(float *x, float *weight, float *cdbk, int nbVec, int nbDim)
{
   int i,j;
   float dist, tmp;
   float best_dist=0;
   int best_id=0;
   float *ptr=cdbk;
   for (i=0;i<nbVec;i++)
   {
      dist=0;
      for (j=0;j<nbDim;j++)
      {
         tmp=(x[j]-*ptr++);
         dist+=weight[j]*tmp*tmp;
      }
      if (dist<best_dist || i==0)
      {
         best_dist=dist;
         best_id=i;
      }
   }
   
   for (j=0;j<nbDim;j++)
      x[j] -= cdbk[best_id*nbDim+j];
   return best_id;
}


void lsp_quant_nb(float *lsp, float *qlsp, int order, FrameBits *bits)
{
   int i;
   float tmp1, tmp2;
   int id;

   for (i=0;i<order;i++)
      qlsp[i]=lsp[i];

   quant_weight[0] = 1/(qlsp[1]-qlsp[0]);
   quant_weight[order-1] = 1/(qlsp[order-1]-qlsp[order-2]);
   for (i=1;i<order-1;i++)
   {
      tmp1 = 1/(qlsp[i]-qlsp[i-1]);
      tmp2 = 1/(qlsp[i+1]-qlsp[i]);
      quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
   }
   id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
   frame_bits_pack(bits, id, 6);

   for (i=0;i<order;i++)
      qlsp[i]=lsp[i]-qlsp[i];
}

void lsp_unquant_nb(float *lsp, int order, FrameBits *bits)
{
   int i, id;
   for (i=0;i<order;i++)
      lsp[i]=0;


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<10;i++)
      lsp[i] += cdbk_nb[id*10+i];

   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<5;i++)
      lsp[i] += cdbk_nb_low1[id*5+i];

   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<5;i++)
      lsp[i] += cdbk_nb_low2[id*5+i];

   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<5;i++)
      lsp[i+5] += cdbk_nb_high1[id*5+i];
   
   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<5;i++)
      lsp[i+5] += cdbk_nb_high2[id*5+i];
}


extern float lsp_cdbk_wb[];
extern float lsp_cdbk_wb11[];
extern float lsp_cdbk_wb12[];
extern float lsp_cdbk_wb21[];
extern float lsp_cdbk_wb22[];
extern float lsp_cdbk_wb31[];
extern float lsp_cdbk_wb32[];
extern float lsp_cdbk_wb41[];
extern float lsp_cdbk_wb42[];

void lsp_quant_wb(float *lsp, float *qlsp, int order, FrameBits *bits)
{
   int i;
   float tmp1, tmp2;
   int id;
   for (i=0;i<order;i++)
      qlsp[i]=lsp[i];

   quant_weight[0] = 1/(qlsp[1]-qlsp[0]);
   quant_weight[order-1] = 1/(qlsp[order-1]-qlsp[order-2]);
   for (i=1;i<order-1;i++)
   {
      tmp1 = 1/(qlsp[i]-qlsp[i-1]);
      tmp2 = 1/(qlsp[i+1]-qlsp[i]);
      quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
   }
   id = lsp_quant(qlsp, lsp_cdbk_wb, 64, order);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp, quant_weight, lsp_cdbk_wb11, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp, quant_weight, lsp_cdbk_wb12, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+4, quant_weight, lsp_cdbk_wb21, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+4, quant_weight, lsp_cdbk_wb22, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+8, quant_weight, lsp_cdbk_wb31, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+8, quant_weight, lsp_cdbk_wb32, 16, 4);
   frame_bits_pack(bits, id, 4);

   id = lsp_weight_quant(qlsp+12, quant_weight, lsp_cdbk_wb41, 64, 4);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp+12, quant_weight, lsp_cdbk_wb42, 16, 4);
   frame_bits_pack(bits, id, 4);

   for (i=0;i<order;i++)
      qlsp[i]=lsp[i]-qlsp[i];
}


void lsp_unquant_wb(float *lsp, int order, FrameBits *bits)
{

   int i, id;
   for (i=0;i<order;i++)
      lsp[i]=0;


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<16;i++)
      lsp[i] += lsp_cdbk_wb[id*16+i];


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i] += lsp_cdbk_wb11[id*4+i];

   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i] += lsp_cdbk_wb12[id*4+i];


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i+4] += lsp_cdbk_wb21[id*4+i];

   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i+4] += lsp_cdbk_wb22[id*4+i];


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i+8] += lsp_cdbk_wb31[id*4+i];

   id=frame_bits_unpack_unsigned(bits, 4);
   for (i=0;i<4;i++)
      lsp[i+8] += lsp_cdbk_wb32[id*4+i];


   id=frame_bits_unpack_unsigned(bits, 6);
   for (i=0;i<4;i++)
      lsp[i+12] += lsp_cdbk_wb41[id*4+i];

   id=frame_bits_unpack_unsigned(bits, 4);
   for (i=0;i<4;i++)
      lsp[i+12] += lsp_cdbk_wb42[id*4+i];

}

extern float high_lsp_cdbk[];
extern float high_lsp_cdbk2[];


void lsp_quant_high(float *lsp, float *qlsp, int order, FrameBits *bits)
{
   int i;
   float tmp1, tmp2;
   int id;
   for (i=0;i<order;i++)
      qlsp[i]=lsp[i];

   quant_weight[0] = 1/(qlsp[1]-qlsp[0]);
   quant_weight[order-1] = 1/(qlsp[order-1]-qlsp[order-2]);
   for (i=1;i<order-1;i++)
   {
      tmp1 = 1/(qlsp[i]-qlsp[i-1]);
      tmp2 = 1/(qlsp[i+1]-qlsp[i]);
      quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
   }
   id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
   frame_bits_pack(bits, id, 6);

   id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
   frame_bits_pack(bits, id, 6);

   for (i=0;i<order;i++)
      qlsp[i]=lsp[i]-qlsp[i];
}