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(
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]; */
282 l_dest = b + (size_t)sn * (size_t)x;
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 * (size_t)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[(size_t)i * stride + 0],
629 LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
630 STOREU(&tiledp_col[(size_t)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,
644 const OPJ_INT32* in_even = &tiledp_col[0];
645 const OPJ_INT32* in_odd = &tiledp_col[(size_t)sn * stride];
649 VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
650 VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
651 const VREG two = LOAD_CST(2);
655 assert(PARALLEL_COLS_53 == 16);
656 assert(VREG_INT_COUNT == 8);
658 assert(PARALLEL_COLS_53 == 8);
659 assert(VREG_INT_COUNT == 4);
662 /* Note: loads of input even/odd values must be done in a unaligned */
663 /* fashion. But stores in tmp can be done with aligned store, since */
664 /* the temporary buffer is properly aligned */
665 assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
667 s1n_0 = LOADU(in_even + 0);
668 s1n_1 = LOADU(in_even + VREG_INT_COUNT);
669 d1n_0 = LOADU(in_odd);
670 d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
672 /* s0n = s1n - ((d1n + 1) >> 1); <==> */
673 /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
674 s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
675 s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
677 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
683 s1n_0 = LOADU(in_even + j * stride);
684 s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
685 d1n_0 = LOADU(in_odd + j * stride);
686 d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
688 /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
689 s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
690 s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
692 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
693 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
695 /* d1c + ((s0c + s0n) >> 1) */
696 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
697 ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
698 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
699 ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
702 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
703 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
706 VREG tmp_len_minus_1;
707 s1n_0 = LOADU(in_even + (size_t)((len - 1) / 2) * stride);
708 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
709 tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
710 STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
711 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
712 STORE(tmp + PARALLEL_COLS_53 * (len - 2),
713 ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
715 s1n_1 = LOADU(in_even + (size_t)((len - 1) / 2) * stride + VREG_INT_COUNT);
716 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
717 tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
718 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
720 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
721 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
722 ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
726 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
728 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
732 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
736 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
737 * 16 in AVX2, when top-most pixel is on odd coordinate */
738 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
742 OPJ_INT32* tiledp_col,
748 VREG s1_0, s2_0, dc_0, dn_0;
749 VREG s1_1, s2_1, dc_1, dn_1;
750 const VREG two = LOAD_CST(2);
752 const OPJ_INT32* in_even = &tiledp_col[(size_t)sn * stride];
753 const OPJ_INT32* in_odd = &tiledp_col[0];
757 assert(PARALLEL_COLS_53 == 16);
758 assert(VREG_INT_COUNT == 8);
760 assert(PARALLEL_COLS_53 == 8);
761 assert(VREG_INT_COUNT == 4);
764 /* Note: loads of input even/odd values must be done in a unaligned */
765 /* fashion. But stores in tmp can be done with aligned store, since */
766 /* the temporary buffer is properly aligned */
767 assert((size_t)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
769 s1_0 = LOADU(in_even + stride);
770 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
771 dc_0 = SUB(LOADU(in_odd + 0),
772 SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
773 STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
775 s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
776 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
777 dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
778 SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
779 STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
780 ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
782 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
784 s2_0 = LOADU(in_even + (j + 1) * stride);
785 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
787 /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
788 dn_0 = SUB(LOADU(in_odd + j * stride),
789 SAR(ADD3(s1_0, s2_0, two), 2));
790 dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
791 SAR(ADD3(s1_1, s2_1, two), 2));
793 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
794 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
796 /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
797 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
798 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
799 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
800 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
807 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
808 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
811 /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
812 dn_0 = SUB(LOADU(in_odd + (size_t)(len / 2 - 1) * stride),
813 SAR(ADD3(s1_0, s1_0, two), 2));
814 dn_1 = SUB(LOADU(in_odd + (size_t)(len / 2 - 1) * stride + VREG_INT_COUNT),
815 SAR(ADD3(s1_1, s1_1, two), 2));
817 /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
818 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
819 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
820 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
821 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
823 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
824 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
826 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
827 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
831 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
845 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
847 #if !defined(STANDARD_SLOW_VERSION)
848 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
849 * pixel is on even coordinate */
850 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
853 OPJ_INT32* tiledp_col,
857 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
861 /* Performs lifting in one single iteration. Saves memory */
862 /* accesses and explicit interleaving. */
865 d1n = tiledp_col[(size_t)sn * stride];
866 s0n = s1n - ((d1n + 1) >> 1);
868 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
872 s1n = tiledp_col[(size_t)(j + 1) * stride];
873 d1n = tiledp_col[(size_t)(sn + j + 1) * stride];
875 s0n = s1n - ((d1c + d1n + 2) >> 2);
878 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
885 tiledp_col[(size_t)((len - 1) / 2) * stride] -
887 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
889 tmp[len - 1] = d1n + s0n;
892 for (i = 0; i < len; ++i) {
893 tiledp_col[(size_t)i * stride] = tmp[i];
898 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
899 * pixel is on odd coordinate */
900 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
903 OPJ_INT32* tiledp_col,
907 OPJ_INT32 s1, s2, dc, dn;
908 const OPJ_INT32* in_even = &tiledp_col[(size_t)sn * stride];
909 const OPJ_INT32* in_odd = &tiledp_col[0];
913 /* Performs lifting in one single iteration. Saves memory */
914 /* accesses and explicit interleaving. */
916 s1 = in_even[stride];
917 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
918 tmp[0] = in_even[0] + dc;
919 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
921 s2 = in_even[(size_t)(j + 1) * stride];
923 dn = in_odd[(size_t)j * stride] - ((s1 + s2 + 2) >> 2);
925 tmp[i + 1] = s1 + ((dn + dc) >> 1);
932 dn = in_odd[(size_t)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
933 tmp[len - 2] = s1 + ((dn + dc) >> 1);
936 tmp[len - 1] = s1 + dc;
939 for (i = 0; i < len; ++i) {
940 tiledp_col[(size_t)i * stride] = tmp[i];
943 #endif /* !defined(STANDARD_SLOW_VERSION) */
946 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
948 /* Performs interleave, inverse wavelet transform and copy back to buffer */
949 static void opj_idwt53_v(const opj_dwt_t *dwt,
950 OPJ_INT32* tiledp_col,
954 #ifdef STANDARD_SLOW_VERSION
955 /* For documentation purpose */
957 for (c = 0; c < nb_cols; c ++) {
958 opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
959 opj_dwt_decode_1(dwt);
960 for (k = 0; k < dwt->sn + dwt->dn; ++k) {
961 tiledp_col[c + k * stride] = dwt->mem[k];
965 const OPJ_INT32 sn = dwt->sn;
966 const OPJ_INT32 len = sn + dwt->dn;
968 /* If len == 1, unmodified value */
970 #if (defined(__SSE2__) || defined(__AVX2__))
971 if (len > 1 && nb_cols == PARALLEL_COLS_53) {
972 /* Same as below general case, except that thanks to SSE2/AVX2 */
973 /* we can efficently process 8/16 columns in parallel */
974 opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
980 for (c = 0; c < nb_cols; c++, tiledp_col++) {
981 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
988 for (c = 0; c < nb_cols; c++, tiledp_col++) {
996 OPJ_INT32* out = dwt->mem;
997 for (c = 0; c < nb_cols; c++, tiledp_col++) {
999 const OPJ_INT32* in_even = &tiledp_col[(size_t)sn * stride];
1000 const OPJ_INT32* in_odd = &tiledp_col[0];
1002 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
1003 out[0] = in_even[0] + out[1];
1005 for (i = 0; i < len; ++i) {
1006 tiledp_col[(size_t)i * stride] = out[i];
1013 #if (defined(__SSE2__) || defined(__AVX2__))
1014 if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1015 /* Same as below general case, except that thanks to SSE2/AVX2 */
1016 /* we can efficently process 8/16 columns in parallel */
1017 opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1023 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1024 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1034 /* Forward 9-7 wavelet transform in 1-D. */
1036 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1041 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1042 for (i = 0; i < dn; i++) {
1043 OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
1045 for (i = 0; i < sn; i++) {
1046 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
1048 for (i = 0; i < dn; i++) {
1049 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
1051 for (i = 0; i < sn; i++) {
1052 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
1054 for (i = 0; i < dn; i++) {
1055 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038); /*5038 */
1057 for (i = 0; i < sn; i++) {
1058 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659); /*6660 */
1062 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
1063 for (i = 0; i < dn; i++) {
1064 OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
1066 for (i = 0; i < sn; i++) {
1067 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
1069 for (i = 0; i < dn; i++) {
1070 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
1072 for (i = 0; i < sn; i++) {
1073 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
1075 for (i = 0; i < dn; i++) {
1076 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038); /*5038 */
1078 for (i = 0; i < sn; i++) {
1079 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659); /*6660 */
1085 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1086 opj_stepsize_t *bandno_stepsize)
1089 p = opj_int_floorlog2(stepsize) - 13;
1090 n = 11 - opj_int_floorlog2(stepsize);
1091 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1092 bandno_stepsize->expn = numbps - p;
1096 ==========================================================
1098 ==========================================================
1103 /* Forward 5-3 wavelet transform in 2-D. */
1105 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,
1106 void (*p_function)(OPJ_INT32 *, OPJ_INT32, OPJ_INT32, OPJ_INT32))
1114 OPJ_INT32 rw; /* width of the resolution level computed */
1115 OPJ_INT32 rh; /* height of the resolution level computed */
1118 opj_tcd_resolution_t * l_cur_res = 0;
1119 opj_tcd_resolution_t * l_last_res = 0;
1121 w = tilec->x1 - tilec->x0;
1122 l = (OPJ_INT32)tilec->numresolutions - 1;
1125 l_cur_res = tilec->resolutions + l;
1126 l_last_res = l_cur_res - 1;
1128 l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1129 /* overflow check */
1130 if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1131 /* FIXME event manager error callback */
1134 l_data_size *= sizeof(OPJ_INT32);
1135 bj = (OPJ_INT32*)opj_malloc(l_data_size);
1136 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1137 /* in that case, so do not error out */
1138 if (l_data_size != 0 && ! bj) {
1144 OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
1145 OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
1146 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1147 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
1150 rw = l_cur_res->x1 - l_cur_res->x0;
1151 rh = l_cur_res->y1 - l_cur_res->y0;
1152 rw1 = l_last_res->x1 - l_last_res->x0;
1153 rh1 = l_last_res->y1 - l_last_res->y0;
1155 cas_row = l_cur_res->x0 & 1;
1156 cas_col = l_cur_res->y0 & 1;
1160 for (j = 0; j < rw; ++j) {
1162 for (k = 0; k < rh; ++k) {
1166 (*p_function)(bj, dn, sn, cas_col);
1168 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1174 for (j = 0; j < rh; j++) {
1176 for (k = 0; k < rw; k++) {
1179 (*p_function)(bj, dn, sn, cas_row);
1180 opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
1183 l_cur_res = l_last_res;
1192 /* Forward 5-3 wavelet transform in 2-D. */
1194 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
1196 return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1);
1200 /* Inverse 5-3 wavelet transform in 2-D. */
1202 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1205 if (p_tcd->whole_tile_decoding) {
1206 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1208 return opj_dwt_decode_partial_tile(tilec, numres);
1214 /* Get gain of 5-3 wavelet transform. */
1216 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient)
1221 if (orient == 1 || orient == 2) {
1228 /* Get norm of 5-3 wavelet. */
1230 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1232 return opj_dwt_norms[orient][level];
1236 /* Forward 9-7 wavelet transform in 2-D. */
1238 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
1240 return opj_dwt_encode_procedure(tilec, opj_dwt_encode_1_real);
1244 /* Get gain of 9-7 wavelet transform. */
1246 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient)
1253 /* Get norm of 9-7 wavelet. */
1255 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1257 return opj_dwt_norms_real[orient][level];
1260 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1262 OPJ_UINT32 numbands, bandno;
1263 numbands = 3 * tccp->numresolutions - 2;
1264 for (bandno = 0; bandno < numbands; bandno++) {
1265 OPJ_FLOAT64 stepsize;
1266 OPJ_UINT32 resno, level, orient, gain;
1268 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1269 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1270 level = tccp->numresolutions - 1 - resno;
1271 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1272 (orient == 2)) ? 1 : 2));
1273 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1276 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1277 stepsize = (1 << (gain)) / norm;
1279 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1280 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1285 /* Determine maximum computed resolution level for inverse wavelet transform */
1287 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1294 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1297 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1308 OPJ_INT32 * OPJ_RESTRICT tiledp;
1311 } opj_dwd_decode_h_job_t;
1313 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1316 opj_dwd_decode_h_job_t* job;
1319 job = (opj_dwd_decode_h_job_t*)user_data;
1320 for (j = job->min_j; j < job->max_j; j++) {
1321 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1324 opj_aligned_free(job->h.mem);
1332 OPJ_INT32 * OPJ_RESTRICT tiledp;
1335 } opj_dwd_decode_v_job_t;
1337 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1340 opj_dwd_decode_v_job_t* job;
1343 job = (opj_dwd_decode_v_job_t*)user_data;
1344 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1345 j += PARALLEL_COLS_53) {
1346 opj_idwt53_v(&job->v, &job->tiledp[j], (size_t)job->w,
1350 opj_idwt53_v(&job->v, &job->tiledp[j], (size_t)job->w,
1351 (OPJ_INT32)(job->max_j - j));
1353 opj_aligned_free(job->v.mem);
1359 /* Inverse wavelet transform in 2-D. */
1361 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1362 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1367 opj_tcd_resolution_t* tr = tilec->resolutions;
1369 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1370 tr->x0); /* width of the resolution level computed */
1371 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1372 tr->y0); /* height of the resolution level computed */
1374 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1376 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1383 num_threads = opj_thread_pool_get_thread_count(tp);
1384 h_mem_size = opj_dwt_max_resolution(tr, numres);
1385 /* overflow check */
1386 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1387 /* FIXME event manager error callback */
1390 /* We need PARALLEL_COLS_53 times the height of the array, */
1391 /* since for the vertical pass */
1392 /* we process PARALLEL_COLS_53 columns at a time */
1393 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1394 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1396 /* FIXME event manager error callback */
1403 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1407 h.sn = (OPJ_INT32)rw;
1408 v.sn = (OPJ_INT32)rh;
1410 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1411 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1413 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1416 if (num_threads <= 1 || rh <= 1) {
1417 for (j = 0; j < rh; ++j) {
1418 opj_idwt53_h(&h, &tiledp[(size_t)j * w]);
1421 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1424 if (rh < num_jobs) {
1427 step_j = (rh / num_jobs);
1429 for (j = 0; j < num_jobs; j++) {
1430 opj_dwd_decode_h_job_t* job;
1432 job = (opj_dwd_decode_h_job_t*) opj_malloc(sizeof(opj_dwd_decode_h_job_t));
1434 /* It would be nice to fallback to single thread case, but */
1435 /* unfortunately some jobs may be launched and have modified */
1436 /* tiledp, so it is not practical to recover from that error */
1437 /* FIXME event manager error callback */
1438 opj_thread_pool_wait_completion(tp, 0);
1439 opj_aligned_free(h.mem);
1445 job->tiledp = tiledp;
1446 job->min_j = j * step_j;
1447 job->max_j = (j + 1U) * step_j; /* this can overflow */
1448 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1451 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1453 /* FIXME event manager error callback */
1454 opj_thread_pool_wait_completion(tp, 0);
1456 opj_aligned_free(h.mem);
1459 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1461 opj_thread_pool_wait_completion(tp, 0);
1464 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1467 if (num_threads <= 1 || rw <= 1) {
1468 for (j = 0; j + PARALLEL_COLS_53 <= rw;
1469 j += PARALLEL_COLS_53) {
1470 opj_idwt53_v(&v, &tiledp[j], (size_t)w, PARALLEL_COLS_53);
1473 opj_idwt53_v(&v, &tiledp[j], (size_t)w, (OPJ_INT32)(rw - j));
1476 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1479 if (rw < num_jobs) {
1482 step_j = (rw / num_jobs);
1484 for (j = 0; j < num_jobs; j++) {
1485 opj_dwd_decode_v_job_t* job;
1487 job = (opj_dwd_decode_v_job_t*) opj_malloc(sizeof(opj_dwd_decode_v_job_t));
1489 /* It would be nice to fallback to single thread case, but */
1490 /* unfortunately some jobs may be launched and have modified */
1491 /* tiledp, so it is not practical to recover from that error */
1492 /* FIXME event manager error callback */
1493 opj_thread_pool_wait_completion(tp, 0);
1494 opj_aligned_free(v.mem);
1500 job->tiledp = tiledp;
1501 job->min_j = j * step_j;
1502 job->max_j = (j + 1U) * step_j; /* this can overflow */
1503 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1506 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1508 /* FIXME event manager error callback */
1509 opj_thread_pool_wait_completion(tp, 0);
1511 opj_aligned_free(v.mem);
1514 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1516 opj_thread_pool_wait_completion(tp, 0);
1519 opj_aligned_free(h.mem);
1523 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1525 opj_sparse_array_int32_t* sa,
1528 OPJ_UINT32 win_l_x0,
1529 OPJ_UINT32 win_l_x1,
1530 OPJ_UINT32 win_h_x0,
1531 OPJ_UINT32 win_h_x1)
1534 ret = opj_sparse_array_int32_read(sa,
1536 win_l_x1, sa_line + 1,
1537 dest + cas + 2 * win_l_x0,
1540 ret = opj_sparse_array_int32_read(sa,
1541 sn + win_h_x0, sa_line,
1542 sn + win_h_x1, sa_line + 1,
1543 dest + 1 - cas + 2 * win_h_x0,
1550 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1552 opj_sparse_array_int32_t* sa,
1555 OPJ_UINT32 win_l_y0,
1556 OPJ_UINT32 win_l_y1,
1557 OPJ_UINT32 win_h_y0,
1558 OPJ_UINT32 win_h_y1)
1561 ret = opj_sparse_array_int32_read(sa,
1563 sa_col + 1, win_l_y1,
1564 dest + cas + 2 * win_l_y0,
1567 ret = opj_sparse_array_int32_read(sa,
1568 sa_col, sn + win_h_y0,
1569 sa_col + 1, sn + win_h_y1,
1570 dest + 1 - cas + 2 * win_h_y0,
1576 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1586 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1588 /* Naive version is :
1589 for (i = win_l_x0; i < i_max; i++) {
1590 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1592 for (i = win_h_x0; i < win_h_x1; i++) {
1593 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1595 but the compiler doesn't manage to unroll it to avoid bound
1596 checking in OPJ_S_ and OPJ_D_ macros
1603 /* Left-most case */
1604 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1611 for (; i < i_max; i++) {
1612 /* No bound checking */
1613 OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
1615 for (; i < win_l_x1; i++) {
1616 /* Right-most case */
1617 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1623 OPJ_INT32 i_max = win_h_x1;
1627 for (; i < i_max; i++) {
1628 /* No bound checking */
1629 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
1631 for (; i < win_h_x1; i++) {
1632 /* Right-most case */
1633 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1638 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
1641 for (i = win_l_x0; i < win_l_x1; i++) {
1642 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1644 for (i = win_h_x0; i < win_h_x1; i++) {
1645 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1651 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
1663 /* Compute number of decomposition for this band. See table F-1 */
1664 OPJ_UINT32 nb = (resno == 0) ?
1665 tilec->numresolutions - 1 :
1666 tilec->numresolutions - resno;
1667 /* Map above tile-based coordinates to sub-band-based coordinates per */
1668 /* equation B-15 of the standard */
1669 OPJ_UINT32 x0b = bandno & 1;
1670 OPJ_UINT32 y0b = bandno >> 1;
1672 *tbx0 = (nb == 0) ? tcx0 :
1673 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
1674 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
1677 *tby0 = (nb == 0) ? tcy0 :
1678 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
1679 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
1682 *tbx1 = (nb == 0) ? tcx1 :
1683 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
1684 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
1687 *tby1 = (nb == 0) ? tcy1 :
1688 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
1689 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
1693 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
1694 OPJ_UINT32 max_size,
1698 *start = opj_uint_subs(*start, filter_width);
1699 *end = opj_uint_adds(*end, filter_width);
1700 *end = opj_uint_min(*end, max_size);
1704 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
1705 opj_tcd_tilecomp_t* tilec,
1708 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
1709 OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
1710 OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
1711 OPJ_UINT32 resno, bandno, precno, cblkno;
1712 opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
1713 w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
1718 for (resno = 0; resno < numres; ++resno) {
1719 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1721 for (bandno = 0; bandno < res->numbands; ++bandno) {
1722 opj_tcd_band_t* band = &res->bands[bandno];
1724 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1725 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1726 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1727 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1728 if (cblk->decoded_data != NULL) {
1729 OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
1730 OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
1731 OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
1732 OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
1734 if (band->bandno & 1) {
1735 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1736 x += (OPJ_UINT32)(pres->x1 - pres->x0);
1738 if (band->bandno & 2) {
1739 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1740 y += (OPJ_UINT32)(pres->y1 - pres->y0);
1743 if (!opj_sparse_array_int32_write(sa, x, y,
1744 x + cblk_w, y + cblk_h,
1746 1, cblk_w, OPJ_TRUE)) {
1747 opj_sparse_array_int32_free(sa);
1760 static OPJ_BOOL opj_dwt_decode_partial_tile(
1761 opj_tcd_tilecomp_t* tilec,
1764 opj_sparse_array_int32_t* sa;
1768 /* This value matches the maximum left/right extension given in tables */
1769 /* F.2 and F.3 of the standard. */
1770 const OPJ_UINT32 filter_width = 2U;
1772 opj_tcd_resolution_t* tr = tilec->resolutions;
1773 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
1775 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1776 tr->x0); /* width of the resolution level computed */
1777 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1778 tr->y0); /* height of the resolution level computed */
1782 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1783 /* with the tile coordinates */
1784 OPJ_UINT32 win_tcx0 = tilec->win_x0;
1785 OPJ_UINT32 win_tcy0 = tilec->win_y0;
1786 OPJ_UINT32 win_tcx1 = tilec->win_x1;
1787 OPJ_UINT32 win_tcy1 = tilec->win_y1;
1789 sa = opj_dwt_init_sparse_array(tilec, numres);
1792 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
1793 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
1794 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
1795 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
1796 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
1798 1, tr_max->win_x1 - tr_max->win_x0,
1802 opj_sparse_array_int32_free(sa);
1805 h_mem_size = opj_dwt_max_resolution(tr, numres);
1806 /* overflow check */
1807 if (h_mem_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1808 /* FIXME event manager error callback */
1809 opj_sparse_array_int32_free(sa);
1813 h_mem_size *= sizeof(OPJ_INT32);
1814 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1816 /* FIXME event manager error callback */
1817 opj_sparse_array_int32_free(sa);
1823 for (resno = 1; resno < numres; resno ++) {
1825 /* Window of interest subband-based coordinates */
1826 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
1827 OPJ_UINT32 win_hl_x0, win_hl_x1;
1828 OPJ_UINT32 win_lh_y0, win_lh_y1;
1829 /* Window of interest tile-resolution-based coordinates */
1830 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
1831 /* Tile-resolution subband-based coordinates */
1832 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
1836 h.sn = (OPJ_INT32)rw;
1837 v.sn = (OPJ_INT32)rh;
1839 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1840 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1842 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1845 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1848 /* Get the subband coordinates for the window of interest */
1850 opj_dwt_get_band_coordinates(tilec, resno, 0,
1851 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1852 &win_ll_x0, &win_ll_y0,
1853 &win_ll_x1, &win_ll_y1);
1856 opj_dwt_get_band_coordinates(tilec, resno, 1,
1857 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1858 &win_hl_x0, NULL, &win_hl_x1, NULL);
1861 opj_dwt_get_band_coordinates(tilec, resno, 2,
1862 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
1863 NULL, &win_lh_y0, NULL, &win_lh_y1);
1865 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
1866 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
1867 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
1868 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
1869 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
1871 /* Substract the origin of the bands for this tile, to the subwindow */
1872 /* of interest band coordinates, so as to get them relative to the */
1874 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
1875 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
1876 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
1877 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
1878 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
1879 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
1880 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
1881 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
1883 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
1884 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
1886 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
1887 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
1889 /* Compute the tile-resolution-based coordinates for the window of interest */
1891 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
1892 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
1894 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
1895 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
1899 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
1900 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
1902 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
1903 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
1906 for (j = 0; j < rh; ++j) {
1907 if ((j >= win_ll_y0 && j < win_ll_y1) ||
1908 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
1910 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
1911 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
1912 /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
1913 /* This is less extreme than memsetting the whole buffer to 0 */
1914 /* although we could potentially do better with better handling of edge conditions */
1915 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
1916 h.mem[win_tr_x1 - 1] = 0;
1918 if (win_tr_x1 < rw) {
1919 h.mem[win_tr_x1] = 0;
1922 opj_dwt_interleave_partial_h(h.mem,
1931 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
1932 (OPJ_INT32)win_ll_x0,
1933 (OPJ_INT32)win_ll_x1,
1934 (OPJ_INT32)win_hl_x0,
1935 (OPJ_INT32)win_hl_x1);
1936 if (!opj_sparse_array_int32_write(sa,
1941 /* FIXME event manager error callback */
1942 opj_sparse_array_int32_free(sa);
1943 opj_aligned_free(h.mem);
1949 for (i = win_tr_x0; i < win_tr_x1; ++i) {
1950 opj_dwt_interleave_partial_v(v.mem,
1959 opj_dwt_decode_partial_1(v.mem, v.dn, v.sn, v.cas,
1960 (OPJ_INT32)win_ll_y0,
1961 (OPJ_INT32)win_ll_y1,
1962 (OPJ_INT32)win_lh_y0,
1963 (OPJ_INT32)win_lh_y1);
1964 if (!opj_sparse_array_int32_write(sa,
1969 /* FIXME event manager error callback */
1970 opj_sparse_array_int32_free(sa);
1971 opj_aligned_free(h.mem);
1976 opj_aligned_free(h.mem);
1979 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
1980 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
1981 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
1982 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
1983 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
1985 1, tr_max->win_x1 - tr_max->win_x0,
1990 opj_sparse_array_int32_free(sa);
1994 static void opj_v4dwt_interleave_h(opj_v4dwt_t* OPJ_RESTRICT dwt,
1995 OPJ_FLOAT32* OPJ_RESTRICT a,
1997 OPJ_UINT32 remaining_height)
1999 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2001 OPJ_UINT32 x0 = dwt->win_l_x0;
2002 OPJ_UINT32 x1 = dwt->win_l_x1;
2004 for (k = 0; k < 2; ++k) {
2005 if (remaining_height >= 4 && ((size_t) a & 0x0f) == 0 &&
2006 ((size_t) bi & 0x0f) == 0 && (width & 0x0f) == 0) {
2007 /* Fast code path */
2008 for (i = x0; i < x1; ++i) {
2012 bi[i * 8 + 1] = a[j];
2014 bi[i * 8 + 2] = a[j];
2016 bi[i * 8 + 3] = a[j];
2019 /* Slow code path */
2020 for (i = x0; i < x1; ++i) {
2024 if (remaining_height == 1) {
2027 bi[i * 8 + 1] = a[j];
2029 if (remaining_height == 2) {
2032 bi[i * 8 + 2] = a[j];
2034 if (remaining_height == 3) {
2037 bi[i * 8 + 3] = a[j]; /* This one*/
2041 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2048 static void opj_v4dwt_interleave_partial_h(opj_v4dwt_t* dwt,
2049 opj_sparse_array_int32_t* sa,
2051 OPJ_UINT32 remaining_height)
2054 for (i = 0; i < remaining_height; i++) {
2056 ret = opj_sparse_array_int32_read(sa,
2057 dwt->win_l_x0, sa_line + i,
2058 dwt->win_l_x1, sa_line + i + 1,
2059 /* Nasty cast from float* to int32* */
2060 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2063 ret = opj_sparse_array_int32_read(sa,
2064 (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2065 (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2066 /* Nasty cast from float* to int32* */
2067 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2074 static void opj_v4dwt_interleave_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
2075 OPJ_FLOAT32* OPJ_RESTRICT a,
2077 OPJ_UINT32 nb_elts_read)
2079 opj_v4_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2082 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2083 memcpy(&bi[i * 2], &a[i * (size_t)width],
2084 (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
2087 a += (OPJ_UINT32)dwt->sn * (size_t)width;
2088 bi = dwt->wavelet + 1 - dwt->cas;
2090 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2091 memcpy(&bi[i * 2], &a[i * (size_t)width],
2092 (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
2096 static void opj_v4dwt_interleave_partial_v(opj_v4dwt_t* OPJ_RESTRICT dwt,
2097 opj_sparse_array_int32_t* sa,
2099 OPJ_UINT32 nb_elts_read)
2102 for (i = 0; i < nb_elts_read; i++) {
2104 ret = opj_sparse_array_int32_read(sa,
2105 sa_col + i, dwt->win_l_x0,
2106 sa_col + i + 1, dwt->win_l_x1,
2107 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2110 ret = opj_sparse_array_int32_read(sa,
2111 sa_col + i, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2112 sa_col + i + 1, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2113 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2122 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w,
2127 __m128* OPJ_RESTRICT vw = (__m128*) w;
2129 /* 4x unrolled loop */
2130 for (i = start; i + 3 < end; i += 4) {
2131 vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
2132 vw[2 * i + 2] = _mm_mul_ps(vw[2 * i + 2], c);
2133 vw[2 * i + 4] = _mm_mul_ps(vw[2 * i + 4], c);
2134 vw[2 * i + 6] = _mm_mul_ps(vw[2 * i + 6], c);
2136 for (; i < end; ++i) {
2137 vw[2 * i] = _mm_mul_ps(vw[2 * i], c);
2141 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w,
2147 __m128* OPJ_RESTRICT vl = (__m128*) l;
2148 __m128* OPJ_RESTRICT vw = (__m128*) w;
2150 OPJ_UINT32 imax = opj_uint_min(end, m);
2151 __m128 tmp1, tmp2, tmp3;
2158 for (i = start; i < imax; ++i) {
2161 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
2166 assert(m + 1 == end);
2167 c = _mm_add_ps(c, c);
2168 c = _mm_mul_ps(c, vw[-2]);
2169 vw[-1] = _mm_add_ps(vw[-1], c);
2175 static void opj_v4dwt_decode_step1(opj_v4_t* w,
2178 const OPJ_FLOAT32 c)
2180 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2182 for (i = start; i < end; ++i) {
2183 OPJ_FLOAT32 tmp1 = fw[i * 8 ];
2184 OPJ_FLOAT32 tmp2 = fw[i * 8 + 1];
2185 OPJ_FLOAT32 tmp3 = fw[i * 8 + 2];
2186 OPJ_FLOAT32 tmp4 = fw[i * 8 + 3];
2187 fw[i * 8 ] = tmp1 * c;
2188 fw[i * 8 + 1] = tmp2 * c;
2189 fw[i * 8 + 2] = tmp3 * c;
2190 fw[i * 8 + 3] = tmp4 * c;
2194 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w,
2200 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2201 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2203 OPJ_UINT32 imax = opj_uint_min(end, m);
2208 for (i = start; i < imax; ++i) {
2209 OPJ_FLOAT32 tmp1_1 = fl[0];
2210 OPJ_FLOAT32 tmp1_2 = fl[1];
2211 OPJ_FLOAT32 tmp1_3 = fl[2];
2212 OPJ_FLOAT32 tmp1_4 = fl[3];
2213 OPJ_FLOAT32 tmp2_1 = fw[-4];
2214 OPJ_FLOAT32 tmp2_2 = fw[-3];
2215 OPJ_FLOAT32 tmp2_3 = fw[-2];
2216 OPJ_FLOAT32 tmp2_4 = fw[-1];
2217 OPJ_FLOAT32 tmp3_1 = fw[0];
2218 OPJ_FLOAT32 tmp3_2 = fw[1];
2219 OPJ_FLOAT32 tmp3_3 = fw[2];
2220 OPJ_FLOAT32 tmp3_4 = fw[3];
2221 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
2222 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
2223 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
2224 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
2229 assert(m + 1 == end);
2231 fw[-4] = fw[-4] + fl[0] * c;
2232 fw[-3] = fw[-3] + fl[1] * c;
2233 fw[-2] = fw[-2] + fl[2] * c;
2234 fw[-1] = fw[-1] + fl[3] * c;
2241 /* Inverse 9-7 wavelet transform in 1-D. */
2243 static void opj_v4dwt_decode(opj_v4dwt_t* OPJ_RESTRICT dwt)
2246 if (dwt->cas == 0) {
2247 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2253 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2260 opj_v4dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2261 _mm_set1_ps(opj_K));
2262 opj_v4dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2263 _mm_set1_ps(opj_c13318));
2264 opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2265 dwt->win_l_x0, dwt->win_l_x1,
2266 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2267 _mm_set1_ps(opj_dwt_delta));
2268 opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2269 dwt->win_h_x0, dwt->win_h_x1,
2270 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2271 _mm_set1_ps(opj_dwt_gamma));
2272 opj_v4dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2273 dwt->win_l_x0, dwt->win_l_x1,
2274 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2275 _mm_set1_ps(opj_dwt_beta));
2276 opj_v4dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2277 dwt->win_h_x0, dwt->win_h_x1,
2278 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2279 _mm_set1_ps(opj_dwt_alpha));
2281 opj_v4dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2283 opj_v4dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2285 opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2286 dwt->win_l_x0, dwt->win_l_x1,
2287 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2289 opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2290 dwt->win_h_x0, dwt->win_h_x1,
2291 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2293 opj_v4dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2294 dwt->win_l_x0, dwt->win_l_x1,
2295 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2297 opj_v4dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2298 dwt->win_h_x0, dwt->win_h_x1,
2299 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2306 /* Inverse 9-7 wavelet transform in 2-D. */
2309 OPJ_BOOL opj_dwt_decode_tile_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2315 opj_tcd_resolution_t* res = tilec->resolutions;
2317 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2318 res->x0); /* width of the resolution level computed */
2319 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2320 res->y0); /* height of the resolution level computed */
2322 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2324 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2328 l_data_size = opj_dwt_max_resolution(res, numres);
2329 /* overflow check */
2330 if (l_data_size > (SIZE_MAX - 5U)) {
2331 /* FIXME event manager error callback */
2335 /* overflow check */
2336 if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2337 /* FIXME event manager error callback */
2340 h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2342 /* FIXME event manager error callback */
2345 v.wavelet = h.wavelet;
2348 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2351 h.sn = (OPJ_INT32)rw;
2352 v.sn = (OPJ_INT32)rh;
2356 rw = (OPJ_UINT32)(res->x1 -
2357 res->x0); /* width of the resolution level computed */
2358 rh = (OPJ_UINT32)(res->y1 -
2359 res->y0); /* height of the resolution level computed */
2361 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2362 h.cas = res->x0 % 2;
2365 h.win_l_x1 = (OPJ_UINT32)h.sn;
2367 h.win_h_x1 = (OPJ_UINT32)h.dn;
2368 for (j = 0; j + 3 < rh; j += 4) {
2370 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2371 opj_v4dwt_decode(&h);
2373 for (k = 0; k < rw; k++) {
2374 aj[k ] = h.wavelet[k].f[0];
2375 aj[k + (size_t)w ] = h.wavelet[k].f[1];
2376 aj[k + (size_t)w * 2] = h.wavelet[k].f[2];
2377 aj[k + (size_t)w * 3] = h.wavelet[k].f[3];
2385 opj_v4dwt_interleave_h(&h, aj, w, rh - j);
2386 opj_v4dwt_decode(&h);
2387 for (k = 0; k < rw; k++) {
2390 aj[k + (size_t)w * 2] = h.wavelet[k].f[2];
2393 aj[k + (size_t)w ] = h.wavelet[k].f[1];
2396 aj[k] = h.wavelet[k].f[0];
2401 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2402 v.cas = res->y0 % 2;
2404 v.win_l_x1 = (OPJ_UINT32)v.sn;
2406 v.win_h_x1 = (OPJ_UINT32)v.dn;
2408 aj = (OPJ_FLOAT32*) tilec->data;
2409 for (j = rw; j > 3; j -= 4) {
2412 opj_v4dwt_interleave_v(&v, aj, w, 4);
2413 opj_v4dwt_decode(&v);
2415 for (k = 0; k < rh; ++k) {
2416 memcpy(&aj[k * (size_t)w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
2426 opj_v4dwt_interleave_v(&v, aj, w, j);
2427 opj_v4dwt_decode(&v);
2429 for (k = 0; k < rh; ++k) {
2430 memcpy(&aj[k * (size_t)w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
2435 opj_aligned_free(h.wavelet);
2440 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2443 opj_sparse_array_int32_t* sa;
2447 /* This value matches the maximum left/right extension given in tables */
2448 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
2449 /* we currently use 3. */
2450 const OPJ_UINT32 filter_width = 4U;
2452 opj_tcd_resolution_t* tr = tilec->resolutions;
2453 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2455 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2456 tr->x0); /* width of the resolution level computed */
2457 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2458 tr->y0); /* height of the resolution level computed */
2462 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2463 /* with the tile coordinates */
2464 OPJ_UINT32 win_tcx0 = tilec->win_x0;
2465 OPJ_UINT32 win_tcy0 = tilec->win_y0;
2466 OPJ_UINT32 win_tcx1 = tilec->win_x1;
2467 OPJ_UINT32 win_tcy1 = tilec->win_y1;
2469 sa = opj_dwt_init_sparse_array(tilec, numres);
2472 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2473 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2474 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2475 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2476 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2478 1, tr_max->win_x1 - tr_max->win_x0,
2482 opj_sparse_array_int32_free(sa);
2486 l_data_size = opj_dwt_max_resolution(tr, numres);
2487 /* overflow check */
2488 if (l_data_size > (SIZE_MAX - 5U)) {
2489 /* FIXME event manager error callback */
2493 /* overflow check */
2494 if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
2495 /* FIXME event manager error callback */
2498 h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
2500 /* FIXME event manager error callback */
2503 v.wavelet = h.wavelet;
2505 for (resno = 1; resno < numres; resno ++) {
2507 /* Window of interest subband-based coordinates */
2508 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2509 OPJ_UINT32 win_hl_x0, win_hl_x1;
2510 OPJ_UINT32 win_lh_y0, win_lh_y1;
2511 /* Window of interest tile-resolution-based coordinates */
2512 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2513 /* Tile-resolution subband-based coordinates */
2514 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2518 h.sn = (OPJ_INT32)rw;
2519 v.sn = (OPJ_INT32)rh;
2521 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2522 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2524 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2527 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2530 /* Get the subband coordinates for the window of interest */
2532 opj_dwt_get_band_coordinates(tilec, resno, 0,
2533 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2534 &win_ll_x0, &win_ll_y0,
2535 &win_ll_x1, &win_ll_y1);
2538 opj_dwt_get_band_coordinates(tilec, resno, 1,
2539 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2540 &win_hl_x0, NULL, &win_hl_x1, NULL);
2543 opj_dwt_get_band_coordinates(tilec, resno, 2,
2544 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2545 NULL, &win_lh_y0, NULL, &win_lh_y1);
2547 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2548 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2549 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2550 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2551 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2553 /* Substract the origin of the bands for this tile, to the subwindow */
2554 /* of interest band coordinates, so as to get them relative to the */
2556 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2557 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2558 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2559 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2560 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2561 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2562 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2563 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2565 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2566 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2568 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2569 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2571 /* Compute the tile-resolution-based coordinates for the window of interest */
2573 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2574 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2576 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2577 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2581 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2582 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2584 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2585 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2588 h.win_l_x0 = win_ll_x0;
2589 h.win_l_x1 = win_ll_x1;
2590 h.win_h_x0 = win_hl_x0;
2591 h.win_h_x1 = win_hl_x1;
2592 for (j = 0; j + 3 < rh; j += 4) {
2593 if ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2594 (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2595 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2597 opj_v4dwt_interleave_partial_h(&h, sa, j, opj_uint_min(4U, rh - j));
2598 opj_v4dwt_decode(&h);
2599 for (k = 0; k < 4; k++) {
2600 if (!opj_sparse_array_int32_write(sa,
2602 win_tr_x1, j + k + 1,
2603 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[k],
2605 /* FIXME event manager error callback */
2606 opj_sparse_array_int32_free(sa);
2607 opj_aligned_free(h.wavelet);
2615 ((j + 3 >= win_ll_y0 && j < win_ll_y1) ||
2616 (j + 3 >= win_lh_y0 + (OPJ_UINT32)v.sn &&
2617 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
2619 opj_v4dwt_interleave_partial_h(&h, sa, j, rh - j);
2620 opj_v4dwt_decode(&h);
2621 for (k = 0; k < rh - j; k++) {
2622 if (!opj_sparse_array_int32_write(sa,
2624 win_tr_x1, j + k + 1,
2625 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[k],
2627 /* FIXME event manager error callback */
2628 opj_sparse_array_int32_free(sa);
2629 opj_aligned_free(h.wavelet);
2635 v.win_l_x0 = win_ll_y0;
2636 v.win_l_x1 = win_ll_y1;
2637 v.win_h_x0 = win_lh_y0;
2638 v.win_h_x1 = win_lh_y1;
2639 for (j = win_tr_x0; j < win_tr_x1; j += 4) {
2640 OPJ_UINT32 nb_elts = opj_uint_min(4U, win_tr_x1 - j);
2643 opj_v4dwt_interleave_partial_v(&v, sa, j, nb_elts);
2644 opj_v4dwt_decode(&v);
2646 for (k = 0; k < nb_elts; k++) {
2647 if (!opj_sparse_array_int32_write(sa,
2649 j + k + 1, win_tr_y1,
2650 (OPJ_INT32*)&h.wavelet[win_tr_y0].f[k],
2652 /* FIXME event manager error callback */
2653 opj_sparse_array_int32_free(sa);
2654 opj_aligned_free(h.wavelet);
2662 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2663 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2664 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2665 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2666 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2668 1, tr_max->win_x1 - tr_max->win_x0,
2673 opj_sparse_array_int32_free(sa);
2675 opj_aligned_free(h.wavelet);
2680 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
2681 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2684 if (p_tcd->whole_tile_decoding) {
2685 return opj_dwt_decode_tile_97(tilec, numres);
2687 return opj_dwt_decode_partial_97(tilec, numres);