Merge branch 'master' of https://github.com/uclouvain/openjpeg into tier1_optimizatio...
[openjpeg.git] / src / lib / openjp2 / dwt.c
index 1a3d3f718b44e4fa2c192e18cc4b968fc9c4ba80..2e28effc855cbb8b749f61acfcee8e7dcc0b87f5 100644 (file)
@@ -1,9 +1,15 @@
 /*
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2007, Professor Benoit Macq
+ * The copyright in this software is being made available under the 2-clauses 
+ * BSD License, included below. This software may be subject to other third 
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
  * Copyright (c) 2001-2003, David Janssens
  * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux 
+ * Copyright (c) 2003-2014, Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
@@ -40,8 +46,8 @@
 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
 /*@{*/
 
-#define WS(i) v->mem[(i)*2]
-#define WD(i) v->mem[(1+(i)*2)]
+#define OPJ_WS(i) v->mem[(i)*2]
+#define OPJ_WD(i) v->mem[(1+(i)*2)]
 
 /** @name Local data structures */
 /*@{*/
@@ -51,18 +57,18 @@ typedef struct dwt_local {
        OPJ_INT32 dn;
        OPJ_INT32 sn;
        OPJ_INT32 cas;
-} dwt_t;
+} opj_dwt_t;
 
 typedef union {
        OPJ_FLOAT32     f[4];
-} v4;
+} opj_v4_t;
 
 typedef struct v4dwt_local {
-       v4*     wavelet ;
+       opj_v4_t*       wavelet ;
        OPJ_INT32               dn ;
        OPJ_INT32               sn ;
        OPJ_INT32               cas ;
-} v4dwt_t ;
+} opj_v4dwt_t ;
 
 static const OPJ_FLOAT32 opj_dwt_alpha =  1.586134342f; /*  12994 */
 static const OPJ_FLOAT32 opj_dwt_beta  =  0.052980118f; /*    434 */
@@ -77,7 +83,7 @@ static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
 /**
 Virtual function type for wavelet transform in 1-D 
 */
-typedef void (*DWT1DFN)(dwt_t* v);
+typedef void (*DWT1DFN)(opj_dwt_t* v);
 
 /** @name Local static functions */
 /*@{*/
@@ -93,11 +99,11 @@ static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ
 /**
 Inverse lazy transform (horizontal)
 */
-static void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a);
+static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a);
 /**
 Inverse lazy transform (vertical)
 */
-static void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
+static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
 /**
 Forward 5-3 wavelet transform in 1-D
 */
@@ -105,7 +111,7 @@ static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32
 /**
 Inverse 5-3 wavelet transform in 1-D
 */
-static void opj_dwt_decode_1(dwt_t *v);
+static void opj_dwt_decode_1(opj_dwt_t *v);
 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
 /**
 Forward 9-7 wavelet transform in 1-D
@@ -118,31 +124,31 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_st
 /**
 Inverse wavelet transform in 2-D.
 */
-static opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
+static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
 
-static opj_bool opj_dwt_encode_procedure(      opj_tcd_tilecomp_t * tilec,
+static OPJ_BOOL opj_dwt_encode_procedure(      opj_tcd_tilecomp_t * tilec,
                                                                                    void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
 
-static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i);
+static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, OPJ_UINT32 i);
 
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 1-D. */
 /* </summary>                            */
-static void opj_v4dwt_decode(v4dwt_t* restrict dwt);
+static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt);
 
-static void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
+static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w, OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size);
 
-static void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
+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);
 
 #ifdef __SSE__
-static void opj_v4dwt_decode_step1_sse(v4* w, OPJ_INT32 count, const __m128 c);
+static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c);
 
-static void opj_v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
+static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
 
 #else
-static void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
+static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
 
-static void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
+static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
 
 #endif
 
@@ -150,13 +156,13 @@ static void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_F
 
 /*@}*/
 
-#define S(i) a[(i)*2]
-#define D(i) a[(1+(i)*2)]
-#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
-#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
+#define OPJ_S(i) a[(i)*2]
+#define OPJ_D(i) a[(1+(i)*2)]
+#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
+#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
 /* new */
