/src/vlc/contrib/contrib-build/speex/libspeex/quant_lsp.c
Line | Count | Source |
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 | 49.5k | #define LSP_LINEAR(i) (.25*(i)+.25) |
62 | 0 | #define LSP_LINEAR_HIGH(i) (.3125*(i)+.75) |
63 | 0 | #define LSP_SCALE 256. |
64 | 33.9k | #define LSP_DIV_256(x) (0.0039062*(x)) |
65 | 33.9k | #define LSP_DIV_512(x) (0.0019531*(x)) |
66 | | #define LSP_DIV_1024(x) (0.00097656*(x)) |
67 | 0 | #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 | 0 | { |
74 | 0 | int i; |
75 | 0 | spx_word16_t tmp1, tmp2; |
76 | 0 | for (i=0;i<order;i++) |
77 | 0 | { |
78 | 0 | if (i==0) |
79 | 0 | tmp1 = qlsp[i]; |
80 | 0 | else |
81 | 0 | tmp1 = qlsp[i]-qlsp[i-1]; |
82 | 0 | if (i==order-1) |
83 | 0 | tmp2 = LSP_PI-qlsp[i]; |
84 | 0 | else |
85 | 0 | tmp2 = qlsp[i+1]-qlsp[i]; |
86 | 0 | if (tmp2<tmp1) |
87 | 0 | tmp1 = tmp2; |
88 | | #ifdef FIXED_POINT |
89 | | quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1)); |
90 | | #else |
91 | 0 | quant_weight[i] = 10/(.04+tmp1); |
92 | 0 | #endif |
93 | 0 | } |
94 | |
|
95 | 0 | } |
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 | 0 | { |
101 | 0 | int i,j; |
102 | 0 | spx_word32_t dist; |
103 | 0 | spx_word16_t tmp; |
104 | 0 | spx_word32_t best_dist=VERY_LARGE32; |
105 | 0 | int best_id=0; |
106 | 0 | const signed char *ptr=cdbk; |
107 | 0 | for (i=0;i<nbVec;i++) |
108 | 0 | { |
109 | 0 | dist=0; |
110 | 0 | for (j=0;j<nbDim;j++) |
111 | 0 | { |
112 | 0 | tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5)); |
113 | 0 | dist=MAC16_16(dist,tmp,tmp); |
114 | 0 | } |
115 | 0 | if (dist<best_dist) |
116 | 0 | { |
117 | 0 | best_dist=dist; |
118 | 0 | best_id=i; |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | 0 | for (j=0;j<nbDim;j++) |
123 | 0 | x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5)); |
124 | |
|
125 | 0 | return best_id; |
126 | 0 | } |
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 | 0 | { |
133 | 0 | int i,j; |
134 | 0 | spx_word32_t dist; |
135 | 0 | spx_word16_t tmp; |
136 | 0 | spx_word32_t best_dist=VERY_LARGE32; |
137 | 0 | int best_id=0; |
138 | 0 | const signed char *ptr=cdbk; |
139 | 0 | for (i=0;i<nbVec;i++) |
140 | 0 | { |
141 | 0 | dist=0; |
142 | 0 | for (j=0;j<nbDim;j++) |
143 | 0 | { |
144 | 0 | tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5)); |
145 | 0 | dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp)); |
146 | 0 | } |
147 | 0 | if (dist<best_dist) |
148 | 0 | { |
149 | 0 | best_dist=dist; |
150 | 0 | best_id=i; |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | 0 | for (j=0;j<nbDim;j++) |
155 | 0 | x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5)); |
156 | 0 | return best_id; |
157 | 0 | } |
158 | | #endif |
159 | | |
160 | | void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits) |
161 | 0 | { |
162 | 0 | int i; |
163 | 0 | int id; |
164 | 0 | spx_word16_t quant_weight[10]; |
165 | |
|
166 | 0 | for (i=0;i<order;i++) |
167 | 0 | qlsp[i]=lsp[i]; |
168 | |
|
169 | 0 | compute_quant_weights(qlsp, quant_weight, order); |
170 | |
|
171 | 0 | for (i=0;i<order;i++) |
172 | 0 | qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i)); |
173 | |
|
174 | 0 | #ifndef FIXED_POINT |
175 | 0 | for (i=0;i<order;i++) |
176 | 0 | qlsp[i] = LSP_SCALE*qlsp[i]; |
177 | 0 | #endif |
178 | 0 | id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order); |
179 | 0 | speex_bits_pack(bits, id, 6); |
180 | |
|
181 | 0 | for (i=0;i<order;i++) |
182 | 0 | qlsp[i]*=2; |
183 | |
|
184 | 0 | id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5); |
185 | 0 | speex_bits_pack(bits, id, 6); |
186 | |
|
187 | 0 | for (i=0;i<5;i++) |
188 | 0 | qlsp[i]*=2; |
189 | |
|
190 | 0 | id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5); |
191 | 0 | speex_bits_pack(bits, id, 6); |
192 | |
|
193 | 0 | id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5); |
194 | 0 | speex_bits_pack(bits, id, 6); |
195 | |
|
196 | 0 | for (i=5;i<10;i++) |
197 | 0 | qlsp[i]*=2; |
198 | |
|
199 | 0 | id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5); |
200 | 0 | 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 | 0 | for (i=0;i<order;i++) |
207 | 0 | qlsp[i]=qlsp[i] * .00097656; |
208 | 0 | #endif |
209 | |
|
210 | 0 | for (i=0;i<order;i++) |
211 | 0 | qlsp[i]=lsp[i]-qlsp[i]; |
212 | 0 | } |
213 | | #endif /* DISABLE_ENCODER */ |
214 | | |
215 | | #ifndef DISABLE_DECODER |
216 | | void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits) |
217 | 1.55k | { |
218 | 1.55k | int i, id; |
219 | 17.1k | for (i=0;i<order;i++) |
220 | 15.5k | lsp[i]=LSP_LINEAR(i); |
221 | | |
222 | | |
223 | 1.55k | id=speex_bits_unpack_unsigned(bits, 6); |
224 | 17.1k | for (i=0;i<10;i++) |
225 | 15.5k | lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i])); |
226 | | |
227 | 1.55k | id=speex_bits_unpack_unsigned(bits, 6); |
228 | 9.35k | for (i=0;i<5;i++) |
229 | 7.79k | lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i])); |
230 | | |
231 | 1.55k | id=speex_bits_unpack_unsigned(bits, 6); |
232 | 9.35k | for (i=0;i<5;i++) |
233 | 7.79k | lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i])); |
234 | | |
235 | 1.55k | id=speex_bits_unpack_unsigned(bits, 6); |
236 | 9.35k | for (i=0;i<5;i++) |
237 | 7.79k | lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i])); |
238 | | |
239 | 1.55k | id=speex_bits_unpack_unsigned(bits, 6); |
240 | 9.35k | for (i=0;i<5;i++) |
241 | 7.79k | lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i])); |
242 | 1.55k | } |
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 | 0 | { |
248 | 0 | int i; |
249 | 0 | int id; |
250 | 0 | spx_word16_t quant_weight[10]; |
251 | |
|
252 | 0 | for (i=0;i<order;i++) |
253 | 0 | qlsp[i]=lsp[i]; |
254 | |
|
255 | 0 | compute_quant_weights(qlsp, quant_weight, order); |
256 | |
|
257 | 0 | for (i=0;i<order;i++) |
258 | 0 | qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i)); |
259 | 0 | #ifndef FIXED_POINT |
260 | 0 | for (i=0;i<order;i++) |
261 | 0 | qlsp[i]=qlsp[i]*LSP_SCALE; |
262 | 0 | #endif |
263 | 0 | id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order); |
264 | 0 | speex_bits_pack(bits, id, 6); |
265 | |
|
266 | 0 | for (i=0;i<order;i++) |
267 | 0 | qlsp[i]*=2; |
268 | |
|
269 | 0 | id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5); |
270 | 0 | speex_bits_pack(bits, id, 6); |
271 | |
|
272 | 0 | id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5); |
273 | 0 | 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 | 0 | for (i=0;i<order;i++) |
280 | 0 | qlsp[i] = qlsp[i]*0.0019531; |
281 | 0 | #endif |
282 | |
|
283 | 0 | for (i=0;i<order;i++) |
284 | 0 | qlsp[i]=lsp[i]-qlsp[i]; |
285 | 0 | } |
286 | | #endif /* DISABLE_ENCODER */ |
287 | | |
288 | | #ifndef DISABLE_DECODER |
289 | | void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits) |
290 | 3.39k | { |
291 | 3.39k | int i, id; |
292 | 37.3k | for (i=0;i<order;i++) |
293 | 33.9k | lsp[i]=LSP_LINEAR(i); |
294 | | |
295 | | |
296 | 3.39k | id=speex_bits_unpack_unsigned(bits, 6); |
297 | 37.3k | for (i=0;i<10;i++) |
298 | 33.9k | lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]); |
299 | | |
300 | 3.39k | id=speex_bits_unpack_unsigned(bits, 6); |
301 | 20.3k | for (i=0;i<5;i++) |
302 | 16.9k | lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]); |
303 | | |
304 | 3.39k | id=speex_bits_unpack_unsigned(bits, 6); |
305 | 20.3k | for (i=0;i<5;i++) |
306 | 16.9k | lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]); |
307 | | |
308 | 3.39k | } |
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 | 0 | { |
318 | 0 | int i; |
319 | 0 | int id; |
320 | 0 | spx_word16_t quant_weight[10]; |
321 | |
|
322 | 0 | for (i=0;i<order;i++) |
323 | 0 | qlsp[i]=lsp[i]; |
324 | |
|
325 | 0 | 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 | 0 | for (i=0;i<order;i++) |
337 | 0 | qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i)); |
338 | 0 | #ifndef FIXED_POINT |
339 | 0 | for (i=0;i<order;i++) |
340 | 0 | qlsp[i] = qlsp[i]*LSP_SCALE; |
341 | 0 | #endif |
342 | 0 | id = lsp_quant(qlsp, high_lsp_cdbk, 64, order); |
343 | 0 | speex_bits_pack(bits, id, 6); |
344 | |
|
345 | 0 | for (i=0;i<order;i++) |
346 | 0 | qlsp[i]*=2; |
347 | |
|
348 | 0 | id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order); |
349 | 0 | 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 | 0 | for (i=0;i<order;i++) |
356 | 0 | qlsp[i] = qlsp[i]*0.0019531; |
357 | 0 | #endif |
358 | |
|
359 | 0 | for (i=0;i<order;i++) |
360 | 0 | qlsp[i]=lsp[i]-qlsp[i]; |
361 | 0 | } |
362 | | #endif /* DISABLE_ENCODER */ |
363 | | |
364 | | |
365 | | #ifndef DISABLE_DECODER |
366 | | void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits) |
367 | 0 | { |
368 | |
|
369 | 0 | int i, id; |
370 | 0 | for (i=0;i<order;i++) |
371 | 0 | lsp[i]=LSP_LINEAR_HIGH(i); |
372 | | |
373 | |
|
374 | 0 | id=speex_bits_unpack_unsigned(bits, 6); |
375 | 0 | for (i=0;i<order;i++) |
376 | 0 | lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]); |
377 | | |
378 | |
|
379 | 0 | id=speex_bits_unpack_unsigned(bits, 6); |
380 | 0 | for (i=0;i<order;i++) |
381 | 0 | lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]); |
382 | 0 | } |
383 | | #endif /* DISABLE_DECODER */ |
384 | | |
385 | | #endif /* DISABLE_WIDEBAND */ |
386 | | |