dwt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 <[email protected]>
  9. * Copyright (c) 2007, Callum Lerwick <[email protected]>
  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. #if SSE2NEON
  34. # define SSE2NEON_ALLOC_DEFINED
  35. # include "sse2neon.h"
  36. /* Since we emulate SSE with NEON, let's make sure __SSE__ is defined */
  37. # if !defined(__SSE__)
  38. # define __SSE__ 1
  39. # endif
  40. #else
  41. # include <immintrin.h>
  42. #endif
  43. /* MSVC does not define __SSE__... */
  44. #if !defined(__SSE__) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1))
  45. # define __SSE__ 1
  46. #endif
  47. #include "opj_includes.h"
  48. /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
  49. /*@{*/
  50. /** @name Local data structures */
  51. /*@{*/
  52. typedef struct dwt_local {
  53. int* mem;
  54. int dn;
  55. int sn;
  56. int cas;
  57. } dwt_t;
  58. typedef union {
  59. float f[4];
  60. } v4;
  61. typedef struct v4dwt_local {
  62. v4* wavelet ;
  63. int dn ;
  64. int sn ;
  65. int cas ;
  66. } v4dwt_t ;
  67. static const float dwt_alpha = 1.586134342f; // 12994
  68. static const float dwt_beta = 0.052980118f; // 434
  69. static const float dwt_gamma = -0.882911075f; // -7233
  70. static const float dwt_delta = -0.443506852f; // -3633
  71. static const float K = 1.230174105f; // 10078
  72. /* FIXME: What is this constant? */
  73. static const float c13318 = 1.625732422f;
  74. /*@}*/
  75. /**
  76. Virtual function type for wavelet transform in 1-D
  77. */
  78. typedef void (*DWT1DFN)(dwt_t* v);
  79. /** @name Local static functions */
  80. /*@{*/
  81. /**
  82. Forward lazy transform (horizontal)
  83. */
  84. static void dwt_deinterleave_h(int* a, int* b, int dn, int sn, int cas);
  85. /**
  86. Forward lazy transform (vertical)
  87. */
  88. static void dwt_deinterleave_v(int* a, int* b, int dn, int sn, int x, int cas);
  89. /**
  90. Inverse lazy transform (horizontal)
  91. */
  92. static void dwt_interleave_h(dwt_t* h, int* a);
  93. /**
  94. Inverse lazy transform (vertical)
  95. */
  96. static void dwt_interleave_v(dwt_t* v, int* a, int x);
  97. /**
  98. Forward 5-3 wavelet transform in 1-D
  99. */
  100. static void dwt_encode_1(int* a, int dn, int sn, int cas);
  101. /**
  102. Inverse 5-3 wavelet transform in 1-D
  103. */
  104. static void dwt_decode_1(dwt_t* v);
  105. /**
  106. Forward 9-7 wavelet transform in 1-D
  107. */
  108. static void dwt_encode_1_real(int* a, int dn, int sn, int cas);
  109. /**
  110. Explicit calculation of the Quantization Stepsizes
  111. */
  112. static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t* bandno_stepsize);
  113. /**
  114. Inverse wavelet transform in 2-D.
  115. */
  116. static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
  117. /*@}*/
  118. /*@}*/
  119. #define S(i) a[(i)*2]
  120. #define D(i) a[(1+(i)*2)]
  121. #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
  122. #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
  123. /* new */
  124. #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
  125. #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
  126. /* <summary> */
  127. /* This table contains the norms of the 5-3 wavelets for different bands. */
  128. /* </summary> */
  129. static const double dwt_norms[4][10] = {
  130. {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
  131. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  132. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  133. {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
  134. };
  135. /* <summary> */
  136. /* This table contains the norms of the 9-7 wavelets for different bands. */
  137. /* </summary> */
  138. static const double dwt_norms_real[4][10] = {
  139. {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
  140. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  141. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  142. {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
  143. };
  144. /*
  145. ==========================================================
  146. local functions
  147. ==========================================================
  148. */
  149. /* <summary> */
  150. /* Forward lazy transform (horizontal). */
  151. /* </summary> */
  152. static void dwt_deinterleave_h(int* a, int* b, int dn, int sn, int cas) {
  153. int i;
  154. for (i=0; i<sn; i++) b[i]=a[2*i+cas];
  155. for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
  156. }
  157. /* <summary> */
  158. /* Forward lazy transform (vertical). */
  159. /* </summary> */
  160. static void dwt_deinterleave_v(int* a, int* b, int dn, int sn, int x, int cas) {
  161. int i;
  162. for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
  163. for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
  164. }
  165. /* <summary> */
  166. /* Inverse lazy transform (horizontal). */
  167. /* </summary> */
  168. static void dwt_interleave_h(dwt_t* h, int* a) {
  169. int* ai = a;
  170. int* bi = h->mem + h->cas;
  171. int i = h->sn;
  172. while( i-- ) {
  173. *bi = *(ai++);
  174. bi += 2;
  175. }
  176. ai = a + h->sn;
  177. bi = h->mem + 1 - h->cas;
  178. i = h->dn ;
  179. while( i-- ) {
  180. *bi = *(ai++);
  181. bi += 2;
  182. }
  183. }
  184. /* <summary> */
  185. /* Inverse lazy transform (vertical). */
  186. /* </summary> */
  187. static void dwt_interleave_v(dwt_t* v, int* a, int x) {
  188. int* ai = a;
  189. int* bi = v->mem + v->cas;
  190. int i = v->sn;
  191. while( i-- ) {
  192. *bi = *ai;
  193. bi += 2;
  194. ai += x;
  195. }
  196. ai = a + (v->sn * x);
  197. bi = v->mem + 1 - v->cas;
  198. i = v->dn ;
  199. while( i-- ) {
  200. *bi = *ai;
  201. bi += 2;
  202. ai += x;
  203. }
  204. }
  205. /* <summary> */
  206. /* Forward 5-3 wavelet transform in 1-D. */
  207. /* </summary> */
  208. static void dwt_encode_1(int* a, int dn, int sn, int cas) {
  209. if (!cas) {
  210. if (dn > 0 || sn > 1) { /* NEW: CASE ONE ELEMENT */
  211. int i;
  212. for (i = 0; i < dn; i++) {
  213. D(i) -= (S_(i) + S_(i + 1)) >> 1;
  214. }
  215. for (i = 0; i < sn; i++) {
  216. S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
  217. }
  218. }
  219. } else if (!sn && dn == 1) { /* NEW: CASE ONE ELEMENT */
  220. S(0) *= 2;
  221. } else {
  222. int i;
  223. for (i = 0; i < dn; i++) {
  224. S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
  225. }
  226. for (i = 0; i < sn; i++) {
  227. D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
  228. }
  229. }
  230. }
  231. /* <summary> */
  232. /* Inverse 5-3 wavelet transform in 1-D. */
  233. /* </summary> */
  234. static void dwt_decode_1_(int* a, int dn, int sn, int cas) {
  235. int i;
  236. if (!cas) {
  237. if (dn > 0 || sn > 1) { /* NEW: CASE ONE ELEMENT */
  238. for (i = 0; i < sn; i++) {
  239. S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
  240. }
  241. for (i = 0; i < dn; i++) {
  242. D(i) += (S_(i) + S_(i + 1)) >> 1;
  243. }
  244. }
  245. } else if (!sn && dn == 1) { /* NEW: CASE ONE ELEMENT */
  246. S(0) /= 2;
  247. } else {
  248. for (i = 0; i < sn; i++) {
  249. D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
  250. }
  251. for (i = 0; i < dn; i++) {
  252. S(i) += (DD_(i) + DD_(i - 1)) >> 1;
  253. }
  254. }
  255. }
  256. /* <summary> */
  257. /* Inverse 5-3 wavelet transform in 1-D. */
  258. /* </summary> */
  259. static void dwt_decode_1(dwt_t* v) {
  260. dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
  261. }
  262. /* <summary> */
  263. /* Forward 9-7 wavelet transform in 1-D. */
  264. /* </summary> */
  265. static void dwt_encode_1_real(int* a, int dn, int sn, int cas) {
  266. int i;
  267. if (!cas) {
  268. if (dn > 0 || sn > 1) { /* NEW : CASE ONE ELEMENT */
  269. for (i = 0; i < dn; ++i) {
  270. D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
  271. }
  272. for (i = 0; i < sn; ++i) {
  273. S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
  274. }
  275. for (i = 0; i < dn; ++i) {
  276. D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
  277. }
  278. for (i = 0; i < sn; ++i) {
  279. S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
  280. }
  281. for (i = 0; i < dn; ++i) {
  282. D(i) = fix_mul(D(i), 5038); /*5038 */
  283. }
  284. for (i = 0; i < sn; ++i) {
  285. S(i) = fix_mul(S(i), 6659); /*6660 */
  286. }
  287. }
  288. } else if (sn > 0 || dn > 1) { /* NEW : CASE ONE ELEMENT */
  289. for (i = 0; i < dn; ++i) {
  290. S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
  291. }
  292. for (i = 0; i < sn; ++i) {
  293. D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
  294. }
  295. for (i = 0; i < dn; ++i) {
  296. S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
  297. }
  298. for (i = 0; i < sn; ++i) {
  299. D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
  300. }
  301. for (i = 0; i < dn; ++i) {
  302. S(i) = fix_mul(S(i), 5038); /*5038 */
  303. }
  304. for (i = 0; i < sn; ++i) {
  305. D(i) = fix_mul(D(i), 6659); /*6660 */
  306. }
  307. }
  308. }
  309. static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t* bandno_stepsize) {
  310. int p = int_floorlog2(stepsize) - 13;
  311. int n = 11 - int_floorlog2(stepsize);
  312. bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
  313. bandno_stepsize->expn = numbps - p;
  314. }
  315. /*
  316. ==========================================================
  317. DWT interface
  318. ==========================================================
  319. */
  320. /* <summary> */
  321. /* Forward 5-3 wavelet transform in 2-D. */
  322. /* </summary> */
  323. void dwt_encode(opj_tcd_tilecomp_t* tilec) {
  324. int w = tilec->x1-tilec->x0;
  325. int l = tilec->numresolutions-1;
  326. int* a = tilec->data;
  327. int i, j, k;
  328. for (i = 0; i < l; ++i) {
  329. /* width of the resolution level computed */
  330. int rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
  331. /* height of the resolution level computed */
  332. int rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
  333. /* width of the resolution level once lower than computed one */
  334. int rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
  335. /* height of the resolution level once lower than computed one */
  336. int rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
  337. /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
  338. int cas_row = tilec->resolutions[l - i].x0 % 2;
  339. /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
  340. int cas_col = tilec->resolutions[l - i].y0 % 2;
  341. int sn = rh1;
  342. int dn = rh - rh1;
  343. int* bj = (int*)opj_malloc(rh * sizeof(int));
  344. if (!bj) {
  345. /* Memory allocation failure... */
  346. return;
  347. }
  348. for (j = 0; j < rw; j++) {
  349. int* aj = a + j;
  350. for (k = 0; k < rh; k++) bj[k] = aj[k*w];
  351. dwt_encode_1(bj, dn, sn, cas_col);
  352. dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
  353. }
  354. opj_free(bj);
  355. sn = rw1;
  356. dn = rw - rw1;
  357. bj = (int*)opj_malloc(rw * sizeof(int));
  358. if (!bj) {
  359. /* Memory allocation failure... */
  360. return;
  361. }
  362. for (j = 0; j < rh; j++) {
  363. int* aj = a + j * w;
  364. for (k = 0; k < rw; k++) bj[k] = aj[k];
  365. dwt_encode_1(bj, dn, sn, cas_row);
  366. dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
  367. }
  368. opj_free(bj);
  369. }
  370. }
  371. /* <summary> */
  372. /* Inverse 5-3 wavelet transform in 2-D. */
  373. /* </summary> */
  374. void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) {
  375. dwt_decode_tile(tilec, numres, &dwt_decode_1);
  376. }
  377. /* <summary> */
  378. /* Get gain of 5-3 wavelet transform. */
  379. /* </summary> */
  380. int dwt_getgain(int orient) {
  381. if (orient == 0)
  382. return 0;
  383. if (orient == 1 || orient == 2)
  384. return 1;
  385. return 2;
  386. }
  387. /* <summary> */
  388. /* Get norm of 5-3 wavelet. */
  389. /* </summary> */
  390. double dwt_getnorm(int level, int orient) {
  391. return dwt_norms[orient][level];
  392. }
  393. /* <summary> */
  394. /* Forward 9-7 wavelet transform in 2-D. */
  395. /* </summary> */
  396. void dwt_encode_real(opj_tcd_tilecomp_t* tilec) {
  397. int w = tilec->x1-tilec->x0;
  398. int l = tilec->numresolutions-1;
  399. int* a = tilec->data;
  400. int i, j, k;
  401. for (i = 0; i < l; i++) {
  402. /* width of the resolution level computed */
  403. int rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
  404. /* height of the resolution level computed */
  405. int rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
  406. /* width of the resolution level once lower than computed one */
  407. int rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
  408. /* height of the resolution level once lower than computed one */
  409. int rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
  410. /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
  411. int cas_row = tilec->resolutions[l - i].x0 % 2;
  412. /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
  413. int cas_col = tilec->resolutions[l - i].y0 % 2;
  414. int sn = rh1;
  415. int dn = rh - rh1;
  416. int* bj = (int*)opj_malloc(rh * sizeof(int));
  417. if (!bj) {
  418. /* Memory allocation failure... */
  419. return;
  420. }
  421. for (j = 0; j < rw; j++) {
  422. int* aj = a + j;
  423. for (k = 0; k < rh; k++) bj[k] = aj[k*w];
  424. dwt_encode_1_real(bj, dn, sn, cas_col);
  425. dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
  426. }
  427. opj_free(bj);
  428. sn = rw1;
  429. dn = rw - rw1;
  430. bj = (int*)opj_malloc(rw * sizeof(int));
  431. if (!bj) {
  432. /* Memory allocation failure... */
  433. return;
  434. }
  435. for (j = 0; j < rh; j++) {
  436. int* aj = a + j * w;
  437. for (k = 0; k < rw; k++) bj[k] = aj[k];
  438. dwt_encode_1_real(bj, dn, sn, cas_row);
  439. dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
  440. }
  441. opj_free(bj);
  442. }
  443. }
  444. /* <summary> */
  445. /* Get gain of 9-7 wavelet transform. */
  446. /* </summary> */
  447. int dwt_getgain_real(int orient) {
  448. (void)orient;
  449. return 0;
  450. }
  451. /* <summary> */
  452. /* Get norm of 9-7 wavelet. */
  453. /* </summary> */
  454. double dwt_getnorm_real(int level, int orient) {
  455. return dwt_norms_real[orient][level];
  456. }
  457. void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
  458. int bandno;
  459. int numbands = 3 * tccp->numresolutions - 2;
  460. for (bandno = 0; bandno < numbands; bandno++) {
  461. int resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
  462. int orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
  463. int level = tccp->numresolutions - 1 - resno;
  464. int gain = tccp->qmfbid == 0 ? 0 : (orient == 0 ? 0 : (orient == 1 || orient == 2 ? 1 : 2));
  465. double stepsize;
  466. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  467. stepsize = 1.0;
  468. } else {
  469. double norm = dwt_norms_real[orient][level];
  470. stepsize = (1 << (gain)) / norm;
  471. }
  472. dwt_encode_stepsize((int)floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
  473. }
  474. }
  475. /* <summary> */
  476. /* Determine maximum computed resolution level for inverse wavelet transform */
  477. /* </summary> */
  478. static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
  479. int mr = 1;
  480. int w;
  481. while (--i) {
  482. ++r;
  483. if (mr < (w = r->x1 - r->x0)) {
  484. mr = w;
  485. }
  486. if (mr < (w = r->y1 - r->y0)) {
  487. mr = w;
  488. }
  489. }
  490. return mr;
  491. }
  492. /* <summary> */
  493. /* Inverse wavelet transform in 2-D. */
  494. /* </summary> */
  495. static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) {
  496. opj_tcd_resolution_t* tr = tilec->resolutions;
  497. int rw = tr->x1 - tr->x0; /* width of the resolution level computed */
  498. int rh = tr->y1 - tr->y0; /* height of the resolution level computed */
  499. int w = tilec->x1 - tilec->x0;
  500. dwt_t h;
  501. h.mem = (int*)opj_aligned_malloc(dwt_decode_max_resolution(tr, numres) * sizeof(int));
  502. if (!h.mem) {
  503. /* Memory allocation failure... */
  504. return;
  505. }
  506. dwt_t v;
  507. v.mem = h.mem;
  508. int* restrict tiledp = tilec->data;
  509. while (--numres) {
  510. h.sn = rw;
  511. v.sn = rh;
  512. ++tr;
  513. rw = tr->x1 - tr->x0;
  514. rh = tr->y1 - tr->y0;
  515. h.dn = rw - h.sn;
  516. h.cas = tr->x0 % 2;
  517. int j = 0;
  518. int jw = 0; /* j * w */
  519. while (1) {
  520. dwt_interleave_h(&h, &tiledp[jw]);
  521. (dwt_1D)(&h);
  522. memcpy(&tiledp[jw], h.mem, rw * sizeof(int));
  523. if (++j >= rh) {
  524. break;
  525. }
  526. jw += w;
  527. }
  528. v.dn = rh - v.sn;
  529. v.cas = tr->y0 % 2;
  530. for (j = 0; j < rw; ++j) {
  531. dwt_interleave_v(&v, &tiledp[j], w);
  532. (dwt_1D)(&v);
  533. int k = 0;
  534. int kwj = j; /* k * w + j */
  535. while (1) {
  536. tiledp[kwj] = v.mem[k];
  537. if (++k >= rh) {
  538. break;
  539. }
  540. kwj += w;
  541. }
  542. }
  543. }
  544. opj_aligned_free(h.mem);
  545. }
  546. static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size) {
  547. float* restrict bi = (float*) (w->wavelet + w->cas);
  548. int count = w->sn;
  549. int i, k;
  550. if ((x & 0x0f) == 0) {
  551. for (k = 0; k < 2; ++k) {
  552. if (((size_t)a & 0x0f) == 0 &&
  553. ((size_t)bi & 0x0f) == 0 &&
  554. count + 3 * x < size) {
  555. /* Fast code path */
  556. for (i = 0; i < count; ++i) {
  557. float* restrict bk = &bi[i << 3];
  558. bk[0] = a[i];
  559. int j = i + x;
  560. bk[1] = a[j];
  561. j += x;
  562. bk[2] = a[j];
  563. j += x;
  564. bk[3] = a[j];
  565. }
  566. } else {
  567. /* Slow code path */
  568. for (i = 0; i < count; ++i) {
  569. float* restrict bk = &bi[i << 3];
  570. bk[0] = a[i];
  571. int j = i + x;
  572. if (j > size) continue;
  573. bk[1] = a[j];
  574. j += x;
  575. if (j > size) continue;
  576. bk[2] = a[j];
  577. j += x;
  578. if (j > size) continue;
  579. bk[3] = a[j];
  580. }
  581. }
  582. bi = (float*)(w->wavelet + 1 - w->cas);
  583. a += w->sn;
  584. size -= w->sn;
  585. count = w->dn;
  586. }
  587. } else {
  588. /* Slow code path */
  589. for (k = 0; k < 2; ++k) {
  590. for (i = 0; i < count; ++i) {
  591. float* restrict bk = &bi[i << 3];
  592. bk[0] = a[i];
  593. int j = i + x;
  594. if (j > size) continue;
  595. bk[1] = a[j];
  596. j += x;
  597. if (j > size) continue;
  598. bk[2] = a[j];
  599. j += x;
  600. if (j > size) continue;
  601. bk[3] = a[j];
  602. }
  603. bi = (float*)(w->wavelet + 1 - w->cas);
  604. a += w->sn;
  605. size -= w->sn;
  606. count = w->dn;
  607. }
  608. }
  609. }
  610. static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x) {
  611. v4* restrict bi = v->wavelet + v->cas;
  612. int i;
  613. int ix = 0; /* i * x */
  614. for (i = 0; i < v->sn; ++i) {
  615. memcpy(&bi[i + i], &a[ix], 4 * sizeof(float));
  616. ix += x;
  617. }
  618. a += v->sn * x;
  619. bi = v->wavelet + 1 - v->cas;
  620. ix = 0; /* i * x */
  621. for (i = 0; i < v->dn; ++i) {
  622. memcpy(&bi[i + i], &a[ix], 4 * sizeof(float));
  623. ix += x;
  624. }
  625. }
  626. #ifdef __SSE__
  627. static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c) {
  628. __m128* restrict vw = (__m128*)w;
  629. int i;
  630. int end = count >> 2;
  631. /* 4x unrolled loop */
  632. for (i = 0; i < end; ++i) {
  633. *vw = _mm_mul_ps(*vw, c);
  634. vw += 2;
  635. *vw = _mm_mul_ps(*vw, c);
  636. vw += 2;
  637. *vw = _mm_mul_ps(*vw, c);
  638. vw += 2;
  639. *vw = _mm_mul_ps(*vw, c);
  640. vw += 2;
  641. }
  642. count &= 3;
  643. for (i = 0; i < count; ++i) {
  644. *vw = _mm_mul_ps(*vw, c);
  645. vw += 2;
  646. }
  647. }
  648. static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c) {
  649. __m128* restrict vl = (__m128*)l;
  650. __m128* restrict vw = (__m128*)w;
  651. __m128 tmp1, tmp2, tmp3;
  652. tmp1 = vl[0];
  653. int i;
  654. /* 4x loop unrolling */
  655. for (i = 0; i < m - 3; i += 4) {
  656. __m128 tmp4, tmp5, tmp6, tmp7, tmp8, tmp9;
  657. tmp2 = vw[-1];
  658. tmp3 = vw[0];
  659. tmp4 = vw[1];
  660. tmp5 = vw[2];
  661. tmp6 = vw[3];
  662. tmp7 = vw[4];
  663. tmp8 = vw[5];
  664. tmp9 = vw[6];
  665. vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
  666. vw[1] = _mm_add_ps(tmp4, _mm_mul_ps(_mm_add_ps(tmp3, tmp5), c));
  667. vw[3] = _mm_add_ps(tmp6, _mm_mul_ps(_mm_add_ps(tmp5, tmp7), c));
  668. vw[5] = _mm_add_ps(tmp8, _mm_mul_ps(_mm_add_ps(tmp7, tmp9), c));
  669. tmp1 = tmp9;
  670. vw += 8;
  671. }
  672. for ( ; i < m; ++i) {
  673. tmp2 = vw[-1];
  674. tmp3 = vw[0];
  675. vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
  676. tmp1 = tmp3;
  677. vw += 2;
  678. }
  679. if (m >= k) {
  680. return;
  681. }
  682. vl = vw - 2;
  683. c = _mm_add_ps(c, c);
  684. c = _mm_mul_ps(c, vl[0]);
  685. for ( ; m < k; ++m) {
  686. __m128 tmp = vw[-1];
  687. vw[-1] = _mm_add_ps(tmp, c);
  688. vw += 2;
  689. }
  690. }
  691. #else
  692. static void v4dwt_decode_step1(v4* w, int count, const float c) {
  693. float* restrict fw = (float*) w;
  694. int i;
  695. for (i = 0; i < count; ++i) {
  696. float tmp1 = fw[i * 8 ];
  697. float tmp2 = fw[i * 8 + 1];
  698. float tmp3 = fw[i * 8 + 2];
  699. float tmp4 = fw[i * 8 + 3];
  700. fw[i * 8] = tmp1 * c;
  701. fw[i * 8 + 1] = tmp2 * c;
  702. fw[i * 8 + 2] = tmp3 * c;
  703. fw[i * 8 + 3] = tmp4 * c;
  704. }
  705. }
  706. static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c) {
  707. float* restrict fl = (float*) l;
  708. float* restrict fw = (float*) w;
  709. int i;
  710. for (i = 0; i < m; ++i) {
  711. float tmp1_1 = fl[0];
  712. float tmp1_2 = fl[1];
  713. float tmp1_3 = fl[2];
  714. float tmp1_4 = fl[3];
  715. float tmp2_1 = fw[-4];
  716. float tmp2_2 = fw[-3];
  717. float tmp2_3 = fw[-2];
  718. float tmp2_4 = fw[-1];
  719. float tmp3_1 = fw[0];
  720. float tmp3_2 = fw[1];
  721. float tmp3_3 = fw[2];
  722. float tmp3_4 = fw[3];
  723. fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
  724. fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
  725. fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
  726. fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
  727. fl = fw;
  728. fw += 8;
  729. }
  730. if (m < k) {
  731. float c1;
  732. float c2;
  733. float c3;
  734. float c4;
  735. c += c;
  736. c1 = fl[0] * c;
  737. c2 = fl[1] * c;
  738. c3 = fl[2] * c;
  739. c4 = fl[3] * c;
  740. for (; m < k; ++m) {
  741. float tmp1 = fw[-4];
  742. float tmp2 = fw[-3];
  743. float tmp3 = fw[-2];
  744. float tmp4 = fw[-1];
  745. fw[-4] = tmp1 + c1;
  746. fw[-3] = tmp2 + c2;
  747. fw[-2] = tmp3 + c3;
  748. fw[-1] = tmp4 + c4;
  749. fw += 8;
  750. }
  751. }
  752. }
  753. #endif
  754. /* <summary> */
  755. /* Inverse 9-7 wavelet transform in 1-D. */
  756. /* </summary> */
  757. static void v4dwt_decode(v4dwt_t* restrict dwt) {
  758. int a, b;
  759. if (dwt->cas == 0) {
  760. if (dwt->dn <= 0 && dwt->sn <= 1) {
  761. return;
  762. }
  763. a = 0;
  764. b = 1;
  765. } else {
  766. if (dwt->sn <= 0 && dwt->dn <= 1) {
  767. return;
  768. }
  769. a = 1;
  770. b = 0;
  771. }
  772. v4* restrict waveleta = dwt->wavelet + a;
  773. v4* restrict waveletb = dwt->wavelet + b;
  774. #ifdef __SSE__
  775. v4dwt_decode_step1_sse(waveleta, dwt->sn, _mm_set1_ps(K));
  776. v4dwt_decode_step1_sse(waveletb, dwt->dn, _mm_set1_ps(c13318));
  777. v4dwt_decode_step2_sse(waveletb, waveleta + 1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_delta));
  778. v4dwt_decode_step2_sse(waveleta, waveletb + 1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_gamma));
  779. v4dwt_decode_step2_sse(waveletb, waveleta + 1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_beta));
  780. v4dwt_decode_step2_sse(waveleta, waveletb + 1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_alpha));
  781. #else
  782. v4dwt_decode_step1(waveleta, dwt->sn, K);
  783. v4dwt_decode_step1(waveletb, dwt->dn, c13318);
  784. v4dwt_decode_step2(waveletb, waveleta + 1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta);
  785. v4dwt_decode_step2(waveleta, waveletb + 1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma);
  786. v4dwt_decode_step2(waveletb, waveleta + 1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta);
  787. v4dwt_decode_step2(waveleta, waveletb + 1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha);
  788. #endif
  789. }
  790. /* <summary> */
  791. /* Inverse 9-7 wavelet transform in 2-D. */
  792. /* </summary> */
  793. void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres) {
  794. v4dwt_t h;
  795. v4dwt_t v;
  796. opj_tcd_resolution_t* res = tilec->resolutions;
  797. int rw = res->x1 - res->x0; /* width of the resolution level computed */
  798. int rh = res->y1 - res->y0; /* height of the resolution level computed */
  799. h.wavelet = (v4*)opj_aligned_malloc((dwt_decode_max_resolution(res, numres) + 5) * sizeof(v4));
  800. if (!h.wavelet) {
  801. /* Memory allocation failure... */
  802. return;
  803. }
  804. v.wavelet = h.wavelet;
  805. const int w = tilec->x1 - tilec->x0;
  806. const int four_w = w << 2;
  807. const int bufsize = w * (tilec->y1 - tilec->y0);
  808. while (--numres) {
  809. float* restrict aj = (float*)tilec->data;
  810. int bufleft = bufsize;
  811. h.sn = rw;
  812. v.sn = rh;
  813. ++res;
  814. rw = res->x1 - res->x0; /* width of the resolution level computed */
  815. rh = res->y1 - res->y0; /* height of the resolution level computed */
  816. h.dn = rw - h.sn;
  817. h.cas = res->x0 % 2;
  818. int j;
  819. for (j = rh; j > 3; j -= 4) {
  820. v4dwt_interleave_h(&h, aj, w, bufleft);
  821. v4dwt_decode(&h);
  822. int k;
  823. int kw; /* k + w * 1,2,3 */
  824. for (k = rw; --k >= 0; ) {
  825. aj[k] = h.wavelet[k].f[0];
  826. kw = k + w;
  827. aj[kw] = h.wavelet[k].f[1];
  828. kw += w;
  829. aj[kw] = h.wavelet[k].f[2];
  830. kw += w;
  831. aj[kw] = h.wavelet[k].f[3];
  832. }
  833. aj += four_w;
  834. bufleft -= four_w;
  835. }
  836. j = rh & 0x03;
  837. if (j) {
  838. v4dwt_interleave_h(&h, aj, w, bufleft);
  839. v4dwt_decode(&h);
  840. int k;
  841. switch (j) {
  842. case 3:
  843. for (k = rw; --k >= 0; ) {
  844. aj[k + (w + w)] = h.wavelet[k].f[2];
  845. aj[k + w] = h.wavelet[k].f[1];
  846. aj[k] = h.wavelet[k].f[0];
  847. }
  848. break;
  849. case 2:
  850. for (k = rw; --k >= 0; ) {
  851. aj[k + w] = h.wavelet[k].f[1];
  852. aj[k] = h.wavelet[k].f[0];
  853. }
  854. break;
  855. case 1:
  856. for (k = rw; --k >= 0; ) {
  857. aj[k] = h.wavelet[k].f[0];
  858. }
  859. }
  860. }
  861. v.dn = rh - v.sn;
  862. v.cas = res->y0 % 2;
  863. aj = (float*)tilec->data;
  864. for (j = rw; j > 3; j -= 4) {
  865. v4dwt_interleave_v(&v, aj, w);
  866. v4dwt_decode(&v);
  867. int k;
  868. int kw = 0; /* k * w */
  869. for (k = 0; k < rh; ++k) {
  870. memcpy(&aj[kw], &v.wavelet[k], 4 * sizeof(float));
  871. kw += w;
  872. }
  873. aj += 4;
  874. }
  875. j = rw & 0x03;
  876. if (j) {
  877. v4dwt_interleave_v(&v, aj, w);
  878. v4dwt_decode(&v);
  879. int k;
  880. int kw = 0; /* k * w */
  881. const int size = j * sizeof(float);
  882. for (k = 0; k < rh; ++k) {
  883. memcpy(&aj[kw], &v.wavelet[k], size);
  884. kw += w;
  885. }
  886. }
  887. }
  888. opj_aligned_free(h.wavelet);
  889. }