-#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
-#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
+#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
+#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
 
 /* <summary>                                                              */
 /* This table contains the norms of the 5-3 wavelets for different bands. */
@@ -187,7 +193,7 @@ static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
 /* <summary>                                    */
 /* Forward lazy transform (horizontal).  */
 /* </summary>                            */ 
-void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
        OPJ_INT32 i;
        OPJ_INT32 * l_dest = b;
        OPJ_INT32 * l_src = a+cas;
@@ -209,7 +215,7 @@ void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32
 /* <summary>                             */  
 /* Forward lazy transform (vertical).    */
 /* </summary>                            */ 
-void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
+static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
     OPJ_INT32 i = sn;
        OPJ_INT32 * l_dest = b;
        OPJ_INT32 * l_src = a+cas;
@@ -234,7 +240,7 @@ void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32
 /* <summary>                             */
 /* Inverse lazy transform (horizontal).  */
 /* </summary>                            */
-void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) {
+static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a) {
     OPJ_INT32 *ai = a;
     OPJ_INT32 *bi = h->mem + h->cas;
     OPJ_INT32  i       = h->sn;
@@ -254,7 +260,7 @@ void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) {
 /* <summary>                             */  
 /* Inverse lazy transform (vertical).    */
 /* </summary>                            */ 
-void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
+static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
     OPJ_INT32 *ai = a;
     OPJ_INT32 *bi = v->mem + v->cas;
     OPJ_INT32  i = v->sn;
@@ -277,20 +283,20 @@ void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
 /* <summary>                            */
 /* Forward 5-3 wavelet transform in 1-D. */
 /* </summary>                           */
-void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
        OPJ_INT32 i;
        
        if (!cas) {
                if ((dn > 0) || (sn > 1)) {     /* NEW :  CASE ONE ELEMENT */
-                       for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
-                       for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
+                       for (i = 0; i < dn; i++) OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
+                       for (i = 0; i < sn; i++) OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
                }
        } else {
                if (!sn && dn == 1)                 /* NEW :  CASE ONE ELEMENT */
-                       S(0) *= 2;
+                       OPJ_S(0) *= 2;
                else {
-                       for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
-                       for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
+                       for (i = 0; i < dn; i++) OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
+                       for (i = 0; i < sn; i++) OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
                }
        }
 }
@@ -298,20 +304,20 @@ void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 1-D. */
 /* </summary>                           */ 
-void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
        OPJ_INT32 i;
        
        if (!cas) {
                if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
-                       for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
-                       for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
+                       for (i = 0; i < sn; i++) OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
+                       for (i = 0; i < dn; i++) OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
                }
        } else {
                if (!sn  && dn == 1)          /* NEW :  CASE ONE ELEMENT */
-                       S(0) /= 2;
+                       OPJ_S(0) /= 2;
                else {
-                       for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
-                       for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
+                       for (i = 0; i < sn; i++) OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
+                       for (i = 0; i < dn; i++) OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
                }
        }
 }
@@ -319,49 +325,49 @@ void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas)
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 1-D. */
 /* </summary>                           */ 
