quant_lsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <math.h>
  32. #ifndef M_PI
  33. #define M_PI 3.14159265358979323846
  34. #endif
  35. #include "misc.h"
  36. #ifdef FIXED_POINT
  37. #define LSP_LINEAR(i) (SHL16(i+1,11))
  38. #define LSP_LINEAR_HIGH(i) (ADD16(MULT16_16_16(i,2560),6144))
  39. #define LSP_DIV_256(x) (SHL16((spx_word16_t)x, 5))
  40. #define LSP_DIV_512(x) (SHL16((spx_word16_t)x, 4))
  41. #define LSP_DIV_1024(x) (SHL16((spx_word16_t)x, 3))
  42. #define LSP_PI 25736
  43. #else
  44. #define LSP_LINEAR(i) (.25*(i)+.25)
  45. #define LSP_LINEAR_HIGH(i) (.3125*(i)+.75)
  46. #define LSP_SCALE 256.
  47. #define LSP_DIV_256(x) (0.0039062*(x))
  48. #define LSP_DIV_512(x) (0.0019531*(x))
  49. #define LSP_DIV_1024(x) (0.00097656*(x))
  50. #define LSP_PI M_PI
  51. #endif
  52. static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order)
  53. {
  54. int i;
  55. spx_word16_t tmp1, tmp2;
  56. for (i=0;i<order;i++)
  57. {
  58. if (i==0)
  59. tmp1 = qlsp[i];
  60. else
  61. tmp1 = qlsp[i]-qlsp[i-1];
  62. if (i==order-1)
  63. tmp2 = LSP_PI-qlsp[i];
  64. else
  65. tmp2 = qlsp[i+1]-qlsp[i];
  66. if (tmp2<tmp1)
  67. tmp1 = tmp2;
  68. #ifdef FIXED_POINT
  69. quant_weight[i] = DIV32_16(81920,ADD16(300,tmp1));
  70. #else
  71. quant_weight[i] = 10/(.04+tmp1);
  72. #endif
  73. }
  74. }
  75. /* Note: x is modified*/
  76. static int lsp_quant(spx_word16_t *x, const signed char *cdbk, int nbVec, int nbDim)
  77. {
  78. int i,j;
  79. spx_word32_t dist;
  80. spx_word16_t tmp;
  81. spx_word32_t best_dist=0;
  82. int best_id=0;
  83. const signed char *ptr=cdbk;
  84. for (i=0;i<nbVec;i++)
  85. {
  86. dist=0;
  87. for (j=0;j<nbDim;j++)
  88. {
  89. tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
  90. dist=MAC16_16(dist,tmp,tmp);
  91. }
  92. if (dist<best_dist || i==0)
  93. {
  94. best_dist=dist;
  95. best_id=i;
  96. }
  97. }
  98. for (j=0;j<nbDim;j++)
  99. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  100. return best_id;
  101. }
  102. /* Note: x is modified*/
  103. static int lsp_weight_quant(spx_word16_t *x, spx_word16_t *weight, const signed char *cdbk, int nbVec, int nbDim)
  104. {
  105. int i,j;
  106. spx_word32_t dist;
  107. spx_word16_t tmp;
  108. spx_word32_t best_dist=0;
  109. int best_id=0;
  110. const signed char *ptr=cdbk;
  111. for (i=0;i<nbVec;i++)
  112. {
  113. dist=0;
  114. for (j=0;j<nbDim;j++)
  115. {
  116. tmp=SUB16(x[j],SHL16((spx_word16_t)*ptr++,5));
  117. dist=MAC16_32_Q15(dist,weight[j],MULT16_16(tmp,tmp));
  118. }
  119. if (dist<best_dist || i==0)
  120. {
  121. best_dist=dist;
  122. best_id=i;
  123. }
  124. }
  125. for (j=0;j<nbDim;j++)
  126. x[j] = SUB16(x[j],SHL16((spx_word16_t)cdbk[best_id*nbDim+j],5));
  127. return best_id;
  128. }
  129. void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  130. {
  131. int i;
  132. int id;
  133. spx_word16_t quant_weight[10];
  134. for (i=0;i<order;i++)
  135. qlsp[i]=lsp[i];
  136. compute_quant_weights(qlsp, quant_weight, order);
  137. for (i=0;i<order;i++)
  138. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  139. #ifndef FIXED_POINT
  140. for (i=0;i<order;i++)
  141. qlsp[i] = LSP_SCALE*qlsp[i];
  142. #endif
  143. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  144. speex_bits_pack(bits, id, 6);
  145. for (i=0;i<order;i++)
  146. qlsp[i]*=2;
  147. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  148. speex_bits_pack(bits, id, 6);
  149. for (i=0;i<5;i++)
  150. qlsp[i]*=2;
  151. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
  152. speex_bits_pack(bits, id, 6);
  153. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  154. speex_bits_pack(bits, id, 6);
  155. for (i=5;i<10;i++)
  156. qlsp[i]*=2;
  157. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
  158. speex_bits_pack(bits, id, 6);
  159. #ifdef FIXED_POINT
  160. for (i=0;i<order;i++)
  161. qlsp[i]=PSHR16(qlsp[i],2);
  162. #else
  163. for (i=0;i<order;i++)
  164. qlsp[i]=qlsp[i] * .00097656;
  165. #endif
  166. for (i=0;i<order;i++)
  167. qlsp[i]=lsp[i]-qlsp[i];
  168. }
  169. void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
  170. {
  171. int i, id;
  172. for (i=0;i<order;i++)
  173. lsp[i]=LSP_LINEAR(i);
  174. id=speex_bits_unpack_unsigned(bits, 6);
  175. for (i=0;i<10;i++)
  176. lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
  177. id=speex_bits_unpack_unsigned(bits, 6);
  178. for (i=0;i<5;i++)
  179. lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
  180. id=speex_bits_unpack_unsigned(bits, 6);
  181. for (i=0;i<5;i++)
  182. lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
  183. id=speex_bits_unpack_unsigned(bits, 6);
  184. for (i=0;i<5;i++)
  185. lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i]));
  186. id=speex_bits_unpack_unsigned(bits, 6);
  187. for (i=0;i<5;i++)
  188. lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i]));
  189. }
  190. void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  191. {
  192. int i;
  193. int id;
  194. spx_word16_t quant_weight[10];
  195. for (i=0;i<order;i++)
  196. qlsp[i]=lsp[i];
  197. compute_quant_weights(qlsp, quant_weight, order);
  198. for (i=0;i<order;i++)
  199. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  200. #ifndef FIXED_POINT
  201. for (i=0;i<order;i++)
  202. qlsp[i]=qlsp[i]*LSP_SCALE;
  203. #endif
  204. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  205. speex_bits_pack(bits, id, 6);
  206. for (i=0;i<order;i++)
  207. qlsp[i]*=2;
  208. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  209. speex_bits_pack(bits, id, 6);
  210. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  211. speex_bits_pack(bits, id, 6);
  212. #ifdef FIXED_POINT
  213. for (i=0;i<order;i++)
  214. qlsp[i] = PSHR16(qlsp[i],1);
  215. #else
  216. for (i=0;i<order;i++)
  217. qlsp[i] = qlsp[i]*0.0019531;
  218. #endif
  219. for (i=0;i<order;i++)
  220. qlsp[i]=lsp[i]-qlsp[i];
  221. }
  222. void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
  223. {
  224. int i, id;
  225. for (i=0;i<order;i++)
  226. lsp[i]=LSP_LINEAR(i);
  227. id=speex_bits_unpack_unsigned(bits, 6);
  228. for (i=0;i<10;i++)
  229. lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
  230. id=speex_bits_unpack_unsigned(bits, 6);
  231. for (i=0;i<5;i++)
  232. lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
  233. id=speex_bits_unpack_unsigned(bits, 6);
  234. for (i=0;i<5;i++)
  235. lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
  236. }
  237. #ifdef DISABLE_WIDEBAND
  238. void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  239. {
  240. speex_error("Wideband and Ultra-wideband are disabled");
  241. }
  242. void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
  243. {
  244. speex_error("Wideband and Ultra-wideband are disabled");
  245. }
  246. #else
  247. extern const signed char high_lsp_cdbk[];
  248. extern const signed char high_lsp_cdbk2[];
  249. void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  250. {
  251. int i;
  252. int id;
  253. spx_word16_t quant_weight[10];
  254. for (i=0;i<order;i++)
  255. qlsp[i]=lsp[i];
  256. compute_quant_weights(qlsp, quant_weight, order);
  257. /* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
  258. quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
  259. for (i=1;i<order-1;i++)
  260. {
  261. tmp1 = 10/(qlsp[i]-qlsp[i-1]);
  262. tmp2 = 10/(qlsp[i+1]-qlsp[i]);
  263. quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
  264. }*/
  265. for (i=0;i<order;i++)
  266. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i));
  267. #ifndef FIXED_POINT
  268. for (i=0;i<order;i++)
  269. qlsp[i] = qlsp[i]*LSP_SCALE;
  270. #endif
  271. id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
  272. speex_bits_pack(bits, id, 6);
  273. for (i=0;i<order;i++)
  274. qlsp[i]*=2;
  275. id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
  276. speex_bits_pack(bits, id, 6);
  277. #ifdef FIXED_POINT
  278. for (i=0;i<order;i++)
  279. qlsp[i] = PSHR16(qlsp[i],1);
  280. #else
  281. for (i=0;i<order;i++)
  282. qlsp[i] = qlsp[i]*0.0019531;
  283. #endif
  284. for (i=0;i<order;i++)
  285. qlsp[i]=lsp[i]-qlsp[i];
  286. }
  287. void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
  288. {
  289. int i, id;
  290. for (i=0;i<order;i++)
  291. lsp[i]=LSP_LINEAR_HIGH(i);
  292. id=speex_bits_unpack_unsigned(bits, 6);
  293. for (i=0;i<order;i++)
  294. lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]);
  295. id=speex_bits_unpack_unsigned(bits, 6);
  296. for (i=0;i<order;i++)
  297. lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]);
  298. }
  299. #endif
  300. #ifdef EPIC_48K
  301. extern const signed char cdbk_lsp_vlbr[5120];
  302. extern const signed char cdbk_lsp2_vlbr[160];
  303. void lsp_quant_48k(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  304. {
  305. int i;
  306. int id;
  307. spx_word16_t quant_weight[10];
  308. for (i=0;i<order;i++)
  309. qlsp[i]=lsp[i];
  310. compute_quant_weights(qlsp, quant_weight, order);
  311. for (i=0;i<order;i++)
  312. qlsp[i]=SUB16(qlsp[i],LSP_SCALING*(.25*i+.3125));
  313. #ifndef FIXED_POINT
  314. for (i=0;i<order;i++)
  315. qlsp[i] = qlsp[i]*LSP_SCALE;
  316. #endif
  317. id = lsp_quant(qlsp, cdbk_lsp_vlbr, 512, order);
  318. speex_bits_pack(bits, id, 9);
  319. for (i=0;i<order;i++)
  320. qlsp[i]*=4;
  321. id = lsp_weight_quant(qlsp, quant_weight, cdbk_lsp2_vlbr, 16, 10);
  322. speex_bits_pack(bits, id, 4);
  323. #ifdef FIXED_POINT
  324. for (i=0;i<order;i++)
  325. qlsp[i]=PSHR(qlsp[i],2);
  326. #else
  327. for (i=0;i<order;i++)
  328. qlsp[i]=qlsp[i]*0.00097655;
  329. #endif
  330. for (i=0;i<order;i++)
  331. qlsp[i]=lsp[i]-qlsp[i];
  332. }
  333. void lsp_unquant_48k(spx_lsp_t *lsp, int order, SpeexBits *bits)
  334. {
  335. int i, id;
  336. for (i=0;i<order;i++)
  337. lsp[i]=LSP_SCALING*(.25*i+.3125);
  338. id=speex_bits_unpack_unsigned(bits, 9);
  339. for (i=0;i<10;i++)
  340. lsp[i] += LSP_SCALING*0.0039062*cdbk_lsp_vlbr[id*10+i];
  341. id=speex_bits_unpack_unsigned(bits, 4);
  342. for (i=0;i<10;i++)
  343. lsp[i] += LSP_SCALING*0.00097655*cdbk_lsp2_vlbr[id*10+i];
  344. }
  345. #endif
粤ICP备19079148号