Forward DWT: small code refactoring to allow future improvements for the horizontal...
[openjpeg.git] / src / lib / openjp2 / dwt.c
1 /*
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.
6  *
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  * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include <assert.h>
42
43 #define OPJ_SKIP_POISON
44 #include "opj_includes.h"
45
46 #ifdef __SSE__
47 #include <xmmintrin.h>
48 #endif
49 #ifdef __SSE2__
50 #include <emmintrin.h>
51 #endif
52 #ifdef __SSSE3__
53 #include <tmmintrin.h>
54 #endif
55 #ifdef __AVX2__
56 #include <immintrin.h>
57 #endif
58
59 #if defined(__GNUC__)
60 #pragma GCC poison malloc calloc realloc free
61 #endif
62
63 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
64 /*@{*/
65
66 #define OPJ_WS(i) v->mem[(i)*2]
67 #define OPJ_WD(i) v->mem[(1+(i)*2)]
68
69 #ifdef __AVX2__
70 /** Number of int32 values in a AVX2 register */
71 #define VREG_INT_COUNT       8
72 #else
73 /** Number of int32 values in a SSE2 register */
74 #define VREG_INT_COUNT       4
75 #endif
76
77 /** Number of columns that we can process in parallel in the vertical pass */
78 #define PARALLEL_COLS_53     (2*VREG_INT_COUNT)
79
80 /** @name Local data structures */
81 /*@{*/
82
83 typedef struct dwt_local {
84     OPJ_INT32* mem;
85     OPJ_INT32 dn;   /* number of elements in high pass band */
86     OPJ_INT32 sn;   /* number of elements in low pass band */
87     OPJ_INT32 cas;  /* 0 = start on even coord, 1 = start on odd coord */
88 } opj_dwt_t;
89
90 #define NB_ELTS_V8  8
91
92 typedef union {
93     OPJ_FLOAT32 f[NB_ELTS_V8];
94 } opj_v8_t;
95
96 typedef struct v8dwt_local {
97     opj_v8_t*   wavelet ;
98     OPJ_INT32       dn ;  /* number of elements in high pass band */
99     OPJ_INT32       sn ;  /* number of elements in low pass band */
100     OPJ_INT32       cas ; /* 0 = start on even coord, 1 = start on odd coord */
101     OPJ_UINT32      win_l_x0; /* start coord in low pass band */
102     OPJ_UINT32      win_l_x1; /* end coord in low pass band */
103     OPJ_UINT32      win_h_x0; /* start coord in high pass band */
104     OPJ_UINT32      win_h_x1; /* end coord in high pass band */
105 } opj_v8dwt_t ;
106
107 /* From table F.4 from the standard */
108 static const OPJ_FLOAT32 opj_dwt_alpha =  -1.586134342f;
109 static const OPJ_FLOAT32 opj_dwt_beta  =  -0.052980118f;
110 static const OPJ_FLOAT32 opj_dwt_gamma = 0.882911075f;
111 static const OPJ_FLOAT32 opj_dwt_delta = 0.443506852f;
112
113 static const OPJ_FLOAT32 opj_K      = 1.230174105f;
114 static const OPJ_FLOAT32 opj_invK   = (OPJ_FLOAT32)(1.0 / 1.230174105);
115
116 /*@}*/
117
118 /**
119 Virtual function type for wavelet transform in 1-D
120 */
121 typedef void (*DWT1DFN)(const opj_dwt_t* v);
122
123 /** @name Local static functions */
124 /*@{*/
125
126 /**
127 Forward lazy transform (horizontal)
128 */
129 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
130                                    OPJ_INT32 * OPJ_RESTRICT b,
131                                    OPJ_INT32 dn,
132                                    OPJ_INT32 sn, OPJ_INT32 cas);
133 /**
134 Forward lazy transform (vertical)
135 */
136 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
137                                    OPJ_INT32 * OPJ_RESTRICT b,
138                                    OPJ_INT32 dn,
139                                    OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas);
140 /**
141 Forward 5-3 wavelet transform in 1-D
142 */
143 static void opj_dwt_encode_1(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
144                              OPJ_INT32 cas);
145 /**
146 Forward 9-7 wavelet transform in 1-D
147 */
148 static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
149                                   OPJ_INT32 cas);
150 /**
151 Explicit calculation of the Quantization Stepsizes
152 */
153 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
154                                     opj_stepsize_t *bandno_stepsize);
155 /**
156 Inverse wavelet transform in 2-D.
157 */
158 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
159                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
160
161 static OPJ_BOOL opj_dwt_decode_partial_tile(
162     opj_tcd_tilecomp_t* tilec,
163     OPJ_UINT32 numres);
164
165 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
166 typedef void (*opj_encode_one_row_fnptr_type)(void *, OPJ_INT32, OPJ_INT32,
167         OPJ_INT32);
168
169 typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
170     void *row,
171     void *tmp,
172     OPJ_UINT32 width,
173     OPJ_BOOL even);
174
175 static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
176         opj_tcd_tilecomp_t * tilec,
177         opj_encode_one_row_fnptr_type p_function,
178         opj_encode_and_deinterleave_h_one_row_fnptr_type
179         p_encode_and_deinterleave_h_one_row);
180
181 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
182         OPJ_UINT32 i);
183
184 /* <summary>                             */
185 /* Inverse 9-7 wavelet transform in 1-D. */
186 /* </summary>                            */
187
188 /*@}*/
189
190 /*@}*/
191
192 #define OPJ_S(i) a[(i)*2]
193 #define OPJ_D(i) a[(1+(i)*2)]
194 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
195 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
196 /* new */
197 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
198 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
199
200 /* <summary>                                                              */
201 /* This table contains the norms of the 5-3 wavelets for different bands. */
202 /* </summary>                                                             */
203 /* FIXME! the array should really be extended up to 33 resolution levels */
204 /* See https://github.com/uclouvain/openjpeg/issues/493 */
205 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
206     {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
207     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
208     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
209     {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
210 };
211
212 /* <summary>                                                              */
213 /* This table contains the norms of the 9-7 wavelets for different bands. */
214 /* </summary>                                                             */
215 /* FIXME! the array should really be extended up to 33 resolution levels */
216 /* See https://github.com/uclouvain/openjpeg/issues/493 */
217 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
218     {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
219     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
220     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
221     {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
222 };
223
224 /*
225 ==========================================================
226    local functions
227 ==========================================================
228 */
229
230 /* <summary>                             */
231 /* Forward lazy transform (horizontal).  */
232 /* </summary>                            */
233 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
234                                    OPJ_INT32 * OPJ_RESTRICT b,
235                                    OPJ_INT32 dn,
236                                    OPJ_INT32 sn, OPJ_INT32 cas)
237 {
238     OPJ_INT32 i;
239     OPJ_INT32 * OPJ_RESTRICT l_dest = b;
240     const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
241
242     for (i = 0; i < sn; ++i) {
243         *l_dest++ = *l_src;
244         l_src += 2;
245     }
246
247     l_dest = b + sn;
248     l_src = a + 1 - cas;
249
250     for (i = 0; i < dn; ++i)  {
251         *l_dest++ = *l_src;
252         l_src += 2;
253     }
254 }
255
256 /* <summary>                             */
257 /* Forward lazy transform (vertical).    */
258 /* </summary>                            */
259 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
260                                    OPJ_INT32 * OPJ_RESTRICT b,
261                                    OPJ_INT32 dn,
262                                    OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas)
263 {
264     OPJ_INT32 i = sn;
265     OPJ_INT32 * OPJ_RESTRICT l_dest = b;
266     const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
267
268     while (i--) {
269         *l_dest = *l_src;
270         l_dest += x;
271         l_src += 2;
272     } /* b[i*x]=a[2*i+cas]; */
273
274     l_dest = b + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)x;
275     l_src = a + 1 - cas;
276
277     i = dn;
278     while (i--) {
279         *l_dest = *l_src;
280         l_dest += x;
281         l_src += 2;
282     } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
283 }
284
285 #ifdef STANDARD_SLOW_VERSION
286 /* <summary>                             */
287 /* Inverse lazy transform (horizontal).  */
288 /* </summary>                            */
289 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
290 {
291     const OPJ_INT32 *ai = a;
292     OPJ_INT32 *bi = h->mem + h->cas;
293     OPJ_INT32  i    = h->sn;
294     while (i--) {
295         *bi = *(ai++);
296         bi += 2;
297     }
298     ai  = a + h->sn;
299     bi  = h->mem + 1 - h->cas;
300     i   = h->dn ;
301     while (i--) {
302         *bi = *(ai++);
303         bi += 2;
304     }
305 }
306
307 /* <summary>                             */
308 /* Inverse lazy transform (vertical).    */
309 /* </summary>                            */
310 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
311 {
312     const OPJ_INT32 *ai = a;
313     OPJ_INT32 *bi = v->mem + v->cas;
314     OPJ_INT32  i = v->sn;
315     while (i--) {
316         *bi = *ai;
317         bi += 2;
318         ai += x;
319     }
320     ai = a + (v->sn * (OPJ_SIZE_T)x);
321     bi = v->mem + 1 - v->cas;
322     i = v->dn ;
323     while (i--) {
324         *bi = *ai;
325         bi += 2;
326         ai += x;
327     }
328 }
329
330 #endif /* STANDARD_SLOW_VERSION */
331
332 /* <summary>                            */
333 /* Forward 5-3 wavelet transform in 1-D. */
334 /* </summary>                           */
335 static void opj_dwt_encode_1(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
336                              OPJ_INT32 cas)
337 {
338     OPJ_INT32 i;
339     OPJ_INT32* a = (OPJ_INT32*)aIn;
340
341     if (!cas) {
342         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
343             for (i = 0; i < dn; i++) {
344                 OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
345             }
346             for (i = 0; i < sn; i++) {
347                 OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
348             }
349         }
350     } else {
351         if (!sn && dn == 1) {       /* NEW :  CASE ONE ELEMENT */
352             OPJ_S(0) *= 2;
353         } else {
354             for (i = 0; i < dn; i++) {
355                 OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
356             }
357             for (i = 0; i < sn; i++) {
358                 OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
359             }
360         }
361     }
362 }
363
364 #ifdef STANDARD_SLOW_VERSION
365 /* <summary>                            */
366 /* Inverse 5-3 wavelet transform in 1-D. */
367 /* </summary>                           */
368 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
369                               OPJ_INT32 cas)
370 {
371     OPJ_INT32 i;
372
373     if (!cas) {
374         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
375             for (i = 0; i < sn; i++) {
376                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
377             }
378             for (i = 0; i < dn; i++) {
379                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
380             }
381         }
382     } else {
383         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
384             OPJ_S(0) /= 2;
385         } else {
386             for (i = 0; i < sn; i++) {
387                 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
388             }
389             for (i = 0; i < dn; i++) {
390                 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
391             }
392         }
393     }
394 }
395
396 static void opj_dwt_decode_1(const opj_dwt_t *v)
397 {
398     opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
399 }
400
401 #endif /* STANDARD_SLOW_VERSION */
402
403 #if !defined(STANDARD_SLOW_VERSION)
404 static void  opj_idwt53_h_cas0(OPJ_INT32* tmp,
405                                const OPJ_INT32 sn,
406                                const OPJ_INT32 len,
407                                OPJ_INT32* tiledp)
408 {
409     OPJ_INT32 i, j;
410     const OPJ_INT32* in_even = &tiledp[0];
411     const OPJ_INT32* in_odd = &tiledp[sn];
412
413 #ifdef TWO_PASS_VERSION
414     /* For documentation purpose: performs lifting in two iterations, */
415     /* but without explicit interleaving */
416
417     assert(len > 1);
418
419     /* Even */
420     tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
421     for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
422         tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
423     }
424     if (len & 1) { /* if len is odd */
425         tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
426     }
427
428     /* Odd */
429     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
430         tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
431     }
432     if (!(len & 1)) { /* if len is even */
433         tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
434     }
435 #else
436     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
437
438     assert(len > 1);
439
440     /* Improved version of the TWO_PASS_VERSION: */
441     /* Performs lifting in one single iteration. Saves memory */
442     /* accesses and explicit interleaving. */
443     s1n = in_even[0];
444     d1n = in_odd[0];
445     s0n = s1n - ((d1n + 1) >> 1);
446
447     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
448         d1c = d1n;
449         s0c = s0n;
450
451         s1n = in_even[j];
452         d1n = in_odd[j];
453
454         s0n = s1n - ((d1c + d1n + 2) >> 2);
455
456         tmp[i  ] = s0c;
457         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
458     }
459
460     tmp[i] = s0n;
461
462     if (len & 1) {
463         tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
464         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
465     } else {
466         tmp[len - 1] = d1n + s0n;
467     }
468 #endif
469     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
470 }
471
472 static void  opj_idwt53_h_cas1(OPJ_INT32* tmp,
473                                const OPJ_INT32 sn,
474                                const OPJ_INT32 len,
475                                OPJ_INT32* tiledp)
476 {
477     OPJ_INT32 i, j;
478     const OPJ_INT32* in_even = &tiledp[sn];
479     const OPJ_INT32* in_odd = &tiledp[0];
480
481 #ifdef TWO_PASS_VERSION
482     /* For documentation purpose: performs lifting in two iterations, */
483     /* but without explicit interleaving */
484
485     assert(len > 2);
486
487     /* Odd */
488     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
489         tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
490     }
491     if (!(len & 1)) {
492         tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
493     }
494
495     /* Even */
496     tmp[0] = in_even[0] + tmp[1];
497     for (i = 2, j = 1; i < len - 1; i += 2, j++) {
498         tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
499     }
500     if (len & 1) {
501         tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
502     }
503 #else
504     OPJ_INT32 s1, s2, dc, dn;
505
506     assert(len > 2);
507
508     /* Improved version of the TWO_PASS_VERSION: */
509     /* Performs lifting in one single iteration. Saves memory */
510     /* accesses and explicit interleaving. */
511
512     s1 = in_even[1];
513     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
514     tmp[0] = in_even[0] + dc;
515
516     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
517
518         s2 = in_even[j + 1];
519
520         dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
521         tmp[i  ] = dc;
522         tmp[i + 1] = s1 + ((dn + dc) >> 1);
523
524         dc = dn;
525         s1 = s2;
526     }
527
528     tmp[i] = dc;
529
530     if (!(len & 1)) {
531         dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
532         tmp[len - 2] = s1 + ((dn + dc) >> 1);
533         tmp[len - 1] = dn;
534     } else {
535         tmp[len - 1] = s1 + dc;
536     }
537 #endif
538     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
539 }
540
541
542 #endif /* !defined(STANDARD_SLOW_VERSION) */
543
544 /* <summary>                            */
545 /* Inverse 5-3 wavelet transform in 1-D for one row. */
546 /* </summary>                           */
547 /* Performs interleave, inverse wavelet transform and copy back to buffer */
548 static void opj_idwt53_h(const opj_dwt_t *dwt,
549                          OPJ_INT32* tiledp)
550 {
551 #ifdef STANDARD_SLOW_VERSION
552     /* For documentation purpose */
553     opj_dwt_interleave_h(dwt, tiledp);
554     opj_dwt_decode_1(dwt);
555     memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
556 #else
557     const OPJ_INT32 sn = dwt->sn;
558     const OPJ_INT32 len = sn + dwt->dn;
559     if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
560         if (len > 1) {
561             opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
562         } else {
563             /* Unmodified value */
564         }
565     } else { /* Left-most sample is on odd coordinate */
566         if (len == 1) {
567             tiledp[0] /= 2;
568         } else if (len == 2) {
569             OPJ_INT32* out = dwt->mem;
570             const OPJ_INT32* in_even = &tiledp[sn];
571             const OPJ_INT32* in_odd = &tiledp[0];
572             out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
573             out[0] = in_even[0] + out[1];
574             memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
575         } else if (len > 2) {
576             opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
577         }
578     }
579 #endif
580 }
581
582 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
583
584 /* Conveniency macros to improve the readabilty of the formulas */
585 #if __AVX2__
586 #define VREG        __m256i
587 #define LOAD_CST(x) _mm256_set1_epi32(x)
588 #define LOAD(x)     _mm256_load_si256((const VREG*)(x))
589 #define LOADU(x)    _mm256_loadu_si256((const VREG*)(x))
590 #define STORE(x,y)  _mm256_store_si256((VREG*)(x),(y))
591 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
592 #define ADD(x,y)    _mm256_add_epi32((x),(y))
593 #define SUB(x,y)    _mm256_sub_epi32((x),(y))
594 #define SAR(x,y)    _mm256_srai_epi32((x),(y))
595 #else
596 #define VREG        __m128i
597 #define LOAD_CST(x) _mm_set1_epi32(x)
598 #define LOAD(x)     _mm_load_si128((const VREG*)(x))
599 #define LOADU(x)    _mm_loadu_si128((const VREG*)(x))
600 #define STORE(x,y)  _mm_store_si128((VREG*)(x),(y))
601 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
602 #define ADD(x,y)    _mm_add_epi32((x),(y))
603 #define SUB(x,y)    _mm_sub_epi32((x),(y))
604 #define SAR(x,y)    _mm_srai_epi32((x),(y))
605 #endif
606 #define ADD3(x,y,z) ADD(ADD(x,y),z)
607
608 static
609 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
610                                const OPJ_INT32* tmp,
611                                OPJ_INT32 len,
612                                OPJ_SIZE_T stride)
613 {
614     OPJ_INT32 i;
615     for (i = 0; i < len; ++i) {
616         /* A memcpy(&tiledp_col[i * stride + 0],
617                     &tmp[PARALLEL_COLS_53 * i + 0],
618                     PARALLEL_COLS_53 * sizeof(OPJ_INT32))
619            would do but would be a tiny bit slower.
620            We can take here advantage of our knowledge of alignment */
621         STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
622                LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
623         STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
624                LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
625     }
626 }
627
628 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
629  * 16 in AVX2, when top-most pixel is on even coordinate */
630 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
631     OPJ_INT32* tmp,
632     const OPJ_INT32 sn,
633     const OPJ_INT32 len,
634     OPJ_INT32* tiledp_col,
635     const OPJ_SIZE_T stride)
636 {
637     const OPJ_INT32* in_even = &tiledp_col[0];
638     const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
639
640     OPJ_INT32 i;
641     OPJ_SIZE_T j;
642     VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
643     VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
644     const VREG two = LOAD_CST(2);
645
646     assert(len > 1);
647 #if __AVX2__
648     assert(PARALLEL_COLS_53 == 16);
649     assert(VREG_INT_COUNT == 8);
650 #else
651     assert(PARALLEL_COLS_53 == 8);
652     assert(VREG_INT_COUNT == 4);
653 #endif
654
655     /* Note: loads of input even/odd values must be done in a unaligned */
656     /* fashion. But stores in tmp can be done with aligned store, since */
657     /* the temporary buffer is properly aligned */
658     assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
659
660     s1n_0 = LOADU(in_even + 0);
661     s1n_1 = LOADU(in_even + VREG_INT_COUNT);
662     d1n_0 = LOADU(in_odd);
663     d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
664
665     /* s0n = s1n - ((d1n + 1) >> 1); <==> */
666     /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
667     s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
668     s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
669
670     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
671         d1c_0 = d1n_0;
672         s0c_0 = s0n_0;
673         d1c_1 = d1n_1;
674         s0c_1 = s0n_1;
675
676         s1n_0 = LOADU(in_even + j * stride);
677         s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
678         d1n_0 = LOADU(in_odd + j * stride);
679         d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
680
681         /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
682         s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
683         s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
684
685         STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
686         STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
687
688         /* d1c + ((s0c + s0n) >> 1) */
689         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
690               ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
691         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
692               ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
693     }
694
695     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
696     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
697
698     if (len & 1) {
699         VREG tmp_len_minus_1;
700         s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
701         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
702         tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
703         STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
704         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
705         STORE(tmp + PARALLEL_COLS_53 * (len - 2),
706               ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
707
708         s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
709         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
710         tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
711         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
712               tmp_len_minus_1);
713         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
714         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
715               ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
716
717
718     } else {
719         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
720               ADD(d1n_0, s0n_0));
721         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
722               ADD(d1n_1, s0n_1));
723     }
724
725     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
726 }
727
728
729 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
730  * 16 in AVX2, when top-most pixel is on odd coordinate */
731 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
732     OPJ_INT32* tmp,
733     const OPJ_INT32 sn,
734     const OPJ_INT32 len,
735     OPJ_INT32* tiledp_col,
736     const OPJ_SIZE_T stride)
737 {
738     OPJ_INT32 i;
739     OPJ_SIZE_T j;
740
741     VREG s1_0, s2_0, dc_0, dn_0;
742     VREG s1_1, s2_1, dc_1, dn_1;
743     const VREG two = LOAD_CST(2);
744
745     const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
746     const OPJ_INT32* in_odd = &tiledp_col[0];
747
748     assert(len > 2);
749 #if __AVX2__
750     assert(PARALLEL_COLS_53 == 16);
751     assert(VREG_INT_COUNT == 8);
752 #else
753     assert(PARALLEL_COLS_53 == 8);
754     assert(VREG_INT_COUNT == 4);
755 #endif
756
757     /* Note: loads of input even/odd values must be done in a unaligned */
758     /* fashion. But stores in tmp can be done with aligned store, since */
759     /* the temporary buffer is properly aligned */
760     assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
761
762     s1_0 = LOADU(in_even + stride);
763     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
764     dc_0 = SUB(LOADU(in_odd + 0),
765                SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
766     STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
767
768     s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
769     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
770     dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
771                SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
772     STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
773           ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
774
775     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
776
777         s2_0 = LOADU(in_even + (j + 1) * stride);
778         s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
779
780         /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
781         dn_0 = SUB(LOADU(in_odd + j * stride),
782                    SAR(ADD3(s1_0, s2_0, two), 2));
783         dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
784                    SAR(ADD3(s1_1, s2_1, two), 2));
785
786         STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
787         STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
788
789         /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
790         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
791               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
792         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
793               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
794
795         dc_0 = dn_0;
796         s1_0 = s2_0;
797         dc_1 = dn_1;
798         s1_1 = s2_1;
799     }
800     STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
801     STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
802
803     if (!(len & 1)) {
804         /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
805         dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
806                    SAR(ADD3(s1_0, s1_0, two), 2));
807         dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
808                    SAR(ADD3(s1_1, s1_1, two), 2));
809
810         /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
811         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
812               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
813         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
814               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
815
816         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
817         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
818     } else {
819         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
820         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
821               ADD(s1_1, dc_1));
822     }
823
824     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
825 }
826
827 #undef VREG
828 #undef LOAD_CST
829 #undef LOADU
830 #undef LOAD
831 #undef STORE
832 #undef STOREU
833 #undef ADD
834 #undef ADD3
835 #undef SUB
836 #undef SAR
837
838 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
839
840 #if !defined(STANDARD_SLOW_VERSION)
841 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
842  * pixel is on even coordinate */
843 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
844                              const OPJ_INT32 sn,
845                              const OPJ_INT32 len,
846                              OPJ_INT32* tiledp_col,
847                              const OPJ_SIZE_T stride)
848 {
849     OPJ_INT32 i, j;
850     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
851
852     assert(len > 1);
853
854     /* Performs lifting in one single iteration. Saves memory */
855     /* accesses and explicit interleaving. */
856
857     s1n = tiledp_col[0];
858     d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
859     s0n = s1n - ((d1n + 1) >> 1);
860
861     for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
862         d1c = d1n;
863         s0c = s0n;
864
865         s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
866         d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
867
868         s0n = s1n - ((d1c + d1n + 2) >> 2);
869
870         tmp[i  ] = s0c;
871         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
872     }
873
874     tmp[i] = s0n;
875
876     if (len & 1) {
877         tmp[len - 1] =
878             tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
879             ((d1n + 1) >> 1);
880         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
881     } else {
882         tmp[len - 1] = d1n + s0n;
883     }
884
885     for (i = 0; i < len; ++i) {
886         tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
887     }
888 }
889
890
891 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
892  * pixel is on odd coordinate */
893 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
894                              const OPJ_INT32 sn,
895                              const OPJ_INT32 len,
896                              OPJ_INT32* tiledp_col,
897                              const OPJ_SIZE_T stride)
898 {
899     OPJ_INT32 i, j;
900     OPJ_INT32 s1, s2, dc, dn;
901     const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
902     const OPJ_INT32* in_odd = &tiledp_col[0];
903
904     assert(len > 2);
905
906     /* Performs lifting in one single iteration. Saves memory */
907     /* accesses and explicit interleaving. */
908
909     s1 = in_even[stride];
910     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
911     tmp[0] = in_even[0] + dc;
912     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
913
914         s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
915
916         dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
917         tmp[i  ] = dc;
918         tmp[i + 1] = s1 + ((dn + dc) >> 1);
919
920         dc = dn;
921         s1 = s2;
922     }
923     tmp[i] = dc;
924     if (!(len & 1)) {
925         dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
926         tmp[len - 2] = s1 + ((dn + dc) >> 1);
927         tmp[len - 1] = dn;
928     } else {
929         tmp[len - 1] = s1 + dc;
930     }
931
932     for (i = 0; i < len; ++i) {
933         tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
934     }
935 }
936 #endif /* !defined(STANDARD_SLOW_VERSION) */
937
938 /* <summary>                            */
939 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
940 /* </summary>                           */
941 /* Performs interleave, inverse wavelet transform and copy back to buffer */
942 static void opj_idwt53_v(const opj_dwt_t *dwt,
943                          OPJ_INT32* tiledp_col,
944                          OPJ_SIZE_T stride,
945                          OPJ_INT32 nb_cols)
946 {
947 #ifdef STANDARD_SLOW_VERSION
948     /* For documentation purpose */
949     OPJ_INT32 k, c;
950     for (c = 0; c < nb_cols; c ++) {
951         opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
952         opj_dwt_decode_1(dwt);
953         for (k = 0; k < dwt->sn + dwt->dn; ++k) {
954             tiledp_col[c + k * stride] = dwt->mem[k];
955         }
956     }
957 #else
958     const OPJ_INT32 sn = dwt->sn;
959     const OPJ_INT32 len = sn + dwt->dn;
960     if (dwt->cas == 0) {
961         /* If len == 1, unmodified value */
962
963 #if (defined(__SSE2__) || defined(__AVX2__))
964         if (len > 1 && nb_cols == PARALLEL_COLS_53) {
965             /* Same as below general case, except that thanks to SSE2/AVX2 */
966             /* we can efficiently process 8/16 columns in parallel */
967             opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
968             return;
969         }
970 #endif
971         if (len > 1) {
972             OPJ_INT32 c;
973             for (c = 0; c < nb_cols; c++, tiledp_col++) {
974                 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
975             }
976             return;
977         }
978     } else {
979         if (len == 1) {
980             OPJ_INT32 c;
981             for (c = 0; c < nb_cols; c++, tiledp_col++) {
982                 tiledp_col[0] /= 2;
983             }
984             return;
985         }
986
987         if (len == 2) {
988             OPJ_INT32 c;
989             OPJ_INT32* out = dwt->mem;
990             for (c = 0; c < nb_cols; c++, tiledp_col++) {
991                 OPJ_INT32 i;
992                 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
993                 const OPJ_INT32* in_odd = &tiledp_col[0];
994
995                 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
996                 out[0] = in_even[0] + out[1];
997
998                 for (i = 0; i < len; ++i) {
999                     tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
1000                 }
1001             }
1002
1003             return;
1004         }
1005
1006 #if (defined(__SSE2__) || defined(__AVX2__))
1007         if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1008             /* Same as below general case, except that thanks to SSE2/AVX2 */
1009             /* we can efficiently process 8/16 columns in parallel */
1010             opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1011             return;
1012         }
1013 #endif
1014         if (len > 2) {
1015             OPJ_INT32 c;
1016             for (c = 0; c < nb_cols; c++, tiledp_col++) {
1017                 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1018             }
1019             return;
1020         }
1021     }
1022 #endif
1023 }
1024
1025 static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
1026                                  OPJ_UINT32 start,
1027                                  OPJ_UINT32 end,
1028                                  const OPJ_FLOAT32 c)
1029 {
1030     OPJ_UINT32 i;
1031     for (i = start; i < end; ++i) {
1032         fw[i * 2] *= c;
1033     }
1034 }
1035 static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1036                                  OPJ_UINT32 start,
1037                                  OPJ_UINT32 end,
1038                                  OPJ_UINT32 m,
1039                                  OPJ_FLOAT32 c)
1040 {
1041     OPJ_UINT32 i;
1042     OPJ_UINT32 imax = opj_uint_min(end, m);
1043     if (start > 0) {
1044         fw += 2 * start;
1045         fl = fw - 2;
1046     }
1047     for (i = start; i < imax; ++i) {
1048         fw[-1] += (fl[0] + fw[0]) * c;
1049         fl = fw;
1050         fw += 2;
1051     }
1052     if (m < end) {
1053         assert(m + 1 == end);
1054         fw[-1] += (2 * fl[0]) * c;
1055     }
1056 }
1057
1058 static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1059                                   OPJ_INT32 cas)
1060 {
1061     OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1062     OPJ_INT32 a, b;
1063     if (cas == 0) {
1064         if (!((dn > 0) || (sn > 1))) {
1065             return;
1066         }
1067         a = 0;
1068         b = 1;
1069     } else {
1070         if (!((sn > 0) || (dn > 1))) {
1071             return;
1072         }
1073         a = 1;
1074         b = 0;
1075     }
1076     opj_dwt_encode_step2(w + a, w + b + 1,
1077                          0, (OPJ_UINT32)dn,
1078                          (OPJ_UINT32)opj_int_min(dn, sn - b),
1079                          opj_dwt_alpha);
1080     opj_dwt_encode_step2(w + b, w + a + 1,
1081                          0, (OPJ_UINT32)sn,
1082                          (OPJ_UINT32)opj_int_min(sn, dn - a),
1083                          opj_dwt_beta);
1084     opj_dwt_encode_step2(w + a, w + b + 1,
1085                          0, (OPJ_UINT32)dn,
1086                          (OPJ_UINT32)opj_int_min(dn, sn - b),
1087                          opj_dwt_gamma);
1088     opj_dwt_encode_step2(w + b, w + a + 1,
1089                          0, (OPJ_UINT32)sn,
1090                          (OPJ_UINT32)opj_int_min(sn, dn - a),
1091                          opj_dwt_delta);
1092     opj_dwt_encode_step1(w + b, 0, (OPJ_UINT32)dn,
1093                          opj_K);
1094     opj_dwt_encode_step1(w + a, 0, (OPJ_UINT32)sn,
1095                          opj_invK);
1096 }
1097
1098 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1099                                     opj_stepsize_t *bandno_stepsize)
1100 {
1101     OPJ_INT32 p, n;
1102     p = opj_int_floorlog2(stepsize) - 13;
1103     n = 11 - opj_int_floorlog2(stepsize);
1104     bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1105     bandno_stepsize->expn = numbps - p;
1106 }
1107
1108 /*
1109 ==========================================================
1110    DWT interface
1111 ==========================================================
1112 */
1113
1114 /** Process one line for the horizontal pass of the 5x3 forward transform */
1115 static
1116 void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1117         void* tmpIn,
1118         OPJ_UINT32 width,
1119         OPJ_BOOL even)
1120 {
1121     OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1122     OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1123     const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1124     const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1125     memcpy(tmp, row, width * sizeof(OPJ_INT32));
1126     opj_dwt_encode_1(tmp, dn, sn, even ? 0 : 1);
1127     opj_dwt_deinterleave_h(tmp, row, dn, sn, even ? 0 : 1);
1128 }
1129
1130 /** Process one line for the horizontal pass of the 9x7 forward transform */
1131 static
1132 void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1133         void* tmpIn,
1134         OPJ_UINT32 width,
1135         OPJ_BOOL even)
1136 {
1137     OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1138     OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1139     const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1140     const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1141     memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1142     opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1143     opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1144                            (OPJ_INT32 * OPJ_RESTRICT)row,
1145                            dn, sn, even ? 0 : 1);
1146 }
1147
1148 typedef struct {
1149     opj_dwt_t h;
1150     OPJ_UINT32 rw; /* Width of the resolution to process */
1151     OPJ_UINT32 w; /* Width of tiledp */
1152     OPJ_INT32 * OPJ_RESTRICT tiledp;
1153     OPJ_UINT32 min_j;
1154     OPJ_UINT32 max_j;
1155     opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1156 } opj_dwt_encode_h_job_t;
1157
1158 static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1159 {
1160     OPJ_UINT32 j;
1161     opj_dwt_encode_h_job_t* job;
1162     (void)tls;
1163
1164     job = (opj_dwt_encode_h_job_t*)user_data;
1165     for (j = job->min_j; j < job->max_j; j++) {
1166         OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1167         (*job->p_function)(aj, job->h.mem, job->rw,
1168                            job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1169     }
1170
1171     opj_aligned_free(job->h.mem);
1172     opj_free(job);
1173 }
1174
1175 typedef struct {
1176     opj_dwt_t v;
1177     OPJ_UINT32 rh;
1178     OPJ_UINT32 w;
1179     OPJ_INT32 * OPJ_RESTRICT tiledp;
1180     OPJ_UINT32 min_j;
1181     OPJ_UINT32 max_j;
1182     opj_encode_one_row_fnptr_type p_function;
1183 } opj_dwt_encode_v_job_t;
1184
1185 static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1186 {
1187     OPJ_UINT32 j;
1188     opj_dwt_encode_v_job_t* job;
1189     (void)tls;
1190
1191     job = (opj_dwt_encode_v_job_t*)user_data;
1192     for (j = job->min_j; j < job->max_j; j++) {
1193         OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j;
1194         OPJ_UINT32 k;
1195         for (k = 0; k < job->rh; ++k) {
1196             job->v.mem[k] = aj[k * job->w];
1197         }
1198
1199         (*job->p_function)(job->v.mem, job->v.dn, job->v.sn, job->v.cas);
1200
1201         opj_dwt_deinterleave_v(job->v.mem, aj, job->v.dn, job->v.sn, job->w,
1202                                job->v.cas);
1203     }
1204
1205     opj_aligned_free(job->v.mem);
1206     opj_free(job);
1207 }
1208
1209 /* <summary>                            */
1210 /* Forward 5-3 wavelet transform in 2-D. */
1211 /* </summary>                           */
1212 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1213         opj_tcd_tilecomp_t * tilec,
1214         opj_encode_one_row_fnptr_type p_function,
1215         opj_encode_and_deinterleave_h_one_row_fnptr_type
1216         p_encode_and_deinterleave_h_one_row)
1217 {
1218     OPJ_INT32 i;
1219     OPJ_INT32 *bj = 00;
1220     OPJ_UINT32 w;
1221     OPJ_INT32 l;
1222
1223     OPJ_SIZE_T l_data_size;
1224
1225     opj_tcd_resolution_t * l_cur_res = 0;
1226     opj_tcd_resolution_t * l_last_res = 0;
1227     const int num_threads = opj_thread_pool_get_thread_count(tp);
1228     OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1229
1230     w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1231     l = (OPJ_INT32)tilec->numresolutions - 1;
1232
1233     l_cur_res = tilec->resolutions + l;
1234     l_last_res = l_cur_res - 1;
1235
1236     l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1237     /* overflow check */
1238     if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1239         /* FIXME event manager error callback */
1240         return OPJ_FALSE;
1241     }
1242     l_data_size *= sizeof(OPJ_INT32);
1243     bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1244     /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1245     /* in that case, so do not error out */
1246     if (l_data_size != 0 && ! bj) {
1247         return OPJ_FALSE;
1248     }
1249     i = l;
1250
1251     while (i--) {
1252         OPJ_UINT32 j;
1253         OPJ_UINT32 rw;           /* width of the resolution level computed   */
1254         OPJ_UINT32 rh;           /* height of the resolution level computed  */
1255         OPJ_UINT32
1256         rw1;      /* width of the resolution level once lower than computed one                                       */
1257         OPJ_UINT32
1258         rh1;      /* height of the resolution level once lower than computed one                                      */
1259         OPJ_INT32 cas_col;  /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1260         OPJ_INT32 cas_row;  /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
1261         OPJ_INT32 dn, sn;
1262
1263         rw  = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1264         rh  = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1265         rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1266         rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1267
1268         cas_row = l_cur_res->x0 & 1;
1269         cas_col = l_cur_res->y0 & 1;
1270
1271         sn = (OPJ_INT32)rh1;
1272         dn = (OPJ_INT32)(rh - rh1);
1273
1274         /* Perform vertical pass */
1275         if (num_threads <= 1 || rw <= 1) {
1276             for (j = 0; j < rw; ++j) {
1277                 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j;
1278                 OPJ_UINT32 k;
1279                 for (k = 0; k < rh; ++k) {
1280                     bj[k] = aj[k * w];
1281                 }
1282
1283                 (*p_function)(bj, dn, sn, cas_col);
1284
1285                 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1286             }
1287         }  else {
1288             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1289             OPJ_UINT32 step_j;
1290
1291             if (rw < num_jobs) {
1292                 num_jobs = rw;
1293             }
1294             step_j = (rw / num_jobs);
1295
1296             for (j = 0; j < num_jobs; j++) {
1297                 opj_dwt_encode_v_job_t* job;
1298
1299                 job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
1300                 if (!job) {
1301                     opj_thread_pool_wait_completion(tp, 0);
1302                     opj_aligned_free(bj);
1303                     return OPJ_FALSE;
1304                 }
1305                 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1306                 if (!job->v.mem) {
1307                     opj_thread_pool_wait_completion(tp, 0);
1308                     opj_free(job);
1309                     opj_aligned_free(bj);
1310                     return OPJ_FALSE;
1311                 }
1312                 job->v.dn = dn;
1313                 job->v.sn = sn;
1314                 job->v.cas = cas_col;
1315                 job->rh = rh;
1316                 job->w = w;
1317                 job->tiledp = tiledp;
1318                 job->min_j = j * step_j;
1319                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1320                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1321                     job->max_j = rw;
1322                 }
1323                 job->p_function = p_function;
1324                 opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
1325             }
1326             opj_thread_pool_wait_completion(tp, 0);
1327         }
1328
1329         sn = (OPJ_INT32)rw1;
1330         dn = (OPJ_INT32)(rw - rw1);
1331
1332         /* Perform horizontal pass */
1333         if (num_threads <= 1 || rh <= 1) {
1334             for (j = 0; j < rh; j++) {
1335                 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
1336                 (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
1337                                                        cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
1338             }
1339         }  else {
1340             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1341             OPJ_UINT32 step_j;
1342
1343             if (rh < num_jobs) {
1344                 num_jobs = rh;
1345             }
1346             step_j = (rh / num_jobs);
1347
1348             for (j = 0; j < num_jobs; j++) {
1349                 opj_dwt_encode_h_job_t* job;
1350
1351                 job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
1352                 if (!job) {
1353                     opj_thread_pool_wait_completion(tp, 0);
1354                     opj_aligned_free(bj);
1355                     return OPJ_FALSE;
1356                 }
1357                 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1358                 if (!job->h.mem) {
1359                     opj_thread_pool_wait_completion(tp, 0);
1360                     opj_free(job);
1361                     opj_aligned_free(bj);
1362                     return OPJ_FALSE;
1363                 }
1364                 job->h.dn = dn;
1365                 job->h.sn = sn;
1366                 job->h.cas = cas_row;
1367                 job->rw = rw;
1368                 job->w = w;
1369                 job->tiledp = tiledp;
1370                 job->min_j = j * step_j;
1371                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1372                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1373                     job->max_j = rh;
1374                 }
1375                 job->p_function = p_encode_and_deinterleave_h_one_row;
1376                 opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
1377             }
1378             opj_thread_pool_wait_completion(tp, 0);
1379         }
1380
1381         l_cur_res = l_last_res;
1382
1383         --l_last_res;
1384     }
1385
1386     opj_aligned_free(bj);
1387     return OPJ_TRUE;
1388 }
1389
1390 /* Forward 5-3 wavelet transform in 2-D. */
1391 /* </summary>                           */
1392 OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
1393                         opj_tcd_tilecomp_t * tilec)
1394 {
1395     return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1396                                     opj_dwt_encode_1,
1397                                     opj_dwt_encode_and_deinterleave_h_one_row);
1398 }
1399
1400 /* <summary>                            */
1401 /* Inverse 5-3 wavelet transform in 2-D. */
1402 /* </summary>                           */
1403 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1404                         OPJ_UINT32 numres)
1405 {
1406     if (p_tcd->whole_tile_decoding) {
1407         return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1408     } else {
1409         return opj_dwt_decode_partial_tile(tilec, numres);
1410     }
1411 }
1412
1413 /* <summary>                */
1414 /* Get norm of 5-3 wavelet. */
1415 /* </summary>               */
1416 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1417 {
1418     /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1419     /* but the array should really be extended up to 33 resolution levels */
1420     /* See https://github.com/uclouvain/openjpeg/issues/493 */
1421     if (orient == 0 && level >= 10) {
1422         level = 9;
1423     } else if (orient > 0 && level >= 9) {
1424         level = 8;
1425     }
1426     return opj_dwt_norms[orient][level];
1427 }
1428
1429 /* <summary>                             */
1430 /* Forward 9-7 wavelet transform in 2-D. */
1431 /* </summary>                            */
1432 OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
1433                              opj_tcd_tilecomp_t * tilec)
1434 {
1435     return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1436                                     opj_dwt_encode_1_real,
1437                                     opj_dwt_encode_and_deinterleave_h_one_row_real);
1438 }
1439
1440 /* <summary>                */
1441 /* Get norm of 9-7 wavelet. */
1442 /* </summary>               */
1443 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1444 {
1445     /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1446     /* but the array should really be extended up to 33 resolution levels */
1447     /* See https://github.com/uclouvain/openjpeg/issues/493 */
1448     if (orient == 0 && level >= 10) {
1449         level = 9;
1450     } else if (orient > 0 && level >= 9) {
1451         level = 8;
1452     }
1453     return opj_dwt_norms_real[orient][level];
1454 }
1455
1456 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1457 {
1458     OPJ_UINT32 numbands, bandno;
1459     numbands = 3 * tccp->numresolutions - 2;
1460     for (bandno = 0; bandno < numbands; bandno++) {
1461         OPJ_FLOAT64 stepsize;
1462         OPJ_UINT32 resno, level, orient, gain;
1463
1464         resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1465         orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1466         level = tccp->numresolutions - 1 - resno;
1467         gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1468                                           (orient == 2)) ? 1 : 2));
1469         if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1470             stepsize = 1.0;
1471         } else {
1472             OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1473             stepsize = (1 << (gain)) / norm;
1474         }
1475         opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1476                                 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1477     }
1478 }
1479
1480 /* <summary>                             */
1481 /* Determine maximum computed resolution level for inverse wavelet transform */
1482 /* </summary>                            */
1483 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1484         OPJ_UINT32 i)
1485 {
1486     OPJ_UINT32 mr   = 0;
1487     OPJ_UINT32 w;
1488     while (--i) {
1489         ++r;
1490         if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1491             mr = w ;
1492         }
1493         if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1494             mr = w ;
1495         }
1496     }
1497     return mr ;
1498 }
1499
1500 typedef struct {
1501     opj_dwt_t h;
1502     OPJ_UINT32 rw;
1503     OPJ_UINT32 w;
1504     OPJ_INT32 * OPJ_RESTRICT tiledp;
1505     OPJ_UINT32 min_j;
1506     OPJ_UINT32 max_j;
1507 } opj_dwt_decode_h_job_t;
1508
1509 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1510 {
1511     OPJ_UINT32 j;
1512     opj_dwt_decode_h_job_t* job;
1513     (void)tls;
1514
1515     job = (opj_dwt_decode_h_job_t*)user_data;
1516     for (j = job->min_j; j < job->max_j; j++) {
1517         opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1518     }
1519
1520     opj_aligned_free(job->h.mem);
1521     opj_free(job);
1522 }
1523
1524 typedef struct {
1525     opj_dwt_t v;
1526     OPJ_UINT32 rh;
1527     OPJ_UINT32 w;
1528     OPJ_INT32 * OPJ_RESTRICT tiledp;
1529     OPJ_UINT32 min_j;
1530     OPJ_UINT32 max_j;
1531 } opj_dwt_decode_v_job_t;
1532
1533 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1534 {
1535     OPJ_UINT32 j;
1536     opj_dwt_decode_v_job_t* job;
1537     (void)tls;
1538
1539     job = (opj_dwt_decode_v_job_t*)user_data;
1540     for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1541             j += PARALLEL_COLS_53) {
1542         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1543                      PARALLEL_COLS_53);
1544     }
1545     if (j < job->max_j)
1546         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1547                      (OPJ_INT32)(job->max_j - j));
1548
1549     opj_aligned_free(job->v.mem);
1550     opj_free(job);
1551 }
1552
1553
1554 /* <summary>                            */
1555 /* Inverse wavelet transform in 2-D.    */
1556 /* </summary>                           */
1557 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1558                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1559 {
1560     opj_dwt_t h;
1561     opj_dwt_t v;
1562
1563     opj_tcd_resolution_t* tr = tilec->resolutions;
1564
1565     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1566                                  tr->x0);  /* width of the resolution level computed */
1567     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1568                                  tr->y0);  /* height of the resolution level computed */
1569
1570     OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1571                                                                1].x1 -
1572                                 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1573     OPJ_SIZE_T h_mem_size;
1574     int num_threads;
1575
1576     if (numres == 1U) {
1577         return OPJ_TRUE;
1578     }
1579     num_threads = opj_thread_pool_get_thread_count(tp);
1580     h_mem_size = opj_dwt_max_resolution(tr, numres);
1581     /* overflow check */
1582     if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1583         /* FIXME event manager error callback */
1584         return OPJ_FALSE;
1585     }
1586     /* We need PARALLEL_COLS_53 times the height of the array, */
1587     /* since for the vertical pass */
1588     /* we process PARALLEL_COLS_53 columns at a time */
1589     h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1590     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1591     if (! h.mem) {
1592         /* FIXME event manager error callback */
1593         return OPJ_FALSE;
1594     }
1595
1596     v.mem = h.mem;
1597
1598     while (--numres) {
1599         OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1600         OPJ_UINT32 j;
1601
1602         ++tr;
1603         h.sn = (OPJ_INT32)rw;
1604         v.sn = (OPJ_INT32)rh;
1605
1606         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1607         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1608
1609         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1610         h.cas = tr->x0 % 2;
1611
1612         if (num_threads <= 1 || rh <= 1) {
1613             for (j = 0; j < rh; ++j) {
1614                 opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
1615             }
1616         } else {
1617             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1618             OPJ_UINT32 step_j;
1619
1620             if (rh < num_jobs) {
1621                 num_jobs = rh;
1622             }
1623             step_j = (rh / num_jobs);
1624
1625             for (j = 0; j < num_jobs; j++) {
1626                 opj_dwt_decode_h_job_t* job;
1627
1628                 job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
1629                 if (!job) {
1630                     /* It would be nice to fallback to single thread case, but */
1631                     /* unfortunately some jobs may be launched and have modified */
1632                     /* tiledp, so it is not practical to recover from that error */
1633                     /* FIXME event manager error callback */
1634                     opj_thread_pool_wait_completion(tp, 0);
1635                     opj_aligned_free(h.mem);
1636                     return OPJ_FALSE;
1637                 }
1638                 job->h = h;
1639                 job->rw = rw;
1640                 job->w = w;
1641                 job->tiledp = tiledp;
1642                 job->min_j = j * step_j;
1643                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1644                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1645                     job->max_j = rh;
1646                 }
1647                 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1648                 if (!job->h.mem) {
1649                     /* FIXME event manager error callback */
1650                     opj_thread_pool_wait_completion(tp, 0);
1651                     opj_free(job);
1652                     opj_aligned_free(h.mem);
1653                     return OPJ_FALSE;
1654                 }
1655                 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1656             }
1657             opj_thread_pool_wait_completion(tp, 0);
1658         }
1659
1660         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1661         v.cas = tr->y0 % 2;
1662
1663         if (num_threads <= 1 || rw <= 1) {
1664             for (j = 0; j + PARALLEL_COLS_53 <= rw;
1665                     j += PARALLEL_COLS_53) {
1666                 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
1667             }
1668             if (j < rw) {
1669                 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
1670             }
1671         } else {
1672             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1673             OPJ_UINT32 step_j;
1674
1675             if (rw < num_jobs) {
1676                 num_jobs = rw;
1677             }
1678             step_j = (rw / num_jobs);
1679
1680             for (j = 0; j < num_jobs; j++) {
1681                 opj_dwt_decode_v_job_t* job;
1682
1683                 job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
1684                 if (!job) {
1685                     /* It would be nice to fallback to single thread case, but */
1686                     /* unfortunately some jobs may be launched and have modified */
1687                     /* tiledp, so it is not practical to recover from that error */
1688                     /* FIXME event manager error callback */
1689                     opj_thread_pool_wait_completion(tp, 0);
1690                     opj_aligned_free(v.mem);
1691                     return OPJ_FALSE;
1692                 }
1693                 job->v = v;
1694                 job->rh = rh;
1695                 job->w = w;
1696                 job->tiledp = tiledp;
1697                 job->min_j = j * step_j;
1698                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1699                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1700                     job->max_j = rw;
1701                 }
1702                 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1703                 if (!job->v.mem) {
1704                     /* FIXME event manager error callback */
1705                     opj_thread_pool_wait_completion(tp, 0);
1706                     opj_free(job);
1707                     opj_aligned_free(v.mem);
1708                     return OPJ_FALSE;
1709                 }
1710                 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1711             }
1712             opj_thread_pool_wait_completion(tp, 0);
1713         }
1714     }
1715     opj_aligned_free(h.mem);
1716     return OPJ_TRUE;
1717 }
1718
1719 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1720         OPJ_INT32 cas,
1721         opj_sparse_array_int32_t* sa,
1722         OPJ_UINT32 sa_line,
1723         OPJ_UINT32 sn,
1724         OPJ_UINT32 win_l_x0,
1725         OPJ_UINT32 win_l_x1,
1726         OPJ_UINT32 win_h_x0,
1727         OPJ_UINT32 win_h_x1)
1728 {
1729     OPJ_BOOL ret;
1730     ret = opj_sparse_array_int32_read(sa,
1731                                       win_l_x0, sa_line,
1732                                       win_l_x1, sa_line + 1,
1733                                       dest + cas + 2 * win_l_x0,
1734                                       2, 0, OPJ_TRUE);
1735     assert(ret);
1736     ret = opj_sparse_array_int32_read(sa,
1737                                       sn + win_h_x0, sa_line,
1738                                       sn + win_h_x1, sa_line + 1,
1739                                       dest + 1 - cas + 2 * win_h_x0,
1740                                       2, 0, OPJ_TRUE);
1741     assert(ret);
1742     OPJ_UNUSED(ret);
1743 }
1744
1745
1746 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1747         OPJ_INT32 cas,
1748         opj_sparse_array_int32_t* sa,
1749         OPJ_UINT32 sa_col,
1750         OPJ_UINT32 nb_cols,
1751         OPJ_UINT32 sn,
1752         OPJ_UINT32 win_l_y0,
1753         OPJ_UINT32 win_l_y1,
1754         OPJ_UINT32 win_h_y0,
1755         OPJ_UINT32 win_h_y1)
1756 {
1757     OPJ_BOOL ret;
1758     ret  = opj_sparse_array_int32_read(sa,
1759                                        sa_col, win_l_y0,
1760                                        sa_col + nb_cols, win_l_y1,
1761                                        dest + cas * 4 + 2 * 4 * win_l_y0,
1762                                        1, 2 * 4, OPJ_TRUE);
1763     assert(ret);
1764     ret = opj_sparse_array_int32_read(sa,
1765                                       sa_col, sn + win_h_y0,
1766                                       sa_col + nb_cols, sn + win_h_y1,
1767                                       dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
1768                                       1, 2 * 4, OPJ_TRUE);
1769     assert(ret);
1770     OPJ_UNUSED(ret);
1771 }
1772
1773 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1774                                      OPJ_INT32 cas,
1775                                      OPJ_INT32 win_l_x0,
1776                                      OPJ_INT32 win_l_x1,
1777                                      OPJ_INT32 win_h_x0,
1778                                      OPJ_INT32 win_h_x1)
1779 {
1780     OPJ_INT32 i;
1781
1782     if (!cas) {
1783         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
1784
1785             /* Naive version is :
1786             for (i = win_l_x0; i < i_max; i++) {
1787                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1788             }
1789             for (i = win_h_x0; i < win_h_x1; i++) {
1790                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1791             }
1792             but the compiler doesn't manage to unroll it to avoid bound
1793             checking in OPJ_S_ and OPJ_D_ macros
1794             */
1795
1796             i = win_l_x0;
1797             if (i < win_l_x1) {
1798                 OPJ_INT32 i_max;
1799
1800                 /* Left-most case */
1801                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1802                 i ++;
1803
1804                 i_max = win_l_x1;
1805                 if (i_max > dn) {
1806                     i_max = dn;
1807                 }
1808                 for (; i < i_max; i++) {
1809                     /* No bound checking */
1810                     OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
1811                 }
1812                 for (; i < win_l_x1; i++) {
1813                     /* Right-most case */
1814                     OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1815                 }
1816             }
1817
1818             i = win_h_x0;
1819             if (i < win_h_x1) {
1820                 OPJ_INT32 i_max = win_h_x1;
1821                 if (i_max >= sn) {
1822                     i_max = sn - 1;
1823                 }
1824                 for (; i < i_max; i++) {
1825                     /* No bound checking */
1826                     OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
1827                 }
1828                 for (; i < win_h_x1; i++) {
1829                     /* Right-most case */
1830                     OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1831                 }
1832             }
1833         }
1834     } else {
1835         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
1836             OPJ_S(0) /= 2;
1837         } else {
1838             for (i = win_l_x0; i < win_l_x1; i++) {
1839                 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1840             }
1841             for (i = win_h_x0; i < win_h_x1; i++) {
1842                 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1843             }
1844         }
1845     }
1846 }
1847
1848 #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
1849 #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
1850 #define OPJ_S__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=sn?OPJ_S_off(sn-1,off):OPJ_S_off(i,off)))
1851 #define OPJ_D__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=dn?OPJ_D_off(dn-1,off):OPJ_D_off(i,off)))
1852 #define OPJ_SS__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=dn?OPJ_S_off(dn-1,off):OPJ_S_off(i,off)))
1853 #define OPJ_DD__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=sn?OPJ_D_off(sn-1,off):OPJ_D_off(i,off)))
1854
1855 static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
1856         OPJ_UINT32 nb_cols,
1857         OPJ_INT32 dn, OPJ_INT32 sn,
1858         OPJ_INT32 cas,
1859         OPJ_INT32 win_l_x0,
1860         OPJ_INT32 win_l_x1,
1861         OPJ_INT32 win_h_x0,
1862         OPJ_INT32 win_h_x1)
1863 {
1864     OPJ_INT32 i;
1865     OPJ_UINT32 off;
1866
1867     (void)nb_cols;
1868
1869     if (!cas) {
1870         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
1871
1872             /* Naive version is :
1873             for (i = win_l_x0; i < i_max; i++) {
1874                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1875             }
1876             for (i = win_h_x0; i < win_h_x1; i++) {
1877                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1878             }
1879             but the compiler doesn't manage to unroll it to avoid bound
1880             checking in OPJ_S_ and OPJ_D_ macros
1881             */
1882
1883             i = win_l_x0;
1884             if (i < win_l_x1) {
1885                 OPJ_INT32 i_max;
1886
1887                 /* Left-most case */
1888                 for (off = 0; off < 4; off++) {
1889                     OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1890                 }
1891                 i ++;
1892
1893                 i_max = win_l_x1;
1894                 if (i_max > dn) {
1895                     i_max = dn;
1896                 }
1897
1898 #ifdef __SSE2__
1899                 if (i + 1 < i_max) {
1900                     const __m128i two = _mm_set1_epi32(2);
1901                     __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
1902                     for (; i + 1 < i_max; i += 2) {
1903                         /* No bound checking */
1904                         __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
1905                         __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1906                         __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1907                         __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
1908                         S = _mm_sub_epi32(S,
1909                                           _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
1910                         S1 = _mm_sub_epi32(S1,
1911                                            _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
1912                         _mm_store_si128((__m128i*)(a + i * 8), S);
1913                         _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
1914                         Dm1 = D1;
1915                     }
1916                 }
1917 #endif
1918
1919                 for (; i < i_max; i++) {
1920                     /* No bound checking */
1921                     for (off = 0; off < 4; off++) {
1922                         OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
1923                     }
1924                 }
1925                 for (; i < win_l_x1; i++) {
1926                     /* Right-most case */
1927                     for (off = 0; off < 4; off++) {
1928                         OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1929                     }
1930                 }
1931             }
1932
1933             i = win_h_x0;
1934             if (i < win_h_x1) {
1935                 OPJ_INT32 i_max = win_h_x1;
1936                 if (i_max >= sn) {
1937                     i_max = sn - 1;
1938                 }
1939
1940 #ifdef __SSE2__
1941                 if (i + 1 < i_max) {
1942                     __m128i S =  _mm_load_si128((__m128i * const)(a + i * 8));
1943                     for (; i + 1 < i_max; i += 2) {
1944                         /* No bound checking */
1945                         __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1946                         __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1947                         __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
1948                         __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
1949                         D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
1950                         D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
1951                         _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
1952                         _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
1953                         S = S2;
1954                     }
1955                 }
1956 #endif
1957
1958                 for (; i < i_max; i++) {
1959                     /* No bound checking */
1960                     for (off = 0; off < 4; off++) {
1961                         OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
1962                     }
1963                 }
1964                 for (; i < win_h_x1; i++) {
1965                     /* Right-most case */
1966                     for (off = 0; off < 4; off++) {
1967                         OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
1968                     }
1969                 }
1970             }
1971         }
1972     } else {
1973         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
1974             for (off = 0; off < 4; off++) {
1975                 OPJ_S_off(0, off) /= 2;
1976             }
1977         } else {
1978             for (i = win_l_x0; i < win_l_x1; i++) {
1979                 for (off = 0; off < 4; off++) {
1980                     OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2;
1981                 }
1982             }
1983             for (i = win_h_x0; i < win_h_x1; i++) {
1984                 for (off = 0; off < 4; off++) {
1985                     OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1;
1986                 }
1987             }
1988         }
1989     }
1990 }
1991
1992 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
1993         OPJ_UINT32 resno,
1994         OPJ_UINT32 bandno,
1995         OPJ_UINT32 tcx0,
1996         OPJ_UINT32 tcy0,
1997         OPJ_UINT32 tcx1,
1998         OPJ_UINT32 tcy1,
1999         OPJ_UINT32* tbx0,
2000         OPJ_UINT32* tby0,
2001         OPJ_UINT32* tbx1,
2002         OPJ_UINT32* tby1)
2003 {
2004     /* Compute number of decomposition for this band. See table F-1 */
2005     OPJ_UINT32 nb = (resno == 0) ?
2006                     tilec->numresolutions - 1 :
2007                     tilec->numresolutions - resno;
2008     /* Map above tile-based coordinates to sub-band-based coordinates per */
2009     /* equation B-15 of the standard */
2010     OPJ_UINT32 x0b = bandno & 1;
2011     OPJ_UINT32 y0b = bandno >> 1;
2012     if (tbx0) {
2013         *tbx0 = (nb == 0) ? tcx0 :
2014                 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2015                 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2016     }
2017     if (tby0) {
2018         *tby0 = (nb == 0) ? tcy0 :
2019                 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2020                 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2021     }
2022     if (tbx1) {
2023         *tbx1 = (nb == 0) ? tcx1 :
2024                 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2025                 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2026     }
2027     if (tby1) {
2028         *tby1 = (nb == 0) ? tcy1 :
2029                 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2030                 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2031     }
2032 }
2033
2034 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2035                                  OPJ_UINT32 max_size,
2036                                  OPJ_UINT32* start,
2037                                  OPJ_UINT32* end)
2038 {
2039     *start = opj_uint_subs(*start, filter_width);
2040     *end = opj_uint_adds(*end, filter_width);
2041     *end = opj_uint_min(*end, max_size);
2042 }
2043
2044
2045 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2046     opj_tcd_tilecomp_t* tilec,
2047     OPJ_UINT32 numres)
2048 {
2049     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2050     OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2051     OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2052     OPJ_UINT32 resno, bandno, precno, cblkno;
2053     opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2054                                        w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2055     if (sa == NULL) {
2056         return NULL;
2057     }
2058
2059     for (resno = 0; resno < numres; ++resno) {
2060         opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2061
2062         for (bandno = 0; bandno < res->numbands; ++bandno) {
2063             opj_tcd_band_t* band = &res->bands[bandno];
2064
2065             for (precno = 0; precno < res->pw * res->ph; ++precno) {
2066                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2067                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2068                     opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2069                     if (cblk->decoded_data != NULL) {
2070                         OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2071                         OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2072                         OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2073                         OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2074
2075                         if (band->bandno & 1) {
2076                             opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2077                             x += (OPJ_UINT32)(pres->x1 - pres->x0);
2078                         }
2079                         if (band->bandno & 2) {
2080                             opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2081                             y += (OPJ_UINT32)(pres->y1 - pres->y0);
2082                         }
2083
2084                         if (!opj_sparse_array_int32_write(sa, x, y,
2085                                                           x + cblk_w, y + cblk_h,
2086                                                           cblk->decoded_data,
2087                                                           1, cblk_w, OPJ_TRUE)) {
2088                             opj_sparse_array_int32_free(sa);
2089                             return NULL;
2090                         }
2091                     }
2092                 }
2093             }
2094         }
2095     }
2096
2097     return sa;
2098 }
2099
2100
2101 static OPJ_BOOL opj_dwt_decode_partial_tile(
2102     opj_tcd_tilecomp_t* tilec,
2103     OPJ_UINT32 numres)
2104 {
2105     opj_sparse_array_int32_t* sa;
2106     opj_dwt_t h;
2107     opj_dwt_t v;
2108     OPJ_UINT32 resno;
2109     /* This value matches the maximum left/right extension given in tables */
2110     /* F.2 and F.3 of the standard. */
2111     const OPJ_UINT32 filter_width = 2U;
2112
2113     opj_tcd_resolution_t* tr = tilec->resolutions;
2114     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2115
2116     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2117                                  tr->x0);  /* width of the resolution level computed */
2118     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2119                                  tr->y0);  /* height of the resolution level computed */
2120
2121     OPJ_SIZE_T h_mem_size;
2122
2123     /* Compute the intersection of the area of interest, expressed in tile coordinates */
2124     /* with the tile coordinates */
2125     OPJ_UINT32 win_tcx0 = tilec->win_x0;
2126     OPJ_UINT32 win_tcy0 = tilec->win_y0;
2127     OPJ_UINT32 win_tcx1 = tilec->win_x1;
2128     OPJ_UINT32 win_tcy1 = tilec->win_y1;
2129
2130     if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2131         return OPJ_TRUE;
2132     }
2133
2134     sa = opj_dwt_init_sparse_array(tilec, numres);
2135     if (sa == NULL) {
2136         return OPJ_FALSE;
2137     }
2138
2139     if (numres == 1U) {
2140         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2141                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2142                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2143                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2144                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2145                        tilec->data_win,
2146                        1, tr_max->win_x1 - tr_max->win_x0,
2147                        OPJ_TRUE);
2148         assert(ret);
2149         OPJ_UNUSED(ret);
2150         opj_sparse_array_int32_free(sa);
2151         return OPJ_TRUE;
2152     }
2153     h_mem_size = opj_dwt_max_resolution(tr, numres);
2154     /* overflow check */
2155     /* in vertical pass, we process 4 columns at a time */
2156     if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2157         /* FIXME event manager error callback */
2158         opj_sparse_array_int32_free(sa);
2159         return OPJ_FALSE;
2160     }
2161
2162     h_mem_size *= 4 * sizeof(OPJ_INT32);
2163     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2164     if (! h.mem) {
2165         /* FIXME event manager error callback */
2166         opj_sparse_array_int32_free(sa);
2167         return OPJ_FALSE;
2168     }
2169
2170     v.mem = h.mem;
2171
2172     for (resno = 1; resno < numres; resno ++) {
2173         OPJ_UINT32 i, j;
2174         /* Window of interest subband-based coordinates */
2175         OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2176         OPJ_UINT32 win_hl_x0, win_hl_x1;
2177         OPJ_UINT32 win_lh_y0, win_lh_y1;
2178         /* Window of interest tile-resolution-based coordinates */
2179         OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2180         /* Tile-resolution subband-based coordinates */
2181         OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2182
2183         ++tr;
2184
2185         h.sn = (OPJ_INT32)rw;
2186         v.sn = (OPJ_INT32)rh;
2187
2188         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2189         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2190
2191         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2192         h.cas = tr->x0 % 2;
2193
2194         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2195         v.cas = tr->y0 % 2;
2196
2197         /* Get the subband coordinates for the window of interest */
2198         /* LL band */
2199         opj_dwt_get_band_coordinates(tilec, resno, 0,
2200                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2201                                      &win_ll_x0, &win_ll_y0,
2202                                      &win_ll_x1, &win_ll_y1);
2203
2204         /* HL band */
2205         opj_dwt_get_band_coordinates(tilec, resno, 1,
2206                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2207                                      &win_hl_x0, NULL, &win_hl_x1, NULL);
2208
2209         /* LH band */
2210         opj_dwt_get_band_coordinates(tilec, resno, 2,
2211                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2212                                      NULL, &win_lh_y0, NULL, &win_lh_y1);
2213
2214         /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2215         tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2216         tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2217         tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2218         tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2219
2220         /* Subtract the origin of the bands for this tile, to the subwindow */
2221         /* of interest band coordinates, so as to get them relative to the */
2222         /* tile */
2223         win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2224         win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2225         win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2226         win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2227         win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2228         win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2229         win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2230         win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2231
2232         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2233         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2234
2235         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2236         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2237
2238         /* Compute the tile-resolution-based coordinates for the window of interest */
2239         if (h.cas == 0) {
2240             win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2241             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2242         } else {
2243             win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2244             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2245         }
2246
2247         if (v.cas == 0) {
2248             win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2249             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2250         } else {
2251             win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2252             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2253         }
2254
2255         for (j = 0; j < rh; ++j) {
2256             if ((j >= win_ll_y0 && j < win_ll_y1) ||
2257                     (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2258
2259                 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2260                 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2261                 /* on opj_decompress -i  ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2262                 /* This is less extreme than memsetting the whole buffer to 0 */
2263                 /* although we could potentially do better with better handling of edge conditions */
2264                 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2265                     h.mem[win_tr_x1 - 1] = 0;
2266                 }
2267                 if (win_tr_x1 < rw) {
2268                     h.mem[win_tr_x1] = 0;
2269                 }
2270
2271                 opj_dwt_interleave_partial_h(h.mem,
2272                                              h.cas,
2273                                              sa,
2274                                              j,
2275                                              (OPJ_UINT32)h.sn,
2276                                              win_ll_x0,
2277                                              win_ll_x1,
2278                                              win_hl_x0,
2279                                              win_hl_x1);
2280                 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
2281                                          (OPJ_INT32)win_ll_x0,
2282                                          (OPJ_INT32)win_ll_x1,
2283                                          (OPJ_INT32)win_hl_x0,
2284                                          (OPJ_INT32)win_hl_x1);
2285                 if (!opj_sparse_array_int32_write(sa,
2286                                                   win_tr_x0, j,
2287                                                   win_tr_x1, j + 1,
2288                                                   h.mem + win_tr_x0,
2289                                                   1, 0, OPJ_TRUE)) {
2290                     /* FIXME event manager error callback */
2291                     opj_sparse_array_int32_free(sa);
2292                     opj_aligned_free(h.mem);
2293                     return OPJ_FALSE;
2294                 }
2295             }
2296         }
2297
2298         for (i = win_tr_x0; i < win_tr_x1;) {
2299             OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
2300             opj_dwt_interleave_partial_v(v.mem,
2301                                          v.cas,
2302                                          sa,
2303                                          i,
2304                                          nb_cols,
2305                                          (OPJ_UINT32)v.sn,
2306                                          win_ll_y0,
2307                                          win_ll_y1,
2308                                          win_lh_y0,
2309                                          win_lh_y1);
2310             opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
2311                                               (OPJ_INT32)win_ll_y0,
2312                                               (OPJ_INT32)win_ll_y1,
2313                                               (OPJ_INT32)win_lh_y0,
2314                                               (OPJ_INT32)win_lh_y1);
2315             if (!opj_sparse_array_int32_write(sa,
2316                                               i, win_tr_y0,
2317                                               i + nb_cols, win_tr_y1,
2318                                               v.mem + 4 * win_tr_y0,
2319                                               1, 4, OPJ_TRUE)) {
2320                 /* FIXME event manager error callback */
2321                 opj_sparse_array_int32_free(sa);
2322                 opj_aligned_free(h.mem);
2323                 return OPJ_FALSE;
2324             }
2325
2326             i += nb_cols;
2327         }
2328     }
2329     opj_aligned_free(h.mem);
2330
2331     {
2332         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2333                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2334                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2335                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2336                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2337                        tilec->data_win,
2338                        1, tr_max->win_x1 - tr_max->win_x0,
2339                        OPJ_TRUE);
2340         assert(ret);
2341         OPJ_UNUSED(ret);
2342     }
2343     opj_sparse_array_int32_free(sa);
2344     return OPJ_TRUE;
2345 }
2346
2347 static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
2348                                    OPJ_FLOAT32* OPJ_RESTRICT a,
2349                                    OPJ_UINT32 width,
2350                                    OPJ_UINT32 remaining_height)
2351 {
2352     OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2353     OPJ_UINT32 i, k;
2354     OPJ_UINT32 x0 = dwt->win_l_x0;
2355     OPJ_UINT32 x1 = dwt->win_l_x1;
2356
2357     for (k = 0; k < 2; ++k) {
2358         if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
2359                 ((OPJ_SIZE_T) bi & 0x0f) == 0) {
2360             /* Fast code path */
2361             for (i = x0; i < x1; ++i) {
2362                 OPJ_UINT32 j = i;
2363                 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2364                 dst[0] = a[j];
2365                 j += width;
2366                 dst[1] = a[j];
2367                 j += width;
2368                 dst[2] = a[j];
2369                 j += width;
2370                 dst[3] = a[j];
2371                 j += width;
2372                 dst[4] = a[j];
2373                 j += width;
2374                 dst[5] = a[j];
2375                 j += width;
2376                 dst[6] = a[j];
2377                 j += width;
2378                 dst[7] = a[j];
2379             }
2380         } else {
2381             /* Slow code path */
2382             for (i = x0; i < x1; ++i) {
2383                 OPJ_UINT32 j = i;
2384                 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2385                 dst[0] = a[j];
2386                 j += width;
2387                 if (remaining_height == 1) {
2388                     continue;
2389                 }
2390                 dst[1] = a[j];
2391                 j += width;
2392                 if (remaining_height == 2) {
2393                     continue;
2394                 }
2395                 dst[2] = a[j];
2396                 j += width;
2397                 if (remaining_height == 3) {
2398                     continue;
2399                 }
2400                 dst[3] = a[j];
2401                 j += width;
2402                 if (remaining_height == 4) {
2403                     continue;
2404                 }
2405                 dst[4] = a[j];
2406                 j += width;
2407                 if (remaining_height == 5) {
2408                     continue;
2409                 }
2410                 dst[5] = a[j];
2411                 j += width;
2412                 if (remaining_height == 6) {
2413                     continue;
2414                 }
2415                 dst[6] = a[j];
2416                 j += width;
2417                 if (remaining_height == 7) {
2418                     continue;
2419                 }
2420                 dst[7] = a[j];
2421             }
2422         }
2423
2424         bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2425         a += dwt->sn;
2426         x0 = dwt->win_h_x0;
2427         x1 = dwt->win_h_x1;
2428     }
2429 }
2430
2431 static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
2432         opj_sparse_array_int32_t* sa,
2433         OPJ_UINT32 sa_line,
2434         OPJ_UINT32 remaining_height)
2435 {
2436     OPJ_UINT32 i;
2437     for (i = 0; i < remaining_height; i++) {
2438         OPJ_BOOL ret;
2439         ret = opj_sparse_array_int32_read(sa,
2440                                           dwt->win_l_x0, sa_line + i,
2441                                           dwt->win_l_x1, sa_line + i + 1,
2442                                           /* Nasty cast from float* to int32* */
2443                                           (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2444                                           2 * NB_ELTS_V8, 0, OPJ_TRUE);
2445         assert(ret);
2446         ret = opj_sparse_array_int32_read(sa,
2447                                           (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2448                                           (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2449                                           /* Nasty cast from float* to int32* */
2450                                           (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2451                                           2 * NB_ELTS_V8, 0, OPJ_TRUE);
2452         assert(ret);
2453         OPJ_UNUSED(ret);
2454     }
2455 }
2456
2457 static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2458         OPJ_FLOAT32* OPJ_RESTRICT a,
2459         OPJ_UINT32 width,
2460         OPJ_UINT32 nb_elts_read)
2461 {
2462     opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2463     OPJ_UINT32 i;
2464
2465     for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2466         memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2467                (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2468     }
2469
2470     a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
2471     bi = dwt->wavelet + 1 - dwt->cas;
2472
2473     for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2474         memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2475                (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2476     }
2477 }
2478
2479 static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2480         opj_sparse_array_int32_t* sa,
2481         OPJ_UINT32 sa_col,
2482         OPJ_UINT32 nb_elts_read)
2483 {
2484     OPJ_BOOL ret;
2485     ret = opj_sparse_array_int32_read(sa,
2486                                       sa_col, dwt->win_l_x0,
2487                                       sa_col + nb_elts_read, dwt->win_l_x1,
2488                                       (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
2489                                       1, 2 * NB_ELTS_V8, OPJ_TRUE);
2490     assert(ret);
2491     ret = opj_sparse_array_int32_read(sa,
2492                                       sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2493                                       sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2494                                       (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
2495                                       1, 2 * NB_ELTS_V8, OPJ_TRUE);
2496     assert(ret);
2497     OPJ_UNUSED(ret);
2498 }
2499
2500 #ifdef __SSE__
2501
2502 static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
2503                                        OPJ_UINT32 start,
2504                                        OPJ_UINT32 end,
2505                                        const __m128 c)
2506 {
2507     __m128* OPJ_RESTRICT vw = (__m128*) w;
2508     OPJ_UINT32 i = start;
2509     /* To be adapted if NB_ELTS_V8 changes */
2510     vw += 4 * start;
2511     /* Note: attempt at loop unrolling x2 doesn't help */
2512     for (; i < end; ++i, vw += 4) {
2513         vw[0] = _mm_mul_ps(vw[0], c);
2514         vw[1] = _mm_mul_ps(vw[1], c);
2515     }
2516 }
2517
2518 static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
2519                                        OPJ_UINT32 start,
2520                                        OPJ_UINT32 end,
2521                                        OPJ_UINT32 m,
2522                                        __m128 c)
2523 {
2524     __m128* OPJ_RESTRICT vl = (__m128*) l;
2525     __m128* OPJ_RESTRICT vw = (__m128*) w;
2526     /* To be adapted if NB_ELTS_V8 changes */
2527     OPJ_UINT32 i;
2528     OPJ_UINT32 imax = opj_uint_min(end, m);
2529     if (start == 0) {
2530         if (imax >= 1) {
2531             vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
2532             vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
2533             vw += 4;
2534             start = 1;
2535         }
2536     } else {
2537         vw += start * 4;
2538     }
2539
2540     i = start;
2541     /* Note: attempt at loop unrolling x2 doesn't help */
2542     for (; i < imax; ++i) {
2543         vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
2544         vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
2545         vw += 4;
2546     }
2547     if (m < end) {
2548         assert(m + 1 == end);
2549         c = _mm_add_ps(c, c);
2550         vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
2551         vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
2552     }
2553 }
2554
2555 #else
2556
2557 static void opj_v8dwt_decode_step1(opj_v8_t* w,
2558                                    OPJ_UINT32 start,
2559                                    OPJ_UINT32 end,
2560                                    const OPJ_FLOAT32 c)
2561 {
2562     OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2563     OPJ_UINT32 i;
2564     /* To be adapted if NB_ELTS_V8 changes */
2565     for (i = start; i < end; ++i) {
2566         fw[i * 2 * 8    ] = fw[i * 2 * 8    ] * c;
2567         fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
2568         fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
2569         fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
2570         fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
2571         fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
2572         fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
2573         fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
2574     }
2575 }
2576
2577 static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
2578                                    OPJ_UINT32 start,
2579                                    OPJ_UINT32 end,
2580                                    OPJ_UINT32 m,
2581                                    OPJ_FLOAT32 c)
2582 {
2583     OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2584     OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2585     OPJ_UINT32 i;
2586     OPJ_UINT32 imax = opj_uint_min(end, m);
2587     if (start > 0) {
2588         fw += 2 * NB_ELTS_V8 * start;
2589         fl = fw - 2 * NB_ELTS_V8;
2590     }
2591     /* To be adapted if NB_ELTS_V8 changes */
2592     for (i = start; i < imax; ++i) {
2593         fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
2594         fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
2595         fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
2596         fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
2597         fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
2598         fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
2599         fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
2600         fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
2601         fl = fw;
2602         fw += 2 * NB_ELTS_V8;
2603     }
2604     if (m < end) {
2605         assert(m + 1 == end);
2606         c += c;
2607         fw[-8] = fw[-8] + fl[0] * c;
2608         fw[-7] = fw[-7] + fl[1] * c;
2609         fw[-6] = fw[-6] + fl[2] * c;
2610         fw[-5] = fw[-5] + fl[3] * c;
2611         fw[-4] = fw[-4] + fl[4] * c;
2612         fw[-3] = fw[-3] + fl[5] * c;
2613         fw[-2] = fw[-2] + fl[6] * c;
2614         fw[-1] = fw[-1] + fl[7] * c;
2615     }
2616 }
2617
2618 #endif
2619
2620 /* <summary>                             */
2621 /* Inverse 9-7 wavelet transform in 1-D. */
2622 /* </summary>                            */
2623 static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
2624 {
2625     OPJ_INT32 a, b;
2626     /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
2627     /* Historic value for 2 / opj_invK */
2628     /* Normally, we should use invK, but if we do so, we have failures in the */
2629     /* conformance test, due to MSE and peak errors significantly higher than */
2630     /* accepted value */
2631     /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
2632     /* the computation of the stepsize for the non LL subbands */
2633     const float two_invK = 1.625732422f;
2634     if (dwt->cas == 0) {
2635         if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2636             return;
2637         }
2638         a = 0;
2639         b = 1;
2640     } else {
2641         if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2642             return;
2643         }
2644         a = 1;
2645         b = 0;
2646     }
2647 #ifdef __SSE__
2648     opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2649                                _mm_set1_ps(opj_K));
2650     opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2651                                _mm_set1_ps(two_invK));
2652     opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2653                                dwt->win_l_x0, dwt->win_l_x1,
2654                                (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2655                                _mm_set1_ps(-opj_dwt_delta));
2656     opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2657                                dwt->win_h_x0, dwt->win_h_x1,
2658                                (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2659                                _mm_set1_ps(-opj_dwt_gamma));
2660     opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2661                                dwt->win_l_x0, dwt->win_l_x1,
2662                                (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2663                                _mm_set1_ps(-opj_dwt_beta));
2664     opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2665                                dwt->win_h_x0, dwt->win_h_x1,
2666                                (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2667                                _mm_set1_ps(-opj_dwt_alpha));
2668 #else
2669     opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2670                            opj_K);
2671     opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2672                            two_invK);
2673     opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2674                            dwt->win_l_x0, dwt->win_l_x1,
2675                            (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2676                            -opj_dwt_delta);
2677     opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2678                            dwt->win_h_x0, dwt->win_h_x1,
2679                            (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2680                            -opj_dwt_gamma);
2681     opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2682                            dwt->win_l_x0, dwt->win_l_x1,
2683                            (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2684                            -opj_dwt_beta);
2685     opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2686                            dwt->win_h_x0, dwt->win_h_x1,
2687                            (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2688                            -opj_dwt_alpha);
2689 #endif
2690 }
2691
2692 typedef struct {
2693     opj_v8dwt_t h;
2694     OPJ_UINT32 rw;
2695     OPJ_UINT32 w;
2696     OPJ_FLOAT32 * OPJ_RESTRICT aj;
2697     OPJ_UINT32 nb_rows;
2698 } opj_dwt97_decode_h_job_t;
2699
2700 static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
2701 {
2702     OPJ_UINT32 j;
2703     opj_dwt97_decode_h_job_t* job;
2704     OPJ_FLOAT32 * OPJ_RESTRICT aj;
2705     OPJ_UINT32 w;
2706     (void)tls;
2707
2708     job = (opj_dwt97_decode_h_job_t*)user_data;
2709     w = job->w;
2710
2711     assert((job->nb_rows % NB_ELTS_V8) == 0);
2712
2713     aj = job->aj;
2714     for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
2715         OPJ_UINT32 k;
2716         opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
2717         opj_v8dwt_decode(&job->h);
2718
2719         /* To be adapted if NB_ELTS_V8 changes */
2720         for (k = 0; k < job->rw; k++) {
2721             aj[k      ] = job->h.wavelet[k].f[0];
2722             aj[k + (OPJ_SIZE_T)w  ] = job->h.wavelet[k].f[1];
2723             aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
2724             aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
2725         }
2726         for (k = 0; k < job->rw; k++) {
2727             aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
2728             aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
2729             aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
2730             aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
2731         }
2732
2733         aj += w * NB_ELTS_V8;
2734     }
2735
2736     opj_aligned_free(job->h.wavelet);
2737     opj_free(job);
2738 }
2739
2740
2741 typedef struct {
2742     opj_v8dwt_t v;
2743     OPJ_UINT32 rh;
2744     OPJ_UINT32 w;
2745     OPJ_FLOAT32 * OPJ_RESTRICT aj;
2746     OPJ_UINT32 nb_columns;
2747 } opj_dwt97_decode_v_job_t;
2748
2749 static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
2750 {
2751     OPJ_UINT32 j;
2752     opj_dwt97_decode_v_job_t* job;
2753     OPJ_FLOAT32 * OPJ_RESTRICT aj;
2754     (void)tls;
2755
2756     job = (opj_dwt97_decode_v_job_t*)user_data;
2757
2758     assert((job->nb_columns % NB_ELTS_V8) == 0);
2759
2760     aj = job->aj;
2761     for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
2762         OPJ_UINT32 k;
2763
2764         opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
2765         opj_v8dwt_decode(&job->v);
2766
2767         for (k = 0; k < job->rh; ++k) {
2768             memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
2769                    NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2770         }
2771         aj += NB_ELTS_V8;
2772     }
2773
2774     opj_aligned_free(job->v.wavelet);
2775     opj_free(job);
2776 }
2777
2778
2779 /* <summary>                             */
2780 /* Inverse 9-7 wavelet transform in 2-D. */
2781 /* </summary>                            */
2782 static
2783 OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
2784                                 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2785                                 OPJ_UINT32 numres)
2786 {
2787     opj_v8dwt_t h;
2788     opj_v8dwt_t v;
2789
2790     opj_tcd_resolution_t* res = tilec->resolutions;
2791
2792     OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2793                                  res->x0);    /* width of the resolution level computed */
2794     OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2795                                  res->y0);    /* height of the resolution level computed */
2796
2797     OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2798                                                                1].x1 -
2799                                 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2800
2801     OPJ_SIZE_T l_data_size;
2802     const int num_threads = opj_thread_pool_get_thread_count(tp);
2803
2804     if (numres == 1) {
2805         return OPJ_TRUE;
2806     }
2807
2808     l_data_size = opj_dwt_max_resolution(res, numres);
2809     /* overflow check */
2810     if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
2811         /* FIXME event manager error callback */
2812         return OPJ_FALSE;
2813     }
2814     h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2815     if (!h.wavelet) {
2816         /* FIXME event manager error callback */
2817         return OPJ_FALSE;
2818     }
2819     v.wavelet = h.wavelet;
2820
2821     while (--numres) {
2822         OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2823         OPJ_UINT32 j;
2824
2825         h.sn = (OPJ_INT32)rw;
2826         v.sn = (OPJ_INT32)rh;
2827
2828         ++res;
2829
2830         rw = (OPJ_UINT32)(res->x1 -
2831                           res->x0);   /* width of the resolution level computed */
2832         rh = (OPJ_UINT32)(res->y1 -
2833                           res->y0);   /* height of the resolution level computed */
2834
2835         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2836         h.cas = res->x0 % 2;
2837
2838         h.win_l_x0 = 0;
2839         h.win_l_x1 = (OPJ_UINT32)h.sn;
2840         h.win_h_x0 = 0;
2841         h.win_h_x1 = (OPJ_UINT32)h.dn;
2842
2843         if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
2844             for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
2845                 OPJ_UINT32 k;
2846                 opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
2847                 opj_v8dwt_decode(&h);
2848
2849                 /* To be adapted if NB_ELTS_V8 changes */
2850                 for (k = 0; k < rw; k++) {
2851                     aj[k      ] = h.wavelet[k].f[0];
2852                     aj[k + (OPJ_SIZE_T)w  ] = h.wavelet[k].f[1];
2853                     aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
2854                     aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
2855                 }
2856                 for (k = 0; k < rw; k++) {
2857                     aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
2858                     aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
2859                     aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
2860                     aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
2861                 }
2862
2863                 aj += w * NB_ELTS_V8;
2864             }
2865         } else {
2866             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2867             OPJ_UINT32 step_j;
2868
2869             if ((rh / NB_ELTS_V8) < num_jobs) {
2870                 num_jobs = rh / NB_ELTS_V8;
2871             }
2872             step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2873             for (j = 0; j < num_jobs; j++) {
2874                 opj_dwt97_decode_h_job_t* job;
2875
2876                 job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
2877                 if (!job) {
2878                     opj_thread_pool_wait_completion(tp, 0);
2879                     opj_aligned_free(h.wavelet);
2880                     return OPJ_FALSE;
2881                 }
2882                 job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2883                 if (!job->h.wavelet) {
2884                     opj_thread_pool_wait_completion(tp, 0);
2885                     opj_free(job);
2886                     opj_aligned_free(h.wavelet);
2887                     return OPJ_FALSE;
2888                 }
2889                 job->h.dn = h.dn;
2890                 job->h.sn = h.sn;
2891                 job->h.cas = h.cas;
2892                 job->h.win_l_x0 = h.win_l_x0;
2893                 job->h.win_l_x1 = h.win_l_x1;
2894                 job->h.win_h_x0 = h.win_h_x0;
2895                 job->h.win_h_x1 = h.win_h_x1;
2896                 job->rw = rw;
2897                 job->w = w;
2898                 job->aj = aj;
2899                 job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
2900                                                       (NB_ELTS_V8 - 1)) - j * step_j : step_j;
2901                 aj += w * job->nb_rows;
2902                 opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
2903             }
2904             opj_thread_pool_wait_completion(tp, 0);
2905             j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
2906         }
2907
2908         if (j < rh) {
2909             OPJ_UINT32 k;
2910             opj_v8dwt_interleave_h(&h, aj, w, rh - j);
2911             opj_v8dwt_decode(&h);
2912             for (k = 0; k < rw; k++) {
2913                 OPJ_UINT32 l;
2914                 for (l = 0; l < rh - j; l++) {
2915                     aj[k + (OPJ_SIZE_T)w  * l ] = h.wavelet[k].f[l];
2916                 }
2917             }
2918         }
2919
2920         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2921         v.cas = res->y0 % 2;
2922         v.win_l_x0 = 0;
2923         v.win_l_x1 = (OPJ_UINT32)v.sn;
2924         v.win_h_x0 = 0;
2925         v.win_h_x1 = (OPJ_UINT32)v.dn;
2926
2927         aj = (OPJ_FLOAT32*) tilec->data;
2928         if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
2929             for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
2930                 OPJ_UINT32 k;
2931
2932                 opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
2933                 opj_v8dwt_decode(&v);
2934
2935                 for (k = 0; k < rh; ++k) {
2936                     memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2937                 }
2938                 aj += NB_ELTS_V8;
2939             }
2940         } else {
2941             /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
2942                 transfer being the limiting factor. So limit the number of
2943                 threads.
2944              */
2945             OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
2946             OPJ_UINT32 step_j;
2947
2948             if ((rw / NB_ELTS_V8) < num_jobs) {
2949                 num_jobs = rw / NB_ELTS_V8;
2950             }
2951             step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2952             for (j = 0; j < num_jobs; j++) {
2953                 opj_dwt97_decode_v_job_t* job;
2954
2955                 job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
2956                 if (!job) {
2957                     opj_thread_pool_wait_completion(tp, 0);
2958                     opj_aligned_free(h.wavelet);
2959                     return OPJ_FALSE;
2960                 }
2961                 job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2962                 if (!job->v.wavelet) {
2963                     opj_thread_pool_wait_completion(tp, 0);
2964                     opj_free(job);
2965                     opj_aligned_free(h.wavelet);
2966                     return OPJ_FALSE;
2967                 }
2968                 job->v.dn = v.dn;
2969                 job->v.sn = v.sn;
2970                 job->v.cas = v.cas;
2971                 job->v.win_l_x0 = v.win_l_x0;
2972                 job->v.win_l_x1 = v.win_l_x1;
2973                 job->v.win_h_x0 = v.win_h_x0;
2974                 job->v.win_h_x1 = v.win_h_x1;
2975                 job->rh = rh;
2976                 job->w = w;
2977                 job->aj = aj;
2978                 job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
2979                                   (NB_ELTS_V8 - 1)) - j * step_j : step_j;
2980                 aj += job->nb_columns;
2981                 opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
2982             }
2983             opj_thread_pool_wait_completion(tp, 0);
2984         }
2985
2986         if (rw & (NB_ELTS_V8 - 1)) {
2987             OPJ_UINT32 k;
2988
2989             j = rw & (NB_ELTS_V8 - 1);
2990
2991             opj_v8dwt_interleave_v(&v, aj, w, j);
2992             opj_v8dwt_decode(&v);
2993
2994             for (k = 0; k < rh; ++k) {
2995                 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
2996                        (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
2997             }
2998         }
2999     }
3000
3001     opj_aligned_free(h.wavelet);
3002     return OPJ_TRUE;
3003 }
3004
3005 static
3006 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3007                                    OPJ_UINT32 numres)
3008 {
3009     opj_sparse_array_int32_t* sa;
3010     opj_v8dwt_t h;
3011     opj_v8dwt_t v;
3012     OPJ_UINT32 resno;
3013     /* This value matches the maximum left/right extension given in tables */
3014     /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3015     /* we currently use 3. */
3016     const OPJ_UINT32 filter_width = 4U;
3017
3018     opj_tcd_resolution_t* tr = tilec->resolutions;
3019     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3020
3021     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3022                                  tr->x0);    /* width of the resolution level computed */
3023     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3024                                  tr->y0);    /* height of the resolution level computed */
3025
3026     OPJ_SIZE_T l_data_size;
3027
3028     /* Compute the intersection of the area of interest, expressed in tile coordinates */
3029     /* with the tile coordinates */
3030     OPJ_UINT32 win_tcx0 = tilec->win_x0;
3031     OPJ_UINT32 win_tcy0 = tilec->win_y0;
3032     OPJ_UINT32 win_tcx1 = tilec->win_x1;
3033     OPJ_UINT32 win_tcy1 = tilec->win_y1;
3034
3035     if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3036         return OPJ_TRUE;
3037     }
3038
3039     sa = opj_dwt_init_sparse_array(tilec, numres);
3040     if (sa == NULL) {
3041         return OPJ_FALSE;
3042     }
3043
3044     if (numres == 1U) {
3045         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3046                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3047                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3048                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3049                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3050                        tilec->data_win,
3051                        1, tr_max->win_x1 - tr_max->win_x0,
3052                        OPJ_TRUE);
3053         assert(ret);
3054         OPJ_UNUSED(ret);
3055         opj_sparse_array_int32_free(sa);
3056         return OPJ_TRUE;
3057     }
3058
3059     l_data_size = opj_dwt_max_resolution(tr, numres);
3060     /* overflow check */
3061     if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3062         /* FIXME event manager error callback */
3063         opj_sparse_array_int32_free(sa);
3064         return OPJ_FALSE;
3065     }
3066     h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3067     if (!h.wavelet) {
3068         /* FIXME event manager error callback */
3069         opj_sparse_array_int32_free(sa);
3070         return OPJ_FALSE;
3071     }
3072     v.wavelet = h.wavelet;
3073
3074     for (resno = 1; resno < numres; resno ++) {
3075         OPJ_UINT32 j;
3076         /* Window of interest subband-based coordinates */
3077         OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3078         OPJ_UINT32 win_hl_x0, win_hl_x1;
3079         OPJ_UINT32 win_lh_y0, win_lh_y1;
3080         /* Window of interest tile-resolution-based coordinates */
3081         OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3082         /* Tile-resolution subband-based coordinates */
3083         OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3084
3085         ++tr;
3086
3087         h.sn = (OPJ_INT32)rw;
3088         v.sn = (OPJ_INT32)rh;
3089
3090         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3091         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3092
3093         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3094         h.cas = tr->x0 % 2;
3095
3096         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3097         v.cas = tr->y0 % 2;
3098
3099         /* Get the subband coordinates for the window of interest */
3100         /* LL band */
3101         opj_dwt_get_band_coordinates(tilec, resno, 0,
3102                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3103                                      &win_ll_x0, &win_ll_y0,
3104                                      &win_ll_x1, &win_ll_y1);
3105
3106         /* HL band */
3107         opj_dwt_get_band_coordinates(tilec, resno, 1,
3108                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3109                                      &win_hl_x0, NULL, &win_hl_x1, NULL);
3110
3111         /* LH band */
3112         opj_dwt_get_band_coordinates(tilec, resno, 2,
3113                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3114                                      NULL, &win_lh_y0, NULL, &win_lh_y1);
3115
3116         /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3117         tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3118         tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3119         tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3120         tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3121
3122         /* Subtract the origin of the bands for this tile, to the subwindow */
3123         /* of interest band coordinates, so as to get them relative to the */
3124         /* tile */
3125         win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3126         win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3127         win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3128         win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3129         win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3130         win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3131         win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3132         win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3133
3134         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3135         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3136
3137         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3138         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3139
3140         /* Compute the tile-resolution-based coordinates for the window of interest */
3141         if (h.cas == 0) {
3142             win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3143             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3144         } else {
3145             win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3146             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3147         }
3148
3149         if (v.cas == 0) {
3150             win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3151             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3152         } else {
3153             win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3154             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3155         }
3156
3157         h.win_l_x0 = win_ll_x0;
3158         h.win_l_x1 = win_ll_x1;
3159         h.win_h_x0 = win_hl_x0;
3160         h.win_h_x1 = win_hl_x1;
3161         for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3162             if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3163                     (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3164                      j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3165                 opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3166                 opj_v8dwt_decode(&h);
3167                 if (!opj_sparse_array_int32_write(sa,
3168                                                   win_tr_x0, j,
3169                                                   win_tr_x1, j + NB_ELTS_V8,
3170                                                   (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3171                                                   NB_ELTS_V8, 1, OPJ_TRUE)) {
3172                     /* FIXME event manager error callback */
3173                     opj_sparse_array_int32_free(sa);
3174                     opj_aligned_free(h.wavelet);
3175                     return OPJ_FALSE;
3176                 }
3177             }
3178         }
3179
3180         if (j < rh &&
3181                 ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3182                  (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3183                   j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3184             opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3185             opj_v8dwt_decode(&h);
3186             if (!opj_sparse_array_int32_write(sa,
3187                                               win_tr_x0, j,
3188                                               win_tr_x1, rh,
3189                                               (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3190                                               NB_ELTS_V8, 1, OPJ_TRUE)) {
3191                 /* FIXME event manager error callback */
3192                 opj_sparse_array_int32_free(sa);
3193                 opj_aligned_free(h.wavelet);
3194                 return OPJ_FALSE;
3195             }
3196         }
3197
3198         v.win_l_x0 = win_ll_y0;
3199         v.win_l_x1 = win_ll_y1;
3200         v.win_h_x0 = win_lh_y0;
3201         v.win_h_x1 = win_lh_y1;
3202         for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3203             OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3204
3205             opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3206             opj_v8dwt_decode(&v);
3207
3208             if (!opj_sparse_array_int32_write(sa,
3209                                               j, win_tr_y0,
3210                                               j + nb_elts, win_tr_y1,
3211                                               (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3212                                               1, NB_ELTS_V8, OPJ_TRUE)) {
3213                 /* FIXME event manager error callback */
3214                 opj_sparse_array_int32_free(sa);
3215                 opj_aligned_free(h.wavelet);
3216                 return OPJ_FALSE;
3217             }
3218         }
3219     }
3220
3221     {
3222         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3223                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3224                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3225                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3226                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3227                        tilec->data_win,
3228                        1, tr_max->win_x1 - tr_max->win_x0,
3229                        OPJ_TRUE);
3230         assert(ret);
3231         OPJ_UNUSED(ret);
3232     }
3233     opj_sparse_array_int32_free(sa);
3234
3235     opj_aligned_free(h.wavelet);
3236     return OPJ_TRUE;
3237 }
3238
3239
3240 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3241                              opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3242                              OPJ_UINT32 numres)
3243 {
3244     if (p_tcd->whole_tile_decoding) {
3245         return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3246     } else {
3247         return opj_dwt_decode_partial_97(tilec, numres);
3248     }
3249 }