-void opj_dwt_decode_1(dwt_t *v) {
+static void opj_dwt_decode_1(opj_dwt_t *v) {
        opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
 }
 
 /* <summary>                             */
 /* Forward 9-7 wavelet transform in 1-D. */
 /* </summary>                            */
-void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
        OPJ_INT32 i;
        if (!cas) {
                if ((dn > 0) || (sn > 1)) {     /* NEW :  CASE ONE ELEMENT */
                        for (i = 0; i < dn; i++)
-                               D(i) -= opj_int_fix_mul(S_(i) + S_(i + 1), 12993);
+                               OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
                        for (i = 0; i < sn; i++)
-                               S(i) -= opj_int_fix_mul(D_(i - 1) + D_(i), 434);
+                               OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
                        for (i = 0; i < dn; i++)
-                               D(i) += opj_int_fix_mul(S_(i) + S_(i + 1), 7233);
+                               OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
                        for (i = 0; i < sn; i++)
-                               S(i) += opj_int_fix_mul(D_(i - 1) + D_(i), 3633);
+                               OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
                        for (i = 0; i < dn; i++)
-                               D(i) = opj_int_fix_mul(D(i), 5038);     /*5038 */
+                               OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038);     /*5038 */
                        for (i = 0; i < sn; i++)
-                               S(i) = opj_int_fix_mul(S(i), 6659);     /*6660 */
+                               OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659);     /*6660 */
                }
        } else {
                if ((sn > 0) || (dn > 1)) {     /* NEW :  CASE ONE ELEMENT */
                        for (i = 0; i < dn; i++)
-                               S(i) -= opj_int_fix_mul(DD_(i) + DD_(i - 1), 12993);
+                               OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
                        for (i = 0; i < sn; i++)
-                               D(i) -= opj_int_fix_mul(SS_(i) + SS_(i + 1), 434);
+                               OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
                        for (i = 0; i < dn; i++)
-                               S(i) += opj_int_fix_mul(DD_(i) + DD_(i - 1), 7233);
+                               OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
                        for (i = 0; i < sn; i++)
-                               D(i) += opj_int_fix_mul(SS_(i) + SS_(i + 1), 3633);
+                               OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
                        for (i = 0; i < dn; i++)
-                               S(i) = opj_int_fix_mul(S(i), 5038);     /*5038 */
+                               OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038);     /*5038 */
                        for (i = 0; i < sn; i++)
-                               D(i) = opj_int_fix_mul(D(i), 6659);     /*6660 */
+                               OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659);     /*6660 */
                }
        }
 }
 
-void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
+static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
        OPJ_INT32 p, n;
        p = opj_int_floorlog2(stepsize) - 13;
        n = 11 - opj_int_floorlog2(stepsize);
@@ -379,7 +385,7 @@ void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_
 /* <summary>                            */
 /* Forward 5-3 wavelet transform in 2-D. */
 /* </summary>                           */
-INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
+static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
 {
        OPJ_INT32 i, j, k;
        OPJ_INT32 *a = 00;
@@ -389,24 +395,23 @@ INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_fun
 
        OPJ_INT32 rw;                   /* width of the resolution level computed   */
        OPJ_INT32 rh;                   /* height of the resolution level computed  */
-       OPJ_INT32 l_data_size;
+       OPJ_UINT32 l_data_size;
 
        opj_tcd_resolution_t * l_cur_res = 0;
        opj_tcd_resolution_t * l_last_res = 0;
 
        w = tilec->x1-tilec->x0;
-       l = tilec->numresolutions-1;
+       l = (OPJ_INT32)tilec->numresolutions-1;
        a = tilec->data;
 
        l_cur_res = tilec->resolutions + l;
        l_last_res = l_cur_res - 1;
 
-       rw = l_cur_res->x1 - l_cur_res->x0;
-       rh = l_cur_res->y1 - l_cur_res->y0;
-
-       l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
-       bj = (OPJ_INT32*)opj_malloc(l_data_size);
-       if (! bj) {
+       l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * (OPJ_UINT32)sizeof(OPJ_INT32);
+       bj = (OPJ_INT32*)opj_malloc((size_t)l_data_size);
+       /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
+       /* in that case, so do not error out */
+       if (l_data_size != 0 && ! bj) {
                return OPJ_FALSE;
        }
        i = l;
@@ -460,7 +465,7 @@ INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_fun
 
 /* Forward 5-3 wavelet transform in 2-D. */
 /* </summary>                           */
-opj_bool opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
+OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
 {
        return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
 }
@@ -468,8 +473,8 @@ opj_bool opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 2-D. */
 /* </summary>                           */
-opj_bool opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
-       return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
+OPJ_BOOL opj_dwt_decode(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
+       return opj_dwt_decode_tile(tp, tilec, numres, &opj_dwt_decode_1);
 }
 
 
@@ -494,7 +499,7 @@ OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
 /* <summary>                             */
 /* Forward 9-7 wavelet transform in 2-D. */
 /* </summary>                            */
-opj_bool opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
+OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
 {
        return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
 }
@@ -531,86 +536,245 @@ void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
                        OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
                        stepsize = (1 << (gain)) / norm;
                }
-               opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
+               opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
        }
 }
 
 /* <summary>                             */
 /* Determine maximum computed resolution level for inverse wavelet transform */
 /* </summary>                            */
-OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
+static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, OPJ_UINT32 i) {
        OPJ_UINT32 mr   = 0;
        OPJ_UINT32 w;
        while( --i ) {
                ++r;
-               if( mr < ( w = r->x1 - r->x0 ) )
+               if( mr < ( w = (OPJ_UINT32)(r->x1 - r->x0) ) )
                        mr = w ;
-               if( mr < ( w = r->y1 - r->y0 ) )
+               if( mr < ( w = (OPJ_UINT32)(r->y1 - r->y0) ) )
                        mr = w ;
        }
        return mr ;
 }
 
+typedef struct
+{
+    opj_dwt_t h;
+    DWT1DFN dwt_1D;
+    OPJ_UINT32 rw;
+    OPJ_UINT32 w;
+    OPJ_INT32 * OPJ_RESTRICT tiledp;
+    int min_j;
+    int max_j;
+} opj_dwd_decode_h_job_t;
+
+static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
+{
+    int j;
+    opj_dwd_decode_h_job_t* job;
+    (void)tls;
+
+    job = (opj_dwd_decode_h_job_t*)user_data;
+    for( j = job->min_j; j < job->max_j; j++ )
+    {
+          opj_dwt_interleave_h(&job->h, &job->tiledp[j*job->w]);
+          (job->dwt_1D)(&job->h);
+          memcpy(&job->tiledp[j*job->w], job->h.mem, job->rw * sizeof(OPJ_INT32));
+    }
+
+    opj_aligned_free(job->h.mem);
+    opj_free(job);
+}
+
+typedef struct
+{
+    opj_dwt_t v;
+    DWT1DFN dwt_1D;
+    OPJ_UINT32 rh;
+    OPJ_UINT32 w;
+    OPJ_INT32 * OPJ_RESTRICT tiledp;
+    int min_j;
+    int max_j;
+} opj_dwd_decode_v_job_t;
+
+static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
+{
+    int j;
+    opj_dwd_decode_v_job_t* job;
+    (void)tls;
+
+    job = (opj_dwd_decode_v_job_t*)user_data;
+    for( j = job->min_j; j < job->max_j; j++ )
+    {
+        OPJ_UINT32 k;
+        opj_dwt_interleave_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w);
+        (job->dwt_1D)(&job->v);
+        for(k = 0; k < job->rh; ++k) {
+            job->tiledp[k * job->w + j] = job->v.mem[k];
+        }
+    }
+
+    opj_aligned_free(job->v.mem);
+    opj_free(job);
+}
+
+
 /* <summary>                            */
 /* Inverse wavelet transform in 2-D.     */
 /* </summary>                           */
-opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
-       dwt_t h;
-       dwt_t v;
+static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
+       opj_dwt_t h;
+       opj_dwt_t v;
 
        opj_tcd_resolution_t* tr = tilec->resolutions;
 
-       OPJ_UINT32 rw = tr->x1 - tr->x0;        /* width of the resolution level computed */
-       OPJ_UINT32 rh = tr->y1 - tr->y0;        /* height of the resolution level computed */
-
-       OPJ_UINT32 w = tilec->x1 - tilec->x0;
+       OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - tr->x0);  /* width of the resolution level computed */
+       OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - tr->y0);  /* height of the resolution level computed */
 
