Debian Codegraph @

libspeex/quant_lsp.c

raw
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: quant_lsp.c
3 LSP vector quantization
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "quant_lsp.h"
38 #include "os_support.h"
39 #include <math.h>
40 #ifndef M_PI
41 #define M_PI 3.14159265358979323846
42 #endif
43
44 #include "arch.h"
45
46 #ifdef BFIN_ASM
47 #include "quant_lsp_bfin.h"
48 #endif
49
50 #ifdef FIXED_POINT
51
52 #define LSP_LINEAR(i) (SHL16(i+1,11))
53 #define LSP_LINEAR_HIGH(i) (ADD16(MULT16_16_16(i,2560),6144))
54 #define LSP_DIV_256(x) (SHL16((spx_word16_t)x, 5))
55 #define LSP_DIV_512(x) (SHL16((spx_word16_t)x, 4))
56 #define LSP_DIV_1024(x) (SHL16((spx_word16_t)x, 3))
57 #define LSP_PI 25736
58
59 #else
60
61 #define LSP_LINEAR(i) (.25*(i)+.25)
62 #define LSP_LINEAR_HIGH(i) (.3125*(i)+.75)
63 #define LSP_SCALE 256.
64 #define LSP_DIV_256(x) (0.0039062*(x))
65 #define LSP_DIV_512(x) (0.0019531*(x))
66 #define LSP_DIV_1024(x) (0.00097656*(x))
67 #define LSP_PI M_PI
68
69 #endif
70
71 #ifndef DISABLE_ENCODER
72 static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order)
73 {
74 int i;
75 spx_word16_t tmp1, tmp2;
76 for (i=0;i<order;i++)
77 {
78 if (i==0)
79 tmp1 = qlsp[i];
80 else
81 tmp1 = qlsp[i]-qlsp[i-1];
82 if (i==order-1)
83 tmp2 = LSP_PI-qlsp[i];
84 else
85 tmp2 = qlsp[i+1]-qlsp[i];
86 if (tmp2<tmp1)
87 tmp1 = tmp2;
88 #ifdef FIXED_POINT
89 quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1));
90 #else
91 quant_weight[i] = 10/(.04+tmp1);
92 #endif
93 }
94
95 }
96
97 /* Note: x is modified*/
98 #ifndef OVERRIDE_LSP_QUANT
99 static int lsp_quant(spx_word16_t *x, const signed char *cdbk, int nbVec, int nbDim)
100 {
101 int i,j;
102 spx_word32_t dist;
103 spx_word16_t tmp;
104 spx_word32_t best_dist=VERY_LARGE32;
105 int best_id=0;
106 const signed char *ptr=cdbk;
107 for (i=0;i<nbVec;i++)
108 {
109 dist=0;
110 for (j=0;j<nbDim;j++)
111 {
112 tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
113 dist=MAC16_16(dist,tmp,tmp);
114 }
115 if (dist<best_dist)
116 {
117 best_dist=dist;
118 best_id=i;
119 }
120 }
121
122 for (j=0;j<nbDim;j++)
123 x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
124
125 return best_id;
126 }
127 #endif
128
129 /* Note: x is modified*/
130 #ifndef OVERRIDE_LSP_WEIGHT_QUANT
131 static int lsp_weight_quant(spx_word16_t *x, spx_word16_t *weight, const signed char *cdbk, int nbVec, int nbDim)
132 {
133 int i,j;
134 spx_word32_t dist;
135 spx_word16_t tmp;
136 spx_word32_t best_dist=VERY_LARGE32;
137 int best_id=0;
138 const signed char *ptr=cdbk;
139 for (i=0;i<nbVec;i++)
140 {
141 dist=0;
142 for (j=0;j<nbDim;j++)
143 {
144 tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
145 dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp));
146 }
147 if (dist<best_dist)
148 {
149 best_dist=dist;
150 best_id=i;
151 }
152 }
153
154 for (j=0;j<nbDim;j++)
155 x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
156 return best_id;
157 }
158 #endif
159
160 void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
161 {
162 int i;
163 int id;
164 spx_word16_t quant_weight[10];
165
166 for (i=0;i<order;i++)
167 qlsp[i]=lsp[i];
168
169 compute_quant_weights(qlsp, quant_weight, order);
170
171 for (i=0;i<order;i++)
172 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
173
174 #ifndef FIXED_POINT
175 for (i=0;i<order;i++)
176 qlsp[i] = LSP_SCALE*qlsp[i];
177 #endif
178 id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
179 speex_bits_pack(bits, id, 6);
180
181 for (i=0;i<order;i++)
182 qlsp[i]*=2;
183
184 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
185 speex_bits_pack(bits, id, 6);
186
187 for (i=0;i<5;i++)
188 qlsp[i]*=2;
189
190 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
191 speex_bits_pack(bits, id, 6);
192
193 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
194 speex_bits_pack(bits, id, 6);
195
196 for (i=5;i<10;i++)
197 qlsp[i]*=2;
198
199 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
200 speex_bits_pack(bits, id, 6);
201
202 #ifdef FIXED_POINT
203 for (i=0;i<order;i++)
204 qlsp[i]=PSHR16(qlsp[i],2);
205 #else
206 for (i=0;i<order;i++)
207 qlsp[i]=qlsp[i] * .00097656;
208 #endif
209
210 for (i=0;i<order;i++)
211 qlsp[i]=lsp[i]-qlsp[i];
212 }
213 #endif /* DISABLE_ENCODER */
214
215 #ifndef DISABLE_DECODER
216 void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
217 {
218 int i, id;
219 for (i=0;i<order;i++)
220 lsp[i]=LSP_LINEAR(i);
221
222
223 id=speex_bits_unpack_unsigned(bits, 6);
224 for (i=0;i<10;i++)
225 lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
226
227 id=speex_bits_unpack_unsigned(bits, 6);
228 for (i=0;i<5;i++)
229 lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
230
231 id=speex_bits_unpack_unsigned(bits, 6);
232 for (i=0;i<5;i++)
233 lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
234
235 id=speex_bits_unpack_unsigned(bits, 6);
236 for (i=0;i<5;i++)
237 lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i]));
238
239 id=speex_bits_unpack_unsigned(bits, 6);
240 for (i=0;i<5;i++)
241 lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i]));
242 }
243 #endif /* DISABLE_DECODER */
244
245 #ifndef DISABLE_ENCODER
246 void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
247 {
248 int i;
249 int id;
250 spx_word16_t quant_weight[10];
251
252 for (i=0;i<order;i++)
253 qlsp[i]=lsp[i];
254
255 compute_quant_weights(qlsp, quant_weight, order);
256
257 for (i=0;i<order;i++)
258 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
259 #ifndef FIXED_POINT
260 for (i=0;i<order;i++)
261 qlsp[i]=qlsp[i]*LSP_SCALE;
262 #endif
263 id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
264 speex_bits_pack(bits, id, 6);
265
266 for (i=0;i<order;i++)
267 qlsp[i]*=2;
268
269 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
270 speex_bits_pack(bits, id, 6);
271
272 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
273 speex_bits_pack(bits, id, 6);
274
275 #ifdef FIXED_POINT
276 for (i=0;i<order;i++)
277 qlsp[i] = PSHR16(qlsp[i],1);
278 #else
279 for (i=0;i<order;i++)
280 qlsp[i] = qlsp[i]*0.0019531;
281 #endif
282
283 for (i=0;i<order;i++)
284 qlsp[i]=lsp[i]-qlsp[i];
285 }
286 #endif /* DISABLE_ENCODER */
287
288 #ifndef DISABLE_DECODER
289 void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
290 {
291 int i, id;
292 for (i=0;i<order;i++)
293 lsp[i]=LSP_LINEAR(i);
294
295
296 id=speex_bits_unpack_unsigned(bits, 6);
297 for (i=0;i<10;i++)
298 lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
299
300 id=speex_bits_unpack_unsigned(bits, 6);
301 for (i=0;i<5;i++)
302 lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
303
304 id=speex_bits_unpack_unsigned(bits, 6);
305 for (i=0;i<5;i++)
306 lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
307
308 }
309 #endif /* DISABLE_DECODER */
310
311 #ifndef DISABLE_WIDEBAND
312 extern const signed char high_lsp_cdbk[];
313 extern const signed char high_lsp_cdbk2[];
314
315 #ifndef DISABLE_ENCODER
316 void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
317 {
318 int i;
319 int id;
320 spx_word16_t quant_weight[10];
321
322 for (i=0;i<order;i++)
323 qlsp[i]=lsp[i];
324
325 compute_quant_weights(qlsp, quant_weight, order);
326
327 /* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
328 quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
329 for (i=1;i<order-1;i++)
330 {
331 tmp1 = 10/(qlsp[i]-qlsp[i-1]);
332 tmp2 = 10/(qlsp[i+1]-qlsp[i]);
333 quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
334 }*/
335
336 for (i=0;i<order;i++)
337 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i));
338 #ifndef FIXED_POINT
339 for (i=0;i<order;i++)
340 qlsp[i] = qlsp[i]*LSP_SCALE;
341 #endif
342 id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
343 speex_bits_pack(bits, id, 6);
344
345 for (i=0;i<order;i++)
346 qlsp[i]*=2;
347
348 id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
349 speex_bits_pack(bits, id, 6);
350
351 #ifdef FIXED_POINT
352 for (i=0;i<order;i++)
353 qlsp[i] = PSHR16(qlsp[i],1);
354 #else
355 for (i=0;i<order;i++)
356 qlsp[i] = qlsp[i]*0.0019531;
357 #endif
358
359 for (i=0;i<order;i++)
360 qlsp[i]=lsp[i]-qlsp[i];
361 }
362 #endif /* DISABLE_ENCODER */
363
364
365 #ifndef DISABLE_DECODER
366 void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
367 {
368
369 int i, id;
370 for (i=0;i<order;i++)
371 lsp[i]=LSP_LINEAR_HIGH(i);
372
373
374 id=speex_bits_unpack_unsigned(bits, 6);
375 for (i=0;i<order;i++)
376 lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]);
377
378
379 id=speex_bits_unpack_unsigned(bits, 6);
380 for (i=0;i<order;i++)
381 lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]);
382 }
383 #endif /* DISABLE_DECODER */
384
385 #endif /* DISABLE_WIDEBAND */
386