Merge pull request #954 from jeroen/static
[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;
86     OPJ_INT32 sn;
87     OPJ_INT32 cas;
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 ;
97     OPJ_INT32       sn ;
98     OPJ_INT32       cas ;
99 } opj_v4dwt_t ;
100
101 static const OPJ_FLOAT32 opj_dwt_alpha =  1.586134342f; /*  12994 */
102 static const OPJ_FLOAT32 opj_dwt_beta  =  0.052980118f; /*    434 */
103 static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /*  -7233 */
104 static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /*  -3633 */
105
106 static const OPJ_FLOAT32 opj_K      = 1.230174105f; /*  10078 */
107 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
108
109 /*@}*/
110
111 /**
112 Virtual function type for wavelet transform in 1-D
113 */
114 typedef void (*DWT1DFN)(const opj_dwt_t* v);
115
116 /** @name Local static functions */
117 /*@{*/
118
119 /**
120 Forward lazy transform (horizontal)
121 */
122 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
123                                    OPJ_INT32 sn, OPJ_INT32 cas);
124 /**
125 Forward lazy transform (vertical)
126 */
127 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
128                                    OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
129 /**
130 Forward 5-3 wavelet transform in 1-D
131 */
132 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
133                              OPJ_INT32 cas);
134 /**
135 Forward 9-7 wavelet transform in 1-D
136 */
137 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
138                                   OPJ_INT32 cas);
139 /**
140 Explicit calculation of the Quantization Stepsizes
141 */
142 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
143                                     opj_stepsize_t *bandno_stepsize);
144 /**
145 Inverse wavelet transform in 2-D.
146 */
147 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
148                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
149
150 static OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
151         void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32));
152
153 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
154         OPJ_UINT32 i);
155
156 /* <summary>                             */
157 /* Inverse 9-7 wavelet transform in 1-D. */
158 /* </summary>                            */
159 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt);
160
161 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w,
162                                    OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size);
163
164 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT v,
165                                    OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 nb_elts_read);
166
167 #ifdef __SSE__
168 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count,
169                                        const __m128 c);
170
171 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k,
172                                        OPJ_INT32 m, __m128 c);
173
174 #else
175 static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count,
176                                    const OPJ_FLOAT32 c);
177
178 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k,
179                                    OPJ_INT32 m, OPJ_FLOAT32 c);
180
181 #endif
182
183 /*@}*/
184
185 /*@}*/
186
187 #define OPJ_S(i) a[(i)*2]
188 #define OPJ_D(i) a[(1+(i)*2)]
189 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
190 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
191 /* new */
192 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
193 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
194
195 /* <summary>                                                              */
196 /* This table contains the norms of the 5-3 wavelets for different bands. */
197 /* </summary>                                                             */
198 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
199     {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
200     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
201     {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
202     {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
203 };
204
205 /* <summary>                                                              */
206 /* This table contains the norms of the 9-7 wavelets for different bands. */
207 /* </summary>                                                             */
208 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
209     {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
210     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
211     {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
212     {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
213 };
214
215 /*
216 ==========================================================
217    local functions
218 ==========================================================
219 */
220
221 /* <summary>                             */
222 /* Forward lazy transform (horizontal).  */
223 /* </summary>                            */
224 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
225                                    OPJ_INT32 sn, OPJ_INT32 cas)
226 {
227     OPJ_INT32 i;
228     OPJ_INT32 * l_dest = b;
229     OPJ_INT32 * l_src = a + cas;
230
231     for (i = 0; i < sn; ++i) {
232         *l_dest++ = *l_src;
233         l_src += 2;
234     }
235
236     l_dest = b + sn;
237     l_src = a + 1 - cas;
238
239     for (i = 0; i < dn; ++i)  {
240         *l_dest++ = *l_src;
241         l_src += 2;
242     }
243 }
244
245 /* <summary>                             */
246 /* Forward lazy transform (vertical).    */
247 /* </summary>                            */
248 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
249                                    OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas)
250 {
251     OPJ_INT32 i = sn;
252     OPJ_INT32 * l_dest = b;
253     OPJ_INT32 * l_src = a + cas;
254
255     while (i--) {
256         *l_dest = *l_src;
257         l_dest += x;
258         l_src += 2;
259     } /* b[i*x]=a[2*i+cas]; */
260
261     l_dest = b + sn * x;
262     l_src = a + 1 - cas;
263
264     i = dn;
265     while (i--) {
266         *l_dest = *l_src;
267         l_dest += x;
268         l_src += 2;
269     } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
270 }
271
272 #ifdef STANDARD_SLOW_VERSION
273 /* <summary>                             */
274 /* Inverse lazy transform (horizontal).  */
275 /* </summary>                            */
276 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
277 {
278     OPJ_INT32 *ai = a;
279     OPJ_INT32 *bi = h->mem + h->cas;
280     OPJ_INT32  i    = h->sn;
281     while (i--) {
282         *bi = *(ai++);
283         bi += 2;
284     }
285     ai  = a + h->sn;
286     bi  = h->mem + 1 - h->cas;
287     i   = h->dn ;
288     while (i--) {
289         *bi = *(ai++);
290         bi += 2;
291     }
292 }
293
294 /* <summary>                             */
295 /* Inverse lazy transform (vertical).    */
296 /* </summary>                            */
297 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
298 {
299     OPJ_INT32 *ai = a;
300     OPJ_INT32 *bi = v->mem + v->cas;
301     OPJ_INT32  i = v->sn;
302     while (i--) {
303         *bi = *ai;
304         bi += 2;
305         ai += x;
306     }
307     ai = a + (v->sn * x);
308     bi = v->mem + 1 - v->cas;
309     i = v->dn ;
310     while (i--) {
311         *bi = *ai;
312         bi += 2;
313         ai += x;
314     }
315 }
316
317 #endif /* STANDARD_SLOW_VERSION */
318
319 /* <summary>                            */
320 /* Forward 5-3 wavelet transform in 1-D. */
321 /* </summary>                           */
322 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
323                              OPJ_INT32 cas)
324 {
325     OPJ_INT32 i;
326
327     if (!cas) {
328         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
329             for (i = 0; i < dn; i++) {
330                 OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
331             }
332             for (i = 0; i < sn; i++) {
333                 OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
334             }
335         }
336     } else {
337         if (!sn && dn == 1) {       /* NEW :  CASE ONE ELEMENT */
338             OPJ_S(0) *= 2;
339         } else {
340             for (i = 0; i < dn; i++) {
341                 OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
342             }
343             for (i = 0; i < sn; i++) {
344                 OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
345             }
346         }
347     }
348 }
349
350 #ifdef STANDARD_SLOW_VERSION
351 /* <summary>                            */
352 /* Inverse 5-3 wavelet transform in 1-D. */
353 /* </summary>                           */
354 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
355                               OPJ_INT32 cas)
356 {
357     OPJ_INT32 i;
358
359     if (!cas) {
360         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
361             for (i = 0; i < sn; i++) {
362                 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
363             }
364             for (i = 0; i < dn; i++) {
365                 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
366             }
367         }
368     } else {
369         if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
370             OPJ_S(0) /= 2;
371         } else {
372             for (i = 0; i < sn; i++) {
373                 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
374             }
375             for (i = 0; i < dn; i++) {
376                 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
377             }
378         }
379     }
380 }
381
382 static void opj_dwt_decode_1(const opj_dwt_t *v)
383 {
384     opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
385 }
386
387 #endif /* STANDARD_SLOW_VERSION */
388
389 #if !defined(STANDARD_SLOW_VERSION)
390 static void  opj_idwt53_h_cas0(OPJ_INT32* tmp,
391                                const OPJ_INT32 sn,
392                                const OPJ_INT32 len,
393                                OPJ_INT32* tiledp)
394 {
395     OPJ_INT32 i, j;
396     const OPJ_INT32* in_even = &tiledp[0];
397     const OPJ_INT32* in_odd = &tiledp[sn];
398
399 #ifdef TWO_PASS_VERSION
400     /* For documentation purpose: performs lifting in two iterations, */
401     /* but withtmp explicit interleaving */
402
403     assert(len > 1);
404
405     /* Even */
406     tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
407     for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
408         tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
409     }
410     if (len & 1) { /* if len is odd */
411         tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
412     }
413
414     /* Odd */
415     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
416         tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
417     }
418     if (!(len & 1)) { /* if len is even */
419         tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
420     }
421 #else
422     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
423
424     assert(len > 1);
425
426     /* Improved version of the TWO_PASS_VERSION: */
427     /* Performs lifting in one single iteration. Saves memory */
428     /* accesses and explicit interleaving. */
429     s1n = in_even[0];
430     d1n = in_odd[0];
431     s0n = s1n - ((d1n + 1) >> 1);
432
433     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
434         d1c = d1n;
435         s0c = s0n;
436
437         s1n = in_even[j];
438         d1n = in_odd[j];
439
440         s0n = s1n - ((d1c + d1n + 2) >> 2);
441
442         tmp[i  ] = s0c;
443         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
444     }
445
446     tmp[i] = s0n;
447
448     if (len & 1) {
449         tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
450         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
451     } else {
452         tmp[len - 1] = d1n + s0n;
453     }
454 #endif
455     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
456 }
457
458 static void  opj_idwt53_h_cas1(OPJ_INT32* tmp,
459                                const OPJ_INT32 sn,
460                                const OPJ_INT32 len,
461                                OPJ_INT32* tiledp)
462 {
463     OPJ_INT32 i, j;
464     const OPJ_INT32* in_even = &tiledp[sn];
465     const OPJ_INT32* in_odd = &tiledp[0];
466
467 #ifdef TWO_PASS_VERSION
468     /* For documentation purpose: performs lifting in two iterations, */
469     /* but withtmp explicit interleaving */
470
471     assert(len > 2);
472
473     /* Odd */
474     for (i = 1, j = 0; i < len - 1; i += 2, j++) {
475         tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
476     }
477     if (!(len & 1)) {
478         tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
479     }
480
481     /* Even */
482     tmp[0] = in_even[0] + tmp[1];
483     for (i = 2, j = 1; i < len - 1; i += 2, j++) {
484         tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
485     }
486     if (len & 1) {
487         tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
488     }
489 #else
490     OPJ_INT32 s1, s2, dc, dn;
491
492     assert(len > 2);
493
494     /* Improved version of the TWO_PASS_VERSION: */
495     /* Performs lifting in one single iteration. Saves memory */
496     /* accesses and explicit interleaving. */
497
498     s1 = in_even[1];
499     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
500     tmp[0] = in_even[0] + dc;
501
502     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
503
504         s2 = in_even[j + 1];
505
506         dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
507         tmp[i  ] = dc;
508         tmp[i + 1] = s1 + ((dn + dc) >> 1);
509
510         dc = dn;
511         s1 = s2;
512     }
513
514     tmp[i] = dc;
515
516     if (!(len & 1)) {
517         dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
518         tmp[len - 2] = s1 + ((dn + dc) >> 1);
519         tmp[len - 1] = dn;
520     } else {
521         tmp[len - 1] = s1 + dc;
522     }
523 #endif
524     memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
525 }
526
527
528 #endif /* !defined(STANDARD_SLOW_VERSION) */
529
530 /* <summary>                            */
531 /* Inverse 5-3 wavelet transform in 1-D for one row. */
532 /* </summary>                           */
533 /* Performs interleave, inverse wavelet transform and copy back to buffer */
534 static void opj_idwt53_h(const opj_dwt_t *dwt,
535                          OPJ_INT32* tiledp)
536 {
537 #ifdef STANDARD_SLOW_VERSION
538     /* For documentation purpose */
539     opj_dwt_interleave_h(dwt, tiledp);
540     opj_dwt_decode_1(dwt);
541     memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
542 #else
543     const OPJ_INT32 sn = dwt->sn;
544     const OPJ_INT32 len = sn + dwt->dn;
545     if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
546         if (len > 1) {
547             opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
548         } else {
549             /* Unmodified value */
550         }
551     } else { /* Left-most sample is on odd coordinate */
552         if (len == 1) {
553             tiledp[0] /= 2;
554         } else if (len == 2) {
555             OPJ_INT32* out = dwt->mem;
556             const OPJ_INT32* in_even = &tiledp[sn];
557             const OPJ_INT32* in_odd = &tiledp[0];
558             out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
559             out[0] = in_even[0] + out[1];
560             memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
561         } else if (len > 2) {
562             opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
563         }
564     }
565 #endif
566 }
567
568 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
569
570 /* Conveniency macros to improve the readabilty of the formulas */
571 #if __AVX2__
572 #define VREG        __m256i
573 #define LOAD_CST(x) _mm256_set1_epi32(x)
574 #define LOAD(x)     _mm256_load_si256((const VREG*)(x))
575 #define LOADU(x)    _mm256_loadu_si256((const VREG*)(x))
576 #define STORE(x,y)  _mm256_store_si256((VREG*)(x),(y))
577 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
578 #define ADD(x,y)    _mm256_add_epi32((x),(y))
579 #define SUB(x,y)    _mm256_sub_epi32((x),(y))
580 #define SAR(x,y)    _mm256_srai_epi32((x),(y))
581 #else
582 #define VREG        __m128i
583 #define LOAD_CST(x) _mm_set1_epi32(x)
584 #define LOAD(x)     _mm_load_si128((const VREG*)(x))
585 #define LOADU(x)    _mm_loadu_si128((const VREG*)(x))
586 #define STORE(x,y)  _mm_store_si128((VREG*)(x),(y))
587 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
588 #define ADD(x,y)    _mm_add_epi32((x),(y))
589 #define SUB(x,y)    _mm_sub_epi32((x),(y))
590 #define SAR(x,y)    _mm_srai_epi32((x),(y))
591 #endif
592 #define ADD3(x,y,z) ADD(ADD(x,y),z)
593
594 static
595 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
596                                const OPJ_INT32* tmp,
597                                OPJ_INT32 len,
598                                OPJ_INT32 stride)
599 {
600     OPJ_INT32 i;
601     for (i = 0; i < len; ++i) {
602         /* A memcpy(&tiledp_col[i * stride + 0],
603                     &tmp[PARALLEL_COLS_53 * i + 0],
604                     PARALLEL_COLS_53 * sizeof(OPJ_INT32))
605            would do but would be a tiny bit slower.
606            We can take here advantage of our knowledge of alignment */
607         STOREU(&tiledp_col[i * stride + 0],
608                LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
609         STOREU(&tiledp_col[i * stride + VREG_INT_COUNT],
610                LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
611     }
612 }
613
614 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
615  * 16 in AVX2, when top-most pixel is on even coordinate */
616 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
617     OPJ_INT32* tmp,
618     const OPJ_INT32 sn,
619     const OPJ_INT32 len,
620     OPJ_INT32* tiledp_col,
621     const OPJ_INT32 stride)
622 {
623     const OPJ_INT32* in_even = &tiledp_col[0];
624     const OPJ_INT32* in_odd = &tiledp_col[sn * stride];
625
626     OPJ_INT32 i, j;
627     VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
628     VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
629     const VREG two = LOAD_CST(2);
630
631     assert(len > 1);
632 #if __AVX2__
633     assert(PARALLEL_COLS_53 == 16);
634     assert(VREG_INT_COUNT == 8);
635 #else
636     assert(PARALLEL_COLS_53 == 8);
637     assert(VREG_INT_COUNT == 4);
638 #endif
639
640     /* Note: loads of input even/odd values must be done in a unaligned */
641     /* fashion. But stores in tmp can be done with aligned store, since */
642     /* the temporary buffer is properly aligned */
643     assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
644
645     s1n_0 = LOADU(in_even + 0);
646     s1n_1 = LOADU(in_even + VREG_INT_COUNT);
647     d1n_0 = LOADU(in_odd);
648     d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
649
650     /* s0n = s1n - ((d1n + 1) >> 1); <==> */
651     /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
652     s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
653     s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
654
655     for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
656         d1c_0 = d1n_0;
657         s0c_0 = s0n_0;
658         d1c_1 = d1n_1;
659         s0c_1 = s0n_1;
660
661         s1n_0 = LOADU(in_even + j * stride);
662         s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
663         d1n_0 = LOADU(in_odd + j * stride);
664         d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
665
666         /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
667         s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
668         s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
669
670         STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
671         STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
672
673         /* d1c + ((s0c + s0n) >> 1) */
674         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
675               ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
676         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
677               ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
678     }
679
680     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
681     STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
682
683     if (len & 1) {
684         VREG tmp_len_minus_1;
685         s1n_0 = LOADU(in_even + ((len - 1) / 2) * stride);
686         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
687         tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
688         STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
689         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
690         STORE(tmp + PARALLEL_COLS_53 * (len - 2),
691               ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
692
693         s1n_1 = LOADU(in_even + ((len - 1) / 2) * stride + VREG_INT_COUNT);
694         /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
695         tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
696         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
697               tmp_len_minus_1);
698         /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
699         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
700               ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
701
702
703     } else {
704         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
705               ADD(d1n_0, s0n_0));
706         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
707               ADD(d1n_1, s0n_1));
708     }
709
710     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
711 }
712
713
714 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
715  * 16 in AVX2, when top-most pixel is on odd coordinate */
716 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
717     OPJ_INT32* tmp,
718     const OPJ_INT32 sn,
719     const OPJ_INT32 len,
720     OPJ_INT32* tiledp_col,
721     const OPJ_INT32 stride)
722 {
723     OPJ_INT32 i, j;
724
725     VREG s1_0, s2_0, dc_0, dn_0;
726     VREG s1_1, s2_1, dc_1, dn_1;
727     const VREG two = LOAD_CST(2);
728
729     const OPJ_INT32* in_even = &tiledp_col[sn * stride];
730     const OPJ_INT32* in_odd = &tiledp_col[0];
731
732     assert(len > 2);
733 #if __AVX2__
734     assert(PARALLEL_COLS_53 == 16);
735     assert(VREG_INT_COUNT == 8);
736 #else
737     assert(PARALLEL_COLS_53 == 8);
738     assert(VREG_INT_COUNT == 4);
739 #endif
740
741     /* Note: loads of input even/odd values must be done in a unaligned */
742     /* fashion. But stores in tmp can be done with aligned store, since */
743     /* the temporary buffer is properly aligned */
744     assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
745
746     s1_0 = LOADU(in_even + stride);
747     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
748     dc_0 = SUB(LOADU(in_odd + 0),
749                SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
750     STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
751
752     s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
753     /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
754     dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
755                SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
756     STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
757           ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
758
759     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
760
761         s2_0 = LOADU(in_even + (j + 1) * stride);
762         s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
763
764         /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
765         dn_0 = SUB(LOADU(in_odd + j * stride),
766                    SAR(ADD3(s1_0, s2_0, two), 2));
767         dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
768                    SAR(ADD3(s1_1, s2_1, two), 2));
769
770         STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
771         STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
772
773         /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
774         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
775               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
776         STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
777               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
778
779         dc_0 = dn_0;
780         s1_0 = s2_0;
781         dc_1 = dn_1;
782         s1_1 = s2_1;
783     }
784     STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
785     STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
786
787     if (!(len & 1)) {
788         /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
789         dn_0 = SUB(LOADU(in_odd + (len / 2 - 1) * stride),
790                    SAR(ADD3(s1_0, s1_0, two), 2));
791         dn_1 = SUB(LOADU(in_odd + (len / 2 - 1) * stride + VREG_INT_COUNT),
792                    SAR(ADD3(s1_1, s1_1, two), 2));
793
794         /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
795         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
796               ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
797         STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
798               ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
799
800         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
801         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
802     } else {
803         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
804         STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
805               ADD(s1_1, dc_1));
806     }
807
808     opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
809 }
810
811 #undef VREG
812 #undef LOAD_CST
813 #undef LOADU
814 #undef LOAD
815 #undef STORE
816 #undef STOREU
817 #undef ADD
818 #undef ADD3
819 #undef SUB
820 #undef SAR
821
822 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
823
824 #if !defined(STANDARD_SLOW_VERSION)
825 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
826  * pixel is on even coordinate */
827 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
828                              const OPJ_INT32 sn,
829                              const OPJ_INT32 len,
830                              OPJ_INT32* tiledp_col,
831                              const OPJ_INT32 stride)
832 {
833     OPJ_INT32 i, j;
834     OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
835
836     assert(len > 1);
837
838     /* Performs lifting in one single iteration. Saves memory */
839     /* accesses and explicit interleaving. */
840
841     s1n = tiledp_col[0];
842     d1n = tiledp_col[sn * stride];
843     s0n = s1n - ((d1n + 1) >> 1);
844
845     for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
846         d1c = d1n;
847         s0c = s0n;
848
849         s1n = tiledp_col[(j + 1) * stride];
850         d1n = tiledp_col[(sn + j + 1) * stride];
851
852         s0n = s1n - ((d1c + d1n + 2) >> 2);
853
854         tmp[i  ] = s0c;
855         tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
856     }
857
858     tmp[i] = s0n;
859
860     if (len & 1) {
861         tmp[len - 1] =
862             tiledp_col[((len - 1) / 2) * stride] -
863             ((d1n + 1) >> 1);
864         tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
865     } else {
866         tmp[len - 1] = d1n + s0n;
867     }
868
869     for (i = 0; i < len; ++i) {
870         tiledp_col[i * stride] = tmp[i];
871     }
872 }
873
874
875 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
876  * pixel is on odd coordinate */
877 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
878                              const OPJ_INT32 sn,
879                              const OPJ_INT32 len,
880                              OPJ_INT32* tiledp_col,
881                              const OPJ_INT32 stride)
882 {
883     OPJ_INT32 i, j;
884     OPJ_INT32 s1, s2, dc, dn;
885     const OPJ_INT32* in_even = &tiledp_col[sn * stride];
886     const OPJ_INT32* in_odd = &tiledp_col[0];
887
888     assert(len > 2);
889
890     /* Performs lifting in one single iteration. Saves memory */
891     /* accesses and explicit interleaving. */
892
893     s1 = in_even[stride];
894     dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
895     tmp[0] = in_even[0] + dc;
896     for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
897
898         s2 = in_even[(j + 1) * stride];
899
900         dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2);
901         tmp[i  ] = dc;
902         tmp[i + 1] = s1 + ((dn + dc) >> 1);
903
904         dc = dn;
905         s1 = s2;
906     }
907     tmp[i] = dc;
908     if (!(len & 1)) {
909         dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
910         tmp[len - 2] = s1 + ((dn + dc) >> 1);
911         tmp[len - 1] = dn;
912     } else {
913         tmp[len - 1] = s1 + dc;
914     }
915
916     for (i = 0; i < len; ++i) {
917         tiledp_col[i * stride] = tmp[i];
918     }
919 }
920 #endif /* !defined(STANDARD_SLOW_VERSION) */
921
922 /* <summary>                            */
923 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
924 /* </summary>                           */
925 /* Performs interleave, inverse wavelet transform and copy back to buffer */
926 static void opj_idwt53_v(const opj_dwt_t *dwt,
927                          OPJ_INT32* tiledp_col,
928                          OPJ_INT32 stride,
929                          OPJ_INT32 nb_cols)
930 {
931 #ifdef STANDARD_SLOW_VERSION
932     /* For documentation purpose */
933     OPJ_INT32 k, c;
934     for (c = 0; c < nb_cols; c ++) {
935         opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
936         opj_dwt_decode_1(dwt);
937         for (k = 0; k < dwt->sn + dwt->dn; ++k) {
938             tiledp_col[c + k * stride] = dwt->mem[k];
939         }
940     }
941 #else
942     const OPJ_INT32 sn = dwt->sn;
943     const OPJ_INT32 len = sn + dwt->dn;
944     if (dwt->cas == 0) {
945         /* If len == 1, unmodified value */
946
947 #if (defined(__SSE2__) || defined(__AVX2__))
948         if (len > 1 && nb_cols == PARALLEL_COLS_53) {
949             /* Same as below general case, except that thanks to SSE2/AVX2 */
950             /* we can efficently process 8/16 columns in parallel */
951             opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
952             return;
953         }
954 #endif
955         if (len > 1) {
956             OPJ_INT32 c;
957             for (c = 0; c < nb_cols; c++, tiledp_col++) {
958                 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
959             }
960             return;
961         }
962     } else {
963         if (len == 1) {
964             OPJ_INT32 c;
965             for (c = 0; c < nb_cols; c++, tiledp_col++) {
966                 tiledp_col[0] /= 2;
967             }
968             return;
969         }
970
971         if (len == 2) {
972             OPJ_INT32 c;
973             OPJ_INT32* out = dwt->mem;
974             for (c = 0; c < nb_cols; c++, tiledp_col++) {
975                 OPJ_INT32 i;
976                 const OPJ_INT32* in_even = &tiledp_col[sn * stride];
977                 const OPJ_INT32* in_odd = &tiledp_col[0];
978
979                 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
980                 out[0] = in_even[0] + out[1];
981
982                 for (i = 0; i < len; ++i) {
983                     tiledp_col[i * stride] = out[i];
984                 }
985             }
986
987             return;
988         }
989
990 #if (defined(__SSE2__) || defined(__AVX2__))
991         if (len > 2 && nb_cols == PARALLEL_COLS_53) {
992             /* Same as below general case, except that thanks to SSE2/AVX2 */
993             /* we can efficently process 8/16 columns in parallel */
994             opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
995             return;
996         }
997 #endif
998         if (len > 2) {
999             OPJ_INT32 c;
1000             for (c = 0; c < nb_cols; c++, tiledp_col++) {
1001                 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1002             }
1003             return;
1004         }
1005     }
1006 #endif
1007 }
1008
1009
1010 /* <summary>                             */
1011 /* Forward 9-7 wavelet transform in 1-D. */
1012 /* </summary>                            */
1013 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1014                                   OPJ_INT32 cas)
1015 {
1016     OPJ_INT32 i;
1017     if (!cas) {
1018         if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
1019             for (i = 0; i < dn; i++) {
1020                 OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
1021             }
1022             for (i = 0; i < sn; i++) {
1023                 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
1024             }
1025             for (i = 0; i < dn; i++) {
1026                 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
1027             }
1028             for (i = 0; i < sn; i++) {
1029                 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
1030             }
1031             for (i = 0; i < dn; i++) {
1032                 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038);    /*5038 */
1033             }
1034             for (i = 0; i < sn; i++) {
1035                 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659);    /*6660 */
1036             }
1037         }
1038     } else {
1039         if ((sn > 0) || (dn > 1)) { /* NEW :  CASE ONE ELEMENT */
1040             for (i = 0; i < dn; i++) {
1041                 OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
1042             }
1043             for (i = 0; i < sn; i++) {
1044                 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
1045             }
1046             for (i = 0; i < dn; i++) {
1047                 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
1048             }
1049             for (i = 0; i < sn; i++) {
1050                 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
1051             }
1052             for (i = 0; i < dn; i++) {
1053                 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038);    /*5038 */
1054             }
1055             for (i = 0; i < sn; i++) {
1056                 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659);    /*6660 */
1057             }
1058         }
1059     }
1060 }
1061
1062 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1063                                     opj_stepsize_t *bandno_stepsize)
1064 {
1065     OPJ_INT32 p, n;
1066     p = opj_int_floorlog2(stepsize) - 13;
1067     n = 11 - opj_int_floorlog2(stepsize);
1068     bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1069     bandno_stepsize->expn = numbps - p;
1070 }
1071
1072 /*
1073 ==========================================================
1074    DWT interface
1075 ==========================================================
1076 */
1077
1078
1079 /* <summary>                            */
1080 /* Forward 5-3 wavelet transform in 2-D. */
1081 /* </summary>                           */
1082 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
1083         void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32))
1084 {
1085     OPJ_INT32 i, j, k;
1086     OPJ_INT32 *a = 00;
1087     OPJ_INT32 *aj = 00;
1088     OPJ_INT32 *bj = 00;
1089     OPJ_INT32 w, l;
1090
1091     OPJ_INT32 rw;           /* width of the resolution level computed   */
1092     OPJ_INT32 rh;           /* height of the resolution level computed  */
1093     size_t l_data_size;
1094
1095     opj_tcd_resolution_t * l_cur_res = 0;
1096     opj_tcd_resolution_t * l_last_res = 0;
1097
1098     w = tilec->x1 - tilec->x0;
1099     l = (OPJ_INT32)tilec->numresolutions - 1;
1100     a = tilec->data;
1101
1102     l_cur_res = tilec->resolutions + l;
1103     l_last_res = l_cur_res - 1;
1104
1105     l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1106     /* overflow check */
1107     if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1108         /* FIXME event manager error callback */
1109         return OPJ_FALSE;
1110     }
1111     l_data_size *= sizeof(OPJ_INT32);
1112     bj = (OPJ_INT32*)opj_malloc(l_data_size);
1113     /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1114     /* in that case, so do not error out */
1115     if (l_data_size != 0 && ! bj) {
1116         return OPJ_FALSE;
1117     }
1118     i = l;
1119
1120     while (i--) {
1121         OPJ_INT32 rw1;      /* width of the resolution level once lower than computed one                                       */
1122         OPJ_INT32 rh1;      /* height of the resolution level once lower than computed one                                      */
1123         OPJ_INT32 cas_col;  /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1124         OPJ_INT32 cas_row;  /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
1125         OPJ_INT32 dn, sn;
1126
1127         rw  = l_cur_res->x1 - l_cur_res->x0;
1128         rh  = l_cur_res->y1 - l_cur_res->y0;
1129         rw1 = l_last_res->x1 - l_last_res->x0;
1130         rh1 = l_last_res->y1 - l_last_res->y0;
1131
1132         cas_row = l_cur_res->x0 & 1;
1133         cas_col = l_cur_res->y0 & 1;
1134
1135         sn = rh1;
1136         dn = rh - rh1;
1137         for (j = 0; j < rw; ++j) {
1138             aj = a + j;
1139             for (k = 0; k < rh; ++k) {
1140                 bj[k] = aj[k * w];
1141             }
1142
1143             (*p_function)(bj, dn, sn, cas_col);
1144
1145             opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1146         }
1147
1148         sn = rw1;
1149         dn = rw - rw1;
1150
1151         for (j = 0; j < rh; j++) {
1152             aj = a + j * w;
1153             for (k = 0; k < rw; k++) {
1154                 bj[k] = aj[k];
1155             }
1156             (*p_function)(bj, dn, sn, cas_row);
1157             opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
1158         }
1159
1160         l_cur_res = l_last_res;
1161
1162         --l_last_res;
1163     }
1164
1165     opj_free(bj);
1166     return OPJ_TRUE;
1167 }
1168
1169 /* Forward 5-3 wavelet transform in 2-D. */
1170 /* </summary>                           */
1171 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
1172 {
1173     return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1);
1174 }
1175
1176 /* <summary>                            */
1177 /* Inverse 5-3 wavelet transform in 2-D. */
1178 /* </summary>                           */
1179 OPJ_BOOL opj_dwt_decode(opj_thread_pool_t* tp, opj_tcd_tilecomp_t* tilec,
1180                         OPJ_UINT32 numres)
1181 {
1182     return opj_dwt_decode_tile(tp, tilec, numres);
1183 }
1184
1185
1186 /* <summary>                          */
1187 /* Get gain of 5-3 wavelet transform. */
1188 /* </summary>                         */
1189 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient)
1190 {
1191     if (orient == 0) {
1192         return 0;
1193     }
1194     if (orient == 1 || orient == 2) {
1195         return 1;
1196     }
1197     return 2;
1198 }
1199
1200 /* <summary>                */
1201 /* Get norm of 5-3 wavelet. */
1202 /* </summary>               */
1203 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1204 {
1205     return opj_dwt_norms[orient][level];
1206 }
1207
1208 /* <summary>                             */
1209 /* Forward 9-7 wavelet transform in 2-D. */
1210 /* </summary>                            */
1211 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
1212 {
1213     return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1_real);
1214 }
1215
1216 /* <summary>                          */
1217 /* Get gain of 9-7 wavelet transform. */
1218 /* </summary>                         */
1219 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient)
1220 {
1221     (void)orient;
1222     return 0;
1223 }
1224
1225 /* <summary>                */
1226 /* Get norm of 9-7 wavelet. */
1227 /* </summary>               */
1228 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1229 {
1230     return opj_dwt_norms_real[orient][level];
1231 }
1232
1233 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1234 {
1235     OPJ_UINT32 numbands, bandno;
1236     numbands = 3 * tccp->numresolutions - 2;
1237     for (bandno = 0; bandno < numbands; bandno++) {
1238         OPJ_FLOAT64 stepsize;
1239         OPJ_UINT32 resno, level, orient, gain;
1240
1241         resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1242         orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1243         level = tccp->numresolutions - 1 - resno;
1244         gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1245                                           (orient == 2)) ? 1 : 2));
1246         if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1247             stepsize = 1.0;
1248         } else {
1249             OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1250             stepsize = (1 << (gain)) / norm;
1251         }
1252         opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1253                                 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1254     }
1255 }
1256
1257 /* <summary>                             */
1258 /* Determine maximum computed resolution level for inverse wavelet transform */
1259 /* </summary>                            */
1260 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1261         OPJ_UINT32 i)
1262 {
1263     OPJ_UINT32 mr   = 0;
1264     OPJ_UINT32 w;
1265     while (--i) {
1266         ++r;
1267         if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1268             mr = w ;
1269         }
1270         if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1271             mr = w ;
1272         }
1273     }
1274     return mr ;
1275 }
1276
1277 typedef struct {
1278     opj_dwt_t h;
1279     OPJ_UINT32 rw;
1280     OPJ_UINT32 w;
1281     OPJ_INT32 * OPJ_RESTRICT tiledp;
1282     OPJ_UINT32 min_j;
1283     OPJ_UINT32 max_j;
1284 } opj_dwd_decode_h_job_t;
1285
1286 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1287 {
1288     OPJ_UINT32 j;
1289     opj_dwd_decode_h_job_t* job;
1290     (void)tls;
1291
1292     job = (opj_dwd_decode_h_job_t*)user_data;
1293     for (j = job->min_j; j < job->max_j; j++) {
1294         opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1295     }
1296
1297     opj_aligned_free(job->h.mem);
1298     opj_free(job);
1299 }
1300
1301 typedef struct {
1302     opj_dwt_t v;
1303     OPJ_UINT32 rh;
1304     OPJ_UINT32 w;
1305     OPJ_INT32 * OPJ_RESTRICT tiledp;
1306     OPJ_UINT32 min_j;
1307     OPJ_UINT32 max_j;
1308 } opj_dwd_decode_v_job_t;
1309
1310 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1311 {
1312     OPJ_UINT32 j;
1313     opj_dwd_decode_v_job_t* job;
1314     (void)tls;
1315
1316     job = (opj_dwd_decode_v_job_t*)user_data;
1317     for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1318             j += PARALLEL_COLS_53) {
1319         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1320                      PARALLEL_COLS_53);
1321     }
1322     if (j < job->max_j)
1323         opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1324                      (OPJ_INT32)(job->max_j - j));
1325
1326     opj_aligned_free(job->v.mem);
1327     opj_free(job);
1328 }
1329
1330
1331 /* <summary>                            */
1332 /* Inverse wavelet transform in 2-D.    */
1333 /* </summary>                           */
1334 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1335                                     opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1336 {
1337     opj_dwt_t h;
1338     opj_dwt_t v;
1339
1340     opj_tcd_resolution_t* tr = tilec->resolutions;
1341
1342     OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1343                                  tr->x0);  /* width of the resolution level computed */
1344     OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1345                                  tr->y0);  /* height of the resolution level computed */
1346
1347     OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1348     size_t h_mem_size;
1349     int num_threads;
1350
1351     if (numres == 1U) {
1352         return OPJ_TRUE;
1353     }
1354     num_threads = opj_thread_pool_get_thread_count(tp);
1355     h_mem_size = opj_dwt_max_resolution(tr, numres);
1356     /* overflow check */
1357     if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1358         /* FIXME event manager error callback */
1359         return OPJ_FALSE;
1360     }
1361     /* We need PARALLEL_COLS_53 times the height of the array, */
1362     /* since for the vertical pass */
1363     /* we process PARALLEL_COLS_53 columns at a time */
1364     h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1365     h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1366     if (! h.mem) {
1367         /* FIXME event manager error callback */
1368         return OPJ_FALSE;
1369     }
1370
1371     v.mem = h.mem;
1372
1373     while (--numres) {
1374         OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1375         OPJ_UINT32 j;
1376
1377         ++tr;
1378         h.sn = (OPJ_INT32)rw;
1379         v.sn = (OPJ_INT32)rh;
1380
1381         rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1382         rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1383
1384         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1385         h.cas = tr->x0 % 2;
1386
1387         if (num_threads <= 1 || rh <= 1) {
1388             for (j = 0; j < rh; ++j) {
1389                 opj_idwt53_h(&h, &tiledp[j * w]);
1390             }
1391         } else {
1392             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1393             OPJ_UINT32 step_j;
1394
1395             if (rh < num_jobs) {
1396                 num_jobs = rh;
1397             }
1398             step_j = (rh / num_jobs);
1399
1400             for (j = 0; j < num_jobs; j++) {
1401                 opj_dwd_decode_h_job_t* job;
1402
1403                 job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
1404                 if (!job) {
1405                     /* It would be nice to fallback to single thread case, but */
1406                     /* unfortunately some jobs may be launched and have modified */
1407                     /* tiledp, so it is not practical to recover from that error */
1408                     /* FIXME event manager error callback */
1409                     opj_thread_pool_wait_completion(tp, 0);
1410                     opj_aligned_free(h.mem);
1411                     return OPJ_FALSE;
1412                 }
1413                 job->h = h;
1414                 job->rw = rw;
1415                 job->w = w;
1416                 job->tiledp = tiledp;
1417                 job->min_j = j * step_j;
1418                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1419                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1420                     job->max_j = rh;
1421                 }
1422                 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1423                 if (!job->h.mem) {
1424                     /* FIXME event manager error callback */
1425                     opj_thread_pool_wait_completion(tp, 0);
1426                     opj_free(job);
1427                     opj_aligned_free(h.mem);
1428                     return OPJ_FALSE;
1429                 }
1430                 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1431             }
1432             opj_thread_pool_wait_completion(tp, 0);
1433         }
1434
1435         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1436         v.cas = tr->y0 % 2;
1437
1438         if (num_threads <= 1 || rw <= 1) {
1439             for (j = 0; j + PARALLEL_COLS_53 <= rw;
1440                     j += PARALLEL_COLS_53) {
1441                 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, PARALLEL_COLS_53);
1442             }
1443             if (j < rw) {
1444                 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, (OPJ_INT32)(rw - j));
1445             }
1446         } else {
1447             OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1448             OPJ_UINT32 step_j;
1449
1450             if (rw < num_jobs) {
1451                 num_jobs = rw;
1452             }
1453             step_j = (rw / num_jobs);
1454
1455             for (j = 0; j < num_jobs; j++) {
1456                 opj_dwd_decode_v_job_t* job;
1457
1458                 job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
1459                 if (!job) {
1460                     /* It would be nice to fallback to single thread case, but */
1461                     /* unfortunately some jobs may be launched and have modified */
1462                     /* tiledp, so it is not practical to recover from that error */
1463                     /* FIXME event manager error callback */
1464                     opj_thread_pool_wait_completion(tp, 0);
1465                     opj_aligned_free(v.mem);
1466                     return OPJ_FALSE;
1467                 }
1468                 job->v = v;
1469                 job->rh = rh;
1470                 job->w = w;
1471                 job->tiledp = tiledp;
1472                 job->min_j = j * step_j;
1473                 job->max_j = (j + 1U) * step_j; /* this can overflow */
1474                 if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
1475                     job->max_j = rw;
1476                 }
1477                 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1478                 if (!job->v.mem) {
1479                     /* FIXME event manager error callback */
1480                     opj_thread_pool_wait_completion(tp, 0);
1481                     opj_free(job);
1482                     opj_aligned_free(v.mem);
1483                     return OPJ_FALSE;
1484                 }
1485                 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1486             }
1487             opj_thread_pool_wait_completion(tp, 0);
1488         }
1489     }
1490     opj_aligned_free(h.mem);
1491     return OPJ_TRUE;
1492 }
1493
1494 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT w,
1495                                    OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 size)
1496 {
1497     OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(w->wavelet + w->cas);
1498     OPJ_INT32 count = w->sn;
1499     OPJ_INT32 i, k;
1500
1501     for (k = 0; k < 2; ++k) {
1502         if (count + 3 * x < size && ((size_t) a & 0x0f) == 0 &&
1503                 ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0) {
1504             /* Fast code path */
1505             for (i = 0; i < count; ++i) {
1506                 OPJ_INT32 j = i;
1507                 bi[i * 8    ] = a[j];
1508                 j += x;
1509                 bi[i * 8 + 1] = a[j];
1510                 j += x;
1511                 bi[i * 8 + 2] = a[j];
1512                 j += x;
1513                 bi[i * 8 + 3] = a[j];
1514             }
1515         } else {
1516             /* Slow code path */
1517             for (i = 0; i < count; ++i) {
1518                 OPJ_INT32 j = i;
1519                 bi[i * 8    ] = a[j];
1520                 j += x;
1521                 if (j >= size) {
1522                     continue;
1523                 }
1524                 bi[i * 8 + 1] = a[j];
1525                 j += x;
1526                 if (j >= size) {
1527                     continue;
1528                 }
1529                 bi[i * 8 + 2] = a[j];
1530                 j += x;
1531                 if (j >= size) {
1532                     continue;
1533                 }
1534                 bi[i * 8 + 3] = a[j]; /* This one*/
1535             }
1536         }
1537
1538         bi = (OPJ_FLOAT32*)(w->wavelet + 1 - w->cas);
1539         a += w->sn;
1540         size -= w->sn;
1541         count = w->dn;
1542     }
1543 }
1544
1545 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT v,
1546                                    OPJ_FLOAT32* OPJ_RESTRICT a, OPJ_INT32 x, OPJ_INT32 nb_elts_read)
1547 {
1548     opj_v4_t* OPJ_RESTRICT bi = v->wavelet + v->cas;
1549     OPJ_INT32 i;
1550
1551     for (i = 0; i < v->sn; ++i) {
1552         memcpy(&bi[i * 2], &a[i * x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
1553     }
1554
1555     a += v->sn * x;
1556     bi = v->wavelet + 1 - v->cas;
1557
1558     for (i = 0; i < v->dn; ++i) {
1559         memcpy(&bi[i * 2], &a[i * x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
1560     }
1561 }
1562
1563 #ifdef __SSE__
1564
1565 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count,
1566                                        const __m128 c)
1567 {
1568     __m128* OPJ_RESTRICT vw = (__m128*) w;
1569     OPJ_INT32 i;
1570     /* 4x unrolled loop */
1571     for (i = 0; i < count >> 2; ++i) {
1572         *vw = _mm_mul_ps(*vw, c);
1573         vw += 2;
1574         *vw = _mm_mul_ps(*vw, c);
1575         vw += 2;
1576         *vw = _mm_mul_ps(*vw, c);
1577         vw += 2;
1578         *vw = _mm_mul_ps(*vw, c);
1579         vw += 2;
1580     }
1581     count &= 3;
1582     for (i = 0; i < count; ++i) {
1583         *vw = _mm_mul_ps(*vw, c);
1584         vw += 2;
1585     }
1586 }
1587
1588 void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k,
1589                                 OPJ_INT32 m, __m128 c)
1590 {
1591     __m128* OPJ_RESTRICT vl = (__m128*) l;
1592     __m128* OPJ_RESTRICT vw = (__m128*) w;
1593     OPJ_INT32 i;
1594     __m128 tmp1, tmp2, tmp3;
1595     tmp1 = vl[0];
1596     for (i = 0; i < m; ++i) {
1597         tmp2 = vw[-1];
1598         tmp3 = vw[ 0];
1599         vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
1600         tmp1 = tmp3;
1601         vw += 2;
1602     }
1603     vl = vw - 2;
1604     if (m >= k) {
1605         return;
1606     }
1607     c = _mm_add_ps(c, c);
1608     c = _mm_mul_ps(c, vl[0]);
1609     for (; m < k; ++m) {
1610         __m128 tmp = vw[-1];
1611         vw[-1] = _mm_add_ps(tmp, c);
1612         vw += 2;
1613     }
1614 }
1615
1616 #else
1617
1618 static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count,
1619                                    const OPJ_FLOAT32 c)
1620 {
1621     OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
1622     OPJ_INT32 i;
1623     for (i = 0; i < count; ++i) {
1624         OPJ_FLOAT32 tmp1 = fw[i * 8    ];
1625         OPJ_FLOAT32 tmp2 = fw[i * 8 + 1];
1626         OPJ_FLOAT32 tmp3 = fw[i * 8 + 2];
1627         OPJ_FLOAT32 tmp4 = fw[i * 8 + 3];
1628         fw[i * 8    ] = tmp1 * c;
1629         fw[i * 8 + 1] = tmp2 * c;
1630         fw[i * 8 + 2] = tmp3 * c;
1631         fw[i * 8 + 3] = tmp4 * c;
1632     }
1633 }
1634
1635 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k,
1636                                    OPJ_INT32 m, OPJ_FLOAT32 c)
1637 {
1638     OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
1639     OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
1640     OPJ_INT32 i;
1641     for (i = 0; i < m; ++i) {
1642         OPJ_FLOAT32 tmp1_1 = fl[0];
1643         OPJ_FLOAT32 tmp1_2 = fl[1];
1644         OPJ_FLOAT32 tmp1_3 = fl[2];
1645         OPJ_FLOAT32 tmp1_4 = fl[3];
1646         OPJ_FLOAT32 tmp2_1 = fw[-4];
1647         OPJ_FLOAT32 tmp2_2 = fw[-3];
1648         OPJ_FLOAT32 tmp2_3 = fw[-2];
1649         OPJ_FLOAT32 tmp2_4 = fw[-1];
1650         OPJ_FLOAT32 tmp3_1 = fw[0];
1651         OPJ_FLOAT32 tmp3_2 = fw[1];
1652         OPJ_FLOAT32 tmp3_3 = fw[2];
1653         OPJ_FLOAT32 tmp3_4 = fw[3];
1654         fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
1655         fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
1656         fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
1657         fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
1658         fl = fw;
1659         fw += 8;
1660     }
1661     if (m < k) {
1662         OPJ_FLOAT32 c1;
1663         OPJ_FLOAT32 c2;
1664         OPJ_FLOAT32 c3;
1665         OPJ_FLOAT32 c4;
1666         c += c;
1667         c1 = fl[0] * c;
1668         c2 = fl[1] * c;
1669         c3 = fl[2] * c;
1670         c4 = fl[3] * c;
1671         for (; m < k; ++m) {
1672             OPJ_FLOAT32 tmp1 = fw[-4];
1673             OPJ_FLOAT32 tmp2 = fw[-3];
1674             OPJ_FLOAT32 tmp3 = fw[-2];
1675             OPJ_FLOAT32 tmp4 = fw[-1];
1676             fw[-4] = tmp1 + c1;
1677             fw[-3] = tmp2 + c2;
1678             fw[-2] = tmp3 + c3;
1679             fw[-1] = tmp4 + c4;
1680             fw += 8;
1681         }
1682     }
1683 }
1684
1685 #endif
1686
1687 /* <summary>                             */
1688 /* Inverse 9-7 wavelet transform in 1-D. */
1689 /* </summary>                            */
1690 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
1691 {
1692     OPJ_INT32 a, b;
1693     if (dwt->cas == 0) {
1694         if (!((dwt->dn > 0) || (dwt->sn > 1))) {
1695             return;
1696         }
1697         a = 0;
1698         b = 1;
1699     } else {
1700         if (!((dwt->sn > 0) || (dwt->dn > 1))) {
1701             return;
1702         }
1703         a = 1;
1704         b = 0;
1705     }
1706 #ifdef __SSE__
1707     opj_v4dwt_decode_step1_sse(dwt->wavelet + a, dwt->sn, _mm_set1_ps(opj_K));
1708     opj_v4dwt_decode_step1_sse(dwt->wavelet + b, dwt->dn, _mm_set1_ps(opj_c13318));
1709     opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1, dwt->sn,
1710                                opj_int_min(dwt->sn, dwt->dn - a), _mm_set1_ps(opj_dwt_delta));
1711     opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1, dwt->dn,
1712                                opj_int_min(dwt->dn, dwt->sn - b), _mm_set1_ps(opj_dwt_gamma));
1713     opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1, dwt->sn,
1714                                opj_int_min(dwt->sn, dwt->dn - a), _mm_set1_ps(opj_dwt_beta));
1715     opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1, dwt->dn,
1716                                opj_int_min(dwt->dn, dwt->sn - b), _mm_set1_ps(opj_dwt_alpha));
1717 #else
1718     opj_v4dwt_decode_step1(dwt->wavelet + a, dwt->sn, opj_K);
1719     opj_v4dwt_decode_step1(dwt->wavelet + b, dwt->dn, opj_c13318);
1720     opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1, dwt->sn,
1721                            opj_int_min(dwt->sn, dwt->dn - a), opj_dwt_delta);
1722     opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1, dwt->dn,
1723                            opj_int_min(dwt->dn, dwt->sn - b), opj_dwt_gamma);
1724     opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1, dwt->sn,
1725                            opj_int_min(dwt->sn, dwt->dn - a), opj_dwt_beta);
1726     opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1, dwt->dn,
1727                            opj_int_min(dwt->dn, dwt->sn - b), opj_dwt_alpha);
1728 #endif
1729 }
1730
1731
1732 /* <summary>                             */
1733 /* Inverse 9-7 wavelet transform in 2-D. */
1734 /* </summary>                            */
1735 OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
1736                              OPJ_UINT32 numres)
1737 {
1738     opj_v4dwt_t h;
1739     opj_v4dwt_t v;
1740
1741     opj_tcd_resolution_t* res = tilec->resolutions;
1742
1743     OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
1744                                  res->x0);    /* width of the resolution level computed */
1745     OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
1746                                  res->y0);    /* height of the resolution level computed */
1747
1748     OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1749
1750     size_t l_data_size;
1751
1752     l_data_size = opj_dwt_max_resolution(res, numres);
1753     /* overflow check */
1754     if (l_data_size > (SIZE_MAX - 5U)) {
1755         /* FIXME event manager error callback */
1756         return OPJ_FALSE;
1757     }
1758     l_data_size += 5U;
1759     /* overflow check */
1760     if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
1761         /* FIXME event manager error callback */
1762         return OPJ_FALSE;
1763     }
1764     h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
1765     if (!h.wavelet) {
1766         /* FIXME event manager error callback */
1767         return OPJ_FALSE;
1768     }
1769     v.wavelet = h.wavelet;
1770
1771     while (--numres) {
1772         OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
1773         OPJ_UINT32 bufsize = (OPJ_UINT32)((tilec->x1 - tilec->x0) *
1774                                           (tilec->y1 - tilec->y0));
1775         OPJ_INT32 j;
1776
1777         h.sn = (OPJ_INT32)rw;
1778         v.sn = (OPJ_INT32)rh;
1779
1780         ++res;
1781
1782         rw = (OPJ_UINT32)(res->x1 -
1783                           res->x0);   /* width of the resolution level computed */
1784         rh = (OPJ_UINT32)(res->y1 -
1785                           res->y0);   /* height of the resolution level computed */
1786
1787         h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1788         h.cas = res->x0 % 2;
1789
1790         for (j = (OPJ_INT32)rh; j > 3; j -= 4) {
1791             OPJ_INT32 k;
1792             opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
1793             opj_v4dwt_decode(&h);
1794
1795             for (k = (OPJ_INT32)rw; --k >= 0;) {
1796                 aj[k               ] = h.wavelet[k].f[0];
1797                 aj[k + (OPJ_INT32)w  ] = h.wavelet[k].f[1];
1798                 aj[k + (OPJ_INT32)w * 2] = h.wavelet[k].f[2];
1799                 aj[k + (OPJ_INT32)w * 3] = h.wavelet[k].f[3];
1800             }
1801
1802             aj += w * 4;
1803             bufsize -= w * 4;
1804         }
1805
1806         if (rh & 0x03) {
1807             OPJ_INT32 k;
1808             j = rh & 0x03;
1809             opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
1810             opj_v4dwt_decode(&h);
1811             for (k = (OPJ_INT32)rw; --k >= 0;) {
1812                 switch (j) {
1813                 case 3:
1814                     aj[k + (OPJ_INT32)w * 2] = h.wavelet[k].f[2];
1815                 /* FALLTHRU */
1816                 case 2:
1817                     aj[k + (OPJ_INT32)w  ] = h.wavelet[k].f[1];
1818                 /* FALLTHRU */
1819                 case 1:
1820                     aj[k               ] = h.wavelet[k].f[0];
1821                 }
1822             }
1823         }
1824
1825         v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1826         v.cas = res->y0 % 2;
1827
1828         aj = (OPJ_FLOAT32*) tilec->data;
1829         for (j = (OPJ_INT32)rw; j > 3; j -= 4) {
1830             OPJ_UINT32 k;
1831
1832             opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, 4);
1833             opj_v4dwt_decode(&v);
1834
1835             for (k = 0; k < rh; ++k) {
1836                 memcpy(&aj[k * w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
1837             }
1838             aj += 4;
1839         }
1840
1841         if (rw & 0x03) {
1842             OPJ_UINT32 k;
1843
1844             j = rw & 0x03;
1845
1846             opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, j);
1847             opj_v4dwt_decode(&v);
1848
1849             for (k = 0; k < rh; ++k) {
1850                 memcpy(&aj[k * w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
1851             }
1852         }
1853     }
1854
1855     opj_aligned_free(h.wavelet);
1856     return OPJ_TRUE;
1857 }