-       h.mem = (OPJ_INT32*)
-       opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
+       OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
+    size_t h_mem_size;
+    int num_threads;
+       
+       if (numres == 1U) {
+               return OPJ_TRUE;
+       }
+       num_threads = opj_thread_pool_get_thread_count(tp);
+       h_mem_size = opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32);
+       h.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
        if (! h.mem){
+               /* FIXME event manager error callback */
                return OPJ_FALSE;
        }
 
        v.mem = h.mem;
 
        while( --numres) {
-               OPJ_INT32 * restrict tiledp = tilec->data;
+               OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
                OPJ_UINT32 j;
 
                ++tr;
-               h.sn = rw;
-               v.sn = rh;
+               h.sn = (OPJ_INT32)rw;
+               v.sn = (OPJ_INT32)rh;
 
-               rw = tr->x1 - tr->x0;
-               rh = tr->y1 - tr->y0;
+               rw = (OPJ_UINT32)(tr->x1 - tr->x0);
+               rh = (OPJ_UINT32)(tr->y1 - tr->y0);
 
-               h.dn = rw - h.sn;
+               h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
                h.cas = tr->x0 % 2;
 
-               for(j = 0; j < rh; ++j) {
-                       opj_dwt_interleave_h(&h, &tiledp[j*w]);
-                       (dwt_1D)(&h);
-                       memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
-               }
-
-               v.dn = rh - v.sn;
+        if( num_threads <= 1 || rh == 1 )
+        {
+            for(j = 0; j < rh; ++j) {
+                opj_dwt_interleave_h(&h, &tiledp[j*w]);
+                (dwt_1D)(&h);
+                memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
+            }
+        }
+        else
+        {
+            int num_jobs = num_threads;
+            if( rh < num_jobs )
+                num_jobs = rh;
+            for( j = 0; j < num_jobs; j++ )
+            {
+                opj_dwd_decode_h_job_t* job;
+
+                job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
+                if( !job )
+                {
+                    /* It would be nice to fallback to single thread case, but */
+                    /* unfortunately some jobs may be launched and have modified */
+                    /* tiledp, so it is not practical to recover from that error */
+                    /* FIXME event manager error callback */
+                    opj_thread_pool_wait_completion(tp, 0);
+                    opj_aligned_free(h.mem);
+                    return OPJ_FALSE;
+                }
+                job->h = h;
+                job->dwt_1D = dwt_1D;
+                job->rw = rw;
+                job->w = w;
+                job->tiledp = tiledp;
+                job->min_j = j * (rh / num_jobs);
+                job->max_j = (j+1) * (rh / num_jobs);
+                if( job->max_j > rh || j == num_jobs - 1 )
+                    job->max_j = rh;
+                job->h.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
+                if (!job->h.mem)
+                {
+                    /* FIXME event manager error callback */
+                    opj_thread_pool_wait_completion(tp, 0);
+                    opj_free(job);
+                    opj_aligned_free(h.mem);
+                    return OPJ_FALSE;
+                }
+                opj_thread_pool_submit_job( tp, opj_dwt_decode_h_func, job );
+            }
+            opj_thread_pool_wait_completion(tp, 0);
+        }
+
+               v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
                v.cas = tr->y0 % 2;
 
-               for(j = 0; j < rw; ++j){
-                       OPJ_UINT32 k;
-                       opj_dwt_interleave_v(&v, &tiledp[j], w);
-                       (dwt_1D)(&v);
-                       for(k = 0; k < rh; ++k) {
-                               tiledp[k * w + j] = v.mem[k];
-                       }
-               }
+        if( num_threads <= 1 || rw == 1 )
+        {
+            for(j = 0; j < rw; ++j){
+                OPJ_UINT32 k;
+                opj_dwt_interleave_v(&v, &tiledp[j], (OPJ_INT32)w);
+                (dwt_1D)(&v);
+                for(k = 0; k < rh; ++k) {
+                    tiledp[k * w + j] = v.mem[k];
+                }
+            }
+        }
+        else
+        {
+            int num_jobs = num_threads;
+            if( rw < num_jobs )
+                num_jobs = rw;
+            for( j = 0; j < num_jobs; j++ )
+            {
+                opj_dwd_decode_v_job_t* job;
+
+                job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
+                if( !job )
+                {
+                    /* It would be nice to fallback to single thread case, but */
+                    /* unfortunately some jobs may be launched and have modified */
+                    /* tiledp, so it is not practical to recover from that error */
+                    /* FIXME event manager error callback */
+                    opj_thread_pool_wait_completion(tp, 0);
+                    opj_aligned_free(v.mem);
+                    return OPJ_FALSE;
+                }
+                job->v = v;
+                job->dwt_1D = dwt_1D;
+                job->rh = rh;
+                job->w = w;
+                job->tiledp = tiledp;
+                job->min_j = j * (rw / num_jobs);
+                job->max_j = (j+1) * (rw / num_jobs);
+                if( job->max_j > rw || j == num_jobs - 1 )
+                    job->max_j = rw;
+                job->v.mem = (OPJ_INT32*)opj_aligned_malloc(h_mem_size);
+                if (!job->v.mem)
+                {
+                    /* FIXME event manager error callback */
+                    opj_thread_pool_wait_completion(tp, 0);
+                    opj_free(job);
+                    opj_aligned_free(v.mem);
+                    return OPJ_FALSE;
+                }
+                opj_thread_pool_submit_job( tp, opj_dwt_decode_v_func, job );
+            }
+            opj_thread_pool_wait_completion(tp, 0);
+        }
        }
        opj_aligned_free(h.mem);
        return OPJ_TRUE;
 }
 
