quant_lsp_tm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright (C) 2007 Hong Zhiqian */
  2. /**
  3. @file quant_lsp_tm.h
  4. @author Hong Zhiqian
  5. @brief Various compatibility routines for Speex (TriMedia version)
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. 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.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <ops/custom_defs.h>
  32. #include "profile_tm.h"
  33. #ifdef FIXED_POINT
  34. #define OVERRIDE_COMPUTE_QUANT_WEIGHTS
  35. static void compute_quant_weights(Int16 *qlsp, Int16 *qw, int order)
  36. {
  37. int qlspi, qlspii;
  38. int w1, w2;
  39. int i;
  40. TMDEBUG_ALIGNMEM(qlsp);
  41. TMDEBUG_ALIGNMEM(qw);
  42. COMPUTEQUANTWEIGHTS_START();
  43. --order;
  44. qlspi = (int)qlsp[0];
  45. qlspii = (int)qlsp[1];
  46. w1 = qlspi;
  47. w2 = qlspii - qlspi;
  48. qw[0] = 81920 / (300 + imin(w1,w2));
  49. for ( i=1 ; i<order ; ++i )
  50. { qlspi = qlspii;
  51. qlspii = qlsp[i+1];
  52. w1 = w2;
  53. w2 = qlspii - qlspi;
  54. qw[i] = 81920 / (300 + imin(w1,w2));
  55. }
  56. w1 = LSP_PI - qlspii;
  57. qw[i] = 81920 / (300 + imin(w1,w2));
  58. COMPUTEQUANTWEIGHTS_STOP();
  59. }
  60. #define OVERRIDE_LSP_QUANT
  61. static int lsp_quant(Int16 *x, const signed char *cdbk, int nbVec, int nbDim)
  62. {
  63. register int best_dist=VERY_LARGE32;
  64. register int best_id=0;
  65. register int i, j;
  66. register int dt0, dt1, dt2, dt3;
  67. register int cb0, cb1, cb2, cb3, xx;
  68. register int ptr_inc = nbDim * 3;
  69. register int five = 5;
  70. const signed char *ptr;
  71. TMDEBUG_ALIGNMEM(x);
  72. LSPQUANT_START();
  73. for ( i=0, ptr=cdbk ; i<nbVec ; i+=4, ptr += ptr_inc )
  74. { dt3 = dt2 = dt1 = dt0 = 0;
  75. for ( j=0 ; j <nbDim ; j += 2 )
  76. {
  77. xx = ld32x(x,j>>1);
  78. cb0 = pack16lsb((int)ptr[1], (int)ptr[0]);
  79. cb0 = dualasl(cb0,five);
  80. cb0 = dspidualsub(xx,cb0);
  81. dt0 += ifir16(cb0,cb0);
  82. cb1 = pack16lsb((int)ptr[nbDim+1], (int)ptr[nbDim]);
  83. cb1 = dualasl(cb1,five);
  84. cb1 = dspidualsub(xx,cb1);
  85. dt1 += ifir16(cb1, cb1);
  86. cb2 = pack16lsb((int)ptr[nbDim*2+1], (int)ptr[nbDim*2]);
  87. cb2 = dualasl(cb2,five);
  88. cb2 = dspidualsub(xx,cb2);
  89. dt2 += ifir16(cb2, cb2);
  90. cb3 = pack16lsb((int)ptr[nbDim*3+1], (int)ptr[nbDim*3]);
  91. cb3 = dualasl(cb3,five);
  92. cb3 = dspidualsub(xx,cb3);
  93. dt3 += ifir16(cb3, cb3);
  94. ptr += 2;
  95. }
  96. if ( dt0<best_dist )
  97. { best_dist = dt0;
  98. best_id = i;
  99. }
  100. if ( dt1<best_dist )
  101. { best_dist = dt1;
  102. best_id = i+1;
  103. }
  104. if ( dt2<best_dist )
  105. { best_dist = dt2;
  106. best_id = i+2;
  107. }
  108. if ( dt3<best_dist )
  109. { best_dist = dt3;
  110. best_id = i+3;
  111. }
  112. }
  113. for ( j=0,ptr=cdbk+best_id*nbDim ; j<nbDim ; j+=2 )
  114. { xx = ld32x(x,j>>1);
  115. cb0 = pack16lsb((int)ptr[j+1], (int)ptr[j]);
  116. cb0 = dualasl(cb0,five);
  117. dt0 = dspidualsub(xx,cb0);
  118. st32d(j<<1, x, dt0);
  119. }
  120. LSPQUANT_STOP();
  121. return best_id;
  122. }
  123. #define OVERRIDE_LSP_WEIGHT_QUANT
  124. static int lsp_weight_quant(Int16 *x, Int16 *weight, const signed char *cdbk, int nbVec, int nbDim)
  125. {
  126. register int best_dist=VERY_LARGE32;
  127. register int best_id=0;
  128. register int i, j;
  129. register int dt1, dt2, dt3, dt4;
  130. register int cb1, cb2, cb3, cb4, wt, xx;
  131. register int ptr_inc = nbDim * 3;
  132. const signed char *ptr;
  133. LSPWEIGHTQUANT_START();
  134. for ( i=0, ptr=cdbk ; i<nbVec ; i+=4, ptr += ptr_inc )
  135. { dt4 = dt3 = dt2 = dt1 = 0;
  136. for ( j=0 ; j<nbDim ; ++j )
  137. { wt = weight[j];
  138. xx = x[j];
  139. cb1 = xx - (ptr[0] << 5);
  140. cb2 = xx - (ptr[nbDim] << 5);
  141. cb3 = xx - (ptr[nbDim*2] << 5);
  142. cb4 = xx - (ptr[nbDim*3] << 5);
  143. ++ptr;
  144. cb1 *= cb1;
  145. cb2 *= cb2;
  146. cb3 *= cb3;
  147. cb4 *= cb4;
  148. dt1 += (wt * (cb1 >> 15)) + ((wt * (cb1 & 0x7fff)) >> 15);
  149. dt2 += (wt * (cb2 >> 15)) + ((wt * (cb2 & 0x7fff)) >> 15);
  150. dt3 += (wt * (cb3 >> 15)) + ((wt * (cb3 & 0x7fff)) >> 15);
  151. dt4 += (wt * (cb4 >> 15)) + ((wt * (cb4 & 0x7fff)) >> 15);
  152. }
  153. if ( dt1<best_dist )
  154. { best_dist = dt1;
  155. best_id = i;
  156. }
  157. if ( dt2<best_dist )
  158. { best_dist = dt2;
  159. best_id = i+1;
  160. }
  161. if ( dt3<best_dist )
  162. { best_dist = dt3;
  163. best_id = i+2;
  164. }
  165. if ( dt4<best_dist )
  166. { best_dist = dt4;
  167. best_id = i+3;
  168. }
  169. }
  170. for ( j=0 ; j<nbDim ; ++j )
  171. { x[j] = x[j] - ((Int16)cdbk[best_id*nbDim+j] << 5);
  172. }
  173. LSPWEIGHTQUANT_STOP();
  174. return best_id;
  175. }
  176. #if 0
  177. // TODO: unroll loops
  178. #define OVERRIDE_LSP_QUANT_NB
  179. void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  180. {
  181. int i;
  182. int id;
  183. spx_word16_t quant_weight[10];
  184. for (i=0;i<order;i++)
  185. qlsp[i]=lsp[i];
  186. compute_quant_weights(qlsp, quant_weight, order);
  187. for (i=0;i<order;i++)
  188. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  189. #ifndef FIXED_POINT
  190. for (i=0;i<order;i++)
  191. qlsp[i] = LSP_SCALE*qlsp[i];
  192. #endif
  193. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  194. speex_bits_pack(bits, id, 6);
  195. for (i=0;i<order;i++)
  196. qlsp[i]*=2;
  197. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  198. speex_bits_pack(bits, id, 6);
  199. for (i=0;i<5;i++)
  200. qlsp[i]*=2;
  201. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low2, NB_CDBK_SIZE_LOW2, 5);
  202. speex_bits_pack(bits, id, 6);
  203. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  204. speex_bits_pack(bits, id, 6);
  205. for (i=5;i<10;i++)
  206. qlsp[i]*=2;
  207. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high2, NB_CDBK_SIZE_HIGH2, 5);
  208. speex_bits_pack(bits, id, 6);
  209. #ifdef FIXED_POINT
  210. for (i=0;i<order;i++)
  211. qlsp[i]=PSHR16(qlsp[i],2);
  212. #else
  213. for (i=0;i<order;i++)
  214. qlsp[i]=qlsp[i] * .00097656;
  215. #endif
  216. for (i=0;i<order;i++)
  217. qlsp[i]=lsp[i]-qlsp[i];
  218. }
  219. #define OVERRIDE_LSP_UNQUANT_NB
  220. void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits)
  221. {
  222. int i, id;
  223. for (i=0;i<order;i++)
  224. lsp[i]=LSP_LINEAR(i);
  225. id=speex_bits_unpack_unsigned(bits, 6);
  226. for (i=0;i<10;i++)
  227. lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i]));
  228. id=speex_bits_unpack_unsigned(bits, 6);
  229. for (i=0;i<5;i++)
  230. lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i]));
  231. id=speex_bits_unpack_unsigned(bits, 6);
  232. for (i=0;i<5;i++)
  233. lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i]));
  234. id=speex_bits_unpack_unsigned(bits, 6);
  235. for (i=0;i<5;i++)
  236. lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i]));
  237. id=speex_bits_unpack_unsigned(bits, 6);
  238. for (i=0;i<5;i++)
  239. lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i]));
  240. }
  241. #define OVERRIDE_LSP_QUANT_LBR
  242. void lsp_quant_lbr(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  243. {
  244. int i;
  245. int id;
  246. spx_word16_t quant_weight[10];
  247. for (i=0;i<order;i++)
  248. qlsp[i]=lsp[i];
  249. compute_quant_weights(qlsp, quant_weight, order);
  250. for (i=0;i<order;i++)
  251. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR(i));
  252. #ifndef FIXED_POINT
  253. for (i=0;i<order;i++)
  254. qlsp[i]=qlsp[i]*LSP_SCALE;
  255. #endif
  256. id = lsp_quant(qlsp, cdbk_nb, NB_CDBK_SIZE, order);
  257. speex_bits_pack(bits, id, 6);
  258. for (i=0;i<order;i++)
  259. qlsp[i]*=2;
  260. id = lsp_weight_quant(qlsp, quant_weight, cdbk_nb_low1, NB_CDBK_SIZE_LOW1, 5);
  261. speex_bits_pack(bits, id, 6);
  262. id = lsp_weight_quant(qlsp+5, quant_weight+5, cdbk_nb_high1, NB_CDBK_SIZE_HIGH1, 5);
  263. speex_bits_pack(bits, id, 6);
  264. #ifdef FIXED_POINT
  265. for (i=0;i<order;i++)
  266. qlsp[i] = PSHR16(qlsp[i],1);
  267. #else
  268. for (i=0;i<order;i++)
  269. qlsp[i] = qlsp[i]*0.0019531;
  270. #endif
  271. for (i=0;i<order;i++)
  272. qlsp[i]=lsp[i]-qlsp[i];
  273. }
  274. #define OVERRIDE_LSP_UNQUANT_LBR
  275. void lsp_unquant_lbr(spx_lsp_t *lsp, int order, SpeexBits *bits)
  276. {
  277. int i, id;
  278. for (i=0;i<order;i++)
  279. lsp[i]=LSP_LINEAR(i);
  280. id=speex_bits_unpack_unsigned(bits, 6);
  281. for (i=0;i<10;i++)
  282. lsp[i] += LSP_DIV_256(cdbk_nb[id*10+i]);
  283. id=speex_bits_unpack_unsigned(bits, 6);
  284. for (i=0;i<5;i++)
  285. lsp[i] += LSP_DIV_512(cdbk_nb_low1[id*5+i]);
  286. id=speex_bits_unpack_unsigned(bits, 6);
  287. for (i=0;i<5;i++)
  288. lsp[i+5] += LSP_DIV_512(cdbk_nb_high1[id*5+i]);
  289. }
  290. extern const signed char high_lsp_cdbk[];
  291. extern const signed char high_lsp_cdbk2[];
  292. #define OVERRIDE_LSP_UNQUANT_HIGH
  293. void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits)
  294. {
  295. int i, id;
  296. for (i=0;i<order;i++)
  297. lsp[i]=LSP_LINEAR_HIGH(i);
  298. id=speex_bits_unpack_unsigned(bits, 6);
  299. for (i=0;i<order;i++)
  300. lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]);
  301. id=speex_bits_unpack_unsigned(bits, 6);
  302. for (i=0;i<order;i++)
  303. lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]);
  304. }
  305. #define OVERRIDE_LSP_QUANT_HIGH
  306. void lsp_quant_high(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits)
  307. {
  308. int i;
  309. int id;
  310. spx_word16_t quant_weight[10];
  311. for (i=0;i<order;i++)
  312. qlsp[i]=lsp[i];
  313. compute_quant_weights(qlsp, quant_weight, order);
  314. /* quant_weight[0] = 10/(qlsp[1]-qlsp[0]);
  315. quant_weight[order-1] = 10/(qlsp[order-1]-qlsp[order-2]);
  316. for (i=1;i<order-1;i++)
  317. {
  318. tmp1 = 10/(qlsp[i]-qlsp[i-1]);
  319. tmp2 = 10/(qlsp[i+1]-qlsp[i]);
  320. quant_weight[i] = tmp1 > tmp2 ? tmp1 : tmp2;
  321. }*/
  322. for (i=0;i<order;i++)
  323. qlsp[i]=SUB16(qlsp[i],LSP_LINEAR_HIGH(i));
  324. #ifndef FIXED_POINT
  325. for (i=0;i<order;i++)
  326. qlsp[i] = qlsp[i]*LSP_SCALE;
  327. #endif
  328. id = lsp_quant(qlsp, high_lsp_cdbk, 64, order);
  329. speex_bits_pack(bits, id, 6);
  330. for (i=0;i<order;i++)
  331. qlsp[i]*=2;
  332. id = lsp_weight_quant(qlsp, quant_weight, high_lsp_cdbk2, 64, order);
  333. speex_bits_pack(bits, id, 6);
  334. #ifdef FIXED_POINT
  335. for (i=0;i<order;i++)
  336. qlsp[i] = PSHR16(qlsp[i],1);
  337. #else
  338. for (i=0;i<order;i++)
  339. qlsp[i] = qlsp[i]*0.0019531;
  340. #endif
  341. for (i=0;i<order;i++)
  342. qlsp[i]=lsp[i]-qlsp[i];
  343. }
  344. #endif
  345. #endif