quant_lsp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order)
  57. {
  58. int i;
  59. spx_word16_t tmp1, tmp2;
  60. for (i=0;i<order;i++)
  61. {
  62. if (i==0)
  63. tmp1 = qlsp[i];
  64. else
  65. tmp1 = qlsp[i]-qlsp[i-1];
  66. if (i==order-1)
  67. tmp2 = LSP_PI-qlsp[i];
  68. else
  69. tmp2 = qlsp[i+1]-qlsp[i];
  70. if (tmp2<tmp1)
  71. tmp1 = tmp2;
  72. #ifdef FIXED_POINT
  73. quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1));
  74. #else
  75. quant_weight[i] = 10/(.04+tmp1);
  76. #endif
  77. }
  78. }
  79. /* Note: x is modified*/
  80. #ifndef OVERRIDE_LSP_QUANT
  81. static int lsp_quant(spx_word16_t *x, const signed char *cdbk, int nbVec, int nbDim)
  82. {
  83. int i,j;
  84. spx_word32_t dist;
  85. spx_word16_t tmp;
  86. spx_word32_t best_dist=VERY_LARGE32;
  87. int best_id=0;
  88. const signed char *ptr=cdbk;
  89. for (i=0;i<nbVec;i++)
  90. {
  91. dist=0;
  92. for (j=0;j<nbDim;j++)
  93. {
  94. tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
  95. dist=MAC16_16(dist,tmp,tmp);
  96. }
  97. if (dist<best_dist)
  98. {
  99. best_dist=dist;
  100. best_id=i;
  101. }
  102. }
  103. for (j=0;j<nbDim;j++)
  104. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  105. return best_id;
  106. }
  107. #endif
  108. /* Note: x is modified*/
  109. #ifndef OVERRIDE_LSP_WEIGHT_QUANT
  110. static int lsp_weight_quant(spx_word16_t *x, spx_word16_t *weight, const signed char *cdbk, int nbVec, int nbDim)
  111. {
  112. int i,j;
  113. spx_word32_t dist;
  114. spx_word16_t tmp;
  115. spx_word32_t best_dist=VERY_LARGE32;
  116. int best_id=0;
  117. const signed char *ptr=cdbk;
  118. for (i=0;i<nbVec;i++)
  119. {
  120. dist=0;
  121. for (j=0;j<nbDim;j++)
  122. {
  123. tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
  124. dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp));
  125. }
  126. if (dist<best_dist)
  127. {
  128. best_dist=dist;
  129. best_id=i;
  130. }
  131. }
  132. for (j=0;j<nbDim;j++)
  133. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  134. return best_id;
  135. }
  136. #endif
  137. void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  138. {
  139. int i;
  140. int id;
  141. spx_word16_t quant_weight[10];
  142. for (i=0;i<order;i++)
  143. qlsp[i]=lsp[i];
  144. compute_quant_weights(qlsp, quant_weight, order);
  145. for (i=0;i<order;i++)
  146. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  147. #ifndef FIXED_POINT
  148. for (i=0;i<order;i++)
  149. qlsp[i] = LSP_SCALE*qlsp[i];
  150. #endif
  151. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  152. speex_bits_pack(bits, id, 6);
  153. for (i=0;i<order;i++)
  154. qlsp[i]*=2;
  155. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  156. speex_bits_pack(bits, id, 6);
  157. for (i=0;i<5;i++)
  158. qlsp[i]*=2;
  159. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
  160. speex_bits_pack(bits, id, 6);
  161. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  162. speex_bits_pack(bits, id, 6);
  163. for (i=5;i<10;i++)
  164. qlsp[i]*=2;
  165. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
  166. speex_bits_pack(bits, id, 6);
  167. #ifdef FIXED_POINT
  168. for (i=0;i<order;i++)
  169. qlsp[i]=PSHR16(qlsp[i],2);
  170. #else
  171. for (i=0;i<order;i++)
  172. qlsp[i]=qlsp[i] * .00097656;
  173. #endif
  174. for (i=0;i<order;i++)
  175. qlsp[i]=lsp[i]-qlsp[i];
  176. }
  177. void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
  178. {
  179. int i, id;
  180. for (i=0;i<order;i++)
  181. lsp[i]=LSP_LINEAR(i);
  182. id=speex_bits_unpack_unsigned(bits, 6);
  183. for (i=0;i<10;i++)
  184. lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
  185. id=speex_bits_unpack_unsigned(bits, 6);
  186. for (i=0;i<5;i++)
  187. lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
  188. id=speex_bits_unpack_unsigned(bits, 6);
  189. for (i=0;i<5;i++)
  190. lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
  191. id=speex_bits_unpack_unsigned(bits, 6);
  192. for (i=0;i<5;i++)
  193. lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[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_1024(cdbk_nb_high2[id*5+i]));
  197. }
  198. void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  199. {
  200. int i;
  201. int id;
  202. spx_word16_t quant_weight[10];
  203. for (i=0;i<order;i++)
  204. qlsp[i]=lsp[i];
  205. compute_quant_weights(qlsp, quant_weight, order);
  206. for (i=0;i<order;i++)
  207. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  208. #ifndef FIXED_POINT
  209. for (i=0;i<order;i++)
  210. qlsp[i]=qlsp[i]*LSP_SCALE;
  211. #endif
  212. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  213. speex_bits_pack(bits, id, 6);
  214. for (i=0;i<order;i++)
  215. qlsp[i]*=2;
  216. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  217. speex_bits_pack(bits, id, 6);
  218. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  219. speex_bits_pack(bits, id, 6);
  220. #ifdef FIXED_POINT
  221. for (i=0;i<order;i++)
  222. qlsp[i] = PSHR16(qlsp[i],1);
  223. #else
  224. for (i=0;i<order;i++)
  225. qlsp[i] = qlsp[i]*0.0019531;
  226. #endif
  227. for (i=0;i<order;i++)
  228. qlsp[i]=lsp[i]-qlsp[i];
  229. }
  230. void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
  231. {
  232. int i, id;
  233. for (i=0;i<order;i++)
  234. lsp[i]=LSP_LINEAR(i);
  235. id=speex_bits_unpack_unsigned(bits, 6);
  236. for (i=0;i<10;i++)
  237. lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
  238. id=speex_bits_unpack_unsigned(bits, 6);
  239. for (i=0;i<5;i++)
  240. lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
  241. id=speex_bits_unpack_unsigned(bits, 6);
  242. for (i=0;i<5;i++)
  243. lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
  244. }
  245. #ifdef DISABLE_WIDEBAND
  246. void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  247. {
  248. speex_fatal("Wideband and Ultra-wideband are disabled");
  249. }
  250. void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
  251. {
  252. speex_fatal("Wideband and Ultra-wideband are disabled");
  253. }
  254. #else
  255. extern const signed char high_lsp_cdbk[];
  256. extern const signed char high_lsp_cdbk2[];
  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. void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
  296. {
  297. int i, id;
  298. for (i=0;i<order;i++)
  299. lsp[i]=LSP_LINEAR_HIGH(i);
  300. id=speex_bits_unpack_unsigned(bits, 6);
  301. for (i=0;i<order;i++)
  302. lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]);
  303. id=speex_bits_unpack_unsigned(bits, 6);
  304. for (i=0;i<order;i++)
  305. lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]);
  306. }
  307. #endif