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"
38
#include "os_support.h"
41
#define M_PI 3.14159265358979323846
47
#include "quant_lsp_bfin.h"
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))
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))
71
static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order)
74
spx_word16_t tmp1, tmp2;
80
tmp1 = qlsp[i]-qlsp[i-1];
82
tmp2 = LSP_PI-qlsp[i];
84
tmp2 = qlsp[i+1]-qlsp[i];
88
quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1));
90
quant_weight[i] = 10/(.04+tmp1);
96
/* Note: x is modified*/
97
#ifndef OVERRIDE_LSP_QUANT
98
static int lsp_quant(spx_word16_t *x, const signed char *cdbk, int nbVec, int nbDim)
103
spx_word32_t best_dist=VERY_LARGE32;
105
const signed char *ptr=cdbk;
106
for (i=0;i<nbVec;i++)
109
for (j=0;j<nbDim;j++)
111
tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
112
dist=MAC16_16(dist,tmp,tmp);
121
for (j=0;j<nbDim;j++)
122
x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
128
/* Note: x is modified*/
129
#ifndef OVERRIDE_LSP_WEIGHT_QUANT
130
static int lsp_weight_quant(spx_word16_t *x, spx_word16_t *weight, const signed char *cdbk, int nbVec, int nbDim)
135
spx_word32_t best_dist=VERY_LARGE32;
137
const signed char *ptr=cdbk;
138
for (i=0;i<nbVec;i++)
141
for (j=0;j<nbDim;j++)
143
tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
144
dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp));
153
for (j=0;j<nbDim;j++)
154
x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
159
void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
163
spx_word16_t quant_weight[10];
165
for (i=0;i<order;i++)
168
compute_quant_weights(qlsp, quant_weight, order);
170
for (i=0;i<order;i++)
171
qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
174
for (i=0;i<order;i++)
175
qlsp[i] = LSP_SCALE*qlsp[i];
177
id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
178
speex_bits_pack(bits, id, 6);
180
for (i=0;i<order;i++)
183
id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
184
speex_bits_pack(bits, id, 6);
189
id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
190
speex_bits_pack(bits, id, 6);
192
id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
193
speex_bits_pack(bits, id, 6);
198
id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
199
speex_bits_pack(bits, id, 6);
202
for (i=0;i<order;i++)
203
qlsp[i]=PSHR16(qlsp[i],2);
205
for (i=0;i<order;i++)
206
qlsp[i]=qlsp[i] * .00097656;
209
for (i=0;i<order;i++)
210
qlsp[i]=lsp[i]-qlsp[i];
213
void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
216
for (i=0;i<order;i++)
217
lsp[i]=LSP_LINEAR(i);
220
id=speex_bits_unpack_unsigned(bits, 6);
222
lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
224
id=speex_bits_unpack_unsigned(bits, 6);
226
lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
228
id=speex_bits_unpack_unsigned(bits, 6);
230
lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
232
id=speex_bits_unpack_unsigned(bits, 6);
234
lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i]));
236
id=speex_bits_unpack_unsigned(bits, 6);
238
lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i]));
242
void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
246
spx_word16_t quant_weight[10];
248
for (i=0;i<order;i++)
251
compute_quant_weights(qlsp, quant_weight, order);
253
for (i=0;i<order;i++)
254
qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
256
for (i=0;i<order;i++)
257
qlsp[i]=qlsp[i]*LSP_SCALE;
259
id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
260
speex_bits_pack(bits, id, 6);
262
for (i=0;i<order;i++)
265
id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
266
speex_bits_pack(bits, id, 6);
268
id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
269
speex_bits_pack(bits, id, 6);
272
for (i=0;i<order;i++)
273
qlsp[i] = PSHR16(qlsp[i],1);
275
for (i=0;i<order;i++)
276
qlsp[i] = qlsp[i]*0.0019531;
279
for (i=0;i<order;i++)
280
qlsp[i]=lsp[i]-qlsp[i];
283
void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
286
for (i=0;i<order;i++)
287
lsp[i]=LSP_LINEAR(i);
290
id=speex_bits_unpack_unsigned(bits, 6);
292
lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
294
id=speex_bits_unpack_unsigned(bits, 6);
296
lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
298
id=speex_bits_unpack_unsigned(bits, 6);
300
lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
305
#ifdef DISABLE_WIDEBAND
306
void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
308
speex_fatal("Wideband and Ultra-wideband are disabled");
310
void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
312
speex_fatal("Wideband and Ultra-wideband are disabled");
315
extern const signed char high_lsp_cdbk[];
316
extern const signed char high_lsp_cdbk2[];
319
void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
323
spx_word16_t quant_weight[10];
325
for (i=0;i<order;i++)
328
compute_quant_weights(qlsp, quant_weight, order);
330
/* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
331
quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
332
for (i=1;i<order-1;i++)
334
tmp1 = 10/(qlsp[i]-qlsp[i-1]);
335
tmp2 = 10/(qlsp[i+1]-qlsp[i]);
336
quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
339
for (i=0;i<order;i++)
340
qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i));
342
for (i=0;i<order;i++)
343
qlsp[i] = qlsp[i]*LSP_SCALE;
345
id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
346
speex_bits_pack(bits, id, 6);
348
for (i=0;i<order;i++)
351
id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
352
speex_bits_pack(bits, id, 6);
355
for (i=0;i<order;i++)
356
qlsp[i] = PSHR16(qlsp[i],1);
358
for (i=0;i<order;i++)
359
qlsp[i] = qlsp[i]*0.0019531;
362
for (i=0;i<order;i++)
363
qlsp[i]=lsp[i]-qlsp[i];
366
void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
370
for (i=0;i<order;i++)
371
lsp[i]=LSP_LINEAR_HIGH(i);
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]);
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]);