quant_lsp.c 9.6 KB

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