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);
135 #define S(i) a[(i)*2]
136 #define D(i) a[(1+(i)*2)]
137 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
138 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
140 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
141 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
144 /* This table contains the norms of the 5-3 wavelets for different bands. */
146 static const double dwt_norms[4][10] = {
147 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
148 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
149 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
150 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
154 /* This table contains the norms of the 9-7 wavelets for different bands. */
156 static const double dwt_norms_real[4][10] = {
157 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
158 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
159 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
160 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
164 ==========================================================
166 ==========================================================
170 /* Forward lazy transform (horizontal). */
172 static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
174 for (i=0; i<sn; i++) b[i]=a[2*i+cas];
175 for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
179 /* Forward lazy transform (vertical). */
181 static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
183 for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
184 for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
188 /* Inverse lazy transform (horizontal). */
190 static void dwt_interleave_h(dwt_t* h, int *a) {
192 int *bi = h->mem + h->cas;
199 bi = h->mem + 1 - h->cas;
208 /* Inverse lazy transform (vertical). */
210 static void dwt_interleave_v(dwt_t* v, int *a, int x) {
212 int *bi = v->mem + v->cas;
219 ai = a + (v->sn * x);
220 bi = v->mem + 1 - v->cas;
231 /* Forward 5-3 wavelet transform in 1-D. */
233 static void dwt_encode_1(int *a, int dn, int sn, int cas) {
237 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
238 for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
239 for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
242 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
245 for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
246 for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
252 /* Inverse 5-3 wavelet transform in 1-D. */
254 static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
258 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
259 for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
260 for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
263 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
266 for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
267 for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
273 /* Inverse 5-3 wavelet transform in 1-D. */
275 static void dwt_decode_1(dwt_t *v) {
276 dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
280 /* Forward 9-7 wavelet transform in 1-D. */
282 static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
285 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
286 for (i = 0; i < dn; i++)
287 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
288 for (i = 0; i < sn; i++)
289 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
290 for (i = 0; i < dn; i++)
291 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
292 for (i = 0; i < sn; i++)
293 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
294 for (i = 0; i < dn; i++)
295 D(i) = fix_mul(D(i), 5038); /*5038 */
296 for (i = 0; i < sn; i++)
297 S(i) = fix_mul(S(i), 6659); /*6660 */
300 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
301 for (i = 0; i < dn; i++)
302 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
303 for (i = 0; i < sn; i++)
304 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
305 for (i = 0; i < dn; i++)
306 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
307 for (i = 0; i < sn; i++)
308 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
309 for (i = 0; i < dn; i++)
310 S(i) = fix_mul(S(i), 5038); /*5038 */
311 for (i = 0; i < sn; i++)
312 D(i) = fix_mul(D(i), 6659); /*6660 */
317 static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) {
319 p = int_floorlog2(stepsize) - 13;
320 n = 11 - int_floorlog2(stepsize);
321 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
322 bandno_stepsize->expn = numbps - p;
326 ==========================================================
328 ==========================================================
332 /* Forward 5-3 wavelet transform in 2-D. */
334 void dwt_encode(opj_tcd_tilecomp_t * tilec) {
341 w = tilec->x1-tilec->x0;
342 l = tilec->numresolutions-1;
345 for (i = 0; i < l; i++) {
346 int rw; /* width of the resolution level computed */
347 int rh; /* height of the resolution level computed */
348 int rw1; /* width of the resolution level once lower than computed one */
349 int rh1; /* height of the resolution level once lower than computed one */
350 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
351 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
354 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
355 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
356 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
357 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
359 cas_row = tilec->resolutions[l - i].x0 % 2;
360 cas_col = tilec->resolutions[l - i].y0 % 2;
364 bj = (int*)opj_malloc(rh * sizeof(int));
365 for (j = 0; j < rw; j++) {
367 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
368 dwt_encode_1(bj, dn, sn, cas_col);
369 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
375 bj = (int*)opj_malloc(rw * sizeof(int));
376 for (j = 0; j < rh; j++) {
378 for (k = 0; k < rw; k++) bj[k] = aj[k];
379 dwt_encode_1(bj, dn, sn, cas_row);
380 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
388 /* Inverse 5-3 wavelet transform in 2-D. */
390 void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) {
391 dwt_decode_tile(tilec, numres, &dwt_decode_1);
396 /* Inverse 5-3 wavelet transform in 2-D. */
398 opj_bool dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
399 return dwt_decode_tile(tilec, numres, &dwt_decode_1);
403 /* Inverse 5-3 wavelet transform in 2-D. */
405 opj_bool dwt_decode_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
406 return dwt_decode_tile_v2(tilec, numres, &dwt_decode_1);
411 /* Get gain of 5-3 wavelet transform. */
413 int dwt_getgain(int orient) {
416 if (orient == 1 || orient == 2)
422 /* Get gain of 5-3 wavelet transform. */
424 OPJ_UINT32 dwt_getgain_v2(OPJ_UINT32 orient) {
427 if (orient == 1 || orient == 2)
433 /* Get norm of 5-3 wavelet. */
435 double dwt_getnorm(int level, int orient) {
436 return dwt_norms[orient][level];
440 /* Forward 9-7 wavelet transform in 2-D. */
443 void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
450 w = tilec->x1-tilec->x0;
451 l = tilec->numresolutions-1;
454 for (i = 0; i < l; i++) {
455 int rw; /* width of the resolution level computed */
456 int rh; /* height of the resolution level computed */
457 int rw1; /* width of the resolution level once lower than computed one */
458 int rh1; /* height of the resolution level once lower than computed one */
459 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
460 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
463 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
464 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
465 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
466 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
468 cas_row = tilec->resolutions[l - i].x0 % 2;
469 cas_col = tilec->resolutions[l - i].y0 % 2;
473 bj = (int*)opj_malloc(rh * sizeof(int));
474 for (j = 0; j < rw; j++) {
476 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
477 dwt_encode_1_real(bj, dn, sn, cas_col);
478 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
484 bj = (int*)opj_malloc(rw * sizeof(int));
485 for (j = 0; j < rh; j++) {
487 for (k = 0; k < rw; k++) bj[k] = aj[k];
488 dwt_encode_1_real(bj, dn, sn, cas_row);
489 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
497 /* Get gain of 9-7 wavelet transform. */
499 int dwt_getgain_real(int orient) {
505 /* Get gain of 9-7 wavelet transform. */
507 OPJ_UINT32 dwt_getgain_real_v2(OPJ_UINT32 orient) {
513 /* Get norm of 9-7 wavelet. */
515 double dwt_getnorm_real(int level, int orient) {
516 return dwt_norms_real[orient][level];
519 void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
520 int numbands, bandno;
521 numbands = 3 * tccp->numresolutions - 2;
522 for (bandno = 0; bandno < numbands; bandno++) {
524 int resno, level, orient, gain;
526 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
527 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
528 level = tccp->numresolutions - 1 - resno;
529 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
530 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
533 double norm = dwt_norms_real[orient][level];
534 stepsize = (1 << (gain)) / norm;
536 dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
542 /* Determine maximum computed resolution level for inverse wavelet transform */
544 static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
549 if( mr < ( w = r->x1 - r->x0 ) )
551 if( mr < ( w = r->y1 - r->y0 ) )
558 /* Determine maximum computed resolution level for inverse wavelet transform */
560 static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
565 if( mr < ( w = r->x1 - r->x0 ) )
567 if( mr < ( w = r->y1 - r->y0 ) )
574 /* Determine maximum computed resolution level for inverse wavelet transform */
576 static OPJ_UINT32 dwt_max_resolution_v2(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i) {
581 if( mr < ( w = r->x1 - r->x0 ) )
583 if( mr < ( w = r->y1 - r->y0 ) )
591 /* Inverse wavelet transform in 2-D. */
593 static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) {
597 opj_tcd_resolution_t* tr = tilec->resolutions;
599 int rw = tr->x1 - tr->x0; /* width of the resolution level computed */
600 int rh = tr->y1 - tr->y0; /* height of the resolution level computed */
602 int w = tilec->x1 - tilec->x0;
604 h.mem = (int*)opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(int));
608 int * restrict tiledp = tilec->data;
615 rw = tr->x1 - tr->x0;
616 rh = tr->y1 - tr->y0;
621 for(j = 0; j < rh; ++j) {
622 dwt_interleave_h(&h, &tiledp[j*w]);
624 memcpy(&tiledp[j*w], h.mem, rw * sizeof(int));
630 for(j = 0; j < rw; ++j){
632 dwt_interleave_v(&v, &tiledp[j], w);
634 for(k = 0; k < rh; ++k) {
635 tiledp[k * w + j] = v.mem[k];
639 opj_aligned_free(h.mem);
644 /* Inverse wavelet transform in 2-D. */
646 static opj_bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
650 opj_tcd_resolution_t* tr = tilec->resolutions;
652 OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */
653 OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */
655 OPJ_UINT32 w = tilec->x1 - tilec->x0;
658 opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
668 OPJ_INT32 * restrict tiledp = tilec->data;
675 rw = tr->x1 - tr->x0;
676 rh = tr->y1 - tr->y0;
681 for(j = 0; j < rh; ++j) {
682 dwt_interleave_h(&h, &tiledp[j*w]);
684 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
690 for(j = 0; j < rw; ++j){
692 dwt_interleave_v(&v, &tiledp[j], w);
694 for(k = 0; k < rh; ++k) {
695 tiledp[k * w + j] = v.mem[k];
699 opj_aligned_free(h.mem);
704 /* Inverse wavelet transform in 2-D. */
706 static opj_bool dwt_decode_tile_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
710 opj_tcd_resolution_v2_t* tr = tilec->resolutions;
712 OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */
713 OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */
715 OPJ_UINT32 w = tilec->x1 - tilec->x0;
718 opj_aligned_malloc(dwt_max_resolution_v2(tr, numres) * sizeof(OPJ_INT32));
728 OPJ_INT32 * restrict tiledp = tilec->data;
735 rw = tr->x1 - tr->x0;
736 rh = tr->y1 - tr->y0;
741 for(j = 0; j < rh; ++j) {
742 dwt_interleave_h(&h, &tiledp[j*w]);
744 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
750 for(j = 0; j < rw; ++j){
752 dwt_interleave_v(&v, &tiledp[j], w);
754 for(k = 0; k < rh; ++k) {
755 tiledp[k * w + j] = v.mem[k];
759 opj_aligned_free(h.mem);
763 static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size){
764 float* restrict bi = (float*) (w->wavelet + w->cas);
768 for(k = 0; k < 2; ++k){
769 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
771 for(i = 0; i < count; ++i){
784 for(i = 0; i < count; ++i){
788 if(j >= size) continue;
791 if(j >= size) continue;
794 if(j >= size) continue;
795 bi[i*8 + 3] = a[j]; /* This one*/
799 bi = (float*) (w->wavelet + 1 - w->cas);
806 static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x, int nb_elts_read){
807 v4* restrict bi = v->wavelet + v->cas;
810 for(i = 0; i < v->sn; ++i){
811 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(float));
815 bi = v->wavelet + 1 - v->cas;
817 for(i = 0; i < v->dn; ++i){
818 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(float));
824 static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
825 __m128* restrict vw = (__m128*) w;
827 /* 4x unrolled loop */
828 for(i = 0; i < count >> 2; ++i){
829 *vw = _mm_mul_ps(*vw, c);
831 *vw = _mm_mul_ps(*vw, c);
833 *vw = _mm_mul_ps(*vw, c);
835 *vw = _mm_mul_ps(*vw, c);
839 for(i = 0; i < count; ++i){
840 *vw = _mm_mul_ps(*vw, c);
845 static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
846 __m128* restrict vl = (__m128*) l;
847 __m128* restrict vw = (__m128*) w;
849 __m128 tmp1, tmp2, tmp3;
851 for(i = 0; i < m; ++i){
854 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
862 c = _mm_add_ps(c, c);
863 c = _mm_mul_ps(c, vl[0]);
866 vw[-1] = _mm_add_ps(tmp, c);
873 static void v4dwt_decode_step1(v4* w, int count, const float c){
874 float* restrict fw = (float*) w;
876 for(i = 0; i < count; ++i){
877 float tmp1 = fw[i*8 ];
878 float tmp2 = fw[i*8 + 1];
879 float tmp3 = fw[i*8 + 2];
880 float tmp4 = fw[i*8 + 3];
882 fw[i*8 + 1] = tmp2 * c;
883 fw[i*8 + 2] = tmp3 * c;
884 fw[i*8 + 3] = tmp4 * c;
888 static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
889 float* restrict fl = (float*) l;
890 float* restrict fw = (float*) w;
892 for(i = 0; i < m; ++i){
893 float tmp1_1 = fl[0];
894 float tmp1_2 = fl[1];
895 float tmp1_3 = fl[2];
896 float tmp1_4 = fl[3];
897 float tmp2_1 = fw[-4];
898 float tmp2_2 = fw[-3];
899 float tmp2_3 = fw[-2];
900 float tmp2_4 = fw[-1];
901 float tmp3_1 = fw[0];
902 float tmp3_2 = fw[1];
903 float tmp3_3 = fw[2];
904 float tmp3_4 = fw[3];
905 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
906 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
907 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
908 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
939 /* Inverse 9-7 wavelet transform in 1-D. */
941 static void v4dwt_decode(v4dwt_t* restrict dwt){
944 if(!((dwt->dn > 0) || (dwt->sn > 1))){
950 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
957 v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K));
958 v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318));
959 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));
960 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));
961 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));
962 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));
964 v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K);
965 v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318);
966 v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta);
967 v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma);
968 v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta);
969 v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha);
974 // KEEP TRUNK VERSION + return type of v2 because rev557
976 /* Inverse 9-7 wavelet transform in 2-D. */
978 // V1 void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
979 opj_bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
983 opj_tcd_resolution_t* res = tilec->resolutions;
985 int rw = res->x1 - res->x0; /* width of the resolution level computed */
986 int rh = res->y1 - res->y0; /* height of the resolution level computed */
988 int w = tilec->x1 - tilec->x0;
990 h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4));
991 v.wavelet = h.wavelet;
994 float * restrict aj = (float*) tilec->data;
995 int bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
1003 rw = res->x1 - res->x0; /* width of the resolution level computed */
1004 rh = res->y1 - res->y0; /* height of the resolution level computed */
1007 h.cas = res->x0 % 2;
1009 for(j = rh; j > 3; j -= 4){
1011 v4dwt_interleave_h(&h, aj, w, bufsize);
1013 for(k = rw; --k >= 0;){
1014 aj[k ] = h.wavelet[k].f[0];
1015 aj[k+w ] = h.wavelet[k].f[1];
1016 aj[k+w*2] = h.wavelet[k].f[2];
1017 aj[k+w*3] = h.wavelet[k].f[3];
1025 v4dwt_interleave_h(&h, aj, w, bufsize);
1027 for(k = rw; --k >= 0;){
1029 case 3: aj[k+w*2] = h.wavelet[k].f[2];
1030 case 2: aj[k+w ] = h.wavelet[k].f[1];
1031 case 1: aj[k ] = h.wavelet[k].f[0];
1037 v.cas = res->y0 % 2;
1039 aj = (float*) tilec->data;
1040 for(j = rw; j > 3; j -= 4){
1042 v4dwt_interleave_v(&v, aj, w, 4);
1044 for(k = 0; k < rh; ++k){
1045 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
1052 v4dwt_interleave_v(&v, aj, w, j);
1054 for(k = 0; k < rh; ++k){
1055 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
1060 opj_aligned_free(h.wavelet);
1066 /* Inverse 9-7 wavelet transform in 2-D. */
1068 opj_bool dwt_decode_real_v2(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 numres){
1072 opj_tcd_resolution_v2_t* res = tilec->resolutions;
1074 OPJ_UINT32 rw = res->x1 - res->x0; /* width of the resolution level computed */
1075 OPJ_UINT32 rh = res->y1 - res->y0; /* height of the resolution level computed */
1077 OPJ_UINT32 w = tilec->x1 - tilec->x0;
1079 h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution_v2(res, numres)+5) * sizeof(v4));
1080 v.wavelet = h.wavelet;
1083 OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
1084 OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
1092 rw = res->x1 - res->x0; /* width of the resolution level computed */
1093 rh = res->y1 - res->y0; /* height of the resolution level computed */
1096 h.cas = res->x0 % 2;
1098 for(j = rh; j > 3; j -= 4) {
1100 v4dwt_interleave_h(&h, aj, w, bufsize);
1103 for(k = rw; --k >= 0;){
1104 aj[k ] = h.wavelet[k].f[0];
1105 aj[k+w ] = h.wavelet[k].f[1];
1106 aj[k+w*2] = h.wavelet[k].f[2];
1107 aj[k+w*3] = h.wavelet[k].f[3];
1117 v4dwt_interleave_h(&h, aj, w, bufsize);
1119 for(k = rw; --k >= 0;){
1121 case 3: aj[k+w*2] = h.wavelet[k].f[2];
1122 case 2: aj[k+w ] = h.wavelet[k].f[1];
1123 case 1: aj[k ] = h.wavelet[k].f[0];
1129 v.cas = res->y0 % 2;
1131 aj = (OPJ_FLOAT32*) tilec->data;
1132 for(j = rw; j > 3; j -= 4){
1135 v4dwt_interleave_v(&v, aj, w, 4);
1138 for(k = 0; k < rh; ++k){
1139 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
1149 v4dwt_interleave_v(&v, aj, w, j);
1152 for(k = 0; k < rh; ++k){
1153 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
1158 opj_aligned_free(h.wavelet);