2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
15 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 #include <xmmintrin.h>
44 #include "opj_includes.h"
46 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
49 #define OPJ_WS(i) v->mem[(i)*2]
50 #define OPJ_WD(i) v->mem[(1+(i)*2)]
52 /** @name Local data structures */
55 typedef struct dwt_local {
66 typedef struct v4dwt_local {
73 static const OPJ_FLOAT32 opj_dwt_alpha = 1.586134342f; /* 12994 */
74 static const OPJ_FLOAT32 opj_dwt_beta = 0.052980118f; /* 434 */
75 static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /* -7233 */
76 static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /* -3633 */
78 static const OPJ_FLOAT32 opj_K = 1.230174105f; /* 10078 */
79 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
84 Virtual function type for wavelet transform in 1-D
86 typedef void (*DWT1DFN)(opj_dwt_t* v);
88 /** @name Local static functions */
92 Forward lazy transform (horizontal)
94 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
96 Forward lazy transform (vertical)
98 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
100 Inverse lazy transform (horizontal)
102 static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a);
104 Inverse lazy transform (vertical)
106 static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
108 Forward 5-3 wavelet transform in 1-D
110 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
112 Inverse 5-3 wavelet transform in 1-D
114 static void opj_dwt_decode_1(opj_dwt_t *v);
115 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
117 Forward 9-7 wavelet transform in 1-D
119 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
121 Explicit calculation of the Quantization Stepsizes
123 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
125 Inverse wavelet transform in 2-D.
127 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
129 static OPJ_BOOL opj_dwt_encode_procedure( opj_tcd_tilecomp_t * tilec,
130 void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
132 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, OPJ_UINT32 i);
135 /* Inverse 9-7 wavelet transform in 1-D. */
137 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt);
139 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w, OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size);
141 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT v , OPJ_FLOAT32* OPJ_RESTRICT a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
144 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c);
146 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
149 static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
151 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
159 #define OPJ_S(i) a[(i)*2]
160 #define OPJ_D(i) a[(1+(i)*2)]
161 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
162 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
164 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
165 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
168 /* This table contains the norms of the 5-3 wavelets for different bands. */
170 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
171 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
172 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
173 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
174 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
178 /* This table contains the norms of the 9-7 wavelets for different bands. */
180 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
181 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
182 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
183 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
184 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
188 ==========================================================
190 ==========================================================
194 /* Forward lazy transform (horizontal). */
196 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
198 OPJ_INT32 * l_dest = b;
199 OPJ_INT32 * l_src = a+cas;
201 for (i=0; i<sn; ++i) {
209 for (i=0; i<dn; ++i) {
216 /* Forward lazy transform (vertical). */
218 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
220 OPJ_INT32 * l_dest = b;
221 OPJ_INT32 * l_src = a+cas;
227 } /* b[i*x]=a[2*i+cas]; */
237 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
241 /* Inverse lazy transform (horizontal). */
243 static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a) {
245 OPJ_INT32 *bi = h->mem + h->cas;
252 bi = h->mem + 1 - h->cas;
261 /* Inverse lazy transform (vertical). */
263 static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
265 OPJ_INT32 *bi = v->mem + v->cas;
272 ai = a + (v->sn * x);
273 bi = v->mem + 1 - v->cas;
284 /* Forward 5-3 wavelet transform in 1-D. */
286 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
290 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
291 for (i = 0; i < dn; i++) OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
292 for (i = 0; i < sn; i++) OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
295 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
298 for (i = 0; i < dn; i++) OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
299 for (i = 0; i < sn; i++) OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
305 /* Inverse 5-3 wavelet transform in 1-D. */
307 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
311 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
312 for (i = 0; i < sn; i++) OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
313 for (i = 0; i < dn; i++) OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
316 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
319 for (i = 0; i < sn; i++) OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
320 for (i = 0; i < dn; i++) OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
326 /* Inverse 5-3 wavelet transform in 1-D. */
328 static void opj_dwt_decode_1(opj_dwt_t *v) {
329 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
333 /* Forward 9-7 wavelet transform in 1-D. */
335 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
338 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
339 for (i = 0; i < dn; i++)
340 OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
341 for (i = 0; i < sn; i++)
342 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
343 for (i = 0; i < dn; i++)
344 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
345 for (i = 0; i < sn; i++)
346 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
347 for (i = 0; i < dn; i++)
348 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038); /*5038 */
349 for (i = 0; i < sn; i++)
350 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659); /*6660 */
353 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
354 for (i = 0; i < dn; i++)
355 OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
356 for (i = 0; i < sn; i++)
357 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
358 for (i = 0; i < dn; i++)
359 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
360 for (i = 0; i < sn; i++)
361 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
362 for (i = 0; i < dn; i++)
363 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038); /*5038 */
364 for (i = 0; i < sn; i++)
365 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659); /*6660 */
370 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
372 p = opj_int_floorlog2(stepsize) - 13;
373 n = 11 - opj_int_floorlog2(stepsize);
374 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
375 bandno_stepsize->expn = numbps - p;
379 ==========================================================
381 ==========================================================
386 /* Forward 5-3 wavelet transform in 2-D. */
388 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
396 OPJ_INT32 rw; /* width of the resolution level computed */
397 OPJ_INT32 rh; /* height of the resolution level computed */
398 OPJ_UINT32 l_data_size;
400 opj_tcd_resolution_t * l_cur_res = 0;
401 opj_tcd_resolution_t * l_last_res = 0;
403 w = tilec->x1-tilec->x0;
404 l = (OPJ_INT32)tilec->numresolutions-1;
407 l_cur_res = tilec->resolutions + l;
408 l_last_res = l_cur_res - 1;
410 l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * (OPJ_UINT32)sizeof(OPJ_INT32);
411 bj = (OPJ_INT32*)opj_malloc((size_t)l_data_size);
412 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
413 /* in that case, so do not error out */
414 if (l_data_size != 0 && ! bj) {
420 OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
421 OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
422 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
423 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
426 rw = l_cur_res->x1 - l_cur_res->x0;
427 rh = l_cur_res->y1 - l_cur_res->y0;
428 rw1 = l_last_res->x1 - l_last_res->x0;
429 rh1 = l_last_res->y1 - l_last_res->y0;
431 cas_row = l_cur_res->x0 & 1;
432 cas_col = l_cur_res->y0 & 1;
436 for (j = 0; j < rw; ++j) {
438 for (k = 0; k < rh; ++k) {
442 (*p_function) (bj, dn, sn, cas_col);
444 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
450 for (j = 0; j < rh; j++) {
452 for (k = 0; k < rw; k++) bj[k] = aj[k];
453 (*p_function) (bj, dn, sn, cas_row);
454 opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
457 l_cur_res = l_last_res;
466 /* Forward 5-3 wavelet transform in 2-D. */
468 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
470 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
474 /* Inverse 5-3 wavelet transform in 2-D. */
476 OPJ_BOOL opj_dwt_decode(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
477 return opj_dwt_decode_tile(tp, tilec, numres, &opj_dwt_decode_1);
482 /* Get gain of 5-3 wavelet transform. */
484 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
487 if (orient == 1 || orient == 2)
493 /* Get norm of 5-3 wavelet. */
495 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
496 return opj_dwt_norms[orient][level];
500 /* Forward 9-7 wavelet transform in 2-D. */
502 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
504 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
508 /* Get gain of 9-7 wavelet transform. */
510 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
516 /* Get norm of 9-7 wavelet. */
518 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
519 return opj_dwt_norms_real[orient][level];
522 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
523 OPJ_UINT32 numbands, bandno;
524 numbands = 3 * tccp->numresolutions - 2;
525 for (bandno = 0; bandno < numbands; bandno++) {
526 OPJ_FLOAT64 stepsize;
527 OPJ_UINT32 resno, level, orient, gain;
529 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
530 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
531 level = tccp->numresolutions - 1 - resno;
532 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
533 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
536 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
537 stepsize = (1 << (gain)) / norm;
539 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
544 /* Determine maximum computed resolution level for inverse wavelet transform */
546 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, OPJ_UINT32 i) {
551 if( mr < ( w = (OPJ_UINT32)(r->x1 - r->x0) ) )
553 if( mr < ( w = (OPJ_UINT32)(r->y1 - r->y0) ) )
565 OPJ_INT32 * OPJ_RESTRICT tiledp;
568 } opj_dwd_decode_h_job_t;
570 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
573 opj_dwd_decode_h_job_t* job;
576 job = (opj_dwd_decode_h_job_t*)user_data;
577 for( j = job->min_j; j < job->max_j; j++ )
579 opj_dwt_interleave_h(&job->h, &job->tiledp[j*job->w]);
580 (job->dwt_1D)(&job->h);
581 memcpy(&job->tiledp[j*job->w], job->h.mem, job->rw * sizeof(OPJ_INT32));
584 opj_aligned_free(job->h.mem);
594 OPJ_INT32 * OPJ_RESTRICT tiledp;
597 } opj_dwd_decode_v_job_t;
599 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
602 opj_dwd_decode_v_job_t* job;
605 job = (opj_dwd_decode_v_job_t*)user_data;
606 for( j = job->min_j; j < job->max_j; j++ )
609 opj_dwt_interleave_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w);
610 (job->dwt_1D)(&job->v);
611 for(k = 0; k < job->rh; ++k) {
612 job->tiledp[k * job->w + j] = job->v.mem[k];
616 opj_aligned_free(job->v.mem);
622 /* Inverse wavelet transform in 2-D. */
624 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
628 opj_tcd_resolution_t* tr = tilec->resolutions;
630 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - tr->x0); /* width of the resolution level computed */
631 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - tr->y0); /* height of the resolution level computed */
633 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
640 num_threads = opj_thread_pool_get_thread_count(tp);
641 h_mem_size = opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32);
642 h.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
644 /* FIXME event manager error callback */
651 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
655 h.sn = (OPJ_INT32)rw;
656 v.sn = (OPJ_INT32)rh;
658 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
659 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
661 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
664 if( num_threads <= 1 || rh == 1 )
666 for(j = 0; j < rh; ++j) {
667 opj_dwt_interleave_h(&h, &tiledp[j*w]);
669 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
674 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
675 if( rh < num_jobs ) {
678 for( j = 0; j < num_jobs; j++ )
680 opj_dwd_decode_h_job_t* job;
682 job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
685 /* It would be nice to fallback to single thread case, but */
686 /* unfortunately some jobs may be launched and have modified */
687 /* tiledp, so it is not practical to recover from that error */
688 /* FIXME event manager error callback */
689 opj_thread_pool_wait_completion(tp, 0);
690 opj_aligned_free(h.mem);
694 job->dwt_1D = dwt_1D;
697 job->tiledp = tiledp;
698 job->min_j = j * (rh / num_jobs);
699 job->max_j = (j+1) * (rh / num_jobs); /* TODO this can overflow */
700 if( job->max_j > rh || j == num_jobs - 1 )
702 job->h.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
705 /* FIXME event manager error callback */
706 opj_thread_pool_wait_completion(tp, 0);
708 opj_aligned_free(h.mem);
711 opj_thread_pool_submit_job( tp, opj_dwt_decode_h_func, job );
713 opj_thread_pool_wait_completion(tp, 0);
716 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
719 if( num_threads <= 1 || rw == 1 )
721 for(j = 0; j < rw; ++j){
723 opj_dwt_interleave_v(&v, &tiledp[j], (OPJ_INT32)w);
725 for(k = 0; k < rh; ++k) {
726 tiledp[k * w + j] = v.mem[k];
732 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
735 for( j = 0; j < num_jobs; j++ )
737 opj_dwd_decode_v_job_t* job;
739 job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
742 /* It would be nice to fallback to single thread case, but */
743 /* unfortunately some jobs may be launched and have modified */
744 /* tiledp, so it is not practical to recover from that error */
745 /* FIXME event manager error callback */
746 opj_thread_pool_wait_completion(tp, 0);
747 opj_aligned_free(v.mem);
751 job->dwt_1D = dwt_1D;
754 job->tiledp = tiledp;
755 job->min_j = j * (rw / num_jobs);
756 job->max_j = (j+1) * (rw / num_jobs); /* TODO this can overflow */
757 if( job->max_j > rw || j == num_jobs - 1 )
759 job->v.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
762 /* FIXME event manager error callback */
763 opj_thread_pool_wait_completion(tp, 0);
765 opj_aligned_free(v.mem);
768 opj_thread_pool_submit_job( tp, opj_dwt_decode_v_func, job );
770 opj_thread_pool_wait_completion(tp, 0);
773 opj_aligned_free(h.mem);
777 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w, OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size){
778 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
779 OPJ_INT32 count = w->sn;
782 for(k = 0; k < 2; ++k){
783 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
785 for(i = 0; i < count; ++i){
798 for(i = 0; i < count; ++i){
802 if(j >= size) continue;
805 if(j >= size) continue;
808 if(j >= size) continue;
809 bi[i*8 + 3] = a[j]; /* This one*/
813 bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas);
820 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT v , OPJ_FLOAT32* OPJ_RESTRICT a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
821 opj_v4_t* OPJ_RESTRICT bi = v->wavelet + v->cas;
824 for(i = 0; i < v->sn; ++i){
825 memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
829 bi = v->wavelet + 1 - v->cas;
831 for(i = 0; i < v->dn; ++i){
832 memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
838 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c){
839 __m128* OPJ_RESTRICT vw = (__m128*) w;
841 /* 4x unrolled loop */
842 for(i = 0; i < count >> 2; ++i){
843 *vw = _mm_mul_ps(*vw, c);
845 *vw = _mm_mul_ps(*vw, c);
847 *vw = _mm_mul_ps(*vw, c);
849 *vw = _mm_mul_ps(*vw, c);
853 for(i = 0; i < count; ++i){
854 *vw = _mm_mul_ps(*vw, c);
859 void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){
860 __m128* OPJ_RESTRICT vl = (__m128*) l;
861 __m128* OPJ_RESTRICT vw = (__m128*) w;
863 __m128 tmp1, tmp2, tmp3;
865 for(i = 0; i < m; ++i){
868 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
876 c = _mm_add_ps(c, c);
877 c = _mm_mul_ps(c, vl[0]);
880 vw[-1] = _mm_add_ps(tmp, c);
887 static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
889 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
891 for(i = 0; i < count; ++i){
892 OPJ_FLOAT32 tmp1 = fw[i*8 ];
893 OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
894 OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
895 OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
897 fw[i*8 + 1] = tmp2 * c;
898 fw[i*8 + 2] = tmp3 * c;
899 fw[i*8 + 3] = tmp4 * c;
903 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
905 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
906 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
908 for(i = 0; i < m; ++i){
909 OPJ_FLOAT32 tmp1_1 = fl[0];
910 OPJ_FLOAT32 tmp1_2 = fl[1];
911 OPJ_FLOAT32 tmp1_3 = fl[2];
912 OPJ_FLOAT32 tmp1_4 = fl[3];
913 OPJ_FLOAT32 tmp2_1 = fw[-4];
914 OPJ_FLOAT32 tmp2_2 = fw[-3];
915 OPJ_FLOAT32 tmp2_3 = fw[-2];
916 OPJ_FLOAT32 tmp2_4 = fw[-1];
917 OPJ_FLOAT32 tmp3_1 = fw[0];
918 OPJ_FLOAT32 tmp3_2 = fw[1];
919 OPJ_FLOAT32 tmp3_3 = fw[2];
920 OPJ_FLOAT32 tmp3_4 = fw[3];
921 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
922 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
923 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
924 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
939 OPJ_FLOAT32 tmp1 = fw[-4];
940 OPJ_FLOAT32 tmp2 = fw[-3];
941 OPJ_FLOAT32 tmp3 = fw[-2];
942 OPJ_FLOAT32 tmp4 = fw[-1];
955 /* Inverse 9-7 wavelet transform in 1-D. */
957 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
961 if(!((dwt->dn > 0) || (dwt->sn > 1))){
967 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
974 opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
975 opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
976 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_delta));
977 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_gamma));
978 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_beta));
979 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_alpha));
981 opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
982 opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
983 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
984 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
985 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
986 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
992 /* Inverse 9-7 wavelet transform in 2-D. */
994 OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, OPJ_UINT32 numres)
999 opj_tcd_resolution_t* res = tilec->resolutions;
1001 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
1002 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
1004 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1006 h.wavelet = (opj_v4_t*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t));
1008 /* FIXME event manager error callback */
1011 v.wavelet = h.wavelet;
1014 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
1015 OPJ_UINT32 bufsize = (OPJ_UINT32)((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
1018 h.sn = (OPJ_INT32)rw;
1019 v.sn = (OPJ_INT32)rh;
1023 rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
1024 rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
1026 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1027 h.cas = res->x0 % 2;
1029 for(j = (OPJ_INT32)rh; j > 3; j -= 4) {
1031 opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
1032 opj_v4dwt_decode(&h);
1034 for(k = (OPJ_INT32)rw; --k >= 0;){
1035 aj[k ] = h.wavelet[k].f[0];
1036 aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
1037 aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
1038 aj[k+(OPJ_INT32)w*3] = h.wavelet[k].f[3];
1048 opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
1049 opj_v4dwt_decode(&h);
1050 for(k = (OPJ_INT32)rw; --k >= 0;){
1052 case 3: aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
1053 case 2: aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
1054 case 1: aj[k ] = h.wavelet[k].f[0];
1059 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1060 v.cas = res->y0 % 2;
1062 aj = (OPJ_FLOAT32*) tilec->data;
1063 for(j = (OPJ_INT32)rw; j > 3; j -= 4){
1066 opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, 4);
1067 opj_v4dwt_decode(&v);
1069 for(k = 0; k < rh; ++k){
1070 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
1080 opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, j);
1081 opj_v4dwt_decode(&v);
1083 for(k = 0; k < rh; ++k){
1084 memcpy(&aj[k*w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
1089 opj_aligned_free(h.wavelet);