1 /* Copyright (C) 2002 Jean-Marc Valin
3 LSP vector quantization
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 #include "quant_lsp.h"
40 #define M_PI 3.14159265358979323846
48 #define LSP_LINEAR(i) (SHL16(i+1,11))
49 #define LSP_LINEAR_HIGH(i) (ADD16(MULT16_16_16(i,2560),6144))
50 #define LSP_DIV_256(x) (SHL16((spx_word16_t)x, 5))
51 #define LSP_DIV_512(x) (SHL16((spx_word16_t)x, 4))
52 #define LSP_DIV_1024(x) (SHL16((spx_word16_t)x, 3))
57 #define LSP_LINEAR(i) (.25*(i)+.25)
58 #define LSP_LINEAR_HIGH(i) (.3125*(i)+.75)
59 #define LSP_SCALE 256.
60 #define LSP_DIV_256(x) (0.0039062*(x))
61 #define LSP_DIV_512(x) (0.0019531*(x))
62 #define LSP_DIV_1024(x) (0.00097656*(x))
67 static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order)
70 spx_word16_t tmp1, tmp2;
76 tmp1 = qlsp[i]-qlsp[i-1];
78 tmp2 = LSP_PI-qlsp[i];
80 tmp2 = qlsp[i+1]-qlsp[i];
84 quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1));
86 quant_weight[i] = 10/(.04+tmp1);
92 /* Note: x is modified*/
93 static int lsp_quant(spx_word16_t *x, const signed char *cdbk, int nbVec, int nbDim)
98 spx_word32_t best_dist=0;
100 const signed char *ptr=cdbk;
101 for (i=0;i<nbVec;i++)
104 for (j=0;j<nbDim;j++)
106 tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
107 dist=MAC16_16(dist,tmp,tmp);
109 if (dist<best_dist || i==0)
116 for (j=0;j<nbDim;j++)
117 x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
122 /* Note: x is modified*/
123 static int lsp_weight_quant(spx_word16_t *x, spx_word16_t *weight, const signed char *cdbk, int nbVec, int nbDim)
128 spx_word32_t best_dist=0;
130 const signed char *ptr=cdbk;
131 for (i=0;i<nbVec;i++)
134 for (j=0;j<nbDim;j++)
136 tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
137 dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp));
139 if (dist<best_dist || i==0)
146 for (j=0;j<nbDim;j++)
147 x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
152 void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
156 spx_word16_t quant_weight[10];
158 for (i=0;i<order;i++)
161 compute_quant_weights(qlsp, quant_weight, order);
163 for (i=0;i<order;i++)
164 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
167 for (i=0;i<order;i++)
168 qlsp[i] = LSP_SCALE*qlsp[i];
170 id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
171 speex_bits_pack(bits, id, 6);
173 for (i=0;i<order;i++)
176 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
177 speex_bits_pack(bits, id, 6);
182 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
183 speex_bits_pack(bits, id, 6);
185 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
186 speex_bits_pack(bits, id, 6);
191 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
192 speex_bits_pack(bits, id, 6);
195 for (i=0;i<order;i++)
196 qlsp[i]=PSHR16(qlsp[i],2);
198 for (i=0;i<order;i++)
199 qlsp[i]=qlsp[i] * .00097656;
202 for (i=0;i<order;i++)
203 qlsp[i]=lsp[i]-qlsp[i];
206 void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
209 for (i=0;i<order;i++)
210 lsp[i]=LSP_LINEAR(i);
213 id=speex_bits_unpack_unsigned(bits, 6);
215 lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
217 id=speex_bits_unpack_unsigned(bits, 6);
219 lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
221 id=speex_bits_unpack_unsigned(bits, 6);
223 lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
225 id=speex_bits_unpack_unsigned(bits, 6);
227 lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i]));
229 id=speex_bits_unpack_unsigned(bits, 6);
231 lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i]));
235 void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
239 spx_word16_t quant_weight[10];
241 for (i=0;i<order;i++)
244 compute_quant_weights(qlsp, quant_weight, order);
246 for (i=0;i<order;i++)
247 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
249 for (i=0;i<order;i++)
250 qlsp[i]=qlsp[i]*LSP_SCALE;
252 id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
253 speex_bits_pack(bits, id, 6);
255 for (i=0;i<order;i++)
258 id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
259 speex_bits_pack(bits, id, 6);
261 id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
262 speex_bits_pack(bits, id, 6);
265 for (i=0;i<order;i++)
266 qlsp[i] = PSHR16(qlsp[i],1);
268 for (i=0;i<order;i++)
269 qlsp[i] = qlsp[i]*0.0019531;
272 for (i=0;i<order;i++)
273 qlsp[i]=lsp[i]-qlsp[i];
276 void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
279 for (i=0;i<order;i++)
280 lsp[i]=LSP_LINEAR(i);
283 id=speex_bits_unpack_unsigned(bits, 6);
285 lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
287 id=speex_bits_unpack_unsigned(bits, 6);
289 lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
291 id=speex_bits_unpack_unsigned(bits, 6);
293 lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
298 #ifdef DISABLE_WIDEBAND
299 void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
301 speex_error("Wideband and Ultra-wideband are disabled");
303 void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
305 speex_error("Wideband and Ultra-wideband are disabled");
308 extern const signed char high_lsp_cdbk[];
309 extern const signed char high_lsp_cdbk2[];
312 void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
316 spx_word16_t quant_weight[10];
318 for (i=0;i<order;i++)
321 compute_quant_weights(qlsp, quant_weight, order);
323 /* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
324 quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
325 for (i=1;i<order-1;i++)
327 tmp1 = 10/(qlsp[i]-qlsp[i-1]);
328 tmp2 = 10/(qlsp[i+1]-qlsp[i]);
329 quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
332 for (i=0;i<order;i++)
333 qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i));
335 for (i=0;i<order;i++)
336 qlsp[i] = qlsp[i]*LSP_SCALE;
338 id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
339 speex_bits_pack(bits, id, 6);
341 for (i=0;i<order;i++)
344 id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
345 speex_bits_pack(bits, id, 6);
348 for (i=0;i<order;i++)
349 qlsp[i] = PSHR16(qlsp[i],1);
351 for (i=0;i<order;i++)
352 qlsp[i] = qlsp[i]*0.0019531;
355 for (i=0;i<order;i++)
356 qlsp[i]=lsp[i]-qlsp[i];
359 void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
363 for (i=0;i<order;i++)
364 lsp[i]=LSP_LINEAR_HIGH(i);
367 id=speex_bits_unpack_unsigned(bits, 6);
368 for (i=0;i<order;i++)
369 lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]);
372 id=speex_bits_unpack_unsigned(bits, 6);
373 for (i=0;i<order;i++)
374 lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]);
382 extern const signed char cdbk_lsp_vlbr[5120];
383 extern const signed char cdbk_lsp2_vlbr[160];
385 void lsp_quant_48k(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
389 spx_word16_t quant_weight[10];
391 for (i=0;i<order;i++)
394 compute_quant_weights(qlsp, quant_weight, order);
396 for (i=0;i<order;i++)
397 qlsp[i]=SUB16(qlsp[i],LSP_SCALING*(.25*i+.3125));
399 for (i=0;i<order;i++)
400 qlsp[i] = qlsp[i]*LSP_SCALE;
403 id = lsp_quant(qlsp, cdbk_lsp_vlbr, 512, order);
404 speex_bits_pack(bits, id, 9);
406 for (i=0;i<order;i++)
409 id = lsp_weight_quant(qlsp, quant_weight, cdbk_lsp2_vlbr, 16, 10);
410 speex_bits_pack(bits, id, 4);
413 for (i=0;i<order;i++)
414 qlsp[i]=PSHR(qlsp[i],2);
416 for (i=0;i<order;i++)
417 qlsp[i]=qlsp[i]*0.00097655;
420 for (i=0;i<order;i++)
421 qlsp[i]=lsp[i]-qlsp[i];
424 void lsp_unquant_48k(spx_lsp_t *lsp, int order, SpeexBits *bits)
427 for (i=0;i<order;i++)
428 lsp[i]=LSP_SCALING*(.25*i+.3125);
431 id=speex_bits_unpack_unsigned(bits, 9);
433 lsp[i] += LSP_SCALING*0.0039062*cdbk_lsp_vlbr[id*10+i];
435 id=speex_bits_unpack_unsigned(bits, 4);
437 lsp[i] += LSP_SCALING*0.00097655*cdbk_lsp2_vlbr[id*10+i];