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.
34 #include "config-speex.h"
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 #ifndef SPEEX_DISABLE_ENCODER
72 static void compute_quant_weights(spx_lsp_t
*qlsp
, spx_word16_t
*quant_weight
, int order
)
75 spx_word16_t tmp1
, tmp2
;
81 tmp1
= qlsp
[i
]-qlsp
[i
-1];
83 tmp2
= LSP_PI
-qlsp
[i
];
85 tmp2
= qlsp
[i
+1]-qlsp
[i
];
89 quant_weight
[i
] = DIV32_16(81920,ADD16(300,tmp1
));
91 quant_weight
[i
] = 10/(.04+tmp1
);
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
)
104 spx_word32_t best_dist
=VERY_LARGE32
;
106 const signed char *ptr
=cdbk
;
107 for (i
=0;i
<nbVec
;i
++)
110 for (j
=0;j
<nbDim
;j
++)
112 tmp
=SUB16(x
[j
],SHL16((spx_word16_t
)*ptr
++,5));
113 dist
=MAC16_16(dist
,tmp
,tmp
);
122 for (j
=0;j
<nbDim
;j
++)
123 x
[j
] = SUB16(x
[j
],SHL16((spx_word16_t
)cdbk
[best_id
*nbDim
+j
],5));
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
)
136 spx_word32_t best_dist
=VERY_LARGE32
;
138 const signed char *ptr
=cdbk
;
139 for (i
=0;i
<nbVec
;i
++)
142 for (j
=0;j
<nbDim
;j
++)
144 tmp
=SUB16(x
[j
],SHL16((spx_word16_t
)*ptr
++,5));
145 dist
=MAC16_32_Q15(dist
,weight
[j
],MULT16_16(tmp
,tmp
));
154 for (j
=0;j
<nbDim
;j
++)
155 x
[j
] = SUB16(x
[j
],SHL16((spx_word16_t
)cdbk
[best_id
*nbDim
+j
],5));
160 void lsp_quant_nb(spx_lsp_t
*lsp
, spx_lsp_t
*qlsp
, int order
, SpeexBits
*bits
)
164 spx_word16_t quant_weight
[10];
166 for (i
=0;i
<order
;i
++)
169 compute_quant_weights(qlsp
, quant_weight
, order
);
171 for (i
=0;i
<order
;i
++)
172 qlsp
[i
]=SUB16(qlsp
[i
],LSP_LINEAR(i
));
175 for (i
=0;i
<order
;i
++)
176 qlsp
[i
] = LSP_SCALE
*qlsp
[i
];
178 id
= lsp_quant(qlsp
, cdbk_nb
, NB_CDBK_SIZE
, order
);
179 speex_bits_pack(bits
, id
, 6);
181 for (i
=0;i
<order
;i
++)
184 id
= lsp_weight_quant(qlsp
, quant_weight
, cdbk_nb_low1
, NB_CDBK_SIZE_LOW1
, 5);
185 speex_bits_pack(bits
, id
, 6);
190 id
= lsp_weight_quant(qlsp
, quant_weight
, cdbk_nb_low2
, NB_CDBK_SIZE_LOW2
, 5);
191 speex_bits_pack(bits
, id
, 6);
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);
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);
203 for (i
=0;i
<order
;i
++)
204 qlsp
[i
]=PSHR16(qlsp
[i
],2);
206 for (i
=0;i
<order
;i
++)
207 qlsp
[i
]=qlsp
[i
] * .00097656;
210 for (i
=0;i
<order
;i
++)
211 qlsp
[i
]=lsp
[i
]-qlsp
[i
];
213 #endif /* SPEEX_DISABLE_ENCODER */
215 void lsp_unquant_nb(spx_lsp_t
*lsp
, int order
, SpeexBits
*bits
)
218 for (i
=0;i
<order
;i
++)
219 lsp
[i
]=LSP_LINEAR(i
);
222 id
=speex_bits_unpack_unsigned(bits
, 6);
224 lsp
[i
] = ADD32(lsp
[i
], LSP_DIV_256(cdbk_nb
[id
*10+i
]));
226 id
=speex_bits_unpack_unsigned(bits
, 6);
228 lsp
[i
] = ADD16(lsp
[i
], LSP_DIV_512(cdbk_nb_low1
[id
*5+i
]));
230 id
=speex_bits_unpack_unsigned(bits
, 6);
232 lsp
[i
] = ADD32(lsp
[i
], LSP_DIV_1024(cdbk_nb_low2
[id
*5+i
]));
234 id
=speex_bits_unpack_unsigned(bits
, 6);
236 lsp
[i
+5] = ADD32(lsp
[i
+5], LSP_DIV_512(cdbk_nb_high1
[id
*5+i
]));
238 id
=speex_bits_unpack_unsigned(bits
, 6);
240 lsp
[i
+5] = ADD32(lsp
[i
+5], LSP_DIV_1024(cdbk_nb_high2
[id
*5+i
]));
244 #ifndef SPEEX_DISABLE_ENCODER
245 void lsp_quant_lbr(spx_lsp_t
*lsp
, spx_lsp_t
*qlsp
, int order
, SpeexBits
*bits
)
249 spx_word16_t quant_weight
[10];
251 for (i
=0;i
<order
;i
++)
254 compute_quant_weights(qlsp
, quant_weight
, order
);
256 for (i
=0;i
<order
;i
++)
257 qlsp
[i
]=SUB16(qlsp
[i
],LSP_LINEAR(i
));
259 for (i
=0;i
<order
;i
++)
260 qlsp
[i
]=qlsp
[i
]*LSP_SCALE
;
262 id
= lsp_quant(qlsp
, cdbk_nb
, NB_CDBK_SIZE
, order
);
263 speex_bits_pack(bits
, id
, 6);
265 for (i
=0;i
<order
;i
++)
268 id
= lsp_weight_quant(qlsp
, quant_weight
, cdbk_nb_low1
, NB_CDBK_SIZE_LOW1
, 5);
269 speex_bits_pack(bits
, id
, 6);
271 id
= lsp_weight_quant(qlsp
+5, quant_weight
+5, cdbk_nb_high1
, NB_CDBK_SIZE_HIGH1
, 5);
272 speex_bits_pack(bits
, id
, 6);
275 for (i
=0;i
<order
;i
++)
276 qlsp
[i
] = PSHR16(qlsp
[i
],1);
278 for (i
=0;i
<order
;i
++)
279 qlsp
[i
] = qlsp
[i
]*0.0019531;
282 for (i
=0;i
<order
;i
++)
283 qlsp
[i
]=lsp
[i
]-qlsp
[i
];
285 #endif /* SPEEX_DISABLE_ENCODER */
287 void lsp_unquant_lbr(spx_lsp_t
*lsp
, int order
, SpeexBits
*bits
)
290 for (i
=0;i
<order
;i
++)
291 lsp
[i
]=LSP_LINEAR(i
);
294 id
=speex_bits_unpack_unsigned(bits
, 6);
296 lsp
[i
] += LSP_DIV_256(cdbk_nb
[id
*10+i
]);
298 id
=speex_bits_unpack_unsigned(bits
, 6);
300 lsp
[i
] += LSP_DIV_512(cdbk_nb_low1
[id
*5+i
]);
302 id
=speex_bits_unpack_unsigned(bits
, 6);
304 lsp
[i
+5] += LSP_DIV_512(cdbk_nb_high1
[id
*5+i
]);
309 #ifdef DISABLE_WIDEBAND
310 void lsp_quant_high(spx_lsp_t
*lsp
, spx_lsp_t
*qlsp
, int order
, SpeexBits
*bits
)
312 speex_fatal("Wideband and Ultra-wideband are disabled");
314 void lsp_unquant_high(spx_lsp_t
*lsp
, int order
, SpeexBits
*bits
)
316 speex_fatal("Wideband and Ultra-wideband are disabled");
319 extern const signed char high_lsp_cdbk
[];
320 extern const signed char high_lsp_cdbk2
[];
323 #ifndef SPEEX_DISABLE_ENCODER
324 void lsp_quant_high(spx_lsp_t
*lsp
, spx_lsp_t
*qlsp
, int order
, SpeexBits
*bits
)
328 spx_word16_t quant_weight
[10];
330 for (i
=0;i
<order
;i
++)
333 compute_quant_weights(qlsp
, quant_weight
, order
);
335 /* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
336 quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
337 for (i=1;i<order-1;i++)
339 tmp1 = 10/(qlsp[i]-qlsp[i-1]);
340 tmp2 = 10/(qlsp[i+1]-qlsp[i]);
341 quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
344 for (i
=0;i
<order
;i
++)
345 qlsp
[i
]=SUB16(qlsp
[i
],LSP_LINEAR_HIGH(i
));
347 for (i
=0;i
<order
;i
++)
348 qlsp
[i
] = qlsp
[i
]*LSP_SCALE
;
350 id
= lsp_quant(qlsp
, high_lsp_cdbk
, 64, order
);
351 speex_bits_pack(bits
, id
, 6);
353 for (i
=0;i
<order
;i
++)
356 id
= lsp_weight_quant(qlsp
, quant_weight
, high_lsp_cdbk2
, 64, order
);
357 speex_bits_pack(bits
, id
, 6);
360 for (i
=0;i
<order
;i
++)
361 qlsp
[i
] = PSHR16(qlsp
[i
],1);
363 for (i
=0;i
<order
;i
++)
364 qlsp
[i
] = qlsp
[i
]*0.0019531;
367 for (i
=0;i
<order
;i
++)
368 qlsp
[i
]=lsp
[i
]-qlsp
[i
];
370 #endif /* SPEEX_DISABLE_ENCODER */
372 void lsp_unquant_high(spx_lsp_t
*lsp
, int order
, SpeexBits
*bits
)
376 for (i
=0;i
<order
;i
++)
377 lsp
[i
]=LSP_LINEAR_HIGH(i
);
380 id
=speex_bits_unpack_unsigned(bits
, 6);
381 for (i
=0;i
<order
;i
++)
382 lsp
[i
] += LSP_DIV_256(high_lsp_cdbk
[id
*order
+i
]);
385 id
=speex_bits_unpack_unsigned(bits
, 6);
386 for (i
=0;i
<order
;i
++)
387 lsp
[i
] += LSP_DIV_512(high_lsp_cdbk2
[id
*order
+i
]);