dwt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
  9. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifdef __SSE__
  34. #include <xmmintrin.h>
  35. #endif
  36. #include "opj_includes.h"
  37. /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
  38. /*@{*/
  39. #define WS(i) v->mem[(i)*2]
  40. #define WD(i) v->mem[(1+(i)*2)]
  41. /** @name Local data structures */
  42. /*@{*/
  43. typedef struct dwt_local {
  44. int* mem;
  45. int dn;
  46. int sn;
  47. int cas;
  48. } dwt_t;
  49. typedef union {
  50. float f[4];
  51. } v4;
  52. typedef struct v4dwt_local {
  53. v4* wavelet ;
  54. int dn ;
  55. int sn ;
  56. int cas ;
  57. } v4dwt_t ;
  58. static const float dwt_alpha = 1.586134342f; /* 12994 */
  59. static const float dwt_beta = 0.052980118f; /* 434 */
  60. static const float dwt_gamma = -0.882911075f; /* -7233 */
  61. static const float dwt_delta = -0.443506852f; /* -3633 */
  62. static const float K = 1.230174105f; /* 10078 */
  63. /* FIXME: What is this constant? */
  64. static const float c13318 = 1.625732422f;
  65. /*@}*/
  66. /**
  67. Virtual function type for wavelet transform in 1-D
  68. */
  69. typedef void (*DWT1DFN)(dwt_t* v);
  70. /** @name Local static functions */
  71. /*@{*/
  72. /**
  73. Forward lazy transform (horizontal)
  74. */
  75. static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas);
  76. /**
  77. Forward lazy transform (vertical)
  78. */
  79. static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas);
  80. /**
  81. Inverse lazy transform (horizontal)
  82. */
  83. static void dwt_interleave_h(dwt_t* h, int *a);
  84. /**
  85. Inverse lazy transform (vertical)
  86. */
  87. static void dwt_interleave_v(dwt_t* v, int *a, int x);
  88. /**
  89. Forward 5-3 wavelet transform in 1-D
  90. */
  91. static void dwt_encode_1(int *a, int dn, int sn, int cas);
  92. /**
  93. Inverse 5-3 wavelet transform in 1-D
  94. */
  95. static void dwt_decode_1(dwt_t *v);
  96. /**
  97. Forward 9-7 wavelet transform in 1-D
  98. */
  99. static void dwt_encode_1_real(int *a, int dn, int sn, int cas);
  100. /**
  101. Explicit calculation of the Quantization Stepsizes
  102. */
  103. static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize);
  104. /**
  105. Inverse wavelet transform in 2-D.
  106. */
  107. static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
  108. /*@}*/
  109. /*@}*/
  110. #define S(i) a[(i)*2]
  111. #define D(i) a[(1+(i)*2)]
  112. #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
  113. #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
  114. /* new */
  115. #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
  116. #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
  117. /* <summary> */
  118. /* This table contains the norms of the 5-3 wavelets for different bands. */
  119. /* </summary> */
  120. static const double dwt_norms[4][10] = {
  121. {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
  122. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  123. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  124. {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
  125. };
  126. /* <summary> */
  127. /* This table contains the norms of the 9-7 wavelets for different bands. */
  128. /* </summary> */
  129. static const double dwt_norms_real[4][10] = {
  130. {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
  131. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  132. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  133. {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
  134. };
  135. /*
  136. ==========================================================
  137. local functions
  138. ==========================================================
  139. */
  140. /* <summary> */
  141. /* Forward lazy transform (horizontal). */
  142. /* </summary> */
  143. static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
  144. int i;
  145. for (i=0; i<sn; i++) b[i]=a[2*i+cas];
  146. for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
  147. }
  148. /* <summary> */
  149. /* Forward lazy transform (vertical). */
  150. /* </summary> */
  151. static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
  152. int i;
  153. for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
  154. for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
  155. }
  156. /* <summary> */
  157. /* Inverse lazy transform (horizontal). */
  158. /* </summary> */
  159. static void dwt_interleave_h(dwt_t* h, int *a) {
  160. int *ai = a;
  161. int *bi = h->mem + h->cas;
  162. int i = h->sn;
  163. while( i-- ) {
  164. *bi = *(ai++);
  165. bi += 2;
  166. }
  167. ai = a + h->sn;
  168. bi = h->mem + 1 - h->cas;
  169. i = h->dn ;
  170. while( i-- ) {
  171. *bi = *(ai++);
  172. bi += 2;
  173. }
  174. }
  175. /* <summary> */
  176. /* Inverse lazy transform (vertical). */
  177. /* </summary> */
  178. static void dwt_interleave_v(dwt_t* v, int *a, int x) {
  179. int *ai = a;
  180. int *bi = v->mem + v->cas;
  181. int i = v->sn;
  182. while( i-- ) {
  183. *bi = *ai;
  184. bi += 2;
  185. ai += x;
  186. }
  187. ai = a + (v->sn * x);
  188. bi = v->mem + 1 - v->cas;
  189. i = v->dn ;
  190. while( i-- ) {
  191. *bi = *ai;
  192. bi += 2;
  193. ai += x;
  194. }
  195. }
  196. /* <summary> */
  197. /* Forward 5-3 wavelet transform in 1-D. */
  198. /* </summary> */
  199. static void dwt_encode_1(int *a, int dn, int sn, int cas) {
  200. int i;
  201. if (!cas) {
  202. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  203. for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
  204. for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
  205. }
  206. } else {
  207. if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
  208. S(0) *= 2;
  209. else {
  210. for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
  211. for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
  212. }
  213. }
  214. }
  215. /* <summary> */
  216. /* Inverse 5-3 wavelet transform in 1-D. */
  217. /* </summary> */
  218. static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
  219. int i;
  220. if (!cas) {
  221. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  222. for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
  223. for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
  224. }
  225. } else {
  226. if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
  227. S(0) /= 2;
  228. else {
  229. for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
  230. for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
  231. }
  232. }
  233. }
  234. /* <summary> */
  235. /* Inverse 5-3 wavelet transform in 1-D. */
  236. /* </summary> */
  237. static void dwt_decode_1(dwt_t *v) {
  238. dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
  239. }
  240. /* <summary> */
  241. /* Forward 9-7 wavelet transform in 1-D. */
  242. /* </summary> */
  243. static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
  244. int i;
  245. if (!cas) {
  246. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  247. for (i = 0; i < dn; i++)
  248. D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
  249. for (i = 0; i < sn; i++)
  250. S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
  251. for (i = 0; i < dn; i++)
  252. D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
  253. for (i = 0; i < sn; i++)
  254. S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
  255. for (i = 0; i < dn; i++)
  256. D(i) = fix_mul(D(i), 5038); /*5038 */
  257. for (i = 0; i < sn; i++)
  258. S(i) = fix_mul(S(i), 6659); /*6660 */
  259. }
  260. } else {
  261. if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
  262. for (i = 0; i < dn; i++)
  263. S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
  264. for (i = 0; i < sn; i++)
  265. D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
  266. for (i = 0; i < dn; i++)
  267. S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
  268. for (i = 0; i < sn; i++)
  269. D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
  270. for (i = 0; i < dn; i++)
  271. S(i) = fix_mul(S(i), 5038); /*5038 */
  272. for (i = 0; i < sn; i++)
  273. D(i) = fix_mul(D(i), 6659); /*6660 */
  274. }
  275. }
  276. }
  277. static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) {
  278. int p, n;
  279. p = int_floorlog2(stepsize) - 13;
  280. n = 11 - int_floorlog2(stepsize);
  281. bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
  282. bandno_stepsize->expn = numbps - p;
  283. }
  284. /*
  285. ==========================================================
  286. DWT interface
  287. ==========================================================
  288. */
  289. /* <summary> */
  290. /* Forward 5-3 wavelet transform in 2-D. */
  291. /* </summary> */
  292. void dwt_encode(opj_tcd_tilecomp_t * tilec) {
  293. int i, j, k;
  294. int *a = NULL;
  295. int *aj = NULL;
  296. int *bj = NULL;
  297. int w, l;
  298. w = tilec->x1-tilec->x0;
  299. l = tilec->numresolutions-1;
  300. a = tilec->data;
  301. for (i = 0; i < l; i++) {
  302. int rw; /* width of the resolution level computed */
  303. int rh; /* height of the resolution level computed */
  304. int rw1; /* width of the resolution level once lower than computed one */
  305. int rh1; /* height of the resolution level once lower than computed one */
  306. int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
  307. int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
  308. int dn, sn;
  309. rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
  310. rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
  311. rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
  312. rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
  313. cas_row = tilec->resolutions[l - i].x0 % 2;
  314. cas_col = tilec->resolutions[l - i].y0 % 2;
  315. sn = rh1;
  316. dn = rh - rh1;
  317. bj = (int*)opj_malloc(rh * sizeof(int));
  318. for (j = 0; j < rw; j++) {
  319. aj = a + j;
  320. for (k = 0; k < rh; k++) bj[k] = aj[k*w];
  321. dwt_encode_1(bj, dn, sn, cas_col);
  322. dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
  323. }
  324. opj_free(bj);
  325. sn = rw1;
  326. dn = rw - rw1;
  327. bj = (int*)opj_malloc(rw * sizeof(int));
  328. for (j = 0; j < rh; j++) {
  329. aj = a + j * w;
  330. for (k = 0; k < rw; k++) bj[k] = aj[k];
  331. dwt_encode_1(bj, dn, sn, cas_row);
  332. dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
  333. }
  334. opj_free(bj);
  335. }
  336. }
  337. /* <summary> */
  338. /* Inverse 5-3 wavelet transform in 2-D. */
  339. /* </summary> */
  340. void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) {
  341. dwt_decode_tile(tilec, numres, &dwt_decode_1);
  342. }
  343. /* <summary> */
  344. /* Get gain of 5-3 wavelet transform. */
  345. /* </summary> */
  346. int dwt_getgain(int orient) {
  347. if (orient == 0)
  348. return 0;
  349. if (orient == 1 || orient == 2)
  350. return 1;
  351. return 2;
  352. }
  353. /* <summary> */
  354. /* Get norm of 5-3 wavelet. */
  355. /* </summary> */
  356. double dwt_getnorm(int level, int orient) {
  357. return dwt_norms[orient][level];
  358. }
  359. /* <summary> */
  360. /* Forward 9-7 wavelet transform in 2-D. */
  361. /* </summary> */
  362. void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
  363. int i, j, k;
  364. int *a = NULL;
  365. int *aj = NULL;
  366. int *bj = NULL;
  367. int w, l;
  368. w = tilec->x1-tilec->x0;
  369. l = tilec->numresolutions-1;
  370. a = tilec->data;
  371. for (i = 0; i < l; i++) {
  372. int rw; /* width of the resolution level computed */
  373. int rh; /* height of the resolution level computed */
  374. int rw1; /* width of the resolution level once lower than computed one */
  375. int rh1; /* height of the resolution level once lower than computed one */
  376. int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
  377. int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
  378. int dn, sn;
  379. rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
  380. rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
  381. rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
  382. rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
  383. cas_row = tilec->resolutions[l - i].x0 % 2;
  384. cas_col = tilec->resolutions[l - i].y0 % 2;
  385. sn = rh1;
  386. dn = rh - rh1;
  387. bj = (int*)opj_malloc(rh * sizeof(int));
  388. for (j = 0; j < rw; j++) {
  389. aj = a + j;
  390. for (k = 0; k < rh; k++) bj[k] = aj[k*w];
  391. dwt_encode_1_real(bj, dn, sn, cas_col);
  392. dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
  393. }
  394. opj_free(bj);
  395. sn = rw1;
  396. dn = rw - rw1;
  397. bj = (int*)opj_malloc(rw * sizeof(int));
  398. for (j = 0; j < rh; j++) {
  399. aj = a + j * w;
  400. for (k = 0; k < rw; k++) bj[k] = aj[k];
  401. dwt_encode_1_real(bj, dn, sn, cas_row);
  402. dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
  403. }
  404. opj_free(bj);
  405. }
  406. }
  407. /* <summary> */
  408. /* Get gain of 9-7 wavelet transform. */
  409. /* </summary> */
  410. int dwt_getgain_real(int orient) {
  411. (void)orient;
  412. return 0;
  413. }
  414. /* <summary> */
  415. /* Get norm of 9-7 wavelet. */
  416. /* </summary> */
  417. double dwt_getnorm_real(int level, int orient) {
  418. return dwt_norms_real[orient][level];
  419. }
  420. void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
  421. int numbands, bandno;
  422. numbands = 3 * tccp->numresolutions - 2;
  423. for (bandno = 0; bandno < numbands; bandno++) {
  424. double stepsize;
  425. int resno, level, orient, gain;
  426. resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
  427. orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
  428. level = tccp->numresolutions - 1 - resno;
  429. gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
  430. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  431. stepsize = 1.0;
  432. } else {
  433. double norm = dwt_norms_real[orient][level];
  434. stepsize = (1 << (gain)) / norm;
  435. }
  436. dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
  437. }
  438. }
  439. /* <summary> */
  440. /* Determine maximum computed resolution level for inverse wavelet transform */
  441. /* </summary> */
  442. static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
  443. int mr = 1;
  444. int w;
  445. while( --i ) {
  446. r++;
  447. if( mr < ( w = r->x1 - r->x0 ) )
  448. mr = w ;
  449. if( mr < ( w = r->y1 - r->y0 ) )
  450. mr = w ;
  451. }
  452. return mr ;
  453. }
  454. /* <summary> */
  455. /* Inverse wavelet transform in 2-D. */
  456. /* </summary> */
  457. static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) {
  458. dwt_t h;
  459. dwt_t v;
  460. opj_tcd_resolution_t* tr = tilec->resolutions;
  461. int rw = tr->x1 - tr->x0; /* width of the resolution level computed */
  462. int rh = tr->y1 - tr->y0; /* height of the resolution level computed */
  463. int w = tilec->x1 - tilec->x0;
  464. h.mem = (int*)opj_aligned_malloc(dwt_decode_max_resolution(tr, numres) * sizeof(int));
  465. v.mem = h.mem;
  466. while( --numres) {
  467. int * restrict tiledp = tilec->data;
  468. int j;
  469. ++tr;
  470. h.sn = rw;
  471. v.sn = rh;
  472. rw = tr->x1 - tr->x0;
  473. rh = tr->y1 - tr->y0;
  474. h.dn = rw - h.sn;
  475. h.cas = tr->x0 % 2;
  476. for(j = 0; j < rh; ++j) {
  477. dwt_interleave_h(&h, &tiledp[j*w]);
  478. (dwt_1D)(&h);
  479. memcpy(&tiledp[j*w], h.mem, rw * sizeof(int));
  480. }
  481. v.dn = rh - v.sn;
  482. v.cas = tr->y0 % 2;
  483. for(j = 0; j < rw; ++j){
  484. int k;
  485. dwt_interleave_v(&v, &tiledp[j], w);
  486. (dwt_1D)(&v);
  487. for(k = 0; k < rh; ++k) {
  488. tiledp[k * w + j] = v.mem[k];
  489. }
  490. }
  491. }
  492. opj_aligned_free(h.mem);
  493. }
  494. static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size){
  495. float* restrict bi = (float*) (w->wavelet + w->cas);
  496. int count = w->sn;
  497. int i, k;
  498. for(k = 0; k < 2; ++k){
  499. if (count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0) {
  500. /* Fast code path */
  501. for(i = 0; i < count; ++i){
  502. int j = i;
  503. bi[i*8 ] = a[j];
  504. j += x;
  505. bi[i*8 + 1] = a[j];
  506. j += x;
  507. bi[i*8 + 2] = a[j];
  508. j += x;
  509. bi[i*8 + 3] = a[j];
  510. }
  511. } else {
  512. /* Slow code path */
  513. for(i = 0; i < count; ++i){
  514. int j = i;
  515. bi[i*8 ] = a[j];
  516. j += x;
  517. if(j > size) continue;
  518. bi[i*8 + 1] = a[j];
  519. j += x;
  520. if(j > size) continue;
  521. bi[i*8 + 2] = a[j];
  522. j += x;
  523. if(j > size) continue;
  524. bi[i*8 + 3] = a[j];
  525. }
  526. }
  527. bi = (float*) (w->wavelet + 1 - w->cas);
  528. a += w->sn;
  529. size -= w->sn;
  530. count = w->dn;
  531. }
  532. }
  533. static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x){
  534. v4* restrict bi = v->wavelet + v->cas;
  535. int i;
  536. for(i = 0; i < v->sn; ++i){
  537. memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float));
  538. }
  539. a += v->sn * x;
  540. bi = v->wavelet + 1 - v->cas;
  541. for(i = 0; i < v->dn; ++i){
  542. memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float));
  543. }
  544. }
  545. #ifdef __SSE__
  546. static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
  547. __m128* restrict vw = (__m128*) w;
  548. int i;
  549. /* 4x unrolled loop */
  550. for(i = 0; i < count >> 2; ++i){
  551. *vw = _mm_mul_ps(*vw, c);
  552. vw += 2;
  553. *vw = _mm_mul_ps(*vw, c);
  554. vw += 2;
  555. *vw = _mm_mul_ps(*vw, c);
  556. vw += 2;
  557. *vw = _mm_mul_ps(*vw, c);
  558. vw += 2;
  559. }
  560. count &= 3;
  561. for(i = 0; i < count; ++i){
  562. *vw = _mm_mul_ps(*vw, c);
  563. vw += 2;
  564. }
  565. }
  566. static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
  567. __m128* restrict vl = (__m128*) l;
  568. __m128* restrict vw = (__m128*) w;
  569. int i;
  570. __m128 tmp1, tmp2, tmp3;
  571. tmp1 = vl[0];
  572. for(i = 0; i < m; ++i){
  573. tmp2 = vw[-1];
  574. tmp3 = vw[ 0];
  575. vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
  576. tmp1 = tmp3;
  577. vw += 2;
  578. }
  579. vl = vw - 2;
  580. if(m >= k){
  581. return;
  582. }
  583. c = _mm_add_ps(c, c);
  584. c = _mm_mul_ps(c, vl[0]);
  585. for(; m < k; ++m){
  586. __m128 tmp = vw[-1];
  587. vw[-1] = _mm_add_ps(tmp, c);
  588. vw += 2;
  589. }
  590. }
  591. #else
  592. static void v4dwt_decode_step1(v4* w, int count, const float c){
  593. float* restrict fw = (float*) w;
  594. int i;
  595. for(i = 0; i < count; ++i){
  596. float tmp1 = fw[i*8 ];
  597. float tmp2 = fw[i*8 + 1];
  598. float tmp3 = fw[i*8 + 2];
  599. float tmp4 = fw[i*8 + 3];
  600. fw[i*8 ] = tmp1 * c;
  601. fw[i*8 + 1] = tmp2 * c;
  602. fw[i*8 + 2] = tmp3 * c;
  603. fw[i*8 + 3] = tmp4 * c;
  604. }
  605. }
  606. static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
  607. float* restrict fl = (float*) l;
  608. float* restrict fw = (float*) w;
  609. int i;
  610. for(i = 0; i < m; ++i){
  611. float tmp1_1 = fl[0];
  612. float tmp1_2 = fl[1];
  613. float tmp1_3 = fl[2];
  614. float tmp1_4 = fl[3];
  615. float tmp2_1 = fw[-4];
  616. float tmp2_2 = fw[-3];
  617. float tmp2_3 = fw[-2];
  618. float tmp2_4 = fw[-1];
  619. float tmp3_1 = fw[0];
  620. float tmp3_2 = fw[1];
  621. float tmp3_3 = fw[2];
  622. float tmp3_4 = fw[3];
  623. fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
  624. fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
  625. fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
  626. fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
  627. fl = fw;
  628. fw += 8;
  629. }
  630. if(m < k){
  631. float c1;
  632. float c2;
  633. float c3;
  634. float c4;
  635. c += c;
  636. c1 = fl[0] * c;
  637. c2 = fl[1] * c;
  638. c3 = fl[2] * c;
  639. c4 = fl[3] * c;
  640. for(; m < k; ++m){
  641. float tmp1 = fw[-4];
  642. float tmp2 = fw[-3];
  643. float tmp3 = fw[-2];
  644. float tmp4 = fw[-1];
  645. fw[-4] = tmp1 + c1;
  646. fw[-3] = tmp2 + c2;
  647. fw[-2] = tmp3 + c3;
  648. fw[-1] = tmp4 + c4;
  649. fw += 8;
  650. }
  651. }
  652. }
  653. #endif
  654. /* <summary> */
  655. /* Inverse 9-7 wavelet transform in 1-D. */
  656. /* </summary> */
  657. static void v4dwt_decode(v4dwt_t* restrict dwt){
  658. int a, b;
  659. if(dwt->cas == 0) {
  660. if(!((dwt->dn > 0) || (dwt->sn > 1))){
  661. return;
  662. }
  663. a = 0;
  664. b = 1;
  665. }else{
  666. if(!((dwt->sn > 0) || (dwt->dn > 1))) {
  667. return;
  668. }
  669. a = 1;
  670. b = 0;
  671. }
  672. #ifdef __SSE__
  673. v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K));
  674. v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318));
  675. v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_delta));
  676. v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_gamma));
  677. v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_beta));
  678. v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_alpha));
  679. #else
  680. v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K);
  681. v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318);
  682. v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta);
  683. v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma);
  684. v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta);
  685. v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha);
  686. #endif
  687. }
  688. /* <summary> */
  689. /* Inverse 9-7 wavelet transform in 2-D. */
  690. /* </summary> */
  691. void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
  692. v4dwt_t h;
  693. v4dwt_t v;
  694. opj_tcd_resolution_t* res = tilec->resolutions;
  695. int rw = res->x1 - res->x0; /* width of the resolution level computed */
  696. int rh = res->y1 - res->y0; /* height of the resolution level computed */
  697. int w = tilec->x1 - tilec->x0;
  698. h.wavelet = (v4*) opj_aligned_malloc((dwt_decode_max_resolution(res, numres)+5) * sizeof(v4));
  699. v.wavelet = h.wavelet;
  700. while( --numres) {
  701. float * restrict aj = (float*) tilec->data;
  702. int bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
  703. int j;
  704. h.sn = rw;
  705. v.sn = rh;
  706. ++res;
  707. rw = res->x1 - res->x0; /* width of the resolution level computed */
  708. rh = res->y1 - res->y0; /* height of the resolution level computed */
  709. h.dn = rw - h.sn;
  710. h.cas = res->x0 % 2;
  711. for(j = rh; j > 3; j -= 4){
  712. int k;
  713. v4dwt_interleave_h(&h, aj, w, bufsize);
  714. v4dwt_decode(&h);
  715. for(k = rw; --k >= 0;){
  716. aj[k ] = h.wavelet[k].f[0];
  717. aj[k+w ] = h.wavelet[k].f[1];
  718. aj[k+w*2] = h.wavelet[k].f[2];
  719. aj[k+w*3] = h.wavelet[k].f[3];
  720. }
  721. aj += w*4;
  722. bufsize -= w*4;
  723. }
  724. if (rh & 0x03) {
  725. int k;
  726. j = rh & 0x03;
  727. v4dwt_interleave_h(&h, aj, w, bufsize);
  728. v4dwt_decode(&h);
  729. for(k = rw; --k >= 0;){
  730. switch(j) {
  731. case 3: aj[k+w*2] = h.wavelet[k].f[2];
  732. case 2: aj[k+w ] = h.wavelet[k].f[1];
  733. case 1: aj[k ] = h.wavelet[k].f[0];
  734. }
  735. }
  736. }
  737. v.dn = rh - v.sn;
  738. v.cas = res->y0 % 2;
  739. aj = (float*) tilec->data;
  740. for(j = rw; j > 3; j -= 4){
  741. int k;
  742. v4dwt_interleave_v(&v, aj, w);
  743. v4dwt_decode(&v);
  744. for(k = 0; k < rh; ++k){
  745. memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
  746. }
  747. aj += 4;
  748. }
  749. if (rw & 0x03){
  750. int k;
  751. j = rw & 0x03;
  752. v4dwt_interleave_v(&v, aj, w);
  753. v4dwt_decode(&v);
  754. for(k = 0; k < rh; ++k){
  755. memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
  756. }
  757. }
  758. }
  759. opj_aligned_free(h.wavelet);
  760. }