-void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
-       OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
+static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w, OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size){
+       OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
        OPJ_INT32 count = w->sn;
        OPJ_INT32 i, k;
 
@@ -645,33 +809,33 @@ void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_IN
                        }
                }
 
-               bi = (float*) (w->wavelet + 1 - w->cas);
+               bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas);
                a += w->sn;
                size -= w->sn;
                count = w->dn;
        }
 }
 
-void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
-       v4* restrict bi = v->wavelet + v->cas;
+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){
+       opj_v4_t* OPJ_RESTRICT bi = v->wavelet + v->cas;
        OPJ_INT32 i;
 
        for(i = 0; i < v->sn; ++i){
-               memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
+               memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
        }
 
        a += v->sn * x;
        bi = v->wavelet + 1 - v->cas;
 
        for(i = 0; i < v->dn; ++i){
-               memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
+               memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
        }
 }
 
 #ifdef __SSE__
 
-void opj_v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
-       __m128* restrict vw = (__m128*) w;
+static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c){
+       __m128* OPJ_RESTRICT vw = (__m128*) w;
        OPJ_INT32 i;
        /* 4x unrolled loop */
        for(i = 0; i < count >> 2; ++i){
@@ -691,10 +855,10 @@ void opj_v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
        }
 }
 
