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 */
93 OPJ_FLOAT32 f[NB_ELTS_V8];
96 typedef struct v8dwt_local {
98 OPJ_INT32 dn ; /* number of elements in high pass band */
99 OPJ_INT32 sn ; /* number of elements in low pass band */
100 OPJ_INT32 cas ; /* 0 = start on even coord, 1 = start on odd coord */
101 OPJ_UINT32 win_l_x0; /* start coord in low pass band */
102 OPJ_UINT32 win_l_x1; /* end coord in low pass band */
103 OPJ_UINT32 win_h_x0; /* start coord in high pass band */
104 OPJ_UINT32 win_h_x1; /* end coord in high pass band */
107 /* From table F.4 from the standard */
108 static const OPJ_FLOAT32 opj_dwt_alpha = -1.586134342f;
109 static const OPJ_FLOAT32 opj_dwt_beta = -0.052980118f;
110 static const OPJ_FLOAT32 opj_dwt_gamma = 0.882911075f;
111 static const OPJ_FLOAT32 opj_dwt_delta = 0.443506852f;
113 static const OPJ_FLOAT32 opj_K = 1.230174105f;
114 static const OPJ_FLOAT32 opj_invK = (OPJ_FLOAT32)(1.0 / 1.230174105);
119 Virtual function type for wavelet transform in 1-D
121 typedef void (*DWT1DFN)(const opj_dwt_t* v);
123 /** @name Local static functions */
127 Forward lazy transform (horizontal)
129 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
130 OPJ_INT32 * OPJ_RESTRICT b,
132 OPJ_INT32 sn, OPJ_INT32 cas);
134 Forward lazy transform (vertical)
136 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
137 OPJ_INT32 * OPJ_RESTRICT b,
139 OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas);
141 Forward 5-3 wavelet transform in 1-D
143 static void opj_dwt_encode_1(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
146 Forward 9-7 wavelet transform in 1-D
148 static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
151 Explicit calculation of the Quantization Stepsizes
153 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
154 opj_stepsize_t *bandno_stepsize);
156 Inverse wavelet transform in 2-D.
158 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
159 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
161 static OPJ_BOOL opj_dwt_decode_partial_tile(
162 opj_tcd_tilecomp_t* tilec,
165 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
166 typedef void (*opj_encode_one_row_fnptr_type)(void *, OPJ_INT32, OPJ_INT32,
169 typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
175 static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
176 opj_tcd_tilecomp_t * tilec,
177 opj_encode_one_row_fnptr_type p_function,
178 opj_encode_and_deinterleave_h_one_row_fnptr_type
179 p_encode_and_deinterleave_h_one_row);
181 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
185 /* Inverse 9-7 wavelet transform in 1-D. */
192 #define OPJ_S(i) a[(i)*2]
193 #define OPJ_D(i) a[(1+(i)*2)]
194 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
195 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
197 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
198 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
201 /* This table contains the norms of the 5-3 wavelets for different bands. */
203 /* FIXME! the array should really be extended up to 33 resolution levels */
204 /* See https://github.com/uclouvain/openjpeg/issues/493 */
205 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
206 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
207 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
208 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
209 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
213 /* This table contains the norms of the 9-7 wavelets for different bands. */
215 /* FIXME! the array should really be extended up to 33 resolution levels */
216 /* See https://github.com/uclouvain/openjpeg/issues/493 */
217 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
218 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
219 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
220 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
221 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
225 ==========================================================
227 ==========================================================
231 /* Forward lazy transform (horizontal). */
233 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
234 OPJ_INT32 * OPJ_RESTRICT b,
236 OPJ_INT32 sn, OPJ_INT32 cas)
239 OPJ_INT32 * OPJ_RESTRICT l_dest = b;
240 const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
242 for (i = 0; i < sn; ++i) {
250 for (i = 0; i < dn; ++i) {
257 /* Forward lazy transform (vertical). */
259 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
260 OPJ_INT32 * OPJ_RESTRICT b,
262 OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas)
265 OPJ_INT32 * OPJ_RESTRICT l_dest = b;
266 const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
272 } /* b[i*x]=a[2*i+cas]; */
274 l_dest = b + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)x;
282 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
285 #ifdef STANDARD_SLOW_VERSION
287 /* Inverse lazy transform (horizontal). */
289 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
291 const OPJ_INT32 *ai = a;
292 OPJ_INT32 *bi = h->mem + h->cas;
299 bi = h->mem + 1 - h->cas;
308 /* Inverse lazy transform (vertical). */
310 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
312 const OPJ_INT32 *ai = a;
313 OPJ_INT32 *bi = v->mem + v->cas;
320 ai = a + (v->sn * (OPJ_SIZE_T)x);
321 bi = v->mem + 1 - v->cas;
330 #endif /* STANDARD_SLOW_VERSION */
333 /* Forward 5-3 wavelet transform in 1-D. */
335 static void opj_dwt_encode_1(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
339 OPJ_INT32* a = (OPJ_INT32*)aIn;
343 for (i = 0; i < sn - 1; i++) {
344 OPJ_D(i) -= (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
346 if (((sn + dn) % 2) == 0) {
347 OPJ_D(i) -= OPJ_S(i);
349 OPJ_S(0) += (OPJ_D(0) + OPJ_D(0) + 2) >> 2;
350 for (i = 1; i < dn; i++) {
351 OPJ_S(i) += (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
353 if (((sn + dn) % 2) == 1) {
354 OPJ_S(i) += (OPJ_D(i - 1) + OPJ_D(i - 1) + 2) >> 2;
361 OPJ_S(0) -= OPJ_D(0);
362 for (i = 1; i < sn; i++) {
363 OPJ_S(i) -= (OPJ_D(i) + OPJ_D(i - 1)) >> 1;
365 if (((sn + dn) % 2) == 1) {
366 OPJ_S(i) -= OPJ_D(i - 1);
368 for (i = 0; i < dn - 1; i++) {
369 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1) + 2) >> 2;
371 if (((sn + dn) % 2) == 0) {
372 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i) + 2) >> 2;
378 #ifdef STANDARD_SLOW_VERSION
380 /* Inverse 5-3 wavelet transform in 1-D. */
382 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
388 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
389 for (i = 0; i < sn; i++) {
390 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
392 for (i = 0; i < dn; i++) {
393 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
397 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
400 for (i = 0; i < sn; i++) {
401 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
403 for (i = 0; i < dn; i++) {
404 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
410 static void opj_dwt_decode_1(const opj_dwt_t *v)
412 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
415 #endif /* STANDARD_SLOW_VERSION */
417 #if !defined(STANDARD_SLOW_VERSION)
418 static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
424 const OPJ_INT32* in_even = &tiledp[0];
425 const OPJ_INT32* in_odd = &tiledp[sn];
427 #ifdef TWO_PASS_VERSION
428 /* For documentation purpose: performs lifting in two iterations, */
429 /* but without explicit interleaving */
434 tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
435 for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
436 tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
438 if (len & 1) { /* if len is odd */
439 tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
443 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
444 tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
446 if (!(len & 1)) { /* if len is even */
447 tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
450 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
454 /* Improved version of the TWO_PASS_VERSION: */
455 /* Performs lifting in one single iteration. Saves memory */
456 /* accesses and explicit interleaving. */
459 s0n = s1n - ((d1n + 1) >> 1);
461 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
468 s0n = s1n - ((d1c + d1n + 2) >> 2);
471 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
477 tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
478 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
480 tmp[len - 1] = d1n + s0n;
483 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
486 static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
492 const OPJ_INT32* in_even = &tiledp[sn];
493 const OPJ_INT32* in_odd = &tiledp[0];
495 #ifdef TWO_PASS_VERSION
496 /* For documentation purpose: performs lifting in two iterations, */
497 /* but without explicit interleaving */
502 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
503 tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
506 tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
510 tmp[0] = in_even[0] + tmp[1];
511 for (i = 2, j = 1; i < len - 1; i += 2, j++) {
512 tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
515 tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
518 OPJ_INT32 s1, s2, dc, dn;
522 /* Improved version of the TWO_PASS_VERSION: */
523 /* Performs lifting in one single iteration. Saves memory */
524 /* accesses and explicit interleaving. */
527 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
528 tmp[0] = in_even[0] + dc;
530 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
534 dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
536 tmp[i + 1] = s1 + ((dn + dc) >> 1);
545 dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
546 tmp[len - 2] = s1 + ((dn + dc) >> 1);
549 tmp[len - 1] = s1 + dc;
552 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
556 #endif /* !defined(STANDARD_SLOW_VERSION) */
559 /* Inverse 5-3 wavelet transform in 1-D for one row. */
561 /* Performs interleave, inverse wavelet transform and copy back to buffer */
562 static void opj_idwt53_h(const opj_dwt_t *dwt,
565 #ifdef STANDARD_SLOW_VERSION
566 /* For documentation purpose */
567 opj_dwt_interleave_h(dwt, tiledp);
568 opj_dwt_decode_1(dwt);
569 memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
571 const OPJ_INT32 sn = dwt->sn;
572 const OPJ_INT32 len = sn + dwt->dn;
573 if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
575 opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
577 /* Unmodified value */
579 } else { /* Left-most sample is on odd coordinate */
582 } else if (len == 2) {
583 OPJ_INT32* out = dwt->mem;
584 const OPJ_INT32* in_even = &tiledp[sn];
585 const OPJ_INT32* in_odd = &tiledp[0];
586 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
587 out[0] = in_even[0] + out[1];
588 memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
589 } else if (len > 2) {
590 opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
596 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
598 /* Conveniency macros to improve the readabilty of the formulas */
601 #define LOAD_CST(x) _mm256_set1_epi32(x)
602 #define LOAD(x) _mm256_load_si256((const VREG*)(x))
603 #define LOADU(x) _mm256_loadu_si256((const VREG*)(x))
604 #define STORE(x,y) _mm256_store_si256((VREG*)(x),(y))
605 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
606 #define ADD(x,y) _mm256_add_epi32((x),(y))
607 #define SUB(x,y) _mm256_sub_epi32((x),(y))
608 #define SAR(x,y) _mm256_srai_epi32((x),(y))
611 #define LOAD_CST(x) _mm_set1_epi32(x)
612 #define LOAD(x) _mm_load_si128((const VREG*)(x))
613 #define LOADU(x) _mm_loadu_si128((const VREG*)(x))
614 #define STORE(x,y) _mm_store_si128((VREG*)(x),(y))
615 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
616 #define ADD(x,y) _mm_add_epi32((x),(y))
617 #define SUB(x,y) _mm_sub_epi32((x),(y))
618 #define SAR(x,y) _mm_srai_epi32((x),(y))
620 #define ADD3(x,y,z) ADD(ADD(x,y),z)
623 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
624 const OPJ_INT32* tmp,
629 for (i = 0; i < len; ++i) {
630 /* A memcpy(&tiledp_col[i * stride + 0],
631 &tmp[PARALLEL_COLS_53 * i + 0],
632 PARALLEL_COLS_53 * sizeof(OPJ_INT32))
633 would do but would be a tiny bit slower.
634 We can take here advantage of our knowledge of alignment */
635 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
636 LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
637 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
638 LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
642 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
643 * 16 in AVX2, when top-most pixel is on even coordinate */
644 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
648 OPJ_INT32* tiledp_col,
649 const OPJ_SIZE_T stride)
651 const OPJ_INT32* in_even = &tiledp_col[0];
652 const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
656 VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
657 VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
658 const VREG two = LOAD_CST(2);
662 assert(PARALLEL_COLS_53 == 16);
663 assert(VREG_INT_COUNT == 8);
665 assert(PARALLEL_COLS_53 == 8);
666 assert(VREG_INT_COUNT == 4);
669 /* Note: loads of input even/odd values must be done in a unaligned */
670 /* fashion. But stores in tmp can be done with aligned store, since */
671 /* the temporary buffer is properly aligned */
672 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
674 s1n_0 = LOADU(in_even + 0);
675 s1n_1 = LOADU(in_even + VREG_INT_COUNT);
676 d1n_0 = LOADU(in_odd);
677 d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
679 /* s0n = s1n - ((d1n + 1) >> 1); <==> */
680 /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
681 s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
682 s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
684 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
690 s1n_0 = LOADU(in_even + j * stride);
691 s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
692 d1n_0 = LOADU(in_odd + j * stride);
693 d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
695 /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
696 s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
697 s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
699 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
700 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
702 /* d1c + ((s0c + s0n) >> 1) */
703 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
704 ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
705 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
706 ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
709 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
710 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
713 VREG tmp_len_minus_1;
714 s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
715 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
716 tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
717 STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
718 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
719 STORE(tmp + PARALLEL_COLS_53 * (len - 2),
720 ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
722 s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
723 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
724 tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
725 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
727 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
728 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
729 ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
733 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
735 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
739 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
743 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
744 * 16 in AVX2, when top-most pixel is on odd coordinate */
745 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
749 OPJ_INT32* tiledp_col,
750 const OPJ_SIZE_T stride)
755 VREG s1_0, s2_0, dc_0, dn_0;
756 VREG s1_1, s2_1, dc_1, dn_1;
757 const VREG two = LOAD_CST(2);
759 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
760 const OPJ_INT32* in_odd = &tiledp_col[0];
764 assert(PARALLEL_COLS_53 == 16);
765 assert(VREG_INT_COUNT == 8);
767 assert(PARALLEL_COLS_53 == 8);
768 assert(VREG_INT_COUNT == 4);
771 /* Note: loads of input even/odd values must be done in a unaligned */
772 /* fashion. But stores in tmp can be done with aligned store, since */
773 /* the temporary buffer is properly aligned */
774 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
776 s1_0 = LOADU(in_even + stride);
777 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
778 dc_0 = SUB(LOADU(in_odd + 0),
779 SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
780 STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
782 s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
783 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
784 dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
785 SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
786 STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
787 ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
789 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
791 s2_0 = LOADU(in_even + (j + 1) * stride);
792 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
794 /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
795 dn_0 = SUB(LOADU(in_odd + j * stride),
796 SAR(ADD3(s1_0, s2_0, two), 2));
797 dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
798 SAR(ADD3(s1_1, s2_1, two), 2));
800 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
801 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
803 /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
804 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
805 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
806 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
807 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
814 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
815 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
818 /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
819 dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
820 SAR(ADD3(s1_0, s1_0, two), 2));
821 dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
822 SAR(ADD3(s1_1, s1_1, two), 2));
824 /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
825 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
826 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
827 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
828 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
830 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
831 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
833 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
834 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
838 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
852 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
854 #if !defined(STANDARD_SLOW_VERSION)
855 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
856 * pixel is on even coordinate */
857 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
860 OPJ_INT32* tiledp_col,
861 const OPJ_SIZE_T stride)
864 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
868 /* Performs lifting in one single iteration. Saves memory */
869 /* accesses and explicit interleaving. */
872 d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
873 s0n = s1n - ((d1n + 1) >> 1);
875 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
879 s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
880 d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
882 s0n = s1n - ((d1c + d1n + 2) >> 2);
885 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
892 tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
894 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
896 tmp[len - 1] = d1n + s0n;
899 for (i = 0; i < len; ++i) {
900 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
905 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
906 * pixel is on odd coordinate */
907 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
910 OPJ_INT32* tiledp_col,
911 const OPJ_SIZE_T stride)
914 OPJ_INT32 s1, s2, dc, dn;
915 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
916 const OPJ_INT32* in_odd = &tiledp_col[0];
920 /* Performs lifting in one single iteration. Saves memory */
921 /* accesses and explicit interleaving. */
923 s1 = in_even[stride];
924 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
925 tmp[0] = in_even[0] + dc;
926 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
928 s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
930 dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
932 tmp[i + 1] = s1 + ((dn + dc) >> 1);
939 dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
940 tmp[len - 2] = s1 + ((dn + dc) >> 1);
943 tmp[len - 1] = s1 + dc;
946 for (i = 0; i < len; ++i) {
947 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
950 #endif /* !defined(STANDARD_SLOW_VERSION) */
953 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
955 /* Performs interleave, inverse wavelet transform and copy back to buffer */
956 static void opj_idwt53_v(const opj_dwt_t *dwt,
957 OPJ_INT32* tiledp_col,
961 #ifdef STANDARD_SLOW_VERSION
962 /* For documentation purpose */
964 for (c = 0; c < nb_cols; c ++) {
965 opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
966 opj_dwt_decode_1(dwt);
967 for (k = 0; k < dwt->sn + dwt->dn; ++k) {
968 tiledp_col[c + k * stride] = dwt->mem[k];
972 const OPJ_INT32 sn = dwt->sn;
973 const OPJ_INT32 len = sn + dwt->dn;
975 /* If len == 1, unmodified value */
977 #if (defined(__SSE2__) || defined(__AVX2__))
978 if (len > 1 && nb_cols == PARALLEL_COLS_53) {
979 /* Same as below general case, except that thanks to SSE2/AVX2 */
980 /* we can efficiently process 8/16 columns in parallel */
981 opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
987 for (c = 0; c < nb_cols; c++, tiledp_col++) {
988 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
995 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1003 OPJ_INT32* out = dwt->mem;
1004 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1006 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
1007 const OPJ_INT32* in_odd = &tiledp_col[0];
1009 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
1010 out[0] = in_even[0] + out[1];
1012 for (i = 0; i < len; ++i) {
1013 tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
1020 #if (defined(__SSE2__) || defined(__AVX2__))
1021 if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1022 /* Same as below general case, except that thanks to SSE2/AVX2 */
1023 /* we can efficiently process 8/16 columns in parallel */
1024 opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1030 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1031 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1039 static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
1042 const OPJ_FLOAT32 c)
1045 for (i = start; i < end; ++i) {
1049 static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1056 OPJ_UINT32 imax = opj_uint_min(end, m);
1061 for (i = start; i < imax; ++i) {
1062 fw[-1] += (fl[0] + fw[0]) * c;
1067 assert(m + 1 == end);
1068 fw[-1] += (2 * fl[0]) * c;
1072 static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1075 OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1078 if (!((dn > 0) || (sn > 1))) {
1084 if (!((sn > 0) || (dn > 1))) {
1090 opj_dwt_encode_step2(w + a, w + b + 1,
1092 (OPJ_UINT32)opj_int_min(dn, sn - b),
1094 opj_dwt_encode_step2(w + b, w + a + 1,
1096 (OPJ_UINT32)opj_int_min(sn, dn - a),
1098 opj_dwt_encode_step2(w + a, w + b + 1,
1100 (OPJ_UINT32)opj_int_min(dn, sn - b),
1102 opj_dwt_encode_step2(w + b, w + a + 1,
1104 (OPJ_UINT32)opj_int_min(sn, dn - a),
1106 opj_dwt_encode_step1(w + b, 0, (OPJ_UINT32)dn,
1108 opj_dwt_encode_step1(w + a, 0, (OPJ_UINT32)sn,
1112 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1113 opj_stepsize_t *bandno_stepsize)
1116 p = opj_int_floorlog2(stepsize) - 13;
1117 n = 11 - opj_int_floorlog2(stepsize);
1118 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1119 bandno_stepsize->expn = numbps - p;
1123 ==========================================================
1125 ==========================================================
1128 /** Process one line for the horizontal pass of the 5x3 forward transform */
1130 void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1135 OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1136 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1137 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1138 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1143 for (i = 0; i < sn - 1; i++) {
1144 tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1);
1146 if ((width % 2) == 0) {
1147 tmp[sn + i] = row[2 * i + 1] - row[(i) * 2];
1149 row[0] += (tmp[sn] + tmp[sn] + 2) >> 2;
1150 for (i = 1; i < dn; i++) {
1151 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2);
1153 if ((width % 2) == 1) {
1154 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2);
1156 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1163 tmp[sn + 0] = row[0] - row[1];
1164 for (i = 1; i < sn; i++) {
1165 tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1);
1167 if ((width % 2) == 1) {
1168 tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1];
1171 for (i = 0; i < dn - 1; i++) {
1172 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2);
1174 if ((width % 2) == 0) {
1175 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2);
1177 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1182 /** Process one line for the horizontal pass of the 9x7 forward transform */
1184 void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1189 OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1190 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1191 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1192 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1193 memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1194 opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1195 opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1196 (OPJ_INT32 * OPJ_RESTRICT)row,
1197 dn, sn, even ? 0 : 1);
1202 OPJ_UINT32 rw; /* Width of the resolution to process */
1203 OPJ_UINT32 w; /* Width of tiledp */
1204 OPJ_INT32 * OPJ_RESTRICT tiledp;
1207 opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1208 } opj_dwt_encode_h_job_t;
1210 static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1213 opj_dwt_encode_h_job_t* job;
1216 job = (opj_dwt_encode_h_job_t*)user_data;
1217 for (j = job->min_j; j < job->max_j; j++) {
1218 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1219 (*job->p_function)(aj, job->h.mem, job->rw,
1220 job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1223 opj_aligned_free(job->h.mem);
1231 OPJ_INT32 * OPJ_RESTRICT tiledp;
1234 opj_encode_one_row_fnptr_type p_function;
1235 } opj_dwt_encode_v_job_t;
1237 static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1240 opj_dwt_encode_v_job_t* job;
1243 job = (opj_dwt_encode_v_job_t*)user_data;
1244 for (j = job->min_j; j < job->max_j; j++) {
1245 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j;
1247 for (k = 0; k < job->rh; ++k) {
1248 job->v.mem[k] = aj[k * job->w];
1251 (*job->p_function)(job->v.mem, job->v.dn, job->v.sn, job->v.cas);
1253 opj_dwt_deinterleave_v(job->v.mem, aj, job->v.dn, job->v.sn, job->w,
1257 opj_aligned_free(job->v.mem);
1262 /* Forward 5-3 wavelet transform in 2-D. */
1264 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1265 opj_tcd_tilecomp_t * tilec,
1266 opj_encode_one_row_fnptr_type p_function,
1267 opj_encode_and_deinterleave_h_one_row_fnptr_type
1268 p_encode_and_deinterleave_h_one_row)
1275 OPJ_SIZE_T l_data_size;
1277 opj_tcd_resolution_t * l_cur_res = 0;
1278 opj_tcd_resolution_t * l_last_res = 0;
1279 const int num_threads = opj_thread_pool_get_thread_count(tp);
1280 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1282 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1283 l = (OPJ_INT32)tilec->numresolutions - 1;
1285 l_cur_res = tilec->resolutions + l;
1286 l_last_res = l_cur_res - 1;
1288 l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1289 /* overflow check */
1290 if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1291 /* FIXME event manager error callback */
1294 l_data_size *= sizeof(OPJ_INT32);
1295 bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1296 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1297 /* in that case, so do not error out */
1298 if (l_data_size != 0 && ! bj) {
1305 OPJ_UINT32 rw; /* width of the resolution level computed */
1306 OPJ_UINT32 rh; /* height of the resolution level computed */
1308 rw1; /* width of the resolution level once lower than computed one */
1310 rh1; /* height of the resolution level once lower than computed one */
1311 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1312 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
1315 rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1316 rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1317 rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1318 rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1320 cas_row = l_cur_res->x0 & 1;
1321 cas_col = l_cur_res->y0 & 1;
1323 sn = (OPJ_INT32)rh1;
1324 dn = (OPJ_INT32)(rh - rh1);
1326 /* Perform vertical pass */
1327 if (num_threads <= 1 || rw <= 1) {
1328 for (j = 0; j < rw; ++j) {
1329 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j;
1331 for (k = 0; k < rh; ++k) {
1335 (*p_function)(bj, dn, sn, cas_col);
1337 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1340 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1343 if (rw < num_jobs) {
1346 step_j = (rw / num_jobs);
1348 for (j = 0; j < num_jobs; j++) {
1349 opj_dwt_encode_v_job_t* job;
1351 job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
1353 opj_thread_pool_wait_completion(tp, 0);
1354 opj_aligned_free(bj);
1357 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1359 opj_thread_pool_wait_completion(tp, 0);
1361 opj_aligned_free(bj);
1366 job->v.cas = cas_col;
1369 job->tiledp = tiledp;
1370 job->min_j = j * step_j;
1371 job->max_j = (j + 1U) * step_j; /* this can overflow */
1372 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1375 job->p_function = p_function;
1376 opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
1378 opj_thread_pool_wait_completion(tp, 0);
1381 sn = (OPJ_INT32)rw1;
1382 dn = (OPJ_INT32)(rw - rw1);
1384 /* Perform horizontal pass */
1385 if (num_threads <= 1 || rh <= 1) {
1386 for (j = 0; j < rh; j++) {
1387 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
1388 (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
1389 cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
1392 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1395 if (rh < num_jobs) {
1398 step_j = (rh / num_jobs);
1400 for (j = 0; j < num_jobs; j++) {
1401 opj_dwt_encode_h_job_t* job;
1403 job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
1405 opj_thread_pool_wait_completion(tp, 0);
1406 opj_aligned_free(bj);
1409 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1411 opj_thread_pool_wait_completion(tp, 0);
1413 opj_aligned_free(bj);
1418 job->h.cas = cas_row;
1421 job->tiledp = tiledp;
1422 job->min_j = j * step_j;
1423 job->max_j = (j + 1U) * step_j; /* this can overflow */
1424 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1427 job->p_function = p_encode_and_deinterleave_h_one_row;
1428 opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
1430 opj_thread_pool_wait_completion(tp, 0);
1433 l_cur_res = l_last_res;
1438 opj_aligned_free(bj);
1442 /* Forward 5-3 wavelet transform in 2-D. */
1444 OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
1445 opj_tcd_tilecomp_t * tilec)
1447 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1449 opj_dwt_encode_and_deinterleave_h_one_row);
1453 /* Inverse 5-3 wavelet transform in 2-D. */
1455 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1458 if (p_tcd->whole_tile_decoding) {
1459 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1461 return opj_dwt_decode_partial_tile(tilec, numres);
1466 /* Get norm of 5-3 wavelet. */
1468 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1470 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1471 /* but the array should really be extended up to 33 resolution levels */
1472 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1473 if (orient == 0 && level >= 10) {
1475 } else if (orient > 0 && level >= 9) {
1478 return opj_dwt_norms[orient][level];
1482 /* Forward 9-7 wavelet transform in 2-D. */
1484 OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
1485 opj_tcd_tilecomp_t * tilec)
1487 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1488 opj_dwt_encode_1_real,
1489 opj_dwt_encode_and_deinterleave_h_one_row_real);
1493 /* Get norm of 9-7 wavelet. */
1495 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1497 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1498 /* but the array should really be extended up to 33 resolution levels */
1499 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1500 if (orient == 0 && level >= 10) {
1502 } else if (orient > 0 && level >= 9) {
1505 return opj_dwt_norms_real[orient][level];
1508 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1510 OPJ_UINT32 numbands, bandno;
1511 numbands = 3 * tccp->numresolutions - 2;
1512 for (bandno = 0; bandno < numbands; bandno++) {
1513 OPJ_FLOAT64 stepsize;
1514 OPJ_UINT32 resno, level, orient, gain;
1516 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1517 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1518 level = tccp->numresolutions - 1 - resno;
1519 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1520 (orient == 2)) ? 1 : 2));
1521 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1524 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1525 stepsize = (1 << (gain)) / norm;
1527 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1528 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1533 /* Determine maximum computed resolution level for inverse wavelet transform */
1535 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1542 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1545 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1556 OPJ_INT32 * OPJ_RESTRICT tiledp;
1559 } opj_dwt_decode_h_job_t;
1561 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1564 opj_dwt_decode_h_job_t* job;
1567 job = (opj_dwt_decode_h_job_t*)user_data;
1568 for (j = job->min_j; j < job->max_j; j++) {
1569 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1572 opj_aligned_free(job->h.mem);
1580 OPJ_INT32 * OPJ_RESTRICT tiledp;
1583 } opj_dwt_decode_v_job_t;
1585 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1588 opj_dwt_decode_v_job_t* job;
1591 job = (opj_dwt_decode_v_job_t*)user_data;
1592 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1593 j += PARALLEL_COLS_53) {
1594 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1598 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1599 (OPJ_INT32)(job->max_j - j));
1601 opj_aligned_free(job->v.mem);
1607 /* Inverse wavelet transform in 2-D. */
1609 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1610 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1615 opj_tcd_resolution_t* tr = tilec->resolutions;
1617 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1618 tr->x0); /* width of the resolution level computed */
1619 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1620 tr->y0); /* height of the resolution level computed */
1622 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1624 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1625 OPJ_SIZE_T h_mem_size;
1631 num_threads = opj_thread_pool_get_thread_count(tp);
1632 h_mem_size = opj_dwt_max_resolution(tr, numres);
1633 /* overflow check */
1634 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1635 /* FIXME event manager error callback */
1638 /* We need PARALLEL_COLS_53 times the height of the array, */
1639 /* since for the vertical pass */
1640 /* we process PARALLEL_COLS_53 columns at a time */
1641 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1642 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1644 /* FIXME event manager error callback */
1651 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1655 h.sn = (OPJ_INT32)rw;
1656 v.sn = (OPJ_INT32)rh;
1658 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1659 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1661 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1664 if (num_threads <= 1 || rh <= 1) {
1665 for (j = 0; j < rh; ++j) {
1666 opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
1669 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1672 if (rh < num_jobs) {
1675 step_j = (rh / num_jobs);
1677 for (j = 0; j < num_jobs; j++) {
1678 opj_dwt_decode_h_job_t* job;
1680 job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
1682 /* It would be nice to fallback to single thread case, but */
1683 /* unfortunately some jobs may be launched and have modified */
1684 /* tiledp, so it is not practical to recover from that error */
1685 /* FIXME event manager error callback */
1686 opj_thread_pool_wait_completion(tp, 0);
1687 opj_aligned_free(h.mem);
1693 job->tiledp = tiledp;
1694 job->min_j = j * step_j;
1695 job->max_j = (j + 1U) * step_j; /* this can overflow */
1696 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1699 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1701 /* FIXME event manager error callback */
1702 opj_thread_pool_wait_completion(tp, 0);
1704 opj_aligned_free(h.mem);
1707 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1709 opj_thread_pool_wait_completion(tp, 0);
1712 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1715 if (num_threads <= 1 || rw <= 1) {
1716 for (j = 0; j + PARALLEL_COLS_53 <= rw;
1717 j += PARALLEL_COLS_53) {
1718 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
1721 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
1724 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1727 if (rw < num_jobs) {
1730 step_j = (rw / num_jobs);
1732 for (j = 0; j < num_jobs; j++) {
1733 opj_dwt_decode_v_job_t* job;
1735 job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
1737 /* It would be nice to fallback to single thread case, but */
1738 /* unfortunately some jobs may be launched and have modified */
1739 /* tiledp, so it is not practical to recover from that error */
1740 /* FIXME event manager error callback */
1741 opj_thread_pool_wait_completion(tp, 0);
1742 opj_aligned_free(v.mem);
1748 job->tiledp = tiledp;
1749 job->min_j = j * step_j;
1750 job->max_j = (j + 1U) * step_j; /* this can overflow */
1751 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1754 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1756 /* FIXME event manager error callback */
1757 opj_thread_pool_wait_completion(tp, 0);
1759 opj_aligned_free(v.mem);
1762 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1764 opj_thread_pool_wait_completion(tp, 0);
1767 opj_aligned_free(h.mem);
1771 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1773 opj_sparse_array_int32_t* sa,
1776 OPJ_UINT32 win_l_x0,
1777 OPJ_UINT32 win_l_x1,
1778 OPJ_UINT32 win_h_x0,
1779 OPJ_UINT32 win_h_x1)
1782 ret = opj_sparse_array_int32_read(sa,
1784 win_l_x1, sa_line + 1,
1785 dest + cas + 2 * win_l_x0,
1788 ret = opj_sparse_array_int32_read(sa,
1789 sn + win_h_x0, sa_line,
1790 sn + win_h_x1, sa_line + 1,
1791 dest + 1 - cas + 2 * win_h_x0,
1798 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1800 opj_sparse_array_int32_t* sa,
1804 OPJ_UINT32 win_l_y0,
1805 OPJ_UINT32 win_l_y1,
1806 OPJ_UINT32 win_h_y0,
1807 OPJ_UINT32 win_h_y1)
1810 ret = opj_sparse_array_int32_read(sa,
1812 sa_col + nb_cols, win_l_y1,
1813 dest + cas * 4 + 2 * 4 * win_l_y0,
1814 1, 2 * 4, OPJ_TRUE);
1816 ret = opj_sparse_array_int32_read(sa,
1817 sa_col, sn + win_h_y0,
1818 sa_col + nb_cols, sn + win_h_y1,
1819 dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
1820 1, 2 * 4, OPJ_TRUE);
1825 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1835 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1837 /* Naive version is :
1838 for (i = win_l_x0; i < i_max; i++) {
1839 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1841 for (i = win_h_x0; i < win_h_x1; i++) {
1842 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1844 but the compiler doesn't manage to unroll it to avoid bound
1845 checking in OPJ_S_ and OPJ_D_ macros
1852 /* Left-most case */
1853 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1860 for (; i < i_max; i++) {
1861 /* No bound checking */
1862 OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
1864 for (; i < win_l_x1; i++) {
1865 /* Right-most case */
1866 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1872 OPJ_INT32 i_max = win_h_x1;
1876 for (; i < i_max; i++) {
1877 /* No bound checking */
1878 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
1880 for (; i < win_h_x1; i++) {
1881 /* Right-most case */
1882 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1887 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
1890 for (i = win_l_x0; i < win_l_x1; i++) {
1891 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1893 for (i = win_h_x0; i < win_h_x1; i++) {
1894 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1900 #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
1901 #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
1902 #define OPJ_S__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=sn?OPJ_S_off(sn-1,off):OPJ_S_off(i,off)))
1903 #define OPJ_D__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=dn?OPJ_D_off(dn-1,off):OPJ_D_off(i,off)))
1904 #define OPJ_SS__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=dn?OPJ_S_off(dn-1,off):OPJ_S_off(i,off)))
1905 #define OPJ_DD__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=sn?OPJ_D_off(sn-1,off):OPJ_D_off(i,off)))
1907 static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
1909 OPJ_INT32 dn, OPJ_INT32 sn,
1922 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1924 /* Naive version is :
1925 for (i = win_l_x0; i < i_max; i++) {
1926 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1928 for (i = win_h_x0; i < win_h_x1; i++) {
1929 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1931 but the compiler doesn't manage to unroll it to avoid bound
1932 checking in OPJ_S_ and OPJ_D_ macros
1939 /* Left-most case */
1940 for (off = 0; off < 4; off++) {
1941 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1951 if (i + 1 < i_max) {
1952 const __m128i two = _mm_set1_epi32(2);
1953 __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
1954 for (; i + 1 < i_max; i += 2) {
1955 /* No bound checking */
1956 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
1957 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1958 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1959 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
1960 S = _mm_sub_epi32(S,
1961 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
1962 S1 = _mm_sub_epi32(S1,
1963 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
1964 _mm_store_si128((__m128i*)(a + i * 8), S);
1965 _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
1971 for (; i < i_max; i++) {
1972 /* No bound checking */
1973 for (off = 0; off < 4; off++) {
1974 OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
1977 for (; i < win_l_x1; i++) {
1978 /* Right-most case */
1979 for (off = 0; off < 4; off++) {
1980 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1987 OPJ_INT32 i_max = win_h_x1;
1993 if (i + 1 < i_max) {
1994 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
1995 for (; i + 1 < i_max; i += 2) {
1996 /* No bound checking */
1997 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1998 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1999 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2000 __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
2001 D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
2002 D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
2003 _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
2004 _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
2010 for (; i < i_max; i++) {
2011 /* No bound checking */
2012 for (off = 0; off < 4; off++) {
2013 OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
2016 for (; i < win_h_x1; i++) {
2017 /* Right-most case */
2018 for (off = 0; off < 4; off++) {
2019 OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
2025 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
2026 for (off = 0; off < 4; off++) {
2027 OPJ_S_off(0, off) /= 2;
2030 for (i = win_l_x0; i < win_l_x1; i++) {
2031 for (off = 0; off < 4; off++) {
2032 OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2;
2035 for (i = win_h_x0; i < win_h_x1; i++) {
2036 for (off = 0; off < 4; off++) {
2037 OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1;
2044 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
2056 /* Compute number of decomposition for this band. See table F-1 */
2057 OPJ_UINT32 nb = (resno == 0) ?
2058 tilec->numresolutions - 1 :
2059 tilec->numresolutions - resno;
2060 /* Map above tile-based coordinates to sub-band-based coordinates per */
2061 /* equation B-15 of the standard */
2062 OPJ_UINT32 x0b = bandno & 1;
2063 OPJ_UINT32 y0b = bandno >> 1;
2065 *tbx0 = (nb == 0) ? tcx0 :
2066 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2067 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2070 *tby0 = (nb == 0) ? tcy0 :
2071 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2072 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2075 *tbx1 = (nb == 0) ? tcx1 :
2076 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2077 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2080 *tby1 = (nb == 0) ? tcy1 :
2081 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2082 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2086 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2087 OPJ_UINT32 max_size,
2091 *start = opj_uint_subs(*start, filter_width);
2092 *end = opj_uint_adds(*end, filter_width);
2093 *end = opj_uint_min(*end, max_size);
2097 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2098 opj_tcd_tilecomp_t* tilec,
2101 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2102 OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2103 OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2104 OPJ_UINT32 resno, bandno, precno, cblkno;
2105 opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2106 w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2111 for (resno = 0; resno < numres; ++resno) {
2112 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2114 for (bandno = 0; bandno < res->numbands; ++bandno) {
2115 opj_tcd_band_t* band = &res->bands[bandno];
2117 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2118 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2119 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2120 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2121 if (cblk->decoded_data != NULL) {
2122 OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2123 OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2124 OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2125 OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2127 if (band->bandno & 1) {
2128 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2129 x += (OPJ_UINT32)(pres->x1 - pres->x0);
2131 if (band->bandno & 2) {
2132 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2133 y += (OPJ_UINT32)(pres->y1 - pres->y0);
2136 if (!opj_sparse_array_int32_write(sa, x, y,
2137 x + cblk_w, y + cblk_h,
2139 1, cblk_w, OPJ_TRUE)) {
2140 opj_sparse_array_int32_free(sa);
2153 static OPJ_BOOL opj_dwt_decode_partial_tile(
2154 opj_tcd_tilecomp_t* tilec,
2157 opj_sparse_array_int32_t* sa;
2161 /* This value matches the maximum left/right extension given in tables */
2162 /* F.2 and F.3 of the standard. */
2163 const OPJ_UINT32 filter_width = 2U;
2165 opj_tcd_resolution_t* tr = tilec->resolutions;
2166 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2168 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2169 tr->x0); /* width of the resolution level computed */
2170 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2171 tr->y0); /* height of the resolution level computed */
2173 OPJ_SIZE_T h_mem_size;
2175 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2176 /* with the tile coordinates */
2177 OPJ_UINT32 win_tcx0 = tilec->win_x0;
2178 OPJ_UINT32 win_tcy0 = tilec->win_y0;
2179 OPJ_UINT32 win_tcx1 = tilec->win_x1;
2180 OPJ_UINT32 win_tcy1 = tilec->win_y1;
2182 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2186 sa = opj_dwt_init_sparse_array(tilec, numres);
2192 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2193 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2194 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2195 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2196 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2198 1, tr_max->win_x1 - tr_max->win_x0,
2202 opj_sparse_array_int32_free(sa);
2205 h_mem_size = opj_dwt_max_resolution(tr, numres);
2206 /* overflow check */
2207 /* in vertical pass, we process 4 columns at a time */
2208 if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2209 /* FIXME event manager error callback */
2210 opj_sparse_array_int32_free(sa);
2214 h_mem_size *= 4 * sizeof(OPJ_INT32);
2215 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2217 /* FIXME event manager error callback */
2218 opj_sparse_array_int32_free(sa);
2224 for (resno = 1; resno < numres; resno ++) {
2226 /* Window of interest subband-based coordinates */
2227 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2228 OPJ_UINT32 win_hl_x0, win_hl_x1;
2229 OPJ_UINT32 win_lh_y0, win_lh_y1;
2230 /* Window of interest tile-resolution-based coordinates */
2231 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2232 /* Tile-resolution subband-based coordinates */
2233 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2237 h.sn = (OPJ_INT32)rw;
2238 v.sn = (OPJ_INT32)rh;
2240 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2241 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2243 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2246 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2249 /* Get the subband coordinates for the window of interest */
2251 opj_dwt_get_band_coordinates(tilec, resno, 0,
2252 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2253 &win_ll_x0, &win_ll_y0,
2254 &win_ll_x1, &win_ll_y1);
2257 opj_dwt_get_band_coordinates(tilec, resno, 1,
2258 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2259 &win_hl_x0, NULL, &win_hl_x1, NULL);
2262 opj_dwt_get_band_coordinates(tilec, resno, 2,
2263 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2264 NULL, &win_lh_y0, NULL, &win_lh_y1);
2266 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2267 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2268 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2269 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2270 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2272 /* Subtract the origin of the bands for this tile, to the subwindow */
2273 /* of interest band coordinates, so as to get them relative to the */
2275 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2276 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2277 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2278 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2279 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2280 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2281 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2282 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2284 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2285 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2287 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2288 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2290 /* Compute the tile-resolution-based coordinates for the window of interest */
2292 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2293 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2295 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2296 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2300 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2301 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2303 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2304 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2307 for (j = 0; j < rh; ++j) {
2308 if ((j >= win_ll_y0 && j < win_ll_y1) ||
2309 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2311 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2312 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2313 /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2314 /* This is less extreme than memsetting the whole buffer to 0 */
2315 /* although we could potentially do better with better handling of edge conditions */
2316 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2317 h.mem[win_tr_x1 - 1] = 0;
2319 if (win_tr_x1 < rw) {
2320 h.mem[win_tr_x1] = 0;
2323 opj_dwt_interleave_partial_h(h.mem,
2332 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
2333 (OPJ_INT32)win_ll_x0,
2334 (OPJ_INT32)win_ll_x1,
2335 (OPJ_INT32)win_hl_x0,
2336 (OPJ_INT32)win_hl_x1);
2337 if (!opj_sparse_array_int32_write(sa,
2342 /* FIXME event manager error callback */
2343 opj_sparse_array_int32_free(sa);
2344 opj_aligned_free(h.mem);
2350 for (i = win_tr_x0; i < win_tr_x1;) {
2351 OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
2352 opj_dwt_interleave_partial_v(v.mem,
2362 opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
2363 (OPJ_INT32)win_ll_y0,
2364 (OPJ_INT32)win_ll_y1,
2365 (OPJ_INT32)win_lh_y0,
2366 (OPJ_INT32)win_lh_y1);
2367 if (!opj_sparse_array_int32_write(sa,
2369 i + nb_cols, win_tr_y1,
2370 v.mem + 4 * win_tr_y0,
2372 /* FIXME event manager error callback */
2373 opj_sparse_array_int32_free(sa);
2374 opj_aligned_free(h.mem);
2381 opj_aligned_free(h.mem);
2384 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2385 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2386 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2387 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2388 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2390 1, tr_max->win_x1 - tr_max->win_x0,
2395 opj_sparse_array_int32_free(sa);
2399 static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
2400 OPJ_FLOAT32* OPJ_RESTRICT a,
2402 OPJ_UINT32 remaining_height)
2404 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2406 OPJ_UINT32 x0 = dwt->win_l_x0;
2407 OPJ_UINT32 x1 = dwt->win_l_x1;
2409 for (k = 0; k < 2; ++k) {
2410 if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
2411 ((OPJ_SIZE_T) bi & 0x0f) == 0) {
2412 /* Fast code path */
2413 for (i = x0; i < x1; ++i) {
2415 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2433 /* Slow code path */
2434 for (i = x0; i < x1; ++i) {
2436 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2439 if (remaining_height == 1) {
2444 if (remaining_height == 2) {
2449 if (remaining_height == 3) {
2454 if (remaining_height == 4) {
2459 if (remaining_height == 5) {
2464 if (remaining_height == 6) {
2469 if (remaining_height == 7) {
2476 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2483 static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
2484 opj_sparse_array_int32_t* sa,
2486 OPJ_UINT32 remaining_height)
2489 for (i = 0; i < remaining_height; i++) {
2491 ret = opj_sparse_array_int32_read(sa,
2492 dwt->win_l_x0, sa_line + i,
2493 dwt->win_l_x1, sa_line + i + 1,
2494 /* Nasty cast from float* to int32* */
2495 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2496 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2498 ret = opj_sparse_array_int32_read(sa,
2499 (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2500 (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2501 /* Nasty cast from float* to int32* */
2502 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2503 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2509 static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2510 OPJ_FLOAT32* OPJ_RESTRICT a,
2512 OPJ_UINT32 nb_elts_read)
2514 opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2517 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2518 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2519 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2522 a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
2523 bi = dwt->wavelet + 1 - dwt->cas;
2525 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2526 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2527 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2531 static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2532 opj_sparse_array_int32_t* sa,
2534 OPJ_UINT32 nb_elts_read)
2537 ret = opj_sparse_array_int32_read(sa,
2538 sa_col, dwt->win_l_x0,
2539 sa_col + nb_elts_read, dwt->win_l_x1,
2540 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
2541 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2543 ret = opj_sparse_array_int32_read(sa,
2544 sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2545 sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2546 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
2547 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2554 static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
2559 __m128* OPJ_RESTRICT vw = (__m128*) w;
2560 OPJ_UINT32 i = start;
2561 /* To be adapted if NB_ELTS_V8 changes */
2563 /* Note: attempt at loop unrolling x2 doesn't help */
2564 for (; i < end; ++i, vw += 4) {
2565 vw[0] = _mm_mul_ps(vw[0], c);
2566 vw[1] = _mm_mul_ps(vw[1], c);
2570 static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
2576 __m128* OPJ_RESTRICT vl = (__m128*) l;
2577 __m128* OPJ_RESTRICT vw = (__m128*) w;
2578 /* To be adapted if NB_ELTS_V8 changes */
2580 OPJ_UINT32 imax = opj_uint_min(end, m);
2583 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
2584 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
2593 /* Note: attempt at loop unrolling x2 doesn't help */
2594 for (; i < imax; ++i) {
2595 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
2596 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
2600 assert(m + 1 == end);
2601 c = _mm_add_ps(c, c);
2602 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
2603 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
2609 static void opj_v8dwt_decode_step1(opj_v8_t* w,
2612 const OPJ_FLOAT32 c)
2614 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2616 /* To be adapted if NB_ELTS_V8 changes */
2617 for (i = start; i < end; ++i) {
2618 fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c;
2619 fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
2620 fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
2621 fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
2622 fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
2623 fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
2624 fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
2625 fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
2629 static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
2635 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2636 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2638 OPJ_UINT32 imax = opj_uint_min(end, m);
2640 fw += 2 * NB_ELTS_V8 * start;
2641 fl = fw - 2 * NB_ELTS_V8;
2643 /* To be adapted if NB_ELTS_V8 changes */
2644 for (i = start; i < imax; ++i) {
2645 fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
2646 fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
2647 fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
2648 fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
2649 fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
2650 fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
2651 fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
2652 fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
2654 fw += 2 * NB_ELTS_V8;
2657 assert(m + 1 == end);
2659 fw[-8] = fw[-8] + fl[0] * c;
2660 fw[-7] = fw[-7] + fl[1] * c;
2661 fw[-6] = fw[-6] + fl[2] * c;
2662 fw[-5] = fw[-5] + fl[3] * c;
2663 fw[-4] = fw[-4] + fl[4] * c;
2664 fw[-3] = fw[-3] + fl[5] * c;
2665 fw[-2] = fw[-2] + fl[6] * c;
2666 fw[-1] = fw[-1] + fl[7] * c;
2673 /* Inverse 9-7 wavelet transform in 1-D. */
2675 static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
2678 /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
2679 /* Historic value for 2 / opj_invK */
2680 /* Normally, we should use invK, but if we do so, we have failures in the */
2681 /* conformance test, due to MSE and peak errors significantly higher than */
2682 /* accepted value */
2683 /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
2684 /* the computation of the stepsize for the non LL subbands */
2685 const float two_invK = 1.625732422f;
2686 if (dwt->cas == 0) {
2687 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2693 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2700 opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2701 _mm_set1_ps(opj_K));
2702 opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2703 _mm_set1_ps(two_invK));
2704 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2705 dwt->win_l_x0, dwt->win_l_x1,
2706 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2707 _mm_set1_ps(-opj_dwt_delta));
2708 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2709 dwt->win_h_x0, dwt->win_h_x1,
2710 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2711 _mm_set1_ps(-opj_dwt_gamma));
2712 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2713 dwt->win_l_x0, dwt->win_l_x1,
2714 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2715 _mm_set1_ps(-opj_dwt_beta));
2716 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2717 dwt->win_h_x0, dwt->win_h_x1,
2718 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2719 _mm_set1_ps(-opj_dwt_alpha));
2721 opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2723 opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2725 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2726 dwt->win_l_x0, dwt->win_l_x1,
2727 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2729 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2730 dwt->win_h_x0, dwt->win_h_x1,
2731 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2733 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2734 dwt->win_l_x0, dwt->win_l_x1,
2735 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2737 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2738 dwt->win_h_x0, dwt->win_h_x1,
2739 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2748 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2750 } opj_dwt97_decode_h_job_t;
2752 static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
2755 opj_dwt97_decode_h_job_t* job;
2756 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2760 job = (opj_dwt97_decode_h_job_t*)user_data;
2763 assert((job->nb_rows % NB_ELTS_V8) == 0);
2766 for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
2768 opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
2769 opj_v8dwt_decode(&job->h);
2771 /* To be adapted if NB_ELTS_V8 changes */
2772 for (k = 0; k < job->rw; k++) {
2773 aj[k ] = job->h.wavelet[k].f[0];
2774 aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1];
2775 aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
2776 aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
2778 for (k = 0; k < job->rw; k++) {
2779 aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
2780 aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
2781 aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
2782 aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
2785 aj += w * NB_ELTS_V8;
2788 opj_aligned_free(job->h.wavelet);
2797 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2798 OPJ_UINT32 nb_columns;
2799 } opj_dwt97_decode_v_job_t;
2801 static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
2804 opj_dwt97_decode_v_job_t* job;
2805 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2808 job = (opj_dwt97_decode_v_job_t*)user_data;
2810 assert((job->nb_columns % NB_ELTS_V8) == 0);
2813 for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
2816 opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
2817 opj_v8dwt_decode(&job->v);
2819 for (k = 0; k < job->rh; ++k) {
2820 memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
2821 NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2826 opj_aligned_free(job->v.wavelet);
2832 /* Inverse 9-7 wavelet transform in 2-D. */
2835 OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
2836 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2842 opj_tcd_resolution_t* res = tilec->resolutions;
2844 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2845 res->x0); /* width of the resolution level computed */
2846 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2847 res->y0); /* height of the resolution level computed */
2849 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2851 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2853 OPJ_SIZE_T l_data_size;
2854 const int num_threads = opj_thread_pool_get_thread_count(tp);
2860 l_data_size = opj_dwt_max_resolution(res, numres);
2861 /* overflow check */
2862 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
2863 /* FIXME event manager error callback */
2866 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2868 /* FIXME event manager error callback */
2871 v.wavelet = h.wavelet;
2874 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2877 h.sn = (OPJ_INT32)rw;
2878 v.sn = (OPJ_INT32)rh;
2882 rw = (OPJ_UINT32)(res->x1 -
2883 res->x0); /* width of the resolution level computed */
2884 rh = (OPJ_UINT32)(res->y1 -
2885 res->y0); /* height of the resolution level computed */
2887 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2888 h.cas = res->x0 % 2;
2891 h.win_l_x1 = (OPJ_UINT32)h.sn;
2893 h.win_h_x1 = (OPJ_UINT32)h.dn;
2895 if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
2896 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
2898 opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
2899 opj_v8dwt_decode(&h);
2901 /* To be adapted if NB_ELTS_V8 changes */
2902 for (k = 0; k < rw; k++) {
2903 aj[k ] = h.wavelet[k].f[0];
2904 aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1];
2905 aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
2906 aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
2908 for (k = 0; k < rw; k++) {
2909 aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
2910 aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
2911 aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
2912 aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
2915 aj += w * NB_ELTS_V8;
2918 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2921 if ((rh / NB_ELTS_V8) < num_jobs) {
2922 num_jobs = rh / NB_ELTS_V8;
2924 step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2925 for (j = 0; j < num_jobs; j++) {
2926 opj_dwt97_decode_h_job_t* job;
2928 job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
2930 opj_thread_pool_wait_completion(tp, 0);
2931 opj_aligned_free(h.wavelet);
2934 job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2935 if (!job->h.wavelet) {
2936 opj_thread_pool_wait_completion(tp, 0);
2938 opj_aligned_free(h.wavelet);
2944 job->h.win_l_x0 = h.win_l_x0;
2945 job->h.win_l_x1 = h.win_l_x1;
2946 job->h.win_h_x0 = h.win_h_x0;
2947 job->h.win_h_x1 = h.win_h_x1;
2951 job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
2952 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
2953 aj += w * job->nb_rows;
2954 opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
2956 opj_thread_pool_wait_completion(tp, 0);
2957 j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
2962 opj_v8dwt_interleave_h(&h, aj, w, rh - j);
2963 opj_v8dwt_decode(&h);
2964 for (k = 0; k < rw; k++) {
2966 for (l = 0; l < rh - j; l++) {
2967 aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l];
2972 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2973 v.cas = res->y0 % 2;
2975 v.win_l_x1 = (OPJ_UINT32)v.sn;
2977 v.win_h_x1 = (OPJ_UINT32)v.dn;
2979 aj = (OPJ_FLOAT32*) tilec->data;
2980 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
2981 for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
2984 opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
2985 opj_v8dwt_decode(&v);
2987 for (k = 0; k < rh; ++k) {
2988 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2993 /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
2994 transfer being the limiting factor. So limit the number of
2997 OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
3000 if ((rw / NB_ELTS_V8) < num_jobs) {
3001 num_jobs = rw / NB_ELTS_V8;
3003 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3004 for (j = 0; j < num_jobs; j++) {
3005 opj_dwt97_decode_v_job_t* job;
3007 job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
3009 opj_thread_pool_wait_completion(tp, 0);
3010 opj_aligned_free(h.wavelet);
3013 job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3014 if (!job->v.wavelet) {
3015 opj_thread_pool_wait_completion(tp, 0);
3017 opj_aligned_free(h.wavelet);
3023 job->v.win_l_x0 = v.win_l_x0;
3024 job->v.win_l_x1 = v.win_l_x1;
3025 job->v.win_h_x0 = v.win_h_x0;
3026 job->v.win_h_x1 = v.win_h_x1;
3030 job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
3031 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3032 aj += job->nb_columns;
3033 opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
3035 opj_thread_pool_wait_completion(tp, 0);
3038 if (rw & (NB_ELTS_V8 - 1)) {
3041 j = rw & (NB_ELTS_V8 - 1);
3043 opj_v8dwt_interleave_v(&v, aj, w, j);
3044 opj_v8dwt_decode(&v);
3046 for (k = 0; k < rh; ++k) {
3047 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
3048 (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
3053 opj_aligned_free(h.wavelet);
3058 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3061 opj_sparse_array_int32_t* sa;
3065 /* This value matches the maximum left/right extension given in tables */
3066 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3067 /* we currently use 3. */
3068 const OPJ_UINT32 filter_width = 4U;
3070 opj_tcd_resolution_t* tr = tilec->resolutions;
3071 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3073 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3074 tr->x0); /* width of the resolution level computed */
3075 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3076 tr->y0); /* height of the resolution level computed */
3078 OPJ_SIZE_T l_data_size;
3080 /* Compute the intersection of the area of interest, expressed in tile coordinates */
3081 /* with the tile coordinates */
3082 OPJ_UINT32 win_tcx0 = tilec->win_x0;
3083 OPJ_UINT32 win_tcy0 = tilec->win_y0;
3084 OPJ_UINT32 win_tcx1 = tilec->win_x1;
3085 OPJ_UINT32 win_tcy1 = tilec->win_y1;
3087 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3091 sa = opj_dwt_init_sparse_array(tilec, numres);
3097 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3098 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3099 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3100 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3101 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3103 1, tr_max->win_x1 - tr_max->win_x0,
3107 opj_sparse_array_int32_free(sa);
3111 l_data_size = opj_dwt_max_resolution(tr, numres);
3112 /* overflow check */
3113 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3114 /* FIXME event manager error callback */
3115 opj_sparse_array_int32_free(sa);
3118 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3120 /* FIXME event manager error callback */
3121 opj_sparse_array_int32_free(sa);
3124 v.wavelet = h.wavelet;
3126 for (resno = 1; resno < numres; resno ++) {
3128 /* Window of interest subband-based coordinates */
3129 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3130 OPJ_UINT32 win_hl_x0, win_hl_x1;
3131 OPJ_UINT32 win_lh_y0, win_lh_y1;
3132 /* Window of interest tile-resolution-based coordinates */
3133 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3134 /* Tile-resolution subband-based coordinates */
3135 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3139 h.sn = (OPJ_INT32)rw;
3140 v.sn = (OPJ_INT32)rh;
3142 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3143 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3145 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3148 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3151 /* Get the subband coordinates for the window of interest */
3153 opj_dwt_get_band_coordinates(tilec, resno, 0,
3154 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3155 &win_ll_x0, &win_ll_y0,
3156 &win_ll_x1, &win_ll_y1);
3159 opj_dwt_get_band_coordinates(tilec, resno, 1,
3160 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3161 &win_hl_x0, NULL, &win_hl_x1, NULL);
3164 opj_dwt_get_band_coordinates(tilec, resno, 2,
3165 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3166 NULL, &win_lh_y0, NULL, &win_lh_y1);
3168 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3169 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3170 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3171 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3172 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3174 /* Subtract the origin of the bands for this tile, to the subwindow */
3175 /* of interest band coordinates, so as to get them relative to the */
3177 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3178 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3179 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3180 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3181 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3182 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3183 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3184 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3186 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3187 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3189 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3190 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3192 /* Compute the tile-resolution-based coordinates for the window of interest */
3194 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3195 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3197 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3198 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3202 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3203 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3205 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3206 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3209 h.win_l_x0 = win_ll_x0;
3210 h.win_l_x1 = win_ll_x1;
3211 h.win_h_x0 = win_hl_x0;
3212 h.win_h_x1 = win_hl_x1;
3213 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3214 if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3215 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3216 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3217 opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3218 opj_v8dwt_decode(&h);
3219 if (!opj_sparse_array_int32_write(sa,
3221 win_tr_x1, j + NB_ELTS_V8,
3222 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3223 NB_ELTS_V8, 1, OPJ_TRUE)) {
3224 /* FIXME event manager error callback */
3225 opj_sparse_array_int32_free(sa);
3226 opj_aligned_free(h.wavelet);
3233 ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3234 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3235 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3236 opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3237 opj_v8dwt_decode(&h);
3238 if (!opj_sparse_array_int32_write(sa,
3241 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3242 NB_ELTS_V8, 1, OPJ_TRUE)) {
3243 /* FIXME event manager error callback */
3244 opj_sparse_array_int32_free(sa);
3245 opj_aligned_free(h.wavelet);
3250 v.win_l_x0 = win_ll_y0;
3251 v.win_l_x1 = win_ll_y1;
3252 v.win_h_x0 = win_lh_y0;
3253 v.win_h_x1 = win_lh_y1;
3254 for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3255 OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3257 opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3258 opj_v8dwt_decode(&v);
3260 if (!opj_sparse_array_int32_write(sa,
3262 j + nb_elts, win_tr_y1,
3263 (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3264 1, NB_ELTS_V8, OPJ_TRUE)) {
3265 /* FIXME event manager error callback */
3266 opj_sparse_array_int32_free(sa);
3267 opj_aligned_free(h.wavelet);
3274 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3275 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3276 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3277 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3278 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3280 1, tr_max->win_x1 - tr_max->win_x0,
3285 opj_sparse_array_int32_free(sa);
3287 opj_aligned_free(h.wavelet);
3292 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3293 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3296 if (p_tcd->whole_tile_decoding) {
3297 return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3299 return opj_dwt_decode_partial_97(tilec, numres);