opj_dwt_decode_partial_tile(): avoid undefined behaviour in lifting operation by...
[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 typedef union {
91     OPJ_FLOAT32 f[4];
92 } opj_v4_t;
93
94 typedef struct v4dwt_local {
95     opj_v4_t*   wavelet ;
96     OPJ_INT32       dn ;  /* number of elements in high pass band */
97     OPJ_INT32       sn ;  /* number of elements in low pass band */
98     OPJ_INT32       cas ; /* 0 = start on even coord, 1 = start on odd coord */
99     OPJ_UINT32      win_l_x0; /* start coord in low pass band */
100     OPJ_UINT32      win_l_x1; /* end coord in low pass band */
101     OPJ_UINT32      win_h_x0; /* start coord in high pass band */
102     OPJ_UINT32      win_h_x1; /* end coord in high pass band */
103 } opj_v4dwt_t ;
104
105 static const OPJ_FLOAT32 opj_dwt_alpha =  1.586134342f; /*  12994 */
106 static const OPJ_FLOAT32 opj_dwt_beta  =  0.052980118f; /*    434 */
107 static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /*  -7233 */
108 static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /*  -3633 */
109
110 static const OPJ_FLOAT32 opj_K      = 1.230174105f; /*  10078 */
111 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
112
113 /*@}*/
114
115 /**
116 Virtual function type for wavelet transform in 1-D
117 */
118 typedef void (*DWT1DFN)(const opj_dwt_t* v);
119
120 /** @name Local static functions */
121 /*@{*/
122
123 /**
124 Forward lazy transform (horizontal)
125 */
126 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
127                                    OPJ_INT32 sn, OPJ_INT32 cas);
128 /**
129 Forward lazy transform (vertical)
130 */
131 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
132                                    OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
133 /**
134 Forward 5-3 wavelet transform in 1-D
135 */
136 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
137                              OPJ_INT32 cas);
138 /**
139 Forward 9-7 wavelet transform in 1-D
140 */
141 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
142                                   OPJ_INT32 cas);
143 /**
144 Explicit calculation of the Quantization Stepsizes
145 */
146 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
147                                     opj_stepsize_t *bandno_stepsize);
148 /**
149 Inverse wavelet transform in 2-D.
150 */
151 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
152                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
153
154 static OPJ_BOOL opj_dwt_decode_partial_tile(
155     opj_tcd_tilecomp_t* tilec,
156     OPJ_UINT32 numres);
157
158 static OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
159         void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32));
160
161 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
162         OPJ_UINT32 i);
163
164 /* <summary>                             */
165 /* Inverse 9-7 wavelet transform in 1-D. */
166 /* </summary>                            */
167 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt);
168
169 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT dwt,
170                                    OPJ_FLOAT32* OPJ_RESTRICT a,
171                                    OPJ_UINT32 width,
172                                    OPJ_UINT32 remaining_height);
173
174 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
175                                    OPJ_FLOAT32* OPJ_RESTRICT a,
176                                    OPJ_UINT32 width,
177                                    OPJ_UINT32 nb_elts_read);
178
179 #ifdef __SSE__
180 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w,
181                                        OPJ_UINT32 start,
182                                        OPJ_UINT32 end,
183                                        const __m128 c);
184
185 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w,
186                                        OPJ_UINT32 start,
187                                        OPJ_UINT32 end,
188                                        OPJ_UINT32 m, __m128 c);
189
190 #else
191 static void opj_v4dwt_decode_step1(opj_v4_t* w,
192                                    OPJ_UINT32 start,
193                                    OPJ_UINT32 end,
194                                    const OPJ_FLOAT32 c);
195
196 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
197                                    OPJ_UINT32 start,
198                                    OPJ_UINT32 end,
199                                    OPJ_UINT32 m,
200                                    OPJ_FLOAT32 c);
201
202 #endif
203
204 /*@}*/
205
206 /*@}*/
207
208 #define OPJ_S(i) a[(i)*2]
209 #define OPJ_D(i) a[(1+(i)*2)]
210 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
211 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
212 /* new */
213 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
214 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
215
216 /* <summary>                                                              */
217 /* This table contains the norms of the 5-3 wavelets for different bands. */
218 /* </summary>                                                             */
219 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
220     {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
221     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
222     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
223     {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
224 };
225
226 /* <summary>                                                              */
227 /* This table contains the norms of the 9-7 wavelets for different bands. */
228 /* </summary>                                                             */
229 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
230     {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
231     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
232     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
233     {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
234 };
235
236 /*
237 ==========================================================
238    local functions
239 ==========================================================
240 */
241
242 /* <summary>                             */
243 /* Forward lazy transform (horizontal).  */
244 /* </summary>                            */
245 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
246                                    OPJ_INT32 sn, OPJ_INT32 cas)
247 {
248     OPJ_INT32 i;
249     OPJ_INT32 * l_dest = b;
250     OPJ_INT32 * l_src = a + cas;
251
252     for (i = 0; i < sn; ++i) {
253         *l_dest++ = *l_src;
254         l_src += 2;
255     }
256
257     l_dest = b + sn;
258     l_src = a + 1 - cas;
259
260     for (i = 0; i < dn; ++i)  {
261         *l_dest++ = *l_src;
262         l_src += 2;
263     }
264 }
265
266 /* <summary>                             */
267 /* Forward lazy transform (vertical).    */
268 /* </summary>                            */
269 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
270                                    OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas)
271 {
272     OPJ_INT32 i = sn;
273     OPJ_INT32 * l_dest = b;
274     OPJ_INT32 * l_src = a + cas;
275
276     while (i--) {
277         *l_dest = *l_src;
278         l_dest += x;
279         l_src += 2;
280     } /* b[i*x]=a[2*i+cas]; */
281
282     l_dest = b + sn * x;
283     l_src = a + 1 - cas;
284
285     i = dn;
286     while (i--) {
287         *l_dest = *l_src;
288         l_dest += x;
289         l_src += 2;
290     } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
291 }
292
293 #ifdef STANDARD_SLOW_VERSION
294 /* <summary>                             */
295 /* Inverse lazy transform (horizontal).  */
296 /* </summary>                            */
297 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
298 {
299     OPJ_INT32 *ai = a;
300     OPJ_INT32 *bi = h->mem + h->cas;
301     OPJ_INT32  i    = h->sn;
302     while (i--) {
303         *bi = *(ai++);
304         bi += 2;
305     }
306     ai  = a + h->sn;
307     bi  = h->mem + 1 - h->cas;
308     i   = h->dn ;
309     while (i--) {
310         *bi = *(ai++);
311         bi += 2;
312     }
313 }
314
315 /* <summary>                             */
316 /* Inverse lazy transform (vertical).    */
317 /* </summary>                            */
318 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
319 {
320     OPJ_INT32 *ai = a;
321     OPJ_INT32 *bi = v->mem + v->cas;
322     OPJ_INT32  i = v->sn;
323     while (i--) {
324         *bi = *ai;
325         bi += 2;
326         ai += x;
327     }
328     ai = a + (v->sn * x);
329     bi = v->mem + 1 - v->cas;
330     i = v->dn ;
331     while (i--) {
332         *bi = *ai;
333         bi += 2;
334         ai += x;
335     }
336 }
337
338 #endif /* STANDARD_SLOW_VERSION */
339
340 /* <summary>                            */
341 /* Forward 5-3 wavelet transform in 1-D. */
342 /* </summary>                           */
343 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
344                              OPJ_INT32 cas)
345 {
346     OPJ_INT32 i;
347
348     if (!cas) {
349         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
350             for (i = 0; i < dn; i++) {
351                 OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
352             }
353             for (i = 0; i < sn; i++) {
354                 OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
355             }
356         }
357     } else {
358         if (!sn && dn == 1) {       /* NEW :  CASE ONE ELEMENT */
359             OPJ_S(0) *= 2;
360         } else {
361             for (i = 0; i < dn; i++) {
362                 OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
363             }
364             for (i = 0; i < sn; i++) {
365                 OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
366             }
367         }
368     }
369 }
370
371 #ifdef STANDARD_SLOW_VERSION
372 /* <summary>                            */
373 /* Inverse 5-3 wavelet transform in 1-D. */
374 /* </summary>                           */
375 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
376                               OPJ_INT32 cas)
377 {
378     OPJ_INT32 i;
379
380     if (!cas) {
381         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
382             for (i = 0; i < sn; i++) {
383                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
384             }
385             for (i = 0; i < dn; i++) {
386                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
387             }
388         }
389     } else {
390         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
391             OPJ_S(0) /= 2;
392         } else {
393             for (i = 0; i < sn; i++) {
394                 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
395             }
396             for (i = 0; i < dn; i++) {
397                 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
398             }
399         }
400     }
401 }
402
403 static void opj_dwt_decode_1(const opj_dwt_t *v)
404 {
405     opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
406 }
407
408 #endif /* STANDARD_SLOW_VERSION */
409
410 #if !defined(STANDARD_SLOW_VERSION)
411 static void  opj_idwt53_h_cas0(OPJ_INT32* tmp,
412                                const OPJ_INT32 sn,
413                                const OPJ_INT32 len,
414                                OPJ_INT32* tiledp)
415 {
416     OPJ_INT32 i, j;
417     const OPJ_INT32* in_even = &tiledp[0];
418     const OPJ_INT32* in_odd = &tiledp[sn];
419
420 #ifdef TWO_PASS_VERSION
421     /* For documentation purpose: performs lifting in two iterations, */
422     /* but without explicit interleaving */
423
424     assert(len > 1);
425
426     /* Even */
427     tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
428     for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
429         tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
430     }
431     if (len & 1) { /* if len is odd */
432         tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
433     }
434
435     /* Odd */
436     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
437         tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
438     }
439     if (!(len & 1)) { /* if len is even */
440         tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
441     }
442 #else
443     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
444
445     assert(len > 1);
446
447     /* Improved version of the TWO_PASS_VERSION: */
448     /* Performs lifting in one single iteration. Saves memory */
449     /* accesses and explicit interleaving. */
450     s1n = in_even[0];
451     d1n = in_odd[0];
452     s0n = s1n - ((d1n + 1) >> 1);
453
454     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
455         d1c = d1n;
456         s0c = s0n;
457
458         s1n = in_even[j];
459         d1n = in_odd[j];
460
461         s0n = s1n - ((d1c + d1n + 2) >> 2);
462
463         tmp[i  ] = s0c;
464         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
465     }
466
467     tmp[i] = s0n;
468
469     if (len & 1) {
470         tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
471         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
472     } else {
473         tmp[len - 1] = d1n + s0n;
474     }
475 #endif
476     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
477 }
478
479 static void  opj_idwt53_h_cas1(OPJ_INT32* tmp,
480                                const OPJ_INT32 sn,
481                                const OPJ_INT32 len,
482                                OPJ_INT32* tiledp)
483 {
484     OPJ_INT32 i, j;
485     const OPJ_INT32* in_even = &tiledp[sn];
486     const OPJ_INT32* in_odd = &tiledp[0];
487
488 #ifdef TWO_PASS_VERSION
489     /* For documentation purpose: performs lifting in two iterations, */
490     /* but without explicit interleaving */
491
492     assert(len > 2);
493
494     /* Odd */
495     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
496         tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
497     }
498     if (!(len & 1)) {
499         tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
500     }
501
502     /* Even */
503     tmp[0] = in_even[0] + tmp[1];
504     for (i = 2, j = 1; i < len - 1; i += 2, j++) {
505         tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
506     }
507     if (len & 1) {
508         tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
509     }
510 #else
511     OPJ_INT32 s1, s2, dc, dn;
512
513     assert(len > 2);
514
515     /* Improved version of the TWO_PASS_VERSION: */
516     /* Performs lifting in one single iteration. Saves memory */
517     /* accesses and explicit interleaving. */
518
519     s1 = in_even[1];
520     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
521     tmp[0] = in_even[0] + dc;
522
523     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
524
525         s2 = in_even[j + 1];
526
527         dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
528         tmp[i  ] = dc;
529         tmp[i + 1] = s1 + ((dn + dc) >> 1);
530
531         dc = dn;
532         s1 = s2;
533     }
534
535     tmp[i] = dc;
536
537     if (!(len & 1)) {
538         dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
539         tmp[len - 2] = s1 + ((dn + dc) >> 1);
540         tmp[len - 1] = dn;
541     } else {
542         tmp[len - 1] = s1 + dc;
543     }
544 #endif
545     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
546 }
547
548
549 #endif /* !defined(STANDARD_SLOW_VERSION) */
550
551 /* <summary>                            */
552 /* Inverse 5-3 wavelet transform in 1-D for one row. */
553 /* </summary>                           */
554 /* Performs interleave, inverse wavelet transform and copy back to buffer */
555 static void opj_idwt53_h(const opj_dwt_t *dwt,
556                          OPJ_INT32* tiledp)
557 {
558 #ifdef STANDARD_SLOW_VERSION
559     /* For documentation purpose */
560     opj_dwt_interleave_h(dwt, tiledp);
561     opj_dwt_decode_1(dwt);
562     memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
563 #else
564     const OPJ_INT32 sn = dwt->sn;
565     const OPJ_INT32 len = sn + dwt->dn;
566     if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
567         if (len > 1) {
568             opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
569         } else {
570             /* Unmodified value */
571         }
572     } else { /* Left-most sample is on odd coordinate */
573         if (len == 1) {
574             tiledp[0] /= 2;
575         } else if (len == 2) {
576             OPJ_INT32* out = dwt->mem;
577             const OPJ_INT32* in_even = &tiledp[sn];
578             const OPJ_INT32* in_odd = &tiledp[0];
579             out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
580             out[0] = in_even[0] + out[1];
581             memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
582         } else if (len > 2) {
583             opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
584         }
585     }
586 #endif
587 }
588
589 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
590
591 /* Conveniency macros to improve the readabilty of the formulas */
592 #if __AVX2__
593 #define VREG        __m256i
594 #define LOAD_CST(x) _mm256_set1_epi32(x)
595 #define LOAD(x)     _mm256_load_si256((const VREG*)(x))
596 #define LOADU(x)    _mm256_loadu_si256((const VREG*)(x))
597 #define STORE(x,y)  _mm256_store_si256((VREG*)(x),(y))
598 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
599 #define ADD(x,y)    _mm256_add_epi32((x),(y))
600 #define SUB(x,y)    _mm256_sub_epi32((x),(y))
601 #define SAR(x,y)    _mm256_srai_epi32((x),(y))
602 #else
603 #define VREG        __m128i
604 #define LOAD_CST(x) _mm_set1_epi32(x)
605 #define LOAD(x)     _mm_load_si128((const VREG*)(x))
606 #define LOADU(x)    _mm_loadu_si128((const VREG*)(x))
607 #define STORE(x,y)  _mm_store_si128((VREG*)(x),(y))
608 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
609 #define ADD(x,y)    _mm_add_epi32((x),(y))
610 #define SUB(x,y)    _mm_sub_epi32((x),(y))
611 #define SAR(x,y)    _mm_srai_epi32((x),(y))
612 #endif
613 #define ADD3(x,y,z) ADD(ADD(x,y),z)
614
615 static
616 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
617                                const OPJ_INT32* tmp,
618                                OPJ_INT32 len,
619                                OPJ_INT32 stride)
620 {
621     OPJ_INT32 i;
622     for (i = 0; i < len; ++i) {
623         /* A memcpy(&tiledp_col[i * stride + 0],
624                     &tmp[PARALLEL_COLS_53 * i + 0],
625                     PARALLEL_COLS_53 * sizeof(OPJ_INT32))
626            would do but would be a tiny bit slower.
627            We can take here advantage of our knowledge of alignment */
628         STOREU(&tiledp_col[i * stride + 0],
629                LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
630         STOREU(&tiledp_col[i * stride + VREG_INT_COUNT],
631                LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
632     }
633 }
634
635 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
636  * 16 in AVX2, when top-most pixel is on even coordinate */
637 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
638     OPJ_INT32* tmp,
639     const OPJ_INT32 sn,
640     const OPJ_INT32 len,
641     OPJ_INT32* tiledp_col,
642     const OPJ_INT32 stride)
643 {
644     const OPJ_INT32* in_even = &tiledp_col[0];
645     const OPJ_INT32* in_odd = &tiledp_col[sn * stride];
646
647     OPJ_INT32 i, j;
648     VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
649     VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
650     const VREG two = LOAD_CST(2);
651
652     assert(len > 1);
653 #if __AVX2__
654     assert(PARALLEL_COLS_53 == 16);
655     assert(VREG_INT_COUNT == 8);
656 #else
657     assert(PARALLEL_COLS_53 == 8);
658     assert(VREG_INT_COUNT == 4);
659 #endif
660
661     /* Note: loads of input even/odd values must be done in a unaligned */
662     /* fashion. But stores in tmp can be done with aligned store, since */
663     /* the temporary buffer is properly aligned */
664     assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
665
666     s1n_0 = LOADU(in_even + 0);
667     s1n_1 = LOADU(in_even + VREG_INT_COUNT);
668     d1n_0 = LOADU(in_odd);
669     d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
670
671     /* s0n = s1n - ((d1n + 1) >> 1); <==> */
672     /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
673     s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
674     s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
675
676     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
677         d1c_0 = d1n_0;
678         s0c_0 = s0n_0;
679         d1c_1 = d1n_1;
680         s0c_1 = s0n_1;
681
682         s1n_0 = LOADU(in_even + j * stride);
683         s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
684         d1n_0 = LOADU(in_odd + j * stride);
685         d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
686
687         /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
688         s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
689         s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
690
691         STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
692         STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
693
694         /* d1c + ((s0c + s0n) >> 1) */
695         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
696               ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
697         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
698               ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
699     }
700
701     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
702     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
703
704     if (len & 1) {
705         VREG tmp_len_minus_1;
706         s1n_0 = LOADU(in_even + ((len - 1) / 2) * stride);
707         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
708         tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
709         STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
710         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
711         STORE(tmp + PARALLEL_COLS_53 * (len - 2),
712               ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
713
714         s1n_1 = LOADU(in_even + ((len - 1) / 2) * stride + VREG_INT_COUNT);
715         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
716         tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
717         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
718               tmp_len_minus_1);
719         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
720         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
721               ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
722
723
724     } else {
725         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
726               ADD(d1n_0, s0n_0));
727         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
728               ADD(d1n_1, s0n_1));
729     }
730
731     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
732 }
733
734
735 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
736  * 16 in AVX2, when top-most pixel is on odd coordinate */
737 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
738     OPJ_INT32* tmp,
739     const OPJ_INT32 sn,
740     const OPJ_INT32 len,
741     OPJ_INT32* tiledp_col,
742     const OPJ_INT32 stride)
743 {
744     OPJ_INT32 i, j;
745
746     VREG s1_0, s2_0, dc_0, dn_0;
747     VREG s1_1, s2_1, dc_1, dn_1;
748     const VREG two = LOAD_CST(2);
749
750     const OPJ_INT32* in_even = &tiledp_col[sn * stride];
751     const OPJ_INT32* in_odd = &tiledp_col[0];
752
753     assert(len > 2);
754 #if __AVX2__
755     assert(PARALLEL_COLS_53 == 16);
756     assert(VREG_INT_COUNT == 8);
757 #else
758     assert(PARALLEL_COLS_53 == 8);
759     assert(VREG_INT_COUNT == 4);
760 #endif
761
762     /* Note: loads of input even/odd values must be done in a unaligned */
763     /* fashion. But stores in tmp can be done with aligned store, since */
764     /* the temporary buffer is properly aligned */
765     assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
766
767     s1_0 = LOADU(in_even + stride);
768     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
769     dc_0 = SUB(LOADU(in_odd + 0),
770                SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
771     STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
772
773     s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
774     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
775     dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
776                SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
777     STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
778           ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
779
780     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
781
782         s2_0 = LOADU(in_even + (j + 1) * stride);
783         s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
784
785         /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
786         dn_0 = SUB(LOADU(in_odd + j * stride),
787                    SAR(ADD3(s1_0, s2_0, two), 2));
788         dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
789                    SAR(ADD3(s1_1, s2_1, two), 2));
790
791         STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
792         STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
793
794         /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
795         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
796               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
797         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
798               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
799
800         dc_0 = dn_0;
801         s1_0 = s2_0;
802         dc_1 = dn_1;
803         s1_1 = s2_1;
804     }
805     STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
806     STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
807
808     if (!(len & 1)) {
809         /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
810         dn_0 = SUB(LOADU(in_odd + (len / 2 - 1) * stride),
811                    SAR(ADD3(s1_0, s1_0, two), 2));
812         dn_1 = SUB(LOADU(in_odd + (len / 2 - 1) * stride + VREG_INT_COUNT),
813                    SAR(ADD3(s1_1, s1_1, two), 2));
814
815         /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
816         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
817               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
818         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
819               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
820
821         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
822         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
823     } else {
824         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
825         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
826               ADD(s1_1, dc_1));
827     }
828
829     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
830 }
831
832 #undef VREG
833 #undef LOAD_CST
834 #undef LOADU
835 #undef LOAD
836 #undef STORE
837 #undef STOREU
838 #undef ADD
839 #undef ADD3
840 #undef SUB
841 #undef SAR
842
843 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
844
845 #if !defined(STANDARD_SLOW_VERSION)
846 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
847  * pixel is on even coordinate */
848 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
849                              const OPJ_INT32 sn,
850                              const OPJ_INT32 len,
851                              OPJ_INT32* tiledp_col,
852                              const OPJ_INT32 stride)
853 {
854     OPJ_INT32 i, j;
855     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
856
857     assert(len > 1);
858
859     /* Performs lifting in one single iteration. Saves memory */
860     /* accesses and explicit interleaving. */
861
862     s1n = tiledp_col[0];
863     d1n = tiledp_col[sn * stride];
864     s0n = s1n - ((d1n + 1) >> 1);
865
866     for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
867         d1c = d1n;
868         s0c = s0n;
869
870         s1n = tiledp_col[(j + 1) * stride];
871         d1n = tiledp_col[(sn + j + 1) * stride];
872
873         s0n = s1n - ((d1c + d1n + 2) >> 2);
874
875         tmp[i  ] = s0c;
876         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
877     }
878
879     tmp[i] = s0n;
880
881     if (len & 1) {
882         tmp[len - 1] =
883             tiledp_col[((len - 1) / 2) * stride] -
884             ((d1n + 1) >> 1);
885         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
886     } else {
887         tmp[len - 1] = d1n + s0n;
888     }
889
890     for (i = 0; i < len; ++i) {
891         tiledp_col[i * stride] = tmp[i];
892     }
893 }
894
895
896 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
897  * pixel is on odd coordinate */
898 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
899                              const OPJ_INT32 sn,
900                              const OPJ_INT32 len,
901                              OPJ_INT32* tiledp_col,
902                              const OPJ_INT32 stride)
903 {
904     OPJ_INT32 i, j;
905     OPJ_INT32 s1, s2, dc, dn;
906     const OPJ_INT32* in_even = &tiledp_col[sn * stride];
907     const OPJ_INT32* in_odd = &tiledp_col[0];
908
909     assert(len > 2);
910
911     /* Performs lifting in one single iteration. Saves memory */
912     /* accesses and explicit interleaving. */
913
914     s1 = in_even[stride];
915     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
916     tmp[0] = in_even[0] + dc;
917     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
918
919         s2 = in_even[(j + 1) * stride];
920
921         dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2);
922         tmp[i  ] = dc;
923         tmp[i + 1] = s1 + ((dn + dc) >> 1);
924
925         dc = dn;
926         s1 = s2;
927     }
928     tmp[i] = dc;
929     if (!(len & 1)) {
930         dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
931         tmp[len - 2] = s1 + ((dn + dc) >> 1);
932         tmp[len - 1] = dn;
933     } else {
934         tmp[len - 1] = s1 + dc;
935     }
936
937     for (i = 0; i < len; ++i) {
938         tiledp_col[i * stride] = tmp[i];
939     }
940 }
941 #endif /* !defined(STANDARD_SLOW_VERSION) */
942
943 /* <summary>                            */
944 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
945 /* </summary>                           */
946 /* Performs interleave, inverse wavelet transform and copy back to buffer */
947 static void opj_idwt53_v(const opj_dwt_t *dwt,
948                          OPJ_INT32* tiledp_col,
949                          OPJ_INT32 stride,
950                          OPJ_INT32 nb_cols)
951 {
952 #ifdef STANDARD_SLOW_VERSION
953     /* For documentation purpose */
954     OPJ_INT32 k, c;
955     for (c = 0; c < nb_cols; c ++) {
956         opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
957         opj_dwt_decode_1(dwt);
958         for (k = 0; k < dwt->sn + dwt->dn; ++k) {
959             tiledp_col[c + k * stride] = dwt->mem[k];
960         }
961     }
962 #else
963     const OPJ_INT32 sn = dwt->sn;
964     const OPJ_INT32 len = sn + dwt->dn;
965     if (dwt->cas == 0) {
966         /* If len == 1, unmodified value */
967
968 #if (defined(__SSE2__) || defined(__AVX2__))
969         if (len > 1 && nb_cols == PARALLEL_COLS_53) {
970             /* Same as below general case, except that thanks to SSE2/AVX2 */
971             /* we can efficently process 8/16 columns in parallel */
972             opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
973             return;
974         }
975 #endif
976         if (len > 1) {
977             OPJ_INT32 c;
978             for (c = 0; c < nb_cols; c++, tiledp_col++) {
979                 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
980             }
981             return;
982         }
983     } else {
984         if (len == 1) {
985             OPJ_INT32 c;
986             for (c = 0; c < nb_cols; c++, tiledp_col++) {
987                 tiledp_col[0] /= 2;
988             }
989             return;
990         }
991
992         if (len == 2) {
993             OPJ_INT32 c;
994             OPJ_INT32* out = dwt->mem;
995             for (c = 0; c < nb_cols; c++, tiledp_col++) {
996                 OPJ_INT32 i;
997                 const OPJ_INT32* in_even = &tiledp_col[sn * stride];
998                 const OPJ_INT32* in_odd = &tiledp_col[0];
999
1000                 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
1001                 out[0] = in_even[0] + out[1];
1002
1003                 for (i = 0; i < len; ++i) {
1004                     tiledp_col[i * stride] = out[i];
1005                 }
1006             }
1007
1008             return;
1009         }
1010
1011 #if (defined(__SSE2__) || defined(__AVX2__))
1012         if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1013             /* Same as below general case, except that thanks to SSE2/AVX2 */
1014             /* we can efficently process 8/16 columns in parallel */
1015             opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1016             return;
1017         }
1018 #endif
1019         if (len > 2) {
1020             OPJ_INT32 c;
1021             for (c = 0; c < nb_cols; c++, tiledp_col++) {
1022                 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1023             }
1024             return;
1025         }
1026     }
1027 #endif
1028 }
1029
1030
1031 /* <summary>                             */
1032 /* Forward 9-7 wavelet transform in 1-D. */
1033 /* </summary>                            */
1034 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1035                                   OPJ_INT32 cas)
1036 {
1037     OPJ_INT32 i;
1038     if (!cas) {
1039         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
1040             for (i = 0; i < dn; i++) {
1041                 OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
1042             }
1043             for (i = 0; i < sn; i++) {
1044                 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
1045             }
1046             for (i = 0; i < dn; i++) {
1047                 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
1048             }
1049             for (i = 0; i < sn; i++) {
1050                 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
1051             }
1052             for (i = 0; i < dn; i++) {
1053                 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038);    /*5038 */
1054             }
1055             for (i = 0; i < sn; i++) {
1056                 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659);    /*6660 */
1057             }
1058         }
1059     } else {
1060         if ((sn > 0) || (dn > 1)) { /* NEW :  CASE ONE ELEMENT */
1061             for (i = 0; i < dn; i++) {
1062                 OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
1063             }
1064             for (i = 0; i < sn; i++) {
1065                 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
1066             }
1067             for (i = 0; i < dn; i++) {
1068                 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
1069             }
1070             for (i = 0; i < sn; i++) {
1071                 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
1072             }
1073             for (i = 0; i < dn; i++) {
1074                 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038);    /*5038 */
1075             }
1076             for (i = 0; i < sn; i++) {
1077                 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659);    /*6660 */
1078             }
1079         }
1080     }
1081 }
1082
1083 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1084                                     opj_stepsize_t *bandno_stepsize)
1085 {
1086     OPJ_INT32 p, n;
1087     p = opj_int_floorlog2(stepsize) - 13;
1088     n = 11 - opj_int_floorlog2(stepsize);
1089     bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1090     bandno_stepsize->expn = numbps - p;
1091 }
1092
1093 /*
1094 ==========================================================
1095    DWT interface
1096 ==========================================================
1097 */
1098
1099
1100 /* <summary>                            */
1101 /* Forward 5-3 wavelet transform in 2-D. */
1102 /* </summary>                           */
1103 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
1104         void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32))
1105 {
1106     OPJ_INT32 i, j, k;
1107     OPJ_INT32 *a = 00;
1108     OPJ_INT32 *aj = 00;
1109     OPJ_INT32 *bj = 00;
1110     OPJ_INT32 w, l;
1111
1112     OPJ_INT32 rw;           /* width of the resolution level computed   */
1113     OPJ_INT32 rh;           /* height of the resolution level computed  */
1114     size_t l_data_size;
1115
1116     opj_tcd_resolution_t * l_cur_res = 0;
1117     opj_tcd_resolution_t * l_last_res = 0;
1118
1119     w = tilec->x1 - tilec->x0;
1120     l = (OPJ_INT32)tilec->numresolutions - 1;
1121     a = tilec->data;
1122
1123     l_cur_res = tilec->resolutions + l;
1124     l_last_res = l_cur_res - 1;
1125
1126     l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1127     /* overflow check */
1128     if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1129         /* FIXME event manager error callback */
1130         return OPJ_FALSE;
1131     }
1132     l_data_size *= sizeof(OPJ_INT32);
1133     bj = (OPJ_INT32*)opj_malloc(l_data_size);
1134     /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1135     /* in that case, so do not error out */
1136     if (l_data_size != 0 && ! bj) {
1137         return OPJ_FALSE;
1138     }
1139     i = l;
1140
1141     while (i--) {
1142         OPJ_INT32 rw1;      /* width of the resolution level once lower than computed one                                       */
1143         OPJ_INT32 rh1;      /* height of the resolution level once lower than computed one                                      */
1144         OPJ_INT32 cas_col;  /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1145         OPJ_INT32 cas_row;  /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
1146         OPJ_INT32 dn, sn;
1147
1148         rw  = l_cur_res->x1 - l_cur_res->x0;
1149         rh  = l_cur_res->y1 - l_cur_res->y0;
1150         rw1 = l_last_res->x1 - l_last_res->x0;
1151         rh1 = l_last_res->y1 - l_last_res->y0;
1152
1153         cas_row = l_cur_res->x0 & 1;
1154         cas_col = l_cur_res->y0 & 1;
1155
1156         sn = rh1;
1157         dn = rh - rh1;
1158         for (j = 0; j < rw; ++j) {
1159             aj = a + j;
1160             for (k = 0; k < rh; ++k) {
1161                 bj[k] = aj[k * w];
1162             }
1163
1164             (*p_function)(bj, dn, sn, cas_col);
1165
1166             opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1167         }
1168
1169         sn = rw1;
1170         dn = rw - rw1;
1171
1172         for (j = 0; j < rh; j++) {
1173             aj = a + j * w;
1174             for (k = 0; k < rw; k++) {
1175                 bj[k] = aj[k];
1176             }
1177             (*p_function)(bj, dn, sn, cas_row);
1178             opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
1179         }
1180
1181         l_cur_res = l_last_res;
1182
1183         --l_last_res;
1184     }
1185
1186     opj_free(bj);
1187     return OPJ_TRUE;
1188 }
1189
1190 /* Forward 5-3 wavelet transform in 2-D. */
1191 /* </summary>                           */
1192 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
1193 {
1194     return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1);
1195 }
1196
1197 /* <summary>                            */
1198 /* Inverse 5-3 wavelet transform in 2-D. */
1199 /* </summary>                           */
1200 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1201                         OPJ_UINT32 numres)
1202 {
1203     if (p_tcd->whole_tile_decoding) {
1204         return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1205     } else {
1206         return opj_dwt_decode_partial_tile(tilec, numres);
1207     }
1208 }
1209
1210
1211 /* <summary>                          */
1212 /* Get gain of 5-3 wavelet transform. */
1213 /* </summary>                         */
1214 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient)
1215 {
1216     if (orient == 0) {
1217         return 0;
1218     }
1219     if (orient == 1 || orient == 2) {
1220         return 1;
1221     }
1222     return 2;
1223 }
1224
1225 /* <summary>                */
1226 /* Get norm of 5-3 wavelet. */
1227 /* </summary>               */
1228 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1229 {
1230     return opj_dwt_norms[orient][level];
1231 }
1232
1233 /* <summary>                             */
1234 /* Forward 9-7 wavelet transform in 2-D. */
1235 /* </summary>                            */
1236 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
1237 {
1238     return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1_real);
1239 }
1240
1241 /* <summary>                          */
1242 /* Get gain of 9-7 wavelet transform. */
1243 /* </summary>                         */
1244 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient)
1245 {
1246     (void)orient;
1247     return 0;
1248 }
1249
1250 /* <summary>                */
1251 /* Get norm of 9-7 wavelet. */
1252 /* </summary>               */
1253 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1254 {
1255     return opj_dwt_norms_real[orient][level];
1256 }
1257
1258 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1259 {
1260     OPJ_UINT32 numbands, bandno;
1261     numbands = 3 * tccp->numresolutions - 2;
1262     for (bandno = 0; bandno < numbands; bandno++) {
1263         OPJ_FLOAT64 stepsize;
1264         OPJ_UINT32 resno, level, orient, gain;
1265
1266         resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1267         orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1268         level = tccp->numresolutions - 1 - resno;
1269         gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1270                                           (orient == 2)) ? 1 : 2));
1271         if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1272             stepsize = 1.0;
1273         } else {
1274             OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1275             stepsize = (1 << (gain)) / norm;
1276         }
1277         opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1278                                 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1279     }
1280 }
1281
1282 /* <summary>                             */
1283 /* Determine maximum computed resolution level for inverse wavelet transform */
1284 /* </summary>                            */
1285 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1286         OPJ_UINT32 i)
1287 {
1288     OPJ_UINT32 mr   = 0;
1289     OPJ_UINT32 w;
1290     while (--i) {
1291         ++r;
1292         if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1293             mr = w ;
1294         }
1295         if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1296             mr = w ;
1297         }
1298     }
1299     return mr ;
1300 }
1301
1302 typedef struct {
1303     opj_dwt_t h;
1304     OPJ_UINT32 rw;
1305     OPJ_UINT32 w;
1306     OPJ_INT32 * OPJ_RESTRICT tiledp;
1307     OPJ_UINT32 min_j;
1308     OPJ_UINT32 max_j;
1309 } opj_dwd_decode_h_job_t;
1310
1311 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1312 {
1313     OPJ_UINT32 j;
1314     opj_dwd_decode_h_job_t* job;
1315     (void)tls;
1316
1317     job = (opj_dwd_decode_h_job_t*)user_data;
1318     for (j = job->min_j; j < job->max_j; j++) {
1319         opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1320     }
1321
1322     opj_aligned_free(job->h.mem);
1323     opj_free(job);
1324 }
1325
1326 typedef struct {
1327     opj_dwt_t v;
1328     OPJ_UINT32 rh;
1329     OPJ_UINT32 w;
1330     OPJ_INT32 * OPJ_RESTRICT tiledp;
1331     OPJ_UINT32 min_j;
1332     OPJ_UINT32 max_j;
1333 } opj_dwd_decode_v_job_t;
1334
1335 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1336 {
1337     OPJ_UINT32 j;
1338     opj_dwd_decode_v_job_t* job;
1339     (void)tls;
1340
1341     job = (opj_dwd_decode_v_job_t*)user_data;
1342     for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1343             j += PARALLEL_COLS_53) {
1344         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1345                      PARALLEL_COLS_53);
1346     }
1347     if (j < job->max_j)
1348         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1349                      (OPJ_INT32)(job->max_j - j));
1350
1351     opj_aligned_free(job->v.mem);
1352     opj_free(job);
1353 }
1354
1355
1356 /* <summary>                            */
1357 /* Inverse wavelet transform in 2-D.    */
1358 /* </summary>                           */
1359 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1360                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1361 {
1362     opj_dwt_t h;
1363     opj_dwt_t v;
1364
1365     opj_tcd_resolution_t* tr = tilec->resolutions;
1366
1367     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1368                                  tr->x0);  /* width of the resolution level computed */
1369     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1370                                  tr->y0);  /* height of the resolution level computed */
1371
1372     OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1373                                                                1].x1 -
1374                                 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1375     size_t h_mem_size;
1376     int num_threads;
1377
1378     if (numres == 1U) {
1379         return OPJ_TRUE;
1380     }
1381     num_threads = opj_thread_pool_get_thread_count(tp);
1382     h_mem_size = opj_dwt_max_resolution(tr, numres);
1383     /* overflow check */
1384     if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1385         /* FIXME event manager error callback */
1386         return OPJ_FALSE;
1387     }
1388     /* We need PARALLEL_COLS_53 times the height of the array, */
1389     /* since for the vertical pass */
1390     /* we process PARALLEL_COLS_53 columns at a time */
1391     h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1392     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1393     if (! h.mem) {
1394         /* FIXME event manager error callback */
1395         return OPJ_FALSE;
1396     }
1397
1398     v.mem = h.mem;
1399
1400     while (--numres) {
1401         OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1402         OPJ_UINT32 j;
1403
1404         ++tr;
1405         h.sn = (OPJ_INT32)rw;
1406         v.sn = (OPJ_INT32)rh;
1407
1408         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1409         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1410
1411         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1412         h.cas = tr->x0 % 2;
1413
1414         if (num_threads <= 1 || rh <= 1) {
1415             for (j = 0; j < rh; ++j) {
1416                 opj_idwt53_h(&h, &tiledp[j * w]);
1417             }
1418         } else {
1419             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1420             OPJ_UINT32 step_j;
1421
1422             if (rh < num_jobs) {
1423                 num_jobs = rh;
1424             }
1425             step_j = (rh / num_jobs);
1426
1427             for (j = 0; j < num_jobs; j++) {
1428                 opj_dwd_decode_h_job_t* job;
1429
1430                 job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
1431                 if (!job) {
1432                     /* It would be nice to fallback to single thread case, but */
1433                     /* unfortunately some jobs may be launched and have modified */
1434                     /* tiledp, so it is not practical to recover from that error */
1435                     /* FIXME event manager error callback */
1436                     opj_thread_pool_wait_completion(tp, 0);
1437                     opj_aligned_free(h.mem);
1438                     return OPJ_FALSE;
1439                 }
1440                 job->h = h;
1441                 job->rw = rw;
1442                 job->w = w;
1443                 job->tiledp = tiledp;
1444                 job->min_j = j * step_j;
1445                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1446                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1447                     job->max_j = rh;
1448                 }
1449                 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1450                 if (!job->h.mem) {
1451                     /* FIXME event manager error callback */
1452                     opj_thread_pool_wait_completion(tp, 0);
1453                     opj_free(job);
1454                     opj_aligned_free(h.mem);
1455                     return OPJ_FALSE;
1456                 }
1457                 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1458             }
1459             opj_thread_pool_wait_completion(tp, 0);
1460         }
1461
1462         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1463         v.cas = tr->y0 % 2;
1464
1465         if (num_threads <= 1 || rw <= 1) {
1466             for (j = 0; j + PARALLEL_COLS_53 <= rw;
1467                     j += PARALLEL_COLS_53) {
1468                 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, PARALLEL_COLS_53);
1469             }
1470             if (j < rw) {
1471                 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, (OPJ_INT32)(rw - j));
1472             }
1473         } else {
1474             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1475             OPJ_UINT32 step_j;
1476
1477             if (rw < num_jobs) {
1478                 num_jobs = rw;
1479             }
1480             step_j = (rw / num_jobs);
1481
1482             for (j = 0; j < num_jobs; j++) {
1483                 opj_dwd_decode_v_job_t* job;
1484
1485                 job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
1486                 if (!job) {
1487                     /* It would be nice to fallback to single thread case, but */
1488                     /* unfortunately some jobs may be launched and have modified */
1489                     /* tiledp, so it is not practical to recover from that error */
1490                     /* FIXME event manager error callback */
1491                     opj_thread_pool_wait_completion(tp, 0);
1492                     opj_aligned_free(v.mem);
1493                     return OPJ_FALSE;
1494                 }
1495                 job->v = v;
1496                 job->rh = rh;
1497                 job->w = w;
1498                 job->tiledp = tiledp;
1499                 job->min_j = j * step_j;
1500                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1501                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1502                     job->max_j = rw;
1503                 }
1504                 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1505                 if (!job->v.mem) {
1506                     /* FIXME event manager error callback */
1507                     opj_thread_pool_wait_completion(tp, 0);
1508                     opj_free(job);
1509                     opj_aligned_free(v.mem);
1510                     return OPJ_FALSE;
1511                 }
1512                 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1513             }
1514             opj_thread_pool_wait_completion(tp, 0);
1515         }
1516     }
1517     opj_aligned_free(h.mem);
1518     return OPJ_TRUE;
1519 }
1520
1521 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1522         OPJ_INT32 cas,
1523         opj_sparse_array_int32_t* sa,
1524         OPJ_UINT32 sa_line,
1525         OPJ_UINT32 sn,
1526         OPJ_UINT32 win_l_x0,
1527         OPJ_UINT32 win_l_x1,
1528         OPJ_UINT32 win_h_x0,
1529         OPJ_UINT32 win_h_x1)
1530 {
1531     OPJ_BOOL ret;
1532     ret = opj_sparse_array_int32_read(sa,
1533                                       win_l_x0, sa_line,
1534                                       win_l_x1, sa_line + 1,
1535                                       dest + cas + 2 * win_l_x0,
1536                                       2, 0, OPJ_TRUE);
1537     assert(ret);
1538     ret = opj_sparse_array_int32_read(sa,
1539                                       sn + win_h_x0, sa_line,
1540                                       sn + win_h_x1, sa_line + 1,
1541                                       dest + 1 - cas + 2 * win_h_x0,
1542                                       2, 0, OPJ_TRUE);
1543     assert(ret);
1544 }
1545
1546
1547 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1548         OPJ_INT32 cas,
1549         opj_sparse_array_int32_t* sa,
1550         OPJ_UINT32 sa_col,
1551         OPJ_UINT32 sn,
1552         OPJ_UINT32 win_l_y0,
1553         OPJ_UINT32 win_l_y1,
1554         OPJ_UINT32 win_h_y0,
1555         OPJ_UINT32 win_h_y1)
1556 {
1557     OPJ_BOOL ret;
1558     ret  = opj_sparse_array_int32_read(sa,
1559                                        sa_col, win_l_y0,
1560                                        sa_col + 1, win_l_y1,
1561                                        dest + cas + 2 * win_l_y0,
1562                                        0, 2, OPJ_TRUE);
1563     assert(ret);
1564     ret = opj_sparse_array_int32_read(sa,
1565                                       sa_col, sn + win_h_y0,
1566                                       sa_col + 1, sn + win_h_y1,
1567                                       dest + 1 - cas + 2 * win_h_y0,
1568                                       0, 2, OPJ_TRUE);
1569     assert(ret);
1570 }
1571
1572 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1573                                      OPJ_INT32 cas,
1574                                      OPJ_INT32 win_l_x0,
1575                                      OPJ_INT32 win_l_x1,
1576                                      OPJ_INT32 win_h_x0,
1577                                      OPJ_INT32 win_h_x1)
1578 {
1579     OPJ_INT32 i;
1580
1581     if (!cas) {
1582         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
1583             for (i = win_l_x0; i < win_l_x1; i++) {
1584                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1585             }
1586             for (i = win_h_x0; i < win_h_x1; i++) {
1587                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1588             }
1589         }
1590     } else {
1591         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
1592             OPJ_S(0) /= 2;
1593         } else {
1594             for (i = win_l_x0; i < win_l_x1; i++) {
1595                 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1596             }
1597             for (i = win_h_x0; i < win_h_x1; i++) {
1598                 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1599             }
1600         }
1601     }
1602 }
1603
1604 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
1605         OPJ_UINT32 resno,
1606         OPJ_UINT32 bandno,
1607         OPJ_UINT32 tcx0,
1608         OPJ_UINT32 tcy0,
1609         OPJ_UINT32 tcx1,
1610         OPJ_UINT32 tcy1,
1611         OPJ_UINT32* tbx0,
1612         OPJ_UINT32* tby0,
1613         OPJ_UINT32* tbx1,
1614         OPJ_UINT32* tby1)
1615 {
1616     /* Compute number of decomposition for this band. See table F-1 */
1617     OPJ_UINT32 nb = (resno == 0) ?
1618                     tilec->numresolutions - 1 :
1619                     tilec->numresolutions - resno;
1620     /* Map above tile-based coordinates to sub-band-based coordinates per */
1621     /* equation B-15 of the standard */
1622     OPJ_UINT32 x0b = bandno & 1;
1623     OPJ_UINT32 y0b = bandno >> 1;
1624     if (tbx0) {
1625         *tbx0 = (nb == 0) ? tcx0 :
1626                 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
1627                 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
1628     }
1629     if (tby0) {
1630         *tby0 = (nb == 0) ? tcy0 :
1631                 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
1632                 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
1633     }
1634     if (tbx1) {
1635         *tbx1 = (nb == 0) ? tcx1 :
1636                 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
1637                 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
1638     }
1639     if (tby1) {
1640         *tby1 = (nb == 0) ? tcy1 :
1641                 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
1642                 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
1643     }
1644 }
1645
1646 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
1647                                  OPJ_UINT32 max_size,
1648                                  OPJ_UINT32* start,
1649                                  OPJ_UINT32* end)
1650 {
1651     *start = opj_uint_subs(*start, filter_width);
1652     *end = opj_uint_adds(*end, filter_width);
1653     *end = opj_uint_min(*end, max_size);
1654 }
1655
1656
1657 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
1658     opj_tcd_tilecomp_t* tilec,
1659     OPJ_UINT32 numres)
1660 {
1661     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
1662     OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
1663     OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
1664     OPJ_UINT32 resno, bandno, precno, cblkno;
1665     opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
1666                                        w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
1667     if (sa == NULL) {
1668         return NULL;
1669     }
1670
1671     for (resno = 0; resno < numres; ++resno) {
1672         opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1673
1674         for (bandno = 0; bandno < res->numbands; ++bandno) {
1675             opj_tcd_band_t* band = &res->bands[bandno];
1676
1677             for (precno = 0; precno < res->pw * res->ph; ++precno) {
1678                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1679                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1680                     opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1681                     if (cblk->decoded_data != NULL) {
1682                         OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
1683                         OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
1684                         OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
1685                         OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
1686
1687                         if (band->bandno & 1) {
1688                             opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1689                             x += (OPJ_UINT32)(pres->x1 - pres->x0);
1690                         }
1691                         if (band->bandno & 2) {
1692                             opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1693                             y += (OPJ_UINT32)(pres->y1 - pres->y0);
1694                         }
1695
1696                         if (!opj_sparse_array_int32_write(sa, x, y,
1697                                                           x + cblk_w, y + cblk_h,
1698                                                           cblk->decoded_data,
1699                                                           1, cblk_w, OPJ_TRUE)) {
1700                             opj_sparse_array_int32_free(sa);
1701                             return NULL;
1702                         }
1703                     }
1704                 }
1705             }
1706         }
1707     }
1708
1709     return sa;
1710 }
1711
1712
1713 static OPJ_BOOL opj_dwt_decode_partial_tile(
1714     opj_tcd_tilecomp_t* tilec,
1715     OPJ_UINT32 numres)
1716 {
1717     opj_sparse_array_int32_t* sa;
1718     opj_dwt_t h;
1719     opj_dwt_t v;
1720     OPJ_UINT32 resno;
1721     /* This value matches the maximum left/right extension given in tables */
1722     /* F.2 and F.3 of the standard. */
1723     const OPJ_UINT32 filter_width = 2U;
1724
1725     opj_tcd_resolution_t* tr = tilec->resolutions;
1726     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
1727
1728     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1729                                  tr->x0);  /* width of the resolution level computed */
1730     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1731                                  tr->y0);  /* height of the resolution level computed */
1732
1733     size_t h_mem_size;
1734
1735     /* Compute the intersection of the area of interest, expressed in tile coordinates */
1736     /* with the tile coordinates */
1737     OPJ_UINT32 win_tcx0 = tilec->win_x0;
1738     OPJ_UINT32 win_tcy0 = tilec->win_y0;
1739     OPJ_UINT32 win_tcx1 = tilec->win_x1;
1740     OPJ_UINT32 win_tcy1 = tilec->win_y1;
1741
1742     sa = opj_dwt_init_sparse_array(tilec, numres);
1743
1744     if (numres == 1U) {
1745         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
1746                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
1747                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
1748                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
1749                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
1750                        tilec->data_win,
1751                        1, tr_max->win_x1 - tr_max->win_x0,
1752                        OPJ_TRUE);
1753         assert(ret);
1754         opj_sparse_array_int32_free(sa);
1755         return OPJ_TRUE;
1756     }
1757     h_mem_size = opj_dwt_max_resolution(tr, numres);
1758     /* overflow check */
1759     if (h_mem_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1760         /* FIXME event manager error callback */
1761         opj_sparse_array_int32_free(sa);
1762         return OPJ_FALSE;
1763     }
1764
1765     h_mem_size *= sizeof(OPJ_INT32);
1766     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1767     if (! h.mem) {
1768         /* FIXME event manager error callback */
1769         opj_sparse_array_int32_free(sa);
1770         return OPJ_FALSE;
1771     }
1772
1773     v.mem = h.mem;
1774
1775     for (resno = 1; resno < numres; resno ++) {
1776         OPJ_UINT32 i, j;
1777         /* Window of interest subband-based coordinates */
1778         OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
1779         OPJ_UINT32 win_hl_x0, win_hl_x1;
1780         OPJ_UINT32 win_lh_y0, win_lh_y1;
1781         /* Window of interest tile-resolution-based coordinates */
1782         OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
1783         /* Tile-resolution subband-based coordinates */
1784         OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
1785
1786         ++tr;
1787
1788         h.sn = (OPJ_INT32)rw;
1789         v.sn = (OPJ_INT32)rh;
1790
1791         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1792         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1793
1794         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1795         h.cas = tr->x0 % 2;
1796
1797         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1798         v.cas = tr->y0 % 2;
1799
1800         /* Get the subband coordinates for the window of interest */
1801         /* LL band */
1802         opj_dwt_get_band_coordinates(tilec, resno, 0,
1803                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1804                                      &win_ll_x0, &win_ll_y0,
1805                                      &win_ll_x1, &win_ll_y1);
1806
1807         /* HL band */
1808         opj_dwt_get_band_coordinates(tilec, resno, 1,
1809                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1810                                      &win_hl_x0, NULL, &win_hl_x1, NULL);
1811
1812         /* LH band */
1813         opj_dwt_get_band_coordinates(tilec, resno, 2,
1814                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1815                                      NULL, &win_lh_y0, NULL, &win_lh_y1);
1816
1817         /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
1818         tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
1819         tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
1820         tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
1821         tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
1822
1823         /* Substract the origin of the bands for this tile, to the subwindow */
1824         /* of interest band coordinates, so as to get them relative to the */
1825         /* tile */
1826         win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
1827         win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
1828         win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
1829         win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
1830         win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
1831         win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
1832         win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
1833         win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
1834
1835         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
1836         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
1837
1838         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
1839         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
1840
1841         /* Compute the tile-resolution-based coordinates for the window of interest */
1842         if (h.cas == 0) {
1843             win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
1844             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
1845         } else {
1846             win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
1847             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
1848         }
1849
1850         if (v.cas == 0) {
1851             win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
1852             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
1853         } else {
1854             win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
1855             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
1856         }
1857
1858         for (j = 0; j < rh; ++j) {
1859             if ((j >= win_ll_y0 && j < win_ll_y1) ||
1860                     (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
1861
1862                 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
1863                 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
1864                 /* on opj_decompress -i  ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
1865                 /* This is less extreme than memsetting the whole buffer to 0 */
1866                 /* although we could potentially do better with better handling of edge conditions */
1867                 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
1868                     h.mem[win_tr_x1 - 1] = 0;
1869                 }
1870                 if (win_tr_x1 < rw) {
1871                     h.mem[win_tr_x1] = 0;
1872                 }
1873
1874                 opj_dwt_interleave_partial_h(h.mem,
1875                                              h.cas,
1876                                              sa,
1877                                              j,
1878                                              (OPJ_UINT32)h.sn,
1879                                              win_ll_x0,
1880                                              win_ll_x1,
1881                                              win_hl_x0,
1882                                              win_hl_x1);
1883                 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
1884                                          (OPJ_INT32)win_ll_x0,
1885                                          (OPJ_INT32)win_ll_x1,
1886                                          (OPJ_INT32)win_hl_x0,
1887                                          (OPJ_INT32)win_hl_x1);
1888                 if (!opj_sparse_array_int32_write(sa,
1889                                                   win_tr_x0, j,
1890                                                   win_tr_x1, j + 1,
1891                                                   h.mem + win_tr_x0,
1892                                                   1, 0, OPJ_TRUE)) {
1893                     /* FIXME event manager error callback */
1894                     opj_sparse_array_int32_free(sa);
1895                     opj_aligned_free(h.mem);
1896                     return OPJ_FALSE;
1897                 }
1898             }
1899         }
1900
1901         for (i = win_tr_x0; i < win_tr_x1; ++i) {
1902             opj_dwt_interleave_partial_v(v.mem,
1903                                          v.cas,
1904                                          sa,
1905                                          i,
1906                                          (OPJ_UINT32)v.sn,
1907                                          win_ll_y0,
1908                                          win_ll_y1,
1909                                          win_lh_y0,
1910                                          win_lh_y1);
1911             opj_dwt_decode_partial_1(v.mem, v.dn, v.sn, v.cas,
1912                                      (OPJ_INT32)win_ll_y0,
1913                                      (OPJ_INT32)win_ll_y1,
1914                                      (OPJ_INT32)win_lh_y0,
1915                                      (OPJ_INT32)win_lh_y1);
1916             if (!opj_sparse_array_int32_write(sa,
1917                                               i, win_tr_y0,
1918                                               i + 1, win_tr_y1,
1919                                               v.mem + win_tr_y0,
1920                                               0, 1, OPJ_TRUE)) {
1921                 /* FIXME event manager error callback */
1922                 opj_sparse_array_int32_free(sa);
1923                 opj_aligned_free(h.mem);
1924                 return OPJ_FALSE;
1925             }
1926         }
1927     }
1928     opj_aligned_free(h.mem);
1929
1930     {
1931         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
1932                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
1933                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
1934                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
1935                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
1936                        tilec->data_win,
1937                        1, tr_max->win_x1 - tr_max->win_x0,
1938                        OPJ_TRUE);
1939         assert(ret);
1940     }
1941     opj_sparse_array_int32_free(sa);
1942     return OPJ_TRUE;
1943 }
1944
1945 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT dwt,
1946                                    OPJ_FLOAT32* OPJ_RESTRICT a,
1947                                    OPJ_UINT32 width,
1948                                    OPJ_UINT32 remaining_height)
1949 {
1950     OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
1951     OPJ_UINT32 i, k;
1952     OPJ_UINT32 x0 = dwt->win_l_x0;
1953     OPJ_UINT32 x1 = dwt->win_l_x1;
1954
1955     for (k = 0; k < 2; ++k) {
1956         if (remaining_height >= 4 && ((size_t) a & 0x0f) == 0 &&
1957                 ((size_t) bi & 0x0f) == 0 && (width & 0x0f) == 0) {
1958             /* Fast code path */
1959             for (i = x0; i < x1; ++i) {
1960                 OPJ_UINT32 j = i;
1961                 bi[i * 8    ] = a[j];
1962                 j += width;
1963                 bi[i * 8 + 1] = a[j];
1964                 j += width;
1965                 bi[i * 8 + 2] = a[j];
1966                 j += width;
1967                 bi[i * 8 + 3] = a[j];
1968             }
1969         } else {
1970             /* Slow code path */
1971             for (i = x0; i < x1; ++i) {
1972                 OPJ_UINT32 j = i;
1973                 bi[i * 8    ] = a[j];
1974                 j += width;
1975                 if (remaining_height == 1) {
1976                     continue;
1977                 }
1978                 bi[i * 8 + 1] = a[j];
1979                 j += width;
1980                 if (remaining_height == 2) {
1981                     continue;
1982                 }
1983                 bi[i * 8 + 2] = a[j];
1984                 j += width;
1985                 if (remaining_height == 3) {
1986                     continue;
1987                 }
1988                 bi[i * 8 + 3] = a[j]; /* This one*/
1989             }
1990         }
1991
1992         bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
1993         a += dwt->sn;
1994         x0 = dwt->win_h_x0;
1995         x1 = dwt->win_h_x1;
1996     }
1997 }
1998
1999 static void opj_v4dwt_interleave_partial_h(opj_v4dwt_t* dwt,
2000         opj_sparse_array_int32_t* sa,
2001         OPJ_UINT32 sa_line,
2002         OPJ_UINT32 remaining_height)
2003 {
2004     OPJ_UINT32 i;
2005     for (i = 0; i < remaining_height; i++) {
2006         OPJ_BOOL ret;
2007         ret = opj_sparse_array_int32_read(sa,
2008                                           dwt->win_l_x0, sa_line + i,
2009                                           dwt->win_l_x1, sa_line + i + 1,
2010                                           /* Nasty cast from float* to int32* */
2011                                           (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2012                                           8, 0, OPJ_TRUE);
2013         assert(ret);
2014         ret = opj_sparse_array_int32_read(sa,
2015                                           (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2016                                           (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2017                                           /* Nasty cast from float* to int32* */
2018                                           (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2019                                           8, 0, OPJ_TRUE);
2020         assert(ret);
2021     }
2022 }
2023
2024 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
2025                                    OPJ_FLOAT32* OPJ_RESTRICT a,
2026                                    OPJ_UINT32 width,
2027                                    OPJ_UINT32 nb_elts_read)
2028 {
2029     opj_v4_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2030     OPJ_UINT32 i;
2031
2032     for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2033         memcpy(&bi[i * 2], &a[i * width], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
2034     }
2035
2036     a += (OPJ_UINT32)dwt->sn * width;
2037     bi = dwt->wavelet + 1 - dwt->cas;
2038
2039     for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2040         memcpy(&bi[i * 2], &a[i * width], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
2041     }
2042 }
2043
2044 static void opj_v4dwt_interleave_partial_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
2045         opj_sparse_array_int32_t* sa,
2046         OPJ_UINT32 sa_col,
2047         OPJ_UINT32 nb_elts_read)
2048 {
2049     OPJ_UINT32 i;
2050     for (i = 0; i < nb_elts_read; i++) {
2051         OPJ_BOOL ret;
2052         ret = opj_sparse_array_int32_read(sa,
2053                                           sa_col + i, dwt->win_l_x0,
2054                                           sa_col + i + 1, dwt->win_l_x1,
2055                                           (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2056                                           0, 8, OPJ_TRUE);
2057         assert(ret);
2058         ret = opj_sparse_array_int32_read(sa,
2059                                           sa_col + i, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2060                                           sa_col + i + 1, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2061                                           (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2062                                           0, 8, OPJ_TRUE);
2063         assert(ret);
2064     }
2065 }
2066
2067 #ifdef __SSE__
2068
2069 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w,
2070                                        OPJ_UINT32 start,
2071                                        OPJ_UINT32 end,
2072                                        const __m128 c)
2073 {
2074     __m128* OPJ_RESTRICT vw = (__m128*) w;
2075     OPJ_UINT32 i;
2076     /* 4x unrolled loop */
2077     for (i = start; i + 3 < end; i += 4) {
2078         vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
2079         vw[2 * i + 2] = _mm_mul_ps(vw[2 * i + 2], c);
2080         vw[2 * i + 4] = _mm_mul_ps(vw[2 * i + 4], c);
2081         vw[2 * i + 6] = _mm_mul_ps(vw[2 * i + 6], c);
2082     }
2083     for (; i < end; ++i) {
2084         vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
2085     }
2086 }
2087
2088 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w,
2089                                        OPJ_UINT32 start,
2090                                        OPJ_UINT32 end,
2091                                        OPJ_UINT32 m,
2092                                        __m128 c)
2093 {
2094     __m128* OPJ_RESTRICT vl = (__m128*) l;
2095     __m128* OPJ_RESTRICT vw = (__m128*) w;
2096     OPJ_UINT32 i;
2097     OPJ_UINT32 imax = opj_uint_min(end, m);
2098     __m128 tmp1, tmp2, tmp3;
2099     if (start == 0) {
2100         tmp1 = vl[0];
2101     } else {
2102         vw += start * 2;
2103         tmp1 = vw[-3];
2104     }
2105     for (i = start; i < imax; ++i) {
2106         tmp2 = vw[-1];
2107         tmp3 = vw[ 0];
2108         vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
2109         tmp1 = tmp3;
2110         vw += 2;
2111     }
2112     if (m < end) {
2113         assert(m + 1 == end);
2114         c = _mm_add_ps(c, c);
2115         c = _mm_mul_ps(c, vw[-2]);
2116         vw[-1] = _mm_add_ps(vw[-1], c);
2117     }
2118 }
2119
2120 #else
2121
2122 static void opj_v4dwt_decode_step1(opj_v4_t* w,
2123                                    OPJ_UINT32 start,
2124                                    OPJ_UINT32 end,
2125                                    const OPJ_FLOAT32 c)
2126 {
2127     OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2128     OPJ_UINT32 i;
2129     for (i = start; i < end; ++i) {
2130         OPJ_FLOAT32 tmp1 = fw[i * 8    ];
2131         OPJ_FLOAT32 tmp2 = fw[i * 8 + 1];
2132         OPJ_FLOAT32 tmp3 = fw[i * 8 + 2];
2133         OPJ_FLOAT32 tmp4 = fw[i * 8 + 3];
2134         fw[i * 8    ] = tmp1 * c;
2135         fw[i * 8 + 1] = tmp2 * c;
2136         fw[i * 8 + 2] = tmp3 * c;
2137         fw[i * 8 + 3] = tmp4 * c;
2138     }
2139 }
2140
2141 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
2142                                    OPJ_UINT32 start,
2143                                    OPJ_UINT32 end,
2144                                    OPJ_UINT32 m,
2145                                    OPJ_FLOAT32 c)
2146 {
2147     OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2148     OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2149     OPJ_UINT32 i;
2150     OPJ_UINT32 imax = opj_uint_min(end, m);
2151     if (start > 0) {
2152         fw += 8 * start;
2153         fl = fw - 8;
2154     }
2155     for (i = start; i < imax; ++i) {
2156         OPJ_FLOAT32 tmp1_1 = fl[0];
2157         OPJ_FLOAT32 tmp1_2 = fl[1];
2158         OPJ_FLOAT32 tmp1_3 = fl[2];
2159         OPJ_FLOAT32 tmp1_4 = fl[3];
2160         OPJ_FLOAT32 tmp2_1 = fw[-4];
2161         OPJ_FLOAT32 tmp2_2 = fw[-3];
2162         OPJ_FLOAT32 tmp2_3 = fw[-2];
2163         OPJ_FLOAT32 tmp2_4 = fw[-1];
2164         OPJ_FLOAT32 tmp3_1 = fw[0];
2165         OPJ_FLOAT32 tmp3_2 = fw[1];
2166         OPJ_FLOAT32 tmp3_3 = fw[2];
2167         OPJ_FLOAT32 tmp3_4 = fw[3];
2168         fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
2169         fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
2170         fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
2171         fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
2172         fl = fw;
2173         fw += 8;
2174     }
2175     if (m < end) {
2176         assert(m + 1 == end);
2177         c += c;
2178         fw[-4] = fw[-4] + fl[0] * c;
2179         fw[-3] = fw[-3] + fl[1] * c;
2180         fw[-2] = fw[-2] + fl[2] * c;
2181         fw[-1] = fw[-1] + fl[3] * c;
2182     }
2183 }
2184
2185 #endif
2186
2187 /* <summary>                             */
2188 /* Inverse 9-7 wavelet transform in 1-D. */
2189 /* </summary>                            */
2190 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
2191 {
2192     OPJ_INT32 a, b;
2193     if (dwt->cas == 0) {
2194         if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2195             return;
2196         }
2197         a = 0;
2198         b = 1;
2199     } else {
2200         if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2201             return;
2202         }
2203         a = 1;
2204         b = 0;
2205     }
2206 #ifdef __SSE__
2207     opj_v4dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2208                                _mm_set1_ps(opj_K));
2209     opj_v4dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2210                                _mm_set1_ps(opj_c13318));
2211     opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2212                                dwt->win_l_x0, dwt->win_l_x1,
2213                                (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2214                                _mm_set1_ps(opj_dwt_delta));
2215     opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2216                                dwt->win_h_x0, dwt->win_h_x1,
2217                                (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2218                                _mm_set1_ps(opj_dwt_gamma));
2219     opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2220                                dwt->win_l_x0, dwt->win_l_x1,
2221                                (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2222                                _mm_set1_ps(opj_dwt_beta));
2223     opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2224                                dwt->win_h_x0, dwt->win_h_x1,
2225                                (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2226                                _mm_set1_ps(opj_dwt_alpha));
2227 #else
2228     opj_v4dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2229                            opj_K);
2230     opj_v4dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2231                            opj_c13318);
2232     opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2233                            dwt->win_l_x0, dwt->win_l_x1,
2234                            (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2235                            opj_dwt_delta);
2236     opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2237                            dwt->win_h_x0, dwt->win_h_x1,
2238                            (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2239                            opj_dwt_gamma);
2240     opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2241                            dwt->win_l_x0, dwt->win_l_x1,
2242                            (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2243                            opj_dwt_beta);
2244     opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2245                            dwt->win_h_x0, dwt->win_h_x1,
2246                            (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2247                            opj_dwt_alpha);
2248 #endif
2249 }
2250
2251
2252 /* <summary>                             */
2253 /* Inverse 9-7 wavelet transform in 2-D. */
2254 /* </summary>                            */
2255 static
2256 OPJ_BOOL opj_dwt_decode_tile_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2257                                 OPJ_UINT32 numres)
2258 {
2259     opj_v4dwt_t h;
2260     opj_v4dwt_t v;
2261
2262     opj_tcd_resolution_t* res = tilec->resolutions;
2263
2264     OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2265                                  res->x0);    /* width of the resolution level computed */
2266     OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2267                                  res->y0);    /* height of the resolution level computed */
2268
2269     OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2270                                                                1].x1 -
2271                                 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2272
2273     size_t l_data_size;
2274
2275     l_data_size = opj_dwt_max_resolution(res, numres);
2276     /* overflow check */
2277     if (l_data_size > (SIZE_MAX - 5U)) {
2278         /* FIXME event manager error callback */
2279         return OPJ_FALSE;
2280     }
2281     l_data_size += 5U;
2282     /* overflow check */
2283     if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2284         /* FIXME event manager error callback */
2285         return OPJ_FALSE;
2286     }
2287     h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2288     if (!h.wavelet) {
2289         /* FIXME event manager error callback */
2290         return OPJ_FALSE;
2291     }
2292     v.wavelet = h.wavelet;
2293
2294     while (--numres) {
2295         OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2296         OPJ_UINT32 j;
2297
2298         h.sn = (OPJ_INT32)rw;
2299         v.sn = (OPJ_INT32)rh;
2300
2301         ++res;
2302
2303         rw = (OPJ_UINT32)(res->x1 -
2304                           res->x0);   /* width of the resolution level computed */
2305         rh = (OPJ_UINT32)(res->y1 -
2306                           res->y0);   /* height of the resolution level computed */
2307
2308         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2309         h.cas = res->x0 % 2;
2310
2311         h.win_l_x0 = 0;
2312         h.win_l_x1 = (OPJ_UINT32)h.sn;
2313         h.win_h_x0 = 0;
2314         h.win_h_x1 = (OPJ_UINT32)h.dn;
2315         for (j = 0; j + 3 < rh; j += 4) {
2316             OPJ_UINT32 k;
2317             opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2318             opj_v4dwt_decode(&h);
2319
2320             for (k = 0; k < rw; k++) {
2321                 aj[k      ] = h.wavelet[k].f[0];
2322                 aj[k + w  ] = h.wavelet[k].f[1];
2323                 aj[k + w * 2] = h.wavelet[k].f[2];
2324                 aj[k + w * 3] = h.wavelet[k].f[3];
2325             }
2326
2327             aj += w * 4;
2328         }
2329
2330         if (j < rh) {
2331             OPJ_UINT32 k;
2332             opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2333             opj_v4dwt_decode(&h);
2334             for (k = 0; k < rw; k++) {
2335                 switch (rh - j) {
2336                 case 3:
2337                     aj[k + w * 2] = h.wavelet[k].f[2];
2338                 /* FALLTHRU */
2339                 case 2:
2340                     aj[k + w  ] = h.wavelet[k].f[1];
2341                 /* FALLTHRU */
2342                 case 1:
2343                     aj[k] = h.wavelet[k].f[0];
2344                 }
2345             }
2346         }
2347
2348         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2349         v.cas = res->y0 % 2;
2350         v.win_l_x0 = 0;
2351         v.win_l_x1 = (OPJ_UINT32)v.sn;
2352         v.win_h_x0 = 0;
2353         v.win_h_x1 = (OPJ_UINT32)v.dn;
2354
2355         aj = (OPJ_FLOAT32*) tilec->data;
2356         for (j = rw; j > 3; j -= 4) {
2357             OPJ_UINT32 k;
2358
2359             opj_v4dwt_interleave_v(&v, aj, w, 4);
2360             opj_v4dwt_decode(&v);
2361
2362             for (k = 0; k < rh; ++k) {
2363                 memcpy(&aj[k * w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
2364             }
2365             aj += 4;
2366         }
2367
2368         if (rw & 0x03) {
2369             OPJ_UINT32 k;
2370
2371             j = rw & 0x03;
2372
2373             opj_v4dwt_interleave_v(&v, aj, w, j);
2374             opj_v4dwt_decode(&v);
2375
2376             for (k = 0; k < rh; ++k) {
2377                 memcpy(&aj[k * w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
2378             }
2379         }
2380     }
2381
2382     opj_aligned_free(h.wavelet);
2383     return OPJ_TRUE;
2384 }
2385
2386 static
2387 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2388                                    OPJ_UINT32 numres)
2389 {
2390     opj_sparse_array_int32_t* sa;
2391     opj_v4dwt_t h;
2392     opj_v4dwt_t v;
2393     OPJ_UINT32 resno;
2394     /* This value matches the maximum left/right extension given in tables */
2395     /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
2396     /* we currently use 3. */
2397     const OPJ_UINT32 filter_width = 4U;
2398
2399     opj_tcd_resolution_t* tr = tilec->resolutions;
2400     opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2401
2402     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2403                                  tr->x0);    /* width of the resolution level computed */
2404     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2405                                  tr->y0);    /* height of the resolution level computed */
2406
2407     size_t l_data_size;
2408
2409     /* Compute the intersection of the area of interest, expressed in tile coordinates */
2410     /* with the tile coordinates */
2411     OPJ_UINT32 win_tcx0 = tilec->win_x0;
2412     OPJ_UINT32 win_tcy0 = tilec->win_y0;
2413     OPJ_UINT32 win_tcx1 = tilec->win_x1;
2414     OPJ_UINT32 win_tcy1 = tilec->win_y1;
2415
2416     sa = opj_dwt_init_sparse_array(tilec, numres);
2417
2418     if (numres == 1U) {
2419         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2420                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2421                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2422                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2423                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2424                        tilec->data_win,
2425                        1, tr_max->win_x1 - tr_max->win_x0,
2426                        OPJ_TRUE);
2427         assert(ret);
2428         opj_sparse_array_int32_free(sa);
2429         return OPJ_TRUE;
2430     }
2431
2432     l_data_size = opj_dwt_max_resolution(tr, numres);
2433     /* overflow check */
2434     if (l_data_size > (SIZE_MAX - 5U)) {
2435         /* FIXME event manager error callback */
2436         return OPJ_FALSE;
2437     }
2438     l_data_size += 5U;
2439     /* overflow check */
2440     if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2441         /* FIXME event manager error callback */
2442         return OPJ_FALSE;
2443     }
2444     h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2445     if (!h.wavelet) {
2446         /* FIXME event manager error callback */
2447         return OPJ_FALSE;
2448     }
2449     v.wavelet = h.wavelet;
2450
2451     for (resno = 1; resno < numres; resno ++) {
2452         OPJ_UINT32 j;
2453         /* Window of interest subband-based coordinates */
2454         OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2455         OPJ_UINT32 win_hl_x0, win_hl_x1;
2456         OPJ_UINT32 win_lh_y0, win_lh_y1;
2457         /* Window of interest tile-resolution-based coordinates */
2458         OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2459         /* Tile-resolution subband-based coordinates */
2460         OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2461
2462         ++tr;
2463
2464         h.sn = (OPJ_INT32)rw;
2465         v.sn = (OPJ_INT32)rh;
2466
2467         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2468         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2469
2470         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2471         h.cas = tr->x0 % 2;
2472
2473         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2474         v.cas = tr->y0 % 2;
2475
2476         /* Get the subband coordinates for the window of interest */
2477         /* LL band */
2478         opj_dwt_get_band_coordinates(tilec, resno, 0,
2479                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2480                                      &win_ll_x0, &win_ll_y0,
2481                                      &win_ll_x1, &win_ll_y1);
2482
2483         /* HL band */
2484         opj_dwt_get_band_coordinates(tilec, resno, 1,
2485                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2486                                      &win_hl_x0, NULL, &win_hl_x1, NULL);
2487
2488         /* LH band */
2489         opj_dwt_get_band_coordinates(tilec, resno, 2,
2490                                      win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2491                                      NULL, &win_lh_y0, NULL, &win_lh_y1);
2492
2493         /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2494         tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2495         tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2496         tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2497         tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2498
2499         /* Substract the origin of the bands for this tile, to the subwindow */
2500         /* of interest band coordinates, so as to get them relative to the */
2501         /* tile */
2502         win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2503         win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2504         win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2505         win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2506         win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2507         win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2508         win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2509         win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2510
2511         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2512         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2513
2514         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2515         opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2516
2517         /* Compute the tile-resolution-based coordinates for the window of interest */
2518         if (h.cas == 0) {
2519             win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2520             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2521         } else {
2522             win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2523             win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2524         }
2525
2526         if (v.cas == 0) {
2527             win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2528             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2529         } else {
2530             win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2531             win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2532         }
2533
2534         h.win_l_x0 = win_ll_x0;
2535         h.win_l_x1 = win_ll_x1;
2536         h.win_h_x0 = win_hl_x0;
2537         h.win_h_x1 = win_hl_x1;
2538         for (j = 0; j + 3 < rh; j += 4) {
2539             if ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2540                     (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2541                      j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2542                 OPJ_UINT32 k;
2543                 opj_v4dwt_interleave_partial_h(&h, sa, j, opj_uint_min(4U, rh - j));
2544                 opj_v4dwt_decode(&h);
2545                 for (k = 0; k < 4; k++) {
2546                     if (!opj_sparse_array_int32_write(sa,
2547                                                       win_tr_x0, j + k,
2548                                                       win_tr_x1, j + k + 1,
2549                                                       (OPJ_INT32*)&h.wavelet[win_tr_x0].f[k],
2550                                                       4, 0, OPJ_TRUE)) {
2551                         /* FIXME event manager error callback */
2552                         opj_sparse_array_int32_free(sa);
2553                         opj_aligned_free(h.wavelet);
2554                         return OPJ_FALSE;
2555                     }
2556                 }
2557             }
2558         }
2559
2560         if (j < rh &&
2561                 ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2562                  (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2563                   j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
2564             OPJ_UINT32 k;
2565             opj_v4dwt_interleave_partial_h(&h, sa, j, rh - j);
2566             opj_v4dwt_decode(&h);
2567             for (k = 0; k < rh - j; k++) {
2568                 if (!opj_sparse_array_int32_write(sa,
2569                                                   win_tr_x0, j + k,
2570                                                   win_tr_x1, j + k + 1,
2571                                                   (OPJ_INT32*)&h.wavelet[win_tr_x0].f[k],
2572                                                   4, 0, OPJ_TRUE)) {
2573                     /* FIXME event manager error callback */
2574                     opj_sparse_array_int32_free(sa);
2575                     opj_aligned_free(h.wavelet);
2576                     return OPJ_FALSE;
2577                 }
2578             }
2579         }
2580
2581         v.win_l_x0 = win_ll_y0;
2582         v.win_l_x1 = win_ll_y1;
2583         v.win_h_x0 = win_lh_y0;
2584         v.win_h_x1 = win_lh_y1;
2585         for (j = win_tr_x0; j < win_tr_x1; j += 4) {
2586             OPJ_UINT32 nb_elts = opj_uint_min(4U, win_tr_x1 - j);
2587             OPJ_UINT32 k;
2588
2589             opj_v4dwt_interleave_partial_v(&v, sa, j, nb_elts);
2590             opj_v4dwt_decode(&v);
2591
2592             for (k = 0; k < nb_elts; k++) {
2593                 if (!opj_sparse_array_int32_write(sa,
2594                                                   j + k, win_tr_y0,
2595                                                   j + k + 1, win_tr_y1,
2596                                                   (OPJ_INT32*)&h.wavelet[win_tr_y0].f[k],
2597                                                   0, 4, OPJ_TRUE)) {
2598                     /* FIXME event manager error callback */
2599                     opj_sparse_array_int32_free(sa);
2600                     opj_aligned_free(h.wavelet);
2601                     return OPJ_FALSE;
2602                 }
2603             }
2604         }
2605     }
2606
2607     {
2608         OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2609                        tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2610                        tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2611                        tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2612                        tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2613                        tilec->data_win,
2614                        1, tr_max->win_x1 - tr_max->win_x0,
2615                        OPJ_TRUE);
2616         assert(ret);
2617     }
2618     opj_sparse_array_int32_free(sa);
2619
2620     opj_aligned_free(h.wavelet);
2621     return OPJ_TRUE;
2622 }
2623
2624
2625 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
2626                              opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2627                              OPJ_UINT32 numres)
2628 {
2629     if (p_tcd->whole_tile_decoding) {
2630         return opj_dwt_decode_tile_97(tilec, numres);
2631     } else {
2632         return opj_dwt_decode_partial_97(tilec, numres);
2633     }
2634 }