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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
35 #include <xmmintrin.h>
38 #include "opj_includes.h"
40 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
43 #define WS(i) v->mem[(i)*2]
44 #define WD(i) v->mem[(1+(i)*2)]
46 /** @name Local data structures */
49 typedef struct dwt_local {
60 typedef struct v4dwt_local {
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 */
72 static const float K = 1.230174105f; /* 10078 */
73 /* FIXME: What is this constant? */
74 static const float c13318 = 1.625732422f;
79 Virtual function type for wavelet transform in 1-D
81 typedef void (*DWT1DFN)(dwt_t* v);
83 /** @name Local static functions */
87 Forward lazy transform (horizontal)
89 static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas);
91 Forward lazy transform (vertical)
93 static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas);
95 Inverse lazy transform (horizontal)
97 static void dwt_interleave_h(dwt_t* h, int *a);
99 Inverse lazy transform (vertical)
101 static void dwt_interleave_v(dwt_t* v, int *a, int x);
103 Forward 5-3 wavelet transform in 1-D
105 static void dwt_encode_1(int *a, int dn, int sn, int cas);
107 Inverse 5-3 wavelet transform in 1-D
109 static void dwt_decode_1(dwt_t *v);
111 Forward 9-7 wavelet transform in 1-D
113 static void dwt_encode_1_real(int *a, int dn, int sn, int cas);
115 Explicit calculation of the Quantization Stepsizes
117 static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize);
119 Inverse wavelet transform in 2-D.
122 static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
124 static opj_bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
127 Inverse wavelet transform in 2-D.
129 static opj_bool dwt_decode_tile_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
131 static opj_bool dwt_encode_procedure( opj_tcd_tilecomp_v2_t * tilec,
132 void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
134 static OPJ_UINT32 dwt_max_resolution_v2(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i);
141 #define S(i) a[(i)*2]
142 #define D(i) a[(1+(i)*2)]
143 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
144 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
146 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
147 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
150 /* This table contains the norms of the 5-3 wavelets for different bands. */
152 static const double dwt_norms[4][10] = {
153 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
154 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
155 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
156 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
160 /* This table contains the norms of the 9-7 wavelets for different bands. */
162 static const double dwt_norms_real[4][10] = {
163 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
164 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
165 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
166 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
170 ==========================================================
172 ==========================================================
176 /* Forward lazy transform (horizontal). */
178 static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
180 for (i=0; i<sn; i++) b[i]=a[2*i+cas];
181 for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
185 /* Forward lazy transform (vertical). */
187 static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
189 for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
190 for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
194 /* Inverse lazy transform (horizontal). */
196 static void dwt_interleave_h(dwt_t* h, int *a) {
198 int *bi = h->mem + h->cas;
205 bi = h->mem + 1 - h->cas;
214 /* Inverse lazy transform (vertical). */
216 static void dwt_interleave_v(dwt_t* v, int *a, int x) {
218 int *bi = v->mem + v->cas;
225 ai = a + (v->sn * x);
226 bi = v->mem + 1 - v->cas;
237 /* Forward 5-3 wavelet transform in 1-D. */
239 static void dwt_encode_1(int *a, int dn, int sn, int cas) {
243 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
244 for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
245 for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
248 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
251 for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
252 for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
258 /* Inverse 5-3 wavelet transform in 1-D. */
260 static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
264 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
265 for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
266 for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
269 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
272 for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
273 for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
279 /* Inverse 5-3 wavelet transform in 1-D. */
281 static void dwt_decode_1(dwt_t *v) {
282 dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
286 /* Forward 9-7 wavelet transform in 1-D. */
288 static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
291 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
292 for (i = 0; i < dn; i++)
293 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
294 for (i = 0; i < sn; i++)
295 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
296 for (i = 0; i < dn; i++)
297 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
298 for (i = 0; i < sn; i++)
299 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
300 for (i = 0; i < dn; i++)
301 D(i) = fix_mul(D(i), 5038); /*5038 */
302 for (i = 0; i < sn; i++)
303 S(i) = fix_mul(S(i), 6659); /*6660 */
306 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
307 for (i = 0; i < dn; i++)
308 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
309 for (i = 0; i < sn; i++)
310 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
311 for (i = 0; i < dn; i++)
312 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
313 for (i = 0; i < sn; i++)
314 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
315 for (i = 0; i < dn; i++)
316 S(i) = fix_mul(S(i), 5038); /*5038 */
317 for (i = 0; i < sn; i++)
318 D(i) = fix_mul(D(i), 6659); /*6660 */
323 static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) {
325 p = int_floorlog2(stepsize) - 13;
326 n = 11 - int_floorlog2(stepsize);
327 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
328 bandno_stepsize->expn = numbps - p;
332 ==========================================================
334 ==========================================================
338 /* Forward 5-3 wavelet transform in 2-D. */
340 void dwt_encode(opj_tcd_tilecomp_t * tilec) {
347 w = tilec->x1-tilec->x0;
348 l = tilec->numresolutions-1;
351 for (i = 0; i < l; i++) {
352 int rw; /* width of the resolution level computed */
353 int rh; /* height of the resolution level computed */
354 int rw1; /* width of the resolution level once lower than computed one */
355 int rh1; /* height of the resolution level once lower than computed one */
356 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
357 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
360 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
361 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
362 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
363 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
365 cas_row = tilec->resolutions[l - i].x0 % 2;
366 cas_col = tilec->resolutions[l - i].y0 % 2;
370 bj = (int*)opj_malloc(rh * sizeof(int));
371 for (j = 0; j < rw; j++) {
373 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
374 dwt_encode_1(bj, dn, sn, cas_col);
375 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
381 bj = (int*)opj_malloc(rw * sizeof(int));
382 for (j = 0; j < rh; j++) {
384 for (k = 0; k < rw; k++) bj[k] = aj[k];
385 dwt_encode_1(bj, dn, sn, cas_row);
386 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
393 /* Forward 5-3 wavelet transform in 2-D. */
395 INLINE opj_bool dwt_encode_procedure(opj_tcd_tilecomp_v2_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
403 OPJ_INT32 rw; /* width of the resolution level computed */
404 OPJ_INT32 rh; /* height of the resolution level computed */
405 OPJ_INT32 l_data_size;
407 opj_tcd_resolution_v2_t * l_cur_res = 0;
408 opj_tcd_resolution_v2_t * l_last_res = 0;
410 w = tilec->x1-tilec->x0;
411 l = tilec->numresolutions-1;
414 l_cur_res = tilec->resolutions + l;
415 l_last_res = l_cur_res - 1;
417 rw = l_cur_res->x1 - l_cur_res->x0;
418 rh = l_cur_res->y1 - l_cur_res->y0;
420 l_data_size = dwt_max_resolution_v2( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
421 bj = (OPJ_INT32*)opj_malloc(l_data_size);
428 OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
429 OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
430 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
431 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
434 rw = l_cur_res->x1 - l_cur_res->x0;
435 rh = l_cur_res->y1 - l_cur_res->y0;
436 rw1 = l_last_res->x1 - l_last_res->x0;
437 rh1 = l_last_res->y1 - l_last_res->y0;
439 cas_row = l_cur_res->x0 & 1;
440 cas_col = l_cur_res->y0 & 1;
444 for (j = 0; j < rw; ++j) {
446 for (k = 0; k < rh; ++k) {
450 (*p_function) (bj, dn, sn, cas_col);
452 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
458 for (j = 0; j < rh; j++) {
460 for (k = 0; k < rw; k++) bj[k] = aj[k];
461 (*p_function) (bj, dn, sn, cas_row);
462 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
465 l_cur_res = l_last_res;
474 /* Forward 5-3 wavelet transform in 2-D. */
476 opj_bool dwt_encode_v2(opj_tcd_tilecomp_v2_t * tilec)
478 return dwt_encode_procedure(tilec,dwt_encode_1);
483 /* Inverse 5-3 wavelet transform in 2-D. */
485 void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) {
486 dwt_decode_tile(tilec, numres, &dwt_decode_1);
491 /* Inverse 5-3 wavelet transform in 2-D. */
493 opj_bool dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
494 return dwt_decode_tile(tilec, numres, &dwt_decode_1);
498 /* Inverse 5-3 wavelet transform in 2-D. */
500 opj_bool dwt_decode_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
501 return dwt_decode_tile_v2(tilec, numres, &dwt_decode_1);
506 /* Get gain of 5-3 wavelet transform. */
508 int dwt_getgain(int orient) {
511 if (orient == 1 || orient == 2)
517 /* Get gain of 5-3 wavelet transform. */
519 OPJ_UINT32 dwt_getgain_v2(OPJ_UINT32 orient) {
522 if (orient == 1 || orient == 2)
528 /* Get norm of 5-3 wavelet. */
530 double dwt_getnorm(int level, int orient) {
531 return dwt_norms[orient][level];
535 /* Forward 9-7 wavelet transform in 2-D. */
538 void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
545 w = tilec->x1-tilec->x0;
546 l = tilec->numresolutions-1;
549 for (i = 0; i < l; i++) {
550 int rw; /* width of the resolution level computed */
551 int rh; /* height of the resolution level computed */
552 int rw1; /* width of the resolution level once lower than computed one */
553 int rh1; /* height of the resolution level once lower than computed one */
554 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
555 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
558 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
559 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
560 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
561 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
563 cas_row = tilec->resolutions[l - i].x0 % 2;
564 cas_col = tilec->resolutions[l - i].y0 % 2;
568 bj = (int*)opj_malloc(rh * sizeof(int));
569 for (j = 0; j < rw; j++) {
571 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
572 dwt_encode_1_real(bj, dn, sn, cas_col);
573 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
579 bj = (int*)opj_malloc(rw * sizeof(int));
580 for (j = 0; j < rh; j++) {
582 for (k = 0; k < rw; k++) bj[k] = aj[k];
583 dwt_encode_1_real(bj, dn, sn, cas_row);
584 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
591 /* Forward 9-7 wavelet transform in 2-D. */
593 opj_bool dwt_encode_real_v2(opj_tcd_tilecomp_v2_t * tilec)
595 return dwt_encode_procedure(tilec,dwt_encode_1_real);
599 /* Get gain of 9-7 wavelet transform. */
601 int dwt_getgain_real(int orient) {
607 /* Get gain of 9-7 wavelet transform. */
609 OPJ_UINT32 dwt_getgain_real_v2(OPJ_UINT32 orient) {
615 /* Get norm of 9-7 wavelet. */
617 double dwt_getnorm_real(int level, int orient) {
618 return dwt_norms_real[orient][level];
621 void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
622 int numbands, bandno;
623 numbands = 3 * tccp->numresolutions - 2;
624 for (bandno = 0; bandno < numbands; bandno++) {
626 int resno, level, orient, gain;
628 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
629 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
630 level = tccp->numresolutions - 1 - resno;
631 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
632 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
635 double norm = dwt_norms_real[orient][level];
636 stepsize = (1 << (gain)) / norm;
638 dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
644 /* Determine maximum computed resolution level for inverse wavelet transform */
646 static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
651 if( mr < ( w = r->x1 - r->x0 ) )
653 if( mr < ( w = r->y1 - r->y0 ) )
660 /* Determine maximum computed resolution level for inverse wavelet transform */
662 static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
667 if( mr < ( w = r->x1 - r->x0 ) )
669 if( mr < ( w = r->y1 - r->y0 ) )
676 /* Determine maximum computed resolution level for inverse wavelet transform */
678 static OPJ_UINT32 dwt_max_resolution_v2(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i) {
683 if( mr < ( w = r->x1 - r->x0 ) )
685 if( mr < ( w = r->y1 - r->y0 ) )
693 /* Inverse wavelet transform in 2-D. */
695 static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) {
699 opj_tcd_resolution_t* tr = tilec->resolutions;
701 int rw = tr->x1 - tr->x0; /* width of the resolution level computed */
702 int rh = tr->y1 - tr->y0; /* height of the resolution level computed */
704 int w = tilec->x1 - tilec->x0;
706 h.mem = (int*)opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(int));
710 int * restrict tiledp = tilec->data;
717 rw = tr->x1 - tr->x0;
718 rh = tr->y1 - tr->y0;
723 for(j = 0; j < rh; ++j) {
724 dwt_interleave_h(&h, &tiledp[j*w]);
726 memcpy(&tiledp[j*w], h.mem, rw * sizeof(int));
732 for(j = 0; j < rw; ++j){
734 dwt_interleave_v(&v, &tiledp[j], w);
736 for(k = 0; k < rh; ++k) {
737 tiledp[k * w + j] = v.mem[k];
741 opj_aligned_free(h.mem);
746 /* Inverse wavelet transform in 2-D. */
748 static opj_bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
752 opj_tcd_resolution_t* tr = tilec->resolutions;
754 OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */
755 OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */
757 OPJ_UINT32 w = tilec->x1 - tilec->x0;
760 opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
770 OPJ_INT32 * restrict tiledp = tilec->data;
777 rw = tr->x1 - tr->x0;
778 rh = tr->y1 - tr->y0;
783 for(j = 0; j < rh; ++j) {
784 dwt_interleave_h(&h, &tiledp[j*w]);
786 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
792 for(j = 0; j < rw; ++j){
794 dwt_interleave_v(&v, &tiledp[j], w);
796 for(k = 0; k < rh; ++k) {
797 tiledp[k * w + j] = v.mem[k];
801 opj_aligned_free(h.mem);
806 /* Inverse wavelet transform in 2-D. */
808 static opj_bool dwt_decode_tile_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
812 opj_tcd_resolution_v2_t* tr = tilec->resolutions;
814 OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */
815 OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */
817 OPJ_UINT32 w = tilec->x1 - tilec->x0;
820 opj_aligned_malloc(dwt_max_resolution_v2(tr, numres) * sizeof(OPJ_INT32));
830 OPJ_INT32 * restrict tiledp = tilec->data;
837 rw = tr->x1 - tr->x0;
838 rh = tr->y1 - tr->y0;
843 for(j = 0; j < rh; ++j) {
844 dwt_interleave_h(&h, &tiledp[j*w]);
846 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
852 for(j = 0; j < rw; ++j){
854 dwt_interleave_v(&v, &tiledp[j], w);
856 for(k = 0; k < rh; ++k) {
857 tiledp[k * w + j] = v.mem[k];
861 opj_aligned_free(h.mem);
865 static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size){
866 float* restrict bi = (float*) (w->wavelet + w->cas);
870 for(k = 0; k < 2; ++k){
871 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
873 for(i = 0; i < count; ++i){
886 for(i = 0; i < count; ++i){
890 if(j >= size) continue;
893 if(j >= size) continue;
896 if(j >= size) continue;
897 bi[i*8 + 3] = a[j]; /* This one*/
901 bi = (float*) (w->wavelet + 1 - w->cas);
908 static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x, int nb_elts_read){
909 v4* restrict bi = v->wavelet + v->cas;
912 for(i = 0; i < v->sn; ++i){
913 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(float));
917 bi = v->wavelet + 1 - v->cas;
919 for(i = 0; i < v->dn; ++i){
920 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(float));
926 static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
927 __m128* restrict vw = (__m128*) w;
929 /* 4x unrolled loop */
930 for(i = 0; i < count >> 2; ++i){
931 *vw = _mm_mul_ps(*vw, c);
933 *vw = _mm_mul_ps(*vw, c);
935 *vw = _mm_mul_ps(*vw, c);
937 *vw = _mm_mul_ps(*vw, c);
941 for(i = 0; i < count; ++i){
942 *vw = _mm_mul_ps(*vw, c);
947 static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
948 __m128* restrict vl = (__m128*) l;
949 __m128* restrict vw = (__m128*) w;
951 __m128 tmp1, tmp2, tmp3;
953 for(i = 0; i < m; ++i){
956 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
964 c = _mm_add_ps(c, c);
965 c = _mm_mul_ps(c, vl[0]);
968 vw[-1] = _mm_add_ps(tmp, c);
975 static void v4dwt_decode_step1(v4* w, int count, const float c){
976 float* restrict fw = (float*) w;
978 for(i = 0; i < count; ++i){
979 float tmp1 = fw[i*8 ];
980 float tmp2 = fw[i*8 + 1];
981 float tmp3 = fw[i*8 + 2];
982 float tmp4 = fw[i*8 + 3];
984 fw[i*8 + 1] = tmp2 * c;
985 fw[i*8 + 2] = tmp3 * c;
986 fw[i*8 + 3] = tmp4 * c;
990 static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
991 float* restrict fl = (float*) l;
992 float* restrict fw = (float*) w;
994 for(i = 0; i < m; ++i){
995 float tmp1_1 = fl[0];
996 float tmp1_2 = fl[1];
997 float tmp1_3 = fl[2];
998 float tmp1_4 = fl[3];
999 float tmp2_1 = fw[-4];
1000 float tmp2_2 = fw[-3];
1001 float tmp2_3 = fw[-2];
1002 float tmp2_4 = fw[-1];
1003 float tmp3_1 = fw[0];
1004 float tmp3_2 = fw[1];
1005 float tmp3_3 = fw[2];
1006 float tmp3_4 = fw[3];
1007 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
1008 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
1009 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
1010 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
1025 float tmp1 = fw[-4];
1026 float tmp2 = fw[-3];
1027 float tmp3 = fw[-2];
1028 float tmp4 = fw[-1];
1041 /* Inverse 9-7 wavelet transform in 1-D. */
1043 static void v4dwt_decode(v4dwt_t* restrict dwt){
1046 if(!((dwt->dn > 0) || (dwt->sn > 1))){
1052 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
1059 v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K));
1060 v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318));
1061 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));
1062 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));
1063 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));
1064 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));
1066 v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K);
1067 v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318);
1068 v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta);
1069 v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma);
1070 v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta);
1071 v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha);
1076 /* KEEP TRUNK VERSION + return type of v2 because rev557 */
1078 /* Inverse 9-7 wavelet transform in 2-D. */
1080 /* V1 void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ */
1081 opj_bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
1085 opj_tcd_resolution_t* res = tilec->resolutions;
1087 int rw = res->x1 - res->x0; /* width of the resolution level computed */
1088 int rh = res->y1 - res->y0; /* height of the resolution level computed */
1090 int w = tilec->x1 - tilec->x0;
1092 h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4));
1093 v.wavelet = h.wavelet;
1096 float * restrict aj = (float*) tilec->data;
1097 int bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
1105 rw = res->x1 - res->x0; /* width of the resolution level computed */
1106 rh = res->y1 - res->y0; /* height of the resolution level computed */
1109 h.cas = res->x0 % 2;
1111 for(j = rh; j > 3; j -= 4){
1113 v4dwt_interleave_h(&h, aj, w, bufsize);
1115 for(k = rw; --k >= 0;){
1116 aj[k ] = h.wavelet[k].f[0];
1117 aj[k+w ] = h.wavelet[k].f[1];
1118 aj[k+w*2] = h.wavelet[k].f[2];
1119 aj[k+w*3] = h.wavelet[k].f[3];
1127 v4dwt_interleave_h(&h, aj, w, bufsize);
1129 for(k = rw; --k >= 0;){
1131 case 3: aj[k+w*2] = h.wavelet[k].f[2];
1132 case 2: aj[k+w ] = h.wavelet[k].f[1];
1133 case 1: aj[k ] = h.wavelet[k].f[0];
1139 v.cas = res->y0 % 2;
1141 aj = (float*) tilec->data;
1142 for(j = rw; j > 3; j -= 4){
1144 v4dwt_interleave_v(&v, aj, w, 4);
1146 for(k = 0; k < rh; ++k){
1147 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
1154 v4dwt_interleave_v(&v, aj, w, j);
1156 for(k = 0; k < rh; ++k){
1157 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
1162 opj_aligned_free(h.wavelet);
1168 /* Inverse 9-7 wavelet transform in 2-D. */
1170 opj_bool dwt_decode_real_v2(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 numres){
1174 opj_tcd_resolution_v2_t* res = tilec->resolutions;
1176 OPJ_UINT32 rw = res->x1 - res->x0; /* width of the resolution level computed */
1177 OPJ_UINT32 rh = res->y1 - res->y0; /* height of the resolution level computed */
1179 OPJ_UINT32 w = tilec->x1 - tilec->x0;
1181 h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution_v2(res, numres)+5) * sizeof(v4));
1182 v.wavelet = h.wavelet;
1185 OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
1186 OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
1194 rw = res->x1 - res->x0; /* width of the resolution level computed */
1195 rh = res->y1 - res->y0; /* height of the resolution level computed */
1198 h.cas = res->x0 % 2;
1200 for(j = rh; j > 3; j -= 4) {
1202 v4dwt_interleave_h(&h, aj, w, bufsize);
1205 for(k = rw; --k >= 0;){
1206 aj[k ] = h.wavelet[k].f[0];
1207 aj[k+w ] = h.wavelet[k].f[1];
1208 aj[k+w*2] = h.wavelet[k].f[2];
1209 aj[k+w*3] = h.wavelet[k].f[3];
1219 v4dwt_interleave_h(&h, aj, w, bufsize);
1221 for(k = rw; --k >= 0;){
1223 case 3: aj[k+w*2] = h.wavelet[k].f[2];
1224 case 2: aj[k+w ] = h.wavelet[k].f[1];
1225 case 1: aj[k ] = h.wavelet[k].f[0];
1231 v.cas = res->y0 % 2;
1233 aj = (OPJ_FLOAT32*) tilec->data;
1234 for(j = rw; j > 3; j -= 4){
1237 v4dwt_interleave_v(&v, aj, w, 4);
1240 for(k = 0; k < rh; ++k){
1241 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
1251 v4dwt_interleave_v(&v, aj, w, j);
1254 for(k = 0; k < rh; ++k){
1255 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
1260 opj_aligned_free(h.wavelet);