2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
15 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
16 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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.
43 #define OPJ_SKIP_POISON
44 #include "opj_includes.h"
47 #include <xmmintrin.h>
50 #include <emmintrin.h>
53 #include <tmmintrin.h>
56 #include <immintrin.h>
60 #pragma GCC poison malloc calloc realloc free
63 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
66 #define OPJ_WS(i) v->mem[(i)*2]
67 #define OPJ_WD(i) v->mem[(1+(i)*2)]
70 /** Number of int32 values in a AVX2 register */
71 #define VREG_INT_COUNT 8
73 /** Number of int32 values in a SSE2 register */
74 #define VREG_INT_COUNT 4
77 /** Number of columns that we can process in parallel in the vertical pass */
78 #define PARALLEL_COLS_53 (2*VREG_INT_COUNT)
80 /** @name Local data structures */
83 typedef struct dwt_local {
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 */
94 typedef struct v4dwt_local {
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 */
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 */
110 static const OPJ_FLOAT32 opj_K = 1.230174105f; /* 10078 */
111 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
116 Virtual function type for wavelet transform in 1-D
118 typedef void (*DWT1DFN)(const opj_dwt_t* v);
120 /** @name Local static functions */
124 Forward lazy transform (horizontal)
126 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
127 OPJ_INT32 sn, OPJ_INT32 cas);
129 Forward lazy transform (vertical)
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);
134 Forward 5-3 wavelet transform in 1-D
136 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
139 Forward 9-7 wavelet transform in 1-D
141 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
144 Explicit calculation of the Quantization Stepsizes
146 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
147 opj_stepsize_t *bandno_stepsize);
149 Inverse wavelet transform in 2-D.
151 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
152 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
154 static OPJ_BOOL opj_dwt_decode_partial_tile(opj_tcd_t *p_tcd,
155 opj_tcd_tilecomp_t* tilec,
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));
161 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
165 /* Inverse 9-7 wavelet transform in 1-D. */
167 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt);
169 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT dwt,
170 OPJ_FLOAT32* OPJ_RESTRICT a,
172 OPJ_UINT32 remaining_height);
174 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
175 OPJ_FLOAT32* OPJ_RESTRICT a,
177 OPJ_UINT32 nb_elts_read);
180 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w,
185 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w,
188 OPJ_UINT32 m, __m128 c);
191 static void opj_v4dwt_decode_step1(opj_v4_t* w,
194 const OPJ_FLOAT32 c);
196 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
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)))
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)))
217 /* This table contains the norms of the 5-3 wavelets for different bands. */
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}
227 /* This table contains the norms of the 9-7 wavelets for different bands. */
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}
237 ==========================================================
239 ==========================================================
243 /* Forward lazy transform (horizontal). */
245 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn,
246 OPJ_INT32 sn, OPJ_INT32 cas)
249 OPJ_INT32 * l_dest = b;
250 OPJ_INT32 * l_src = a + cas;
252 for (i = 0; i < sn; ++i) {
260 for (i = 0; i < dn; ++i) {
267 /* Forward lazy transform (vertical). */
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)
273 OPJ_INT32 * l_dest = b;
274 OPJ_INT32 * l_src = a + cas;
280 } /* b[i*x]=a[2*i+cas]; */
290 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
293 #ifdef STANDARD_SLOW_VERSION
295 /* Inverse lazy transform (horizontal). */
297 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
300 OPJ_INT32 *bi = h->mem + h->cas;
307 bi = h->mem + 1 - h->cas;
316 /* Inverse lazy transform (vertical). */
318 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
321 OPJ_INT32 *bi = v->mem + v->cas;
328 ai = a + (v->sn * x);
329 bi = v->mem + 1 - v->cas;
338 #endif /* STANDARD_SLOW_VERSION */
341 /* Forward 5-3 wavelet transform in 1-D. */
343 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
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;
353 for (i = 0; i < sn; i++) {
354 OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
358 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
361 for (i = 0; i < dn; i++) {
362 OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
364 for (i = 0; i < sn; i++) {
365 OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
371 #ifdef STANDARD_SLOW_VERSION
373 /* Inverse 5-3 wavelet transform in 1-D. */
375 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
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;
385 for (i = 0; i < dn; i++) {
386 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
390 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
393 for (i = 0; i < sn; i++) {
394 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
396 for (i = 0; i < dn; i++) {
397 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
403 static void opj_dwt_decode_1(const opj_dwt_t *v)
405 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
408 #endif /* STANDARD_SLOW_VERSION */
410 #if !defined(STANDARD_SLOW_VERSION)
411 static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
417 const OPJ_INT32* in_even = &tiledp[0];
418 const OPJ_INT32* in_odd = &tiledp[sn];
420 #ifdef TWO_PASS_VERSION
421 /* For documentation purpose: performs lifting in two iterations, */
422 /* but without explicit interleaving */
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);
431 if (len & 1) { /* if len is odd */
432 tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
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);
439 if (!(len & 1)) { /* if len is even */
440 tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
443 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
447 /* Improved version of the TWO_PASS_VERSION: */
448 /* Performs lifting in one single iteration. Saves memory */
449 /* accesses and explicit interleaving. */
452 s0n = s1n - ((d1n + 1) >> 1);
454 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
461 s0n = s1n - ((d1c + d1n + 2) >> 2);
464 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
470 tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
471 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
473 tmp[len - 1] = d1n + s0n;
476 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
479 static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
485 const OPJ_INT32* in_even = &tiledp[sn];
486 const OPJ_INT32* in_odd = &tiledp[0];
488 #ifdef TWO_PASS_VERSION
489 /* For documentation purpose: performs lifting in two iterations, */
490 /* but without explicit interleaving */
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);
499 tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
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);
508 tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
511 OPJ_INT32 s1, s2, dc, dn;
515 /* Improved version of the TWO_PASS_VERSION: */
516 /* Performs lifting in one single iteration. Saves memory */
517 /* accesses and explicit interleaving. */
520 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
521 tmp[0] = in_even[0] + dc;
523 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
527 dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
529 tmp[i + 1] = s1 + ((dn + dc) >> 1);
538 dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
539 tmp[len - 2] = s1 + ((dn + dc) >> 1);
542 tmp[len - 1] = s1 + dc;
545 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
549 #endif /* !defined(STANDARD_SLOW_VERSION) */
552 /* Inverse 5-3 wavelet transform in 1-D for one row. */
554 /* Performs interleave, inverse wavelet transform and copy back to buffer */
555 static void opj_idwt53_h(const opj_dwt_t *dwt,
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));
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 */
568 opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
570 /* Unmodified value */
572 } else { /* Left-most sample is on odd coordinate */
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);
589 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
591 /* Conveniency macros to improve the readabilty of the formulas */
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))
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))
613 #define ADD3(x,y,z) ADD(ADD(x,y),z)
616 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
617 const OPJ_INT32* tmp,
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]));
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(
641 OPJ_INT32* tiledp_col,
642 const OPJ_INT32 stride)
644 const OPJ_INT32* in_even = &tiledp_col[0];
645 const OPJ_INT32* in_odd = &tiledp_col[sn * stride];
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);
654 assert(PARALLEL_COLS_53 == 16);
655 assert(VREG_INT_COUNT == 8);
657 assert(PARALLEL_COLS_53 == 8);
658 assert(VREG_INT_COUNT == 4);
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);
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);
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));
676 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
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);
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));
691 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
692 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
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)));
701 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
702 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_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)));
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,
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)));
725 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
727 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
731 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
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(
741 OPJ_INT32* tiledp_col,
742 const OPJ_INT32 stride)
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);
750 const OPJ_INT32* in_even = &tiledp_col[sn * stride];
751 const OPJ_INT32* in_odd = &tiledp_col[0];
755 assert(PARALLEL_COLS_53 == 16);
756 assert(VREG_INT_COUNT == 8);
758 assert(PARALLEL_COLS_53 == 8);
759 assert(VREG_INT_COUNT == 4);
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);
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));
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));
780 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
782 s2_0 = LOADU(in_even + (j + 1) * stride);
783 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
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));
791 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
792 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
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)));
805 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
806 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_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));
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)));
821 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
822 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
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,
829 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
843 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
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,
851 OPJ_INT32* tiledp_col,
852 const OPJ_INT32 stride)
855 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
859 /* Performs lifting in one single iteration. Saves memory */
860 /* accesses and explicit interleaving. */
863 d1n = tiledp_col[sn * stride];
864 s0n = s1n - ((d1n + 1) >> 1);
866 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
870 s1n = tiledp_col[(j + 1) * stride];
871 d1n = tiledp_col[(sn + j + 1) * stride];
873 s0n = s1n - ((d1c + d1n + 2) >> 2);
876 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
883 tiledp_col[((len - 1) / 2) * stride] -
885 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
887 tmp[len - 1] = d1n + s0n;
890 for (i = 0; i < len; ++i) {
891 tiledp_col[i * stride] = tmp[i];
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,
901 OPJ_INT32* tiledp_col,
902 const OPJ_INT32 stride)
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];
911 /* Performs lifting in one single iteration. Saves memory */
912 /* accesses and explicit interleaving. */
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++) {
919 s2 = in_even[(j + 1) * stride];
921 dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2);
923 tmp[i + 1] = s1 + ((dn + dc) >> 1);
930 dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
931 tmp[len - 2] = s1 + ((dn + dc) >> 1);
934 tmp[len - 1] = s1 + dc;
937 for (i = 0; i < len; ++i) {
938 tiledp_col[i * stride] = tmp[i];
941 #endif /* !defined(STANDARD_SLOW_VERSION) */
944 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
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,
952 #ifdef STANDARD_SLOW_VERSION
953 /* For documentation purpose */
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];
963 const OPJ_INT32 sn = dwt->sn;
964 const OPJ_INT32 len = sn + dwt->dn;
966 /* If len == 1, unmodified value */
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);
978 for (c = 0; c < nb_cols; c++, tiledp_col++) {
979 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
986 for (c = 0; c < nb_cols; c++, tiledp_col++) {
994 OPJ_INT32* out = dwt->mem;
995 for (c = 0; c < nb_cols; c++, tiledp_col++) {
997 const OPJ_INT32* in_even = &tiledp_col[sn * stride];
998 const OPJ_INT32* in_odd = &tiledp_col[0];
1000 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
1001 out[0] = in_even[0] + out[1];
1003 for (i = 0; i < len; ++i) {
1004 tiledp_col[i * stride] = out[i];
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);
1021 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1022 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1032 /* Forward 9-7 wavelet transform in 1-D. */
1034 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
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);
1043 for (i = 0; i < sn; i++) {
1044 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
1046 for (i = 0; i < dn; i++) {
1047 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
1049 for (i = 0; i < sn; i++) {
1050 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
1052 for (i = 0; i < dn; i++) {
1053 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038); /*5038 */
1055 for (i = 0; i < sn; i++) {
1056 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659); /*6660 */
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);
1064 for (i = 0; i < sn; i++) {
1065 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
1067 for (i = 0; i < dn; i++) {
1068 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
1070 for (i = 0; i < sn; i++) {
1071 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
1073 for (i = 0; i < dn; i++) {
1074 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038); /*5038 */
1076 for (i = 0; i < sn; i++) {
1077 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659); /*6660 */
1083 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1084 opj_stepsize_t *bandno_stepsize)
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;
1094 ==========================================================
1096 ==========================================================
1101 /* Forward 5-3 wavelet transform in 2-D. */
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))
1112 OPJ_INT32 rw; /* width of the resolution level computed */
1113 OPJ_INT32 rh; /* height of the resolution level computed */
1116 opj_tcd_resolution_t * l_cur_res = 0;
1117 opj_tcd_resolution_t * l_last_res = 0;
1119 w = tilec->x1 - tilec->x0;
1120 l = (OPJ_INT32)tilec->numresolutions - 1;
1123 l_cur_res = tilec->resolutions + l;
1124 l_last_res = l_cur_res - 1;
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 */
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) {
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 */
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;
1153 cas_row = l_cur_res->x0 & 1;
1154 cas_col = l_cur_res->y0 & 1;
1158 for (j = 0; j < rw; ++j) {
1160 for (k = 0; k < rh; ++k) {
1164 (*p_function)(bj, dn, sn, cas_col);
1166 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1172 for (j = 0; j < rh; j++) {
1174 for (k = 0; k < rw; k++) {
1177 (*p_function)(bj, dn, sn, cas_row);
1178 opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
1181 l_cur_res = l_last_res;
1190 /* Forward 5-3 wavelet transform in 2-D. */
1192 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
1194 return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1);
1197 static OPJ_BOOL opj_dwt_is_whole_tile_decoding(opj_tcd_t *p_tcd,
1198 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1200 opj_image_comp_t* image_comp = &(p_tcd->image->comps[tilec->compno]);
1201 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1202 /* with the tile coordinates */
1203 OPJ_UINT32 tcx0 = opj_uint_max(
1204 (OPJ_UINT32)tilec->x0,
1205 opj_uint_ceildiv(p_tcd->decoded_x0, image_comp->dx));
1206 OPJ_UINT32 tcy0 = opj_uint_max(
1207 (OPJ_UINT32)tilec->y0,
1208 opj_uint_ceildiv(p_tcd->decoded_y0, image_comp->dy));
1209 OPJ_UINT32 tcx1 = opj_uint_min(
1210 (OPJ_UINT32)tilec->x1,
1211 opj_uint_ceildiv(p_tcd->decoded_x1, image_comp->dx));
1212 OPJ_UINT32 tcy1 = opj_uint_min(
1213 (OPJ_UINT32)tilec->y1,
1214 opj_uint_ceildiv(p_tcd->decoded_y1, image_comp->dy));
1216 OPJ_UINT32 shift = tilec->numresolutions - numres;
1218 /* Tolerate small margin within the reduced resolution factor to consider if */
1219 /* the whole tile path must be taken */
1220 return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
1221 ((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
1222 tcy0 >= (OPJ_UINT32)tilec->y0 &&
1223 ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
1224 tcx1 <= (OPJ_UINT32)tilec->x1 &&
1225 (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
1226 tcy1 <= (OPJ_UINT32)tilec->y1 &&
1227 (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0);
1231 /* Inverse 5-3 wavelet transform in 2-D. */
1233 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1236 if (opj_dwt_is_whole_tile_decoding(p_tcd, tilec, numres)) {
1237 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1239 return opj_dwt_decode_partial_tile(p_tcd, tilec, numres);
1245 /* Get gain of 5-3 wavelet transform. */
1247 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient)
1252 if (orient == 1 || orient == 2) {
1259 /* Get norm of 5-3 wavelet. */
1261 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1263 return opj_dwt_norms[orient][level];
1267 /* Forward 9-7 wavelet transform in 2-D. */
1269 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
1271 return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1_real);
1275 /* Get gain of 9-7 wavelet transform. */
1277 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient)
1284 /* Get norm of 9-7 wavelet. */
1286 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1288 return opj_dwt_norms_real[orient][level];
1291 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1293 OPJ_UINT32 numbands, bandno;
1294 numbands = 3 * tccp->numresolutions - 2;
1295 for (bandno = 0; bandno < numbands; bandno++) {
1296 OPJ_FLOAT64 stepsize;
1297 OPJ_UINT32 resno, level, orient, gain;
1299 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1300 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1301 level = tccp->numresolutions - 1 - resno;
1302 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1303 (orient == 2)) ? 1 : 2));
1304 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1307 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1308 stepsize = (1 << (gain)) / norm;
1310 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1311 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1316 /* Determine maximum computed resolution level for inverse wavelet transform */
1318 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1325 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1328 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1339 OPJ_INT32 * OPJ_RESTRICT tiledp;
1342 } opj_dwd_decode_h_job_t;
1344 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1347 opj_dwd_decode_h_job_t* job;
1350 job = (opj_dwd_decode_h_job_t*)user_data;
1351 for (j = job->min_j; j < job->max_j; j++) {
1352 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1355 opj_aligned_free(job->h.mem);
1363 OPJ_INT32 * OPJ_RESTRICT tiledp;
1366 } opj_dwd_decode_v_job_t;
1368 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1371 opj_dwd_decode_v_job_t* job;
1374 job = (opj_dwd_decode_v_job_t*)user_data;
1375 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1376 j += PARALLEL_COLS_53) {
1377 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1381 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_INT32)job->w,
1382 (OPJ_INT32)(job->max_j - j));
1384 opj_aligned_free(job->v.mem);
1390 /* Inverse wavelet transform in 2-D. */
1392 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1393 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1398 opj_tcd_resolution_t* tr = tilec->resolutions;
1400 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1401 tr->x0); /* width of the resolution level computed */
1402 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1403 tr->y0); /* height of the resolution level computed */
1405 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1412 num_threads = opj_thread_pool_get_thread_count(tp);
1413 h_mem_size = opj_dwt_max_resolution(tr, numres);
1414 /* overflow check */
1415 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1416 /* FIXME event manager error callback */
1419 /* We need PARALLEL_COLS_53 times the height of the array, */
1420 /* since for the vertical pass */
1421 /* we process PARALLEL_COLS_53 columns at a time */
1422 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1423 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1425 /* FIXME event manager error callback */
1432 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1436 h.sn = (OPJ_INT32)rw;
1437 v.sn = (OPJ_INT32)rh;
1439 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1440 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1442 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1445 if (num_threads <= 1 || rh <= 1) {
1446 for (j = 0; j < rh; ++j) {
1447 opj_idwt53_h(&h, &tiledp[j * w]);
1450 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1453 if (rh < num_jobs) {
1456 step_j = (rh / num_jobs);
1458 for (j = 0; j < num_jobs; j++) {
1459 opj_dwd_decode_h_job_t* job;
1461 job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
1463 /* It would be nice to fallback to single thread case, but */
1464 /* unfortunately some jobs may be launched and have modified */
1465 /* tiledp, so it is not practical to recover from that error */
1466 /* FIXME event manager error callback */
1467 opj_thread_pool_wait_completion(tp, 0);
1468 opj_aligned_free(h.mem);
1474 job->tiledp = tiledp;
1475 job->min_j = j * step_j;
1476 job->max_j = (j + 1U) * step_j; /* this can overflow */
1477 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1480 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1482 /* FIXME event manager error callback */
1483 opj_thread_pool_wait_completion(tp, 0);
1485 opj_aligned_free(h.mem);
1488 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1490 opj_thread_pool_wait_completion(tp, 0);
1493 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1496 if (num_threads <= 1 || rw <= 1) {
1497 for (j = 0; j + PARALLEL_COLS_53 <= rw;
1498 j += PARALLEL_COLS_53) {
1499 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, PARALLEL_COLS_53);
1502 opj_idwt53_v(&v, &tiledp[j], (OPJ_INT32)w, (OPJ_INT32)(rw - j));
1505 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1508 if (rw < num_jobs) {
1511 step_j = (rw / num_jobs);
1513 for (j = 0; j < num_jobs; j++) {
1514 opj_dwd_decode_v_job_t* job;
1516 job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
1518 /* It would be nice to fallback to single thread case, but */
1519 /* unfortunately some jobs may be launched and have modified */
1520 /* tiledp, so it is not practical to recover from that error */
1521 /* FIXME event manager error callback */
1522 opj_thread_pool_wait_completion(tp, 0);
1523 opj_aligned_free(v.mem);
1529 job->tiledp = tiledp;
1530 job->min_j = j * step_j;
1531 job->max_j = (j + 1U) * step_j; /* this can overflow */
1532 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1535 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1537 /* FIXME event manager error callback */
1538 opj_thread_pool_wait_completion(tp, 0);
1540 opj_aligned_free(v.mem);
1543 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1545 opj_thread_pool_wait_completion(tp, 0);
1548 opj_aligned_free(h.mem);
1552 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1554 const OPJ_INT32* src,
1561 const OPJ_INT32 *ai = src;
1562 OPJ_INT32 *bi = dest + cas;
1565 for (i = win_l_x0; i < win_l_x1; i++) {
1570 bi = dest + 1 - cas;
1571 for (i = win_h_x0; i < win_h_x1; i++) {
1576 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1578 const OPJ_INT32* src,
1586 const OPJ_INT32 *ai = src;
1587 OPJ_INT32 *bi = dest + cas;
1590 for (i = win_l_y0; i < win_l_y1; i++) {
1591 bi[2 * i] = ai[i * stride];
1594 ai = src + sn * stride;
1595 bi = dest + 1 - cas;
1596 for (i = win_h_y0; i < win_h_y1; i++) {
1597 bi[2 * i] = ai[i * stride];
1601 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1611 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1612 for (i = win_l_x0; i < win_l_x1; i++) {
1613 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1615 for (i = win_h_x0; i < win_h_x1; i++) {
1616 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1620 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
1623 for (i = win_l_x0; i < win_l_x1; i++) {
1624 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1626 for (i = win_h_x0; i < win_h_x1; i++) {
1627 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1633 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
1645 /* Compute number of decomposition for this band. See table F-1 */
1646 OPJ_UINT32 nb = (resno == 0) ?
1647 tilec->numresolutions - 1 :
1648 tilec->numresolutions - resno;
1649 /* Map above tile-based coordinates to sub-band-based coordinates per */
1650 /* equation B-15 of the standard */
1651 OPJ_UINT32 x0b = bandno & 1;
1652 OPJ_UINT32 y0b = bandno >> 1;
1654 *tbx0 = (nb == 0) ? tcx0 :
1655 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
1656 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
1659 *tby0 = (nb == 0) ? tcy0 :
1660 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
1661 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
1664 *tbx1 = (nb == 0) ? tcx1 :
1665 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
1666 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
1669 *tby1 = (nb == 0) ? tcy1 :
1670 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
1671 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
1675 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
1676 OPJ_UINT32 max_size,
1680 *start = opj_uint_subs(*start, filter_width);
1681 *end = opj_uint_adds(*end, filter_width);
1682 *end = opj_uint_min(*end, max_size);
1685 static OPJ_BOOL opj_dwt_decode_partial_tile(opj_tcd_t *tcd,
1686 opj_tcd_tilecomp_t* tilec,
1692 /* This value matches the maximum left/right extension given in tables */
1693 /* F.2 and F.3 of the standard. */
1694 const OPJ_UINT32 filter_width = 2U;
1696 opj_tcd_resolution_t* tr = tilec->resolutions;
1698 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1699 tr->x0); /* width of the resolution level computed */
1700 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1701 tr->y0); /* height of the resolution level computed */
1703 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1706 opj_image_comp_t* image_comp = &(tcd->image->comps[tilec->compno]);
1707 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1708 /* with the tile coordinates */
1709 OPJ_UINT32 win_tcx0 = opj_uint_max(
1710 (OPJ_UINT32)tilec->x0,
1711 opj_uint_ceildiv(tcd->decoded_x0, image_comp->dx));
1712 OPJ_UINT32 win_tcy0 = opj_uint_max(
1713 (OPJ_UINT32)tilec->y0,
1714 opj_uint_ceildiv(tcd->decoded_y0, image_comp->dy));
1715 OPJ_UINT32 win_tcx1 = opj_uint_min(
1716 (OPJ_UINT32)tilec->x1,
1717 opj_uint_ceildiv(tcd->decoded_x1, image_comp->dx));
1718 OPJ_UINT32 win_tcy1 = opj_uint_min(
1719 (OPJ_UINT32)tilec->y1,
1720 opj_uint_ceildiv(tcd->decoded_y1, image_comp->dy));
1725 h_mem_size = opj_dwt_max_resolution(tr, numres);
1726 /* overflow check */
1727 if (h_mem_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1728 /* FIXME event manager error callback */
1732 h_mem_size *= sizeof(OPJ_INT32);
1733 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1735 /* FIXME event manager error callback */
1741 for (resno = 1; --numres > 0; resno ++) {
1742 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1744 /* Window of interest subband-based coordinates */
1745 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
1746 OPJ_UINT32 win_hl_x0, win_hl_x1;
1747 OPJ_UINT32 win_lh_y0, win_lh_y1;
1748 /* Window of interest tile-resolution-based coordinates */
1749 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
1750 /* Tile-resolution subband-based coordinates */
1751 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
1755 h.sn = (OPJ_INT32)rw;
1756 v.sn = (OPJ_INT32)rh;
1758 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1759 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1761 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1764 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1767 /* Get the subband coordinates for the window of interest */
1769 opj_dwt_get_band_coordinates(tilec, resno, 0,
1770 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1771 &win_ll_x0, &win_ll_y0,
1772 &win_ll_x1, &win_ll_y1);
1775 opj_dwt_get_band_coordinates(tilec, resno, 1,
1776 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1777 &win_hl_x0, NULL, &win_hl_x1, NULL);
1780 opj_dwt_get_band_coordinates(tilec, resno, 2,
1781 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1782 NULL, &win_lh_y0, NULL, &win_lh_y1);
1784 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
1785 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
1786 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
1787 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
1788 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
1790 /* Substract the origin of the bands for this tile, to the subwindow */
1791 /* of interest band coordinates, so as to get them relative to the */
1793 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
1794 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
1795 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
1796 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
1797 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
1798 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
1799 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
1800 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
1802 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
1803 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
1805 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
1806 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
1808 /* Compute the tile-resolution-based coordinates for the window of interest */
1810 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
1811 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
1813 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
1814 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
1818 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
1819 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
1821 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
1822 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
1825 for (j = 0; j < rh; ++j) {
1826 if ((j >= win_ll_y0 && j < win_ll_y1) ||
1827 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
1828 memset(h.mem, 0, (OPJ_UINT32)(h.sn + h.dn) * sizeof(OPJ_INT32));
1829 opj_dwt_interleave_partial_h(h.mem,
1833 (OPJ_INT32)win_ll_x0,
1834 (OPJ_INT32)win_ll_x1,
1835 (OPJ_INT32)win_hl_x0,
1836 (OPJ_INT32)win_hl_x1);
1837 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
1838 (OPJ_INT32)win_ll_x0,
1839 (OPJ_INT32)win_ll_x1,
1840 (OPJ_INT32)win_hl_x0,
1841 (OPJ_INT32)win_hl_x1);
1842 memcpy(&tiledp[j * w] + win_tr_x0, h.mem + win_tr_x0,
1843 (win_tr_x1 - win_tr_x0) * sizeof(OPJ_INT32));
1847 for (i = win_tr_x0; i < win_tr_x1; ++i) {
1848 memset(v.mem, 0, (OPJ_UINT32)(v.sn + v.dn) * sizeof(OPJ_INT32));
1849 opj_dwt_interleave_partial_v(v.mem,
1854 (OPJ_INT32)win_ll_y0,
1855 (OPJ_INT32)win_ll_y1,
1856 (OPJ_INT32)win_lh_y0,
1857 (OPJ_INT32)win_lh_y1);
1858 opj_dwt_decode_partial_1(v.mem, v.dn, v.sn, v.cas,
1859 (OPJ_INT32)win_ll_y0,
1860 (OPJ_INT32)win_ll_y1,
1861 (OPJ_INT32)win_lh_y0,
1862 (OPJ_INT32)win_lh_y1);
1863 for (j = win_tr_y0; j < win_tr_y1; j++) {
1864 tiledp[j * w + i] = v.mem[j];
1868 opj_aligned_free(h.mem);
1872 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT dwt,
1873 OPJ_FLOAT32* OPJ_RESTRICT a,
1875 OPJ_UINT32 remaining_height)
1877 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
1879 OPJ_UINT32 x0 = dwt->win_l_x0;
1880 OPJ_UINT32 x1 = dwt->win_l_x1;
1882 for (k = 0; k < 2; ++k) {
1883 if (remaining_height >= 4 && ((size_t) a & 0x0f) == 0 &&
1884 ((size_t) bi & 0x0f) == 0 && (width & 0x0f) == 0) {
1885 /* Fast code path */
1886 for (i = x0; i < x1; ++i) {
1890 bi[i * 8 + 1] = a[j];
1892 bi[i * 8 + 2] = a[j];
1894 bi[i * 8 + 3] = a[j];
1897 /* Slow code path */
1898 for (i = x0; i < x1; ++i) {
1902 if (remaining_height == 1) {
1905 bi[i * 8 + 1] = a[j];
1907 if (remaining_height == 2) {
1910 bi[i * 8 + 2] = a[j];
1912 if (remaining_height == 3) {
1915 bi[i * 8 + 3] = a[j]; /* This one*/
1919 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
1926 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
1927 OPJ_FLOAT32* OPJ_RESTRICT a,
1929 OPJ_UINT32 nb_elts_read)
1931 opj_v4_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
1934 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
1935 memcpy(&bi[i * 2], &a[i * width], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
1938 a += (OPJ_UINT32)dwt->sn * width;
1939 bi = dwt->wavelet + 1 - dwt->cas;
1941 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
1942 memcpy(&bi[i * 2], &a[i * width], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
1948 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w,
1953 __m128* OPJ_RESTRICT vw = (__m128*) w;
1955 /* 4x unrolled loop */
1956 for (i = start; i + 3 < end; i += 4) {
1957 vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
1958 vw[2 * i + 2] = _mm_mul_ps(vw[2 * i + 2], c);
1959 vw[2 * i + 4] = _mm_mul_ps(vw[2 * i + 4], c);
1960 vw[2 * i + 6] = _mm_mul_ps(vw[2 * i + 6], c);
1962 for (; i < end; ++i) {
1963 vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
1967 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w,
1973 __m128* OPJ_RESTRICT vl = (__m128*) l;
1974 __m128* OPJ_RESTRICT vw = (__m128*) w;
1976 OPJ_UINT32 imax = opj_uint_min(end, m);
1977 __m128 tmp1, tmp2, tmp3;
1984 for (i = start; i < imax; ++i) {
1987 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
1992 assert(m + 1 == end);
1993 c = _mm_add_ps(c, c);
1994 c = _mm_mul_ps(c, vw[-2]);
1995 vw[-1] = _mm_add_ps(vw[-1], c);
2001 static void opj_v4dwt_decode_step1(opj_v4_t* w,
2004 const OPJ_FLOAT32 c)
2006 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2008 for (i = start; i < end; ++i) {
2009 OPJ_FLOAT32 tmp1 = fw[i * 8 ];
2010 OPJ_FLOAT32 tmp2 = fw[i * 8 + 1];
2011 OPJ_FLOAT32 tmp3 = fw[i * 8 + 2];
2012 OPJ_FLOAT32 tmp4 = fw[i * 8 + 3];
2013 fw[i * 8 ] = tmp1 * c;
2014 fw[i * 8 + 1] = tmp2 * c;
2015 fw[i * 8 + 2] = tmp3 * c;
2016 fw[i * 8 + 3] = tmp4 * c;
2020 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
2026 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2027 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2029 OPJ_UINT32 imax = opj_uint_min(end, m);
2034 for (i = start; i < imax; ++i) {
2035 OPJ_FLOAT32 tmp1_1 = fl[0];
2036 OPJ_FLOAT32 tmp1_2 = fl[1];
2037 OPJ_FLOAT32 tmp1_3 = fl[2];
2038 OPJ_FLOAT32 tmp1_4 = fl[3];
2039 OPJ_FLOAT32 tmp2_1 = fw[-4];
2040 OPJ_FLOAT32 tmp2_2 = fw[-3];
2041 OPJ_FLOAT32 tmp2_3 = fw[-2];
2042 OPJ_FLOAT32 tmp2_4 = fw[-1];
2043 OPJ_FLOAT32 tmp3_1 = fw[0];
2044 OPJ_FLOAT32 tmp3_2 = fw[1];
2045 OPJ_FLOAT32 tmp3_3 = fw[2];
2046 OPJ_FLOAT32 tmp3_4 = fw[3];
2047 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
2048 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
2049 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
2050 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
2055 assert(m + 1 == end);
2057 fw[-4] = fw[-4] + fl[0] * c;
2058 fw[-3] = fw[-3] + fl[1] * c;
2059 fw[-2] = fw[-2] + fl[2] * c;
2060 fw[-1] = fw[-1] + fl[3] * c;
2067 /* Inverse 9-7 wavelet transform in 1-D. */
2069 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
2072 if (dwt->cas == 0) {
2073 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2079 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2086 opj_v4dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2087 _mm_set1_ps(opj_K));
2088 opj_v4dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2089 _mm_set1_ps(opj_c13318));
2090 opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2091 dwt->win_l_x0, dwt->win_l_x1,
2092 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2093 _mm_set1_ps(opj_dwt_delta));
2094 opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2095 dwt->win_h_x0, dwt->win_h_x1,
2096 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2097 _mm_set1_ps(opj_dwt_gamma));
2098 opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2099 dwt->win_l_x0, dwt->win_l_x1,
2100 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2101 _mm_set1_ps(opj_dwt_beta));
2102 opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2103 dwt->win_h_x0, dwt->win_h_x1,
2104 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2105 _mm_set1_ps(opj_dwt_alpha));
2107 opj_v4dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2109 opj_v4dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2111 opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2112 dwt->win_l_x0, dwt->win_l_x1,
2113 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2115 opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2116 dwt->win_h_x0, dwt->win_h_x1,
2117 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2119 opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2120 dwt->win_l_x0, dwt->win_l_x1,
2121 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2123 opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2124 dwt->win_h_x0, dwt->win_h_x1,
2125 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2132 /* Inverse 9-7 wavelet transform in 2-D. */
2135 OPJ_BOOL opj_dwt_decode_tile_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2141 opj_tcd_resolution_t* res = tilec->resolutions;
2143 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2144 res->x0); /* width of the resolution level computed */
2145 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2146 res->y0); /* height of the resolution level computed */
2148 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2152 l_data_size = opj_dwt_max_resolution(res, numres);
2153 /* overflow check */
2154 if (l_data_size > (SIZE_MAX - 5U)) {
2155 /* FIXME event manager error callback */
2159 /* overflow check */
2160 if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2161 /* FIXME event manager error callback */
2164 h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2166 /* FIXME event manager error callback */
2169 v.wavelet = h.wavelet;
2172 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2175 h.sn = (OPJ_INT32)rw;
2176 v.sn = (OPJ_INT32)rh;
2180 rw = (OPJ_UINT32)(res->x1 -
2181 res->x0); /* width of the resolution level computed */
2182 rh = (OPJ_UINT32)(res->y1 -
2183 res->y0); /* height of the resolution level computed */
2185 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2186 h.cas = res->x0 % 2;
2189 h.win_l_x1 = (OPJ_UINT32)h.sn;
2191 h.win_h_x1 = (OPJ_UINT32)h.dn;
2192 for (j = 0; j + 3 < rh; j += 4) {
2194 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2195 opj_v4dwt_decode(&h);
2197 for (k = 0; k < rw; k++) {
2198 aj[k ] = h.wavelet[k].f[0];
2199 aj[k + w ] = h.wavelet[k].f[1];
2200 aj[k + w * 2] = h.wavelet[k].f[2];
2201 aj[k + w * 3] = h.wavelet[k].f[3];
2209 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2210 opj_v4dwt_decode(&h);
2211 for (k = 0; k < rw; k++) {
2214 aj[k + w * 2] = h.wavelet[k].f[2];
2217 aj[k + w ] = h.wavelet[k].f[1];
2220 aj[k] = h.wavelet[k].f[0];
2225 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2226 v.cas = res->y0 % 2;
2228 v.win_l_x1 = (OPJ_UINT32)v.sn;
2230 v.win_h_x1 = (OPJ_UINT32)v.dn;
2232 aj = (OPJ_FLOAT32*) tilec->data;
2233 for (j = rw; j > 3; j -= 4) {
2236 opj_v4dwt_interleave_v(&v, aj, w, 4);
2237 opj_v4dwt_decode(&v);
2239 for (k = 0; k < rh; ++k) {
2240 memcpy(&aj[k * w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
2250 opj_v4dwt_interleave_v(&v, aj, w, j);
2251 opj_v4dwt_decode(&v);
2253 for (k = 0; k < rh; ++k) {
2254 memcpy(&aj[k * w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
2259 opj_aligned_free(h.wavelet);
2264 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_t *tcd,
2265 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2271 /* This value matches the maximum left/right extension given in tables */
2272 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
2273 /* we currently use 3. */
2274 const OPJ_UINT32 filter_width = 4U;
2276 opj_tcd_resolution_t* tr = tilec->resolutions;
2278 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2279 tr->x0); /* width of the resolution level computed */
2280 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2281 tr->y0); /* height of the resolution level computed */
2283 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2287 opj_image_comp_t* image_comp = &(tcd->image->comps[tilec->compno]);
2288 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2289 /* with the tile coordinates */
2290 OPJ_UINT32 win_tcx0 = opj_uint_max(
2291 (OPJ_UINT32)tilec->x0,
2292 opj_uint_ceildiv(tcd->decoded_x0, image_comp->dx));
2293 OPJ_UINT32 win_tcy0 = opj_uint_max(
2294 (OPJ_UINT32)tilec->y0,
2295 opj_uint_ceildiv(tcd->decoded_y0, image_comp->dy));
2296 OPJ_UINT32 win_tcx1 = opj_uint_min(
2297 (OPJ_UINT32)tilec->x1,
2298 opj_uint_ceildiv(tcd->decoded_x1, image_comp->dx));
2299 OPJ_UINT32 win_tcy1 = opj_uint_min(
2300 (OPJ_UINT32)tilec->y1,
2301 opj_uint_ceildiv(tcd->decoded_y1, image_comp->dy));
2303 l_data_size = opj_dwt_max_resolution(tr, numres);
2304 /* overflow check */
2305 if (l_data_size > (SIZE_MAX - 5U)) {
2306 /* FIXME event manager error callback */
2310 /* overflow check */
2311 if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2312 /* FIXME event manager error callback */
2315 h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2317 /* FIXME event manager error callback */
2320 v.wavelet = h.wavelet;
2322 for (resno = 1; --numres; resno++) {
2323 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2325 /* Window of interest subband-based coordinates */
2326 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2327 OPJ_UINT32 win_hl_x0, win_hl_x1;
2328 OPJ_UINT32 win_lh_y0, win_lh_y1;
2329 /* Window of interest tile-resolution-based coordinates */
2330 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2331 /* Tile-resolution subband-based coordinates */
2332 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2336 h.sn = (OPJ_INT32)rw;
2337 v.sn = (OPJ_INT32)rh;
2339 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2340 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2342 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2345 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2348 /* Get the subband coordinates for the window of interest */
2350 opj_dwt_get_band_coordinates(tilec, resno, 0,
2351 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2352 &win_ll_x0, &win_ll_y0,
2353 &win_ll_x1, &win_ll_y1);
2356 opj_dwt_get_band_coordinates(tilec, resno, 1,
2357 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2358 &win_hl_x0, NULL, &win_hl_x1, NULL);
2361 opj_dwt_get_band_coordinates(tilec, resno, 2,
2362 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2363 NULL, &win_lh_y0, NULL, &win_lh_y1);
2365 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2366 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2367 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2368 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2369 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2371 /* Substract the origin of the bands for this tile, to the subwindow */
2372 /* of interest band coordinates, so as to get them relative to the */
2374 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2375 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2376 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2377 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2378 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2379 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2380 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2381 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2383 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2384 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2386 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2387 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2389 /* Compute the tile-resolution-based coordinates for the window of interest */
2391 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2392 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2394 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2395 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2399 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2400 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2402 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2403 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2406 h.win_l_x0 = win_ll_x0;
2407 h.win_l_x1 = win_ll_x1;
2408 h.win_h_x0 = win_hl_x0;
2409 h.win_h_x1 = win_hl_x1;
2410 for (j = 0; j + 3 < rh; j += 4, aj += w * 4) {
2411 if ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2412 (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2413 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2415 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2416 opj_v4dwt_decode(&h);
2418 for (k = win_tr_x0; k < win_tr_x1; k++) {
2419 aj[k ] = h.wavelet[k].f[0];
2420 aj[k + w ] = h.wavelet[k].f[1];
2421 aj[k + w * 2] = h.wavelet[k].f[2];
2422 aj[k + w * 3] = h.wavelet[k].f[3];
2428 ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2429 (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2430 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
2432 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2433 opj_v4dwt_decode(&h);
2434 for (k = win_tr_x0; k < win_tr_x1; k++) {
2437 aj[k + w * 2] = h.wavelet[k].f[2];
2440 aj[k + w ] = h.wavelet[k].f[1];
2443 aj[k ] = h.wavelet[k].f[0];
2448 v.win_l_x0 = win_ll_y0;
2449 v.win_l_x1 = win_ll_y1;
2450 v.win_h_x0 = win_lh_y0;
2451 v.win_h_x1 = win_lh_y1;
2452 aj = (OPJ_FLOAT32*) tilec->data;
2454 for (j = win_tr_x0; j < win_tr_x1; j += 4, aj += 4) {
2455 OPJ_UINT32 nb_elts = opj_uint_min(4U, win_tr_x1 - j);
2458 opj_v4dwt_interleave_v(&v, aj, w, nb_elts);
2459 opj_v4dwt_decode(&v);
2461 for (k = win_tr_y0; k < win_tr_y1; ++k) {
2462 memcpy(&aj[k * w], &v.wavelet[k], nb_elts * sizeof(OPJ_FLOAT32));
2467 opj_aligned_free(h.wavelet);
2472 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
2473 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2476 if (opj_dwt_is_whole_tile_decoding(p_tcd, tilec, numres)) {
2477 return opj_dwt_decode_tile_97(tilec, numres);
2479 return opj_dwt_decode_partial_97(p_tcd, tilec, numres);