-void opj_v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
-       __m128* restrict vl = (__m128*) l;
-       __m128* restrict vw = (__m128*) w;
-       int i;
+void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){
+       __m128* OPJ_RESTRICT vl = (__m128*) l;
+       __m128* OPJ_RESTRICT vw = (__m128*) w;
+       OPJ_INT32 i;
        __m128 tmp1, tmp2, tmp3;
        tmp1 = vl[0];
        for(i = 0; i < m; ++i){
@@ -719,9 +883,9 @@ void opj_v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
 
 #else
 
-void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
+static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
 {
-       OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
+       OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
        OPJ_INT32 i;
        for(i = 0; i < count; ++i){
                OPJ_FLOAT32 tmp1 = fw[i*8    ];
@@ -735,11 +899,11 @@ void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
        }
 }
 
-void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
+static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
 {
-       OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
-       OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
-       int i;
+       OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
+       OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
+       OPJ_INT32 i;
        for(i = 0; i < m; ++i){
                OPJ_FLOAT32 tmp1_1 = fl[0];
                OPJ_FLOAT32 tmp1_2 = fl[1];
@@ -789,9 +953,9 @@ void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 1-D. */
 /* </summary>                            */
-void opj_v4dwt_decode(v4dwt_t* restrict dwt)
+static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
 {
-       int a, b;
+       OPJ_INT32 a, b;
        if(dwt->cas == 0) {
                if(!((dwt->dn > 0) || (dwt->sn > 1))){
                        return;
@@ -826,47 +990,51 @@ void opj_v4dwt_decode(v4dwt_t* restrict dwt)
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 2-D. */
 /* </summary>                            */
-opj_bool opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres)
+OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, OPJ_UINT32 numres)
 {
-       v4dwt_t h;
-       v4dwt_t v;
+       opj_v4dwt_t h;
+       opj_v4dwt_t v;
 
        opj_tcd_resolution_t* res = tilec->resolutions;
 
-       OPJ_UINT32 rw = res->x1 - res->x0;      /* width of the resolution level computed */
-       OPJ_UINT32 rh = res->y1 - res->y0;      /* height of the resolution level computed */
+       OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 - res->x0);        /* width of the resolution level computed */
+       OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 - res->y0);        /* height of the resolution level computed */
 
-       OPJ_UINT32 w = tilec->x1 - tilec->x0;
+       OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
 
-       h.wavelet = (v4*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(v4));
+       h.wavelet = (opj_v4_t*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t));
+       if (!h.wavelet) {
+               /* FIXME event manager error callback */
+               return OPJ_FALSE;
+       }
        v.wavelet = h.wavelet;
 
        while( --numres) {
-               OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
-               OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
+               OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
+               OPJ_UINT32 bufsize = (OPJ_UINT32)((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
                OPJ_INT32 j;
 
-               h.sn = rw;
-               v.sn = rh;
+               h.sn = (OPJ_INT32)rw;
+               v.sn = (OPJ_INT32)rh;
 
                ++res;
 
-               rw = res->x1 - res->x0; /* width of the resolution level computed */
-               rh = res->y1 - res->y0; /* height of the resolution level computed */
+               rw = (OPJ_UINT32)(res->x1 - res->x0);   /* width of the resolution level computed */
+               rh = (OPJ_UINT32)(res->y1 - res->y0);   /* height of the resolution level computed */
 
-               h.dn = rw - h.sn;
+               h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
                h.cas = res->x0 % 2;
 
-               for(j = rh; j > 3; j -= 4) {
+               for(j = (OPJ_INT32)rh; j > 3; j -= 4) {
                        OPJ_INT32 k;
-                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
                        opj_v4dwt_decode(&h);
 
-                       for(k = rw; --k >= 0;){
-                               aj[k    ] = h.wavelet[k].f[0];
-                               aj[k+w  ] = h.wavelet[k].f[1];
-                               aj[k+w*2] = h.wavelet[k].f[2];
-                               aj[k+w*3] = h.wavelet[k].f[3];
+                       for(k = (OPJ_INT32)rw; --k >= 0;){
+                               aj[k               ] = h.wavelet[k].f[0];
+                               aj[k+(OPJ_INT32)w  ] = h.wavelet[k].f[1];
+                               aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
+                               aj[k+(OPJ_INT32)w*3] = h.wavelet[k].f[3];
                        }
 
                        aj += w*4;
@@ -876,25 +1044,25 @@ opj_bool opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numr
                if (rh & 0x03) {
                        OPJ_INT32 k;
                        j = rh & 0x03;
-                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
                        opj_v4dwt_decode(&h);
-                       for(k = rw; --k >= 0;){
+                       for(k = (OPJ_INT32)rw; --k >= 0;){
                                switch(j) {
-                                       case 3: aj[k+w*2] = h.wavelet[k].f[2];
-                                       case 2: aj[k+w  ] = h.wavelet[k].f[1];
-                                       case 1: aj[k    ] = h.wavelet[k].f[0];
+                                       case 3: aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
+                                       case 2: aj[k+(OPJ_INT32)w  ] = h.wavelet[k].f[1];
+                                       case 1: aj[k               ] = h.wavelet[k].f[0];
                                }
                        }
                }
 
-               v.dn = rh - v.sn;
+               v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
                v.cas = res->y0 % 2;
 
                aj = (OPJ_FLOAT32*) tilec->data;
-               for(j = rw; j > 3; j -= 4){
+               for(j = (OPJ_INT32)rw; j > 3; j -= 4){
                        OPJ_UINT32 k;
 
-                       opj_v4dwt_interleave_v(&v, aj, w, 4);
+                       opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, 4);
                        opj_v4dwt_decode(&v);
 
                        for(k = 0; k < rh; ++k){
@@ -908,11 +1076,11 @@ opj_bool opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numr
 
                        j = rw & 0x03;
 
-                       opj_v4dwt_interleave_v(&v, aj, w, j);
+                       opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, j);
                        opj_v4dwt_decode(&v);
 
                        for(k = 0; k < rh; ++k){
-                               memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
+                               memcpy(&aj[k*w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
                        }
                }
        }