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 OPJ_FLOAT32 opj_dwt_alpha = 1.586134342f; /* 12994 */
68 static const OPJ_FLOAT32 opj_dwt_beta = 0.052980118f; /* 434 */
69 static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /* -7233 */
70 static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /* -3633 */
72 static const OPJ_FLOAT32 opj_K = 1.230174105f; /* 10078 */
73 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
78 Virtual function type for wavelet transform in 1-D
80 typedef void (*DWT1DFN)(dwt_t* v);
82 /** @name Local static functions */
86 Forward lazy transform (horizontal)
88 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
90 Forward lazy transform (vertical)
92 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
94 Inverse lazy transform (horizontal)
96 static void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a);
98 Inverse lazy transform (vertical)
100 static void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
102 Forward 5-3 wavelet transform in 1-D
104 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
106 Inverse 5-3 wavelet transform in 1-D
108 static void opj_dwt_decode_1(dwt_t *v);
109 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
111 Forward 9-7 wavelet transform in 1-D
113 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
115 Explicit calculation of the Quantization Stepsizes
117 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
119 Inverse wavelet transform in 2-D.
121 static opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
123 static opj_bool opj_dwt_encode_procedure( opj_tcd_tilecomp_v2_t * tilec,
124 void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
126 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i);
129 /* Inverse 9-7 wavelet transform in 1-D. */
131 static void opj_v4dwt_decode(v4dwt_t* restrict dwt);
133 static void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
135 static void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
138 static void opj_v4dwt_decode_step1_sse(v4* w, OPJ_INT32 count, const __m128 c);
140 static void opj_v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
143 static void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
145 static void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
153 #define S(i) a[(i)*2]
154 #define D(i) a[(1+(i)*2)]
155 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
156 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
158 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
159 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
162 /* This table contains the norms of the 5-3 wavelets for different bands. */
164 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
165 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
166 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
167 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
168 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
172 /* This table contains the norms of the 9-7 wavelets for different bands. */
174 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
175 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
176 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
177 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
178 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
182 ==========================================================
184 ==========================================================
188 /* Forward lazy transform (horizontal). */
190 void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
192 OPJ_INT32 * l_dest = b;
193 OPJ_INT32 * l_src = a+cas;
195 for (i=0; i<sn; ++i) {
203 for (i=0; i<dn; ++i) {
210 /* Forward lazy transform (vertical). */
212 void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
214 OPJ_INT32 * l_dest = b;
215 OPJ_INT32 * l_src = a+cas;
221 } /* b[i*x]=a[2*i+cas]; */
231 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
235 /* Inverse lazy transform (horizontal). */
237 void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) {
239 OPJ_INT32 *bi = h->mem + h->cas;
246 bi = h->mem + 1 - h->cas;
255 /* Inverse lazy transform (vertical). */
257 void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
259 OPJ_INT32 *bi = v->mem + v->cas;
266 ai = a + (v->sn * x);
267 bi = v->mem + 1 - v->cas;
278 /* Forward 5-3 wavelet transform in 1-D. */
280 void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
284 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
285 for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
286 for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
289 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
292 for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
293 for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
299 /* Inverse 5-3 wavelet transform in 1-D. */
301 void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
305 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
306 for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
307 for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
310 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
313 for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
314 for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
320 /* Inverse 5-3 wavelet transform in 1-D. */
322 void opj_dwt_decode_1(dwt_t *v) {
323 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
327 /* Forward 9-7 wavelet transform in 1-D. */
329 void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
332 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
333 for (i = 0; i < dn; i++)
334 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
335 for (i = 0; i < sn; i++)
336 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
337 for (i = 0; i < dn; i++)
338 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
339 for (i = 0; i < sn; i++)
340 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
341 for (i = 0; i < dn; i++)
342 D(i) = fix_mul(D(i), 5038); /*5038 */
343 for (i = 0; i < sn; i++)
344 S(i) = fix_mul(S(i), 6659); /*6660 */
347 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
348 for (i = 0; i < dn; i++)
349 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
350 for (i = 0; i < sn; i++)
351 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
352 for (i = 0; i < dn; i++)
353 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
354 for (i = 0; i < sn; i++)
355 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
356 for (i = 0; i < dn; i++)
357 S(i) = fix_mul(S(i), 5038); /*5038 */
358 for (i = 0; i < sn; i++)
359 D(i) = fix_mul(D(i), 6659); /*6660 */
364 void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
366 p = int_floorlog2(stepsize) - 13;
367 n = 11 - int_floorlog2(stepsize);
368 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
369 bandno_stepsize->expn = numbps - p;
373 ==========================================================
375 ==========================================================
380 /* Forward 5-3 wavelet transform in 2-D. */
382 INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_v2_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
390 OPJ_INT32 rw; /* width of the resolution level computed */
391 OPJ_INT32 rh; /* height of the resolution level computed */
392 OPJ_INT32 l_data_size;
394 opj_tcd_resolution_v2_t * l_cur_res = 0;
395 opj_tcd_resolution_v2_t * l_last_res = 0;
397 w = tilec->x1-tilec->x0;
398 l = tilec->numresolutions-1;
401 l_cur_res = tilec->resolutions + l;
402 l_last_res = l_cur_res - 1;
404 rw = l_cur_res->x1 - l_cur_res->x0;
405 rh = l_cur_res->y1 - l_cur_res->y0;
407 l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
408 bj = (OPJ_INT32*)opj_malloc(l_data_size);
415 OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
416 OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
417 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
418 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
421 rw = l_cur_res->x1 - l_cur_res->x0;
422 rh = l_cur_res->y1 - l_cur_res->y0;
423 rw1 = l_last_res->x1 - l_last_res->x0;
424 rh1 = l_last_res->y1 - l_last_res->y0;
426 cas_row = l_cur_res->x0 & 1;
427 cas_col = l_cur_res->y0 & 1;
431 for (j = 0; j < rw; ++j) {
433 for (k = 0; k < rh; ++k) {
437 (*p_function) (bj, dn, sn, cas_col);
439 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
445 for (j = 0; j < rh; j++) {
447 for (k = 0; k < rw; k++) bj[k] = aj[k];
448 (*p_function) (bj, dn, sn, cas_row);
449 opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
452 l_cur_res = l_last_res;
461 /* Forward 5-3 wavelet transform in 2-D. */
463 opj_bool opj_dwt_encode(opj_tcd_tilecomp_v2_t * tilec)
465 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
469 /* Inverse 5-3 wavelet transform in 2-D. */
471 opj_bool opj_dwt_decode(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
472 return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
477 /* Get gain of 5-3 wavelet transform. */
479 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
482 if (orient == 1 || orient == 2)
488 /* Get norm of 5-3 wavelet. */
490 double dwt_getnorm(int level, int orient) {
491 return opj_dwt_norms[orient][level];
495 /* Get norm of 5-3 wavelet. */
497 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
498 return opj_dwt_norms[orient][level];
502 /* Forward 9-7 wavelet transform in 2-D. */
504 opj_bool opj_dwt_encode_real(opj_tcd_tilecomp_v2_t * tilec)
506 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
510 /* Get gain of 9-7 wavelet transform. */
512 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
518 /* Get norm of 9-7 wavelet. */
520 double dwt_getnorm_real(int level, int orient) {
521 return opj_dwt_norms_real[orient][level];
525 /* Get norm of 9-7 wavelet. */
527 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
528 return opj_dwt_norms_real[orient][level];
531 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
532 OPJ_UINT32 numbands, bandno;
533 numbands = 3 * tccp->numresolutions - 2;
534 for (bandno = 0; bandno < numbands; bandno++) {
535 OPJ_FLOAT64 stepsize;
536 OPJ_UINT32 resno, level, orient, gain;
538 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
539 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
540 level = tccp->numresolutions - 1 - resno;
541 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
542 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
545 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
546 stepsize = (1 << (gain)) / norm;
548 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
554 /* Determine maximum computed resolution level for inverse wavelet transform */
556 static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
561 if( mr < ( w = r->x1 - r->x0 ) )
563 if( mr < ( w = r->y1 - r->y0 ) )
570 /* Determine maximum computed resolution level for inverse wavelet transform */
572 static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
577 if( mr < ( w = r->x1 - r->x0 ) )
579 if( mr < ( w = r->y1 - r->y0 ) )
586 /* Determine maximum computed resolution level for inverse wavelet transform */
588 OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i) {
593 if( mr < ( w = r->x1 - r->x0 ) )
595 if( mr < ( w = r->y1 - r->y0 ) )
602 /* Inverse wavelet transform in 2-D. */
604 opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
608 opj_tcd_resolution_v2_t* tr = tilec->resolutions;
610 OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */
611 OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */
613 OPJ_UINT32 w = tilec->x1 - tilec->x0;
616 opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
626 OPJ_INT32 * restrict tiledp = tilec->data;
633 rw = tr->x1 - tr->x0;
634 rh = tr->y1 - tr->y0;
639 for(j = 0; j < rh; ++j) {
640 opj_dwt_interleave_h(&h, &tiledp[j*w]);
642 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
648 for(j = 0; j < rw; ++j){
650 opj_dwt_interleave_v(&v, &tiledp[j], w);
652 for(k = 0; k < rh; ++k) {
653 tiledp[k * w + j] = v.mem[k];
657 opj_aligned_free(h.mem);
661 void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
662 OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
663 OPJ_INT32 count = w->sn;
666 for(k = 0; k < 2; ++k){
667 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
669 for(i = 0; i < count; ++i){
682 for(i = 0; i < count; ++i){
686 if(j >= size) continue;
689 if(j >= size) continue;
692 if(j >= size) continue;
693 bi[i*8 + 3] = a[j]; /* This one*/
697 bi = (float*) (w->wavelet + 1 - w->cas);
704 void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
705 v4* restrict bi = v->wavelet + v->cas;
708 for(i = 0; i < v->sn; ++i){
709 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
713 bi = v->wavelet + 1 - v->cas;
715 for(i = 0; i < v->dn; ++i){
716 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
722 void opj_v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
723 __m128* restrict vw = (__m128*) w;
725 /* 4x unrolled loop */
726 for(i = 0; i < count >> 2; ++i){
727 *vw = _mm_mul_ps(*vw, c);
729 *vw = _mm_mul_ps(*vw, c);
731 *vw = _mm_mul_ps(*vw, c);
733 *vw = _mm_mul_ps(*vw, c);
737 for(i = 0; i < count; ++i){
738 *vw = _mm_mul_ps(*vw, c);
743 void opj_v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
744 __m128* restrict vl = (__m128*) l;
745 __m128* restrict vw = (__m128*) w;
747 __m128 tmp1, tmp2, tmp3;
749 for(i = 0; i < m; ++i){
752 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
760 c = _mm_add_ps(c, c);
761 c = _mm_mul_ps(c, vl[0]);
764 vw[-1] = _mm_add_ps(tmp, c);
771 void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
773 OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
775 for(i = 0; i < count; ++i){
776 OPJ_FLOAT32 tmp1 = fw[i*8 ];
777 OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
778 OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
779 OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
781 fw[i*8 + 1] = tmp2 * c;
782 fw[i*8 + 2] = tmp3 * c;
783 fw[i*8 + 3] = tmp4 * c;
787 void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
789 OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
790 OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
792 for(i = 0; i < m; ++i){
793 OPJ_FLOAT32 tmp1_1 = fl[0];
794 OPJ_FLOAT32 tmp1_2 = fl[1];
795 OPJ_FLOAT32 tmp1_3 = fl[2];
796 OPJ_FLOAT32 tmp1_4 = fl[3];
797 OPJ_FLOAT32 tmp2_1 = fw[-4];
798 OPJ_FLOAT32 tmp2_2 = fw[-3];
799 OPJ_FLOAT32 tmp2_3 = fw[-2];
800 OPJ_FLOAT32 tmp2_4 = fw[-1];
801 OPJ_FLOAT32 tmp3_1 = fw[0];
802 OPJ_FLOAT32 tmp3_2 = fw[1];
803 OPJ_FLOAT32 tmp3_3 = fw[2];
804 OPJ_FLOAT32 tmp3_4 = fw[3];
805 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
806 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
807 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
808 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
823 OPJ_FLOAT32 tmp1 = fw[-4];
824 OPJ_FLOAT32 tmp2 = fw[-3];
825 OPJ_FLOAT32 tmp3 = fw[-2];
826 OPJ_FLOAT32 tmp4 = fw[-1];
839 /* Inverse 9-7 wavelet transform in 1-D. */
841 void opj_v4dwt_decode(v4dwt_t* restrict dwt)
845 if(!((dwt->dn > 0) || (dwt->sn > 1))){
851 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
858 opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
859 opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
860 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_delta));
861 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_gamma));
862 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_beta));
863 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_alpha));
865 opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
866 opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
867 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
868 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
869 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
870 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
875 /* KEEP TRUNK VERSION + return type of v2 because rev557 */
877 /* Inverse 9-7 wavelet transform in 2-D. */
879 /* V1 void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ */
880 opj_bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres)
885 opj_tcd_resolution_t* res = tilec->resolutions;
887 int rw = res->x1 - res->x0; /* width of the resolution level computed */
888 int rh = res->y1 - res->y0; /* height of the resolution level computed */
890 int w = tilec->x1 - tilec->x0;
892 h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4));
893 v.wavelet = h.wavelet;
896 float * restrict aj = (float*) tilec->data;
897 int bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
905 rw = res->x1 - res->x0; /* width of the resolution level computed */
906 rh = res->y1 - res->y0; /* height of the resolution level computed */
911 for(j = rh; j > 3; j -= 4){
913 opj_v4dwt_interleave_h(&h, aj, w, bufsize);
914 opj_v4dwt_decode(&h);
915 for(k = rw; --k >= 0;){
916 aj[k ] = h.wavelet[k].f[0];
917 aj[k+w ] = h.wavelet[k].f[1];
918 aj[k+w*2] = h.wavelet[k].f[2];
919 aj[k+w*3] = h.wavelet[k].f[3];
927 opj_v4dwt_interleave_h(&h, aj, w, bufsize);
928 opj_v4dwt_decode(&h);
929 for(k = rw; --k >= 0;){
931 case 3: aj[k+w*2] = h.wavelet[k].f[2];
932 case 2: aj[k+w ] = h.wavelet[k].f[1];
933 case 1: aj[k ] = h.wavelet[k].f[0];
941 aj = (float*) tilec->data;
942 for(j = rw; j > 3; j -= 4){
944 opj_v4dwt_interleave_v(&v, aj, w, 4);
945 opj_v4dwt_decode(&v);
946 for(k = 0; k < rh; ++k){
947 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
954 opj_v4dwt_interleave_v(&v, aj, w, j);
955 opj_v4dwt_decode(&v);
956 for(k = 0; k < rh; ++k){
957 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
962 opj_aligned_free(h.wavelet);
968 /* Inverse 9-7 wavelet transform in 2-D. */
970 opj_bool dwt_decode_real_v2(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 numres)
975 opj_tcd_resolution_v2_t* res = tilec->resolutions;
977 OPJ_UINT32 rw = res->x1 - res->x0; /* width of the resolution level computed */
978 OPJ_UINT32 rh = res->y1 - res->y0; /* height of the resolution level computed */
980 OPJ_UINT32 w = tilec->x1 - tilec->x0;
982 h.wavelet = (v4*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(v4));
983 v.wavelet = h.wavelet;
986 OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
987 OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
995 rw = res->x1 - res->x0; /* width of the resolution level computed */
996 rh = res->y1 - res->y0; /* height of the resolution level computed */
1001 for(j = rh; j > 3; j -= 4) {
1003 opj_v4dwt_interleave_h(&h, aj, w, bufsize);
1004 opj_v4dwt_decode(&h);
1006 for(k = rw; --k >= 0;){
1007 aj[k ] = h.wavelet[k].f[0];
1008 aj[k+w ] = h.wavelet[k].f[1];
1009 aj[k+w*2] = h.wavelet[k].f[2];
1010 aj[k+w*3] = h.wavelet[k].f[3];
1020 opj_v4dwt_interleave_h(&h, aj, w, bufsize);
1021 opj_v4dwt_decode(&h);
1022 for(k = rw; --k >= 0;){
1024 case 3: aj[k+w*2] = h.wavelet[k].f[2];
1025 case 2: aj[k+w ] = h.wavelet[k].f[1];
1026 case 1: aj[k ] = h.wavelet[k].f[0];
1032 v.cas = res->y0 % 2;
1034 aj = (OPJ_FLOAT32*) tilec->data;
1035 for(j = rw; j > 3; j -= 4){
1038 opj_v4dwt_interleave_v(&v, aj, w, 4);
1039 opj_v4dwt_decode(&v);
1041 for(k = 0; k < rh; ++k){
1042 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
1052 opj_v4dwt_interleave_v(&v, aj, w, j);
1053 opj_v4dwt_decode(&v);
1055 for(k = 0; k < rh; ++k){
1056 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
1061 opj_aligned_free(h.wavelet);