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;
342 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
343 for (i = 0; i < dn; i++) {
344 OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
346 for (i = 0; i < sn; i++) {
347 OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
351 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
354 for (i = 0; i < dn; i++) {
355 OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
357 for (i = 0; i < sn; i++) {
358 OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
364 #ifdef STANDARD_SLOW_VERSION
366 /* Inverse 5-3 wavelet transform in 1-D. */
368 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
374 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
375 for (i = 0; i < sn; i++) {
376 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
378 for (i = 0; i < dn; i++) {
379 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
383 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
386 for (i = 0; i < sn; i++) {
387 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
389 for (i = 0; i < dn; i++) {
390 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
396 static void opj_dwt_decode_1(const opj_dwt_t *v)
398 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
401 #endif /* STANDARD_SLOW_VERSION */
403 #if !defined(STANDARD_SLOW_VERSION)
404 static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
410 const OPJ_INT32* in_even = &tiledp[0];
411 const OPJ_INT32* in_odd = &tiledp[sn];
413 #ifdef TWO_PASS_VERSION
414 /* For documentation purpose: performs lifting in two iterations, */
415 /* but without explicit interleaving */
420 tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
421 for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
422 tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
424 if (len & 1) { /* if len is odd */
425 tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
429 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
430 tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
432 if (!(len & 1)) { /* if len is even */
433 tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
436 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
440 /* Improved version of the TWO_PASS_VERSION: */
441 /* Performs lifting in one single iteration. Saves memory */
442 /* accesses and explicit interleaving. */
445 s0n = s1n - ((d1n + 1) >> 1);
447 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
454 s0n = s1n - ((d1c + d1n + 2) >> 2);
457 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
463 tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
464 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
466 tmp[len - 1] = d1n + s0n;
469 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
472 static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
478 const OPJ_INT32* in_even = &tiledp[sn];
479 const OPJ_INT32* in_odd = &tiledp[0];
481 #ifdef TWO_PASS_VERSION
482 /* For documentation purpose: performs lifting in two iterations, */
483 /* but without explicit interleaving */
488 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
489 tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
492 tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
496 tmp[0] = in_even[0] + tmp[1];
497 for (i = 2, j = 1; i < len - 1; i += 2, j++) {
498 tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
501 tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
504 OPJ_INT32 s1, s2, dc, dn;
508 /* Improved version of the TWO_PASS_VERSION: */
509 /* Performs lifting in one single iteration. Saves memory */
510 /* accesses and explicit interleaving. */
513 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
514 tmp[0] = in_even[0] + dc;
516 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
520 dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
522 tmp[i + 1] = s1 + ((dn + dc) >> 1);
531 dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
532 tmp[len - 2] = s1 + ((dn + dc) >> 1);
535 tmp[len - 1] = s1 + dc;
538 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
542 #endif /* !defined(STANDARD_SLOW_VERSION) */
545 /* Inverse 5-3 wavelet transform in 1-D for one row. */
547 /* Performs interleave, inverse wavelet transform and copy back to buffer */
548 static void opj_idwt53_h(const opj_dwt_t *dwt,
551 #ifdef STANDARD_SLOW_VERSION
552 /* For documentation purpose */
553 opj_dwt_interleave_h(dwt, tiledp);
554 opj_dwt_decode_1(dwt);
555 memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
557 const OPJ_INT32 sn = dwt->sn;
558 const OPJ_INT32 len = sn + dwt->dn;
559 if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
561 opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
563 /* Unmodified value */
565 } else { /* Left-most sample is on odd coordinate */
568 } else if (len == 2) {
569 OPJ_INT32* out = dwt->mem;
570 const OPJ_INT32* in_even = &tiledp[sn];
571 const OPJ_INT32* in_odd = &tiledp[0];
572 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
573 out[0] = in_even[0] + out[1];
574 memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
575 } else if (len > 2) {
576 opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
582 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
584 /* Conveniency macros to improve the readabilty of the formulas */
587 #define LOAD_CST(x) _mm256_set1_epi32(x)
588 #define LOAD(x) _mm256_load_si256((const VREG*)(x))
589 #define LOADU(x) _mm256_loadu_si256((const VREG*)(x))
590 #define STORE(x,y) _mm256_store_si256((VREG*)(x),(y))
591 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
592 #define ADD(x,y) _mm256_add_epi32((x),(y))
593 #define SUB(x,y) _mm256_sub_epi32((x),(y))
594 #define SAR(x,y) _mm256_srai_epi32((x),(y))
597 #define LOAD_CST(x) _mm_set1_epi32(x)
598 #define LOAD(x) _mm_load_si128((const VREG*)(x))
599 #define LOADU(x) _mm_loadu_si128((const VREG*)(x))
600 #define STORE(x,y) _mm_store_si128((VREG*)(x),(y))
601 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
602 #define ADD(x,y) _mm_add_epi32((x),(y))
603 #define SUB(x,y) _mm_sub_epi32((x),(y))
604 #define SAR(x,y) _mm_srai_epi32((x),(y))
606 #define ADD3(x,y,z) ADD(ADD(x,y),z)
609 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
610 const OPJ_INT32* tmp,
615 for (i = 0; i < len; ++i) {
616 /* A memcpy(&tiledp_col[i * stride + 0],
617 &tmp[PARALLEL_COLS_53 * i + 0],
618 PARALLEL_COLS_53 * sizeof(OPJ_INT32))
619 would do but would be a tiny bit slower.
620 We can take here advantage of our knowledge of alignment */
621 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
622 LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
623 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
624 LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
628 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
629 * 16 in AVX2, when top-most pixel is on even coordinate */
630 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
634 OPJ_INT32* tiledp_col,
635 const OPJ_SIZE_T stride)
637 const OPJ_INT32* in_even = &tiledp_col[0];
638 const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
642 VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
643 VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
644 const VREG two = LOAD_CST(2);
648 assert(PARALLEL_COLS_53 == 16);
649 assert(VREG_INT_COUNT == 8);
651 assert(PARALLEL_COLS_53 == 8);
652 assert(VREG_INT_COUNT == 4);
655 /* Note: loads of input even/odd values must be done in a unaligned */
656 /* fashion. But stores in tmp can be done with aligned store, since */
657 /* the temporary buffer is properly aligned */
658 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
660 s1n_0 = LOADU(in_even + 0);
661 s1n_1 = LOADU(in_even + VREG_INT_COUNT);
662 d1n_0 = LOADU(in_odd);
663 d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
665 /* s0n = s1n - ((d1n + 1) >> 1); <==> */
666 /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
667 s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
668 s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
670 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
676 s1n_0 = LOADU(in_even + j * stride);
677 s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
678 d1n_0 = LOADU(in_odd + j * stride);
679 d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
681 /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
682 s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
683 s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
685 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
686 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
688 /* d1c + ((s0c + s0n) >> 1) */
689 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
690 ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
691 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
692 ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
695 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
696 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
699 VREG tmp_len_minus_1;
700 s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
701 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
702 tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
703 STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
704 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
705 STORE(tmp + PARALLEL_COLS_53 * (len - 2),
706 ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
708 s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
709 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
710 tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
711 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
713 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
714 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
715 ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
719 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
721 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
725 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
729 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
730 * 16 in AVX2, when top-most pixel is on odd coordinate */
731 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
735 OPJ_INT32* tiledp_col,
736 const OPJ_SIZE_T stride)
741 VREG s1_0, s2_0, dc_0, dn_0;
742 VREG s1_1, s2_1, dc_1, dn_1;
743 const VREG two = LOAD_CST(2);
745 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
746 const OPJ_INT32* in_odd = &tiledp_col[0];
750 assert(PARALLEL_COLS_53 == 16);
751 assert(VREG_INT_COUNT == 8);
753 assert(PARALLEL_COLS_53 == 8);
754 assert(VREG_INT_COUNT == 4);
757 /* Note: loads of input even/odd values must be done in a unaligned */
758 /* fashion. But stores in tmp can be done with aligned store, since */
759 /* the temporary buffer is properly aligned */
760 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
762 s1_0 = LOADU(in_even + stride);
763 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
764 dc_0 = SUB(LOADU(in_odd + 0),
765 SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
766 STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
768 s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
769 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
770 dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
771 SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
772 STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
773 ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
775 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
777 s2_0 = LOADU(in_even + (j + 1) * stride);
778 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
780 /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
781 dn_0 = SUB(LOADU(in_odd + j * stride),
782 SAR(ADD3(s1_0, s2_0, two), 2));
783 dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
784 SAR(ADD3(s1_1, s2_1, two), 2));
786 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
787 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
789 /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
790 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
791 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
792 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
793 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
800 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
801 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
804 /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
805 dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
806 SAR(ADD3(s1_0, s1_0, two), 2));
807 dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
808 SAR(ADD3(s1_1, s1_1, two), 2));
810 /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
811 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
812 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
813 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
814 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
816 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
817 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
819 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
820 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
824 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
838 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
840 #if !defined(STANDARD_SLOW_VERSION)
841 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
842 * pixel is on even coordinate */
843 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
846 OPJ_INT32* tiledp_col,
847 const OPJ_SIZE_T stride)
850 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
854 /* Performs lifting in one single iteration. Saves memory */
855 /* accesses and explicit interleaving. */
858 d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
859 s0n = s1n - ((d1n + 1) >> 1);
861 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
865 s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
866 d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
868 s0n = s1n - ((d1c + d1n + 2) >> 2);
871 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
878 tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
880 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
882 tmp[len - 1] = d1n + s0n;
885 for (i = 0; i < len; ++i) {
886 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
891 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
892 * pixel is on odd coordinate */
893 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
896 OPJ_INT32* tiledp_col,
897 const OPJ_SIZE_T stride)
900 OPJ_INT32 s1, s2, dc, dn;
901 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
902 const OPJ_INT32* in_odd = &tiledp_col[0];
906 /* Performs lifting in one single iteration. Saves memory */
907 /* accesses and explicit interleaving. */
909 s1 = in_even[stride];
910 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
911 tmp[0] = in_even[0] + dc;
912 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
914 s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
916 dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
918 tmp[i + 1] = s1 + ((dn + dc) >> 1);
925 dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
926 tmp[len - 2] = s1 + ((dn + dc) >> 1);
929 tmp[len - 1] = s1 + dc;
932 for (i = 0; i < len; ++i) {
933 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
936 #endif /* !defined(STANDARD_SLOW_VERSION) */
939 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
941 /* Performs interleave, inverse wavelet transform and copy back to buffer */
942 static void opj_idwt53_v(const opj_dwt_t *dwt,
943 OPJ_INT32* tiledp_col,
947 #ifdef STANDARD_SLOW_VERSION
948 /* For documentation purpose */
950 for (c = 0; c < nb_cols; c ++) {
951 opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
952 opj_dwt_decode_1(dwt);
953 for (k = 0; k < dwt->sn + dwt->dn; ++k) {
954 tiledp_col[c + k * stride] = dwt->mem[k];
958 const OPJ_INT32 sn = dwt->sn;
959 const OPJ_INT32 len = sn + dwt->dn;
961 /* If len == 1, unmodified value */
963 #if (defined(__SSE2__) || defined(__AVX2__))
964 if (len > 1 && nb_cols == PARALLEL_COLS_53) {
965 /* Same as below general case, except that thanks to SSE2/AVX2 */
966 /* we can efficiently process 8/16 columns in parallel */
967 opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
973 for (c = 0; c < nb_cols; c++, tiledp_col++) {
974 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
981 for (c = 0; c < nb_cols; c++, tiledp_col++) {
989 OPJ_INT32* out = dwt->mem;
990 for (c = 0; c < nb_cols; c++, tiledp_col++) {
992 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
993 const OPJ_INT32* in_odd = &tiledp_col[0];
995 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
996 out[0] = in_even[0] + out[1];
998 for (i = 0; i < len; ++i) {
999 tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
1006 #if (defined(__SSE2__) || defined(__AVX2__))
1007 if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1008 /* Same as below general case, except that thanks to SSE2/AVX2 */
1009 /* we can efficiently process 8/16 columns in parallel */
1010 opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1016 for (c = 0; c < nb_cols; c++, tiledp_col++) {
1017 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1025 static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
1028 const OPJ_FLOAT32 c)
1031 for (i = start; i < end; ++i) {
1035 static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1042 OPJ_UINT32 imax = opj_uint_min(end, m);
1047 for (i = start; i < imax; ++i) {
1048 fw[-1] += (fl[0] + fw[0]) * c;
1053 assert(m + 1 == end);
1054 fw[-1] += (2 * fl[0]) * c;
1058 static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1061 OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1064 if (!((dn > 0) || (sn > 1))) {
1070 if (!((sn > 0) || (dn > 1))) {
1076 opj_dwt_encode_step2(w + a, w + b + 1,
1078 (OPJ_UINT32)opj_int_min(dn, sn - b),
1080 opj_dwt_encode_step2(w + b, w + a + 1,
1082 (OPJ_UINT32)opj_int_min(sn, dn - a),
1084 opj_dwt_encode_step2(w + a, w + b + 1,
1086 (OPJ_UINT32)opj_int_min(dn, sn - b),
1088 opj_dwt_encode_step2(w + b, w + a + 1,
1090 (OPJ_UINT32)opj_int_min(sn, dn - a),
1092 opj_dwt_encode_step1(w + b, 0, (OPJ_UINT32)dn,
1094 opj_dwt_encode_step1(w + a, 0, (OPJ_UINT32)sn,
1098 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1099 opj_stepsize_t *bandno_stepsize)
1102 p = opj_int_floorlog2(stepsize) - 13;
1103 n = 11 - opj_int_floorlog2(stepsize);
1104 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1105 bandno_stepsize->expn = numbps - p;
1109 ==========================================================
1111 ==========================================================
1114 /** Process one line for the horizontal pass of the 5x3 forward transform */
1116 void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1121 OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1122 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1123 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1124 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1125 memcpy(tmp, row, width * sizeof(OPJ_INT32));
1126 opj_dwt_encode_1(tmp, dn, sn, even ? 0 : 1);
1127 opj_dwt_deinterleave_h(tmp, row, dn, sn, even ? 0 : 1);
1130 /** Process one line for the horizontal pass of the 9x7 forward transform */
1132 void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1137 OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1138 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1139 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1140 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1141 memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1142 opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1143 opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1144 (OPJ_INT32 * OPJ_RESTRICT)row,
1145 dn, sn, even ? 0 : 1);
1150 OPJ_UINT32 rw; /* Width of the resolution to process */
1151 OPJ_UINT32 w; /* Width of tiledp */
1152 OPJ_INT32 * OPJ_RESTRICT tiledp;
1155 opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1156 } opj_dwt_encode_h_job_t;
1158 static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1161 opj_dwt_encode_h_job_t* job;
1164 job = (opj_dwt_encode_h_job_t*)user_data;
1165 for (j = job->min_j; j < job->max_j; j++) {
1166 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1167 (*job->p_function)(aj, job->h.mem, job->rw,
1168 job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1171 opj_aligned_free(job->h.mem);
1179 OPJ_INT32 * OPJ_RESTRICT tiledp;
1182 opj_encode_one_row_fnptr_type p_function;
1183 } opj_dwt_encode_v_job_t;
1185 static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1188 opj_dwt_encode_v_job_t* job;
1191 job = (opj_dwt_encode_v_job_t*)user_data;
1192 for (j = job->min_j; j < job->max_j; j++) {
1193 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j;
1195 for (k = 0; k < job->rh; ++k) {
1196 job->v.mem[k] = aj[k * job->w];
1199 (*job->p_function)(job->v.mem, job->v.dn, job->v.sn, job->v.cas);
1201 opj_dwt_deinterleave_v(job->v.mem, aj, job->v.dn, job->v.sn, job->w,
1205 opj_aligned_free(job->v.mem);
1210 /* Forward 5-3 wavelet transform in 2-D. */
1212 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1213 opj_tcd_tilecomp_t * tilec,
1214 opj_encode_one_row_fnptr_type p_function,
1215 opj_encode_and_deinterleave_h_one_row_fnptr_type
1216 p_encode_and_deinterleave_h_one_row)
1223 OPJ_SIZE_T l_data_size;
1225 opj_tcd_resolution_t * l_cur_res = 0;
1226 opj_tcd_resolution_t * l_last_res = 0;
1227 const int num_threads = opj_thread_pool_get_thread_count(tp);
1228 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1230 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1231 l = (OPJ_INT32)tilec->numresolutions - 1;
1233 l_cur_res = tilec->resolutions + l;
1234 l_last_res = l_cur_res - 1;
1236 l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1237 /* overflow check */
1238 if (l_data_size > (SIZE_MAX / sizeof(OPJ_INT32))) {
1239 /* FIXME event manager error callback */
1242 l_data_size *= sizeof(OPJ_INT32);
1243 bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1244 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1245 /* in that case, so do not error out */
1246 if (l_data_size != 0 && ! bj) {
1253 OPJ_UINT32 rw; /* width of the resolution level computed */
1254 OPJ_UINT32 rh; /* height of the resolution level computed */
1256 rw1; /* width of the resolution level once lower than computed one */
1258 rh1; /* height of the resolution level once lower than computed one */
1259 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1260 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
1263 rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1264 rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1265 rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1266 rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1268 cas_row = l_cur_res->x0 & 1;
1269 cas_col = l_cur_res->y0 & 1;
1271 sn = (OPJ_INT32)rh1;
1272 dn = (OPJ_INT32)(rh - rh1);
1274 /* Perform vertical pass */
1275 if (num_threads <= 1 || rw <= 1) {
1276 for (j = 0; j < rw; ++j) {
1277 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j;
1279 for (k = 0; k < rh; ++k) {
1283 (*p_function)(bj, dn, sn, cas_col);
1285 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
1288 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1291 if (rw < num_jobs) {
1294 step_j = (rw / num_jobs);
1296 for (j = 0; j < num_jobs; j++) {
1297 opj_dwt_encode_v_job_t* job;
1299 job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
1301 opj_thread_pool_wait_completion(tp, 0);
1302 opj_aligned_free(bj);
1305 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1307 opj_thread_pool_wait_completion(tp, 0);
1309 opj_aligned_free(bj);
1314 job->v.cas = cas_col;
1317 job->tiledp = tiledp;
1318 job->min_j = j * step_j;
1319 job->max_j = (j + 1U) * step_j; /* this can overflow */
1320 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1323 job->p_function = p_function;
1324 opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
1326 opj_thread_pool_wait_completion(tp, 0);
1329 sn = (OPJ_INT32)rw1;
1330 dn = (OPJ_INT32)(rw - rw1);
1332 /* Perform horizontal pass */
1333 if (num_threads <= 1 || rh <= 1) {
1334 for (j = 0; j < rh; j++) {
1335 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
1336 (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
1337 cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
1340 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1343 if (rh < num_jobs) {
1346 step_j = (rh / num_jobs);
1348 for (j = 0; j < num_jobs; j++) {
1349 opj_dwt_encode_h_job_t* job;
1351 job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
1353 opj_thread_pool_wait_completion(tp, 0);
1354 opj_aligned_free(bj);
1357 job->h.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->h.cas = cas_row;
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_encode_and_deinterleave_h_one_row;
1376 opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
1378 opj_thread_pool_wait_completion(tp, 0);
1381 l_cur_res = l_last_res;
1386 opj_aligned_free(bj);
1390 /* Forward 5-3 wavelet transform in 2-D. */
1392 OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
1393 opj_tcd_tilecomp_t * tilec)
1395 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1397 opj_dwt_encode_and_deinterleave_h_one_row);
1401 /* Inverse 5-3 wavelet transform in 2-D. */
1403 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1406 if (p_tcd->whole_tile_decoding) {
1407 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1409 return opj_dwt_decode_partial_tile(tilec, numres);
1414 /* Get norm of 5-3 wavelet. */
1416 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1418 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1419 /* but the array should really be extended up to 33 resolution levels */
1420 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1421 if (orient == 0 && level >= 10) {
1423 } else if (orient > 0 && level >= 9) {
1426 return opj_dwt_norms[orient][level];
1430 /* Forward 9-7 wavelet transform in 2-D. */
1432 OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
1433 opj_tcd_tilecomp_t * tilec)
1435 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1436 opj_dwt_encode_1_real,
1437 opj_dwt_encode_and_deinterleave_h_one_row_real);
1441 /* Get norm of 9-7 wavelet. */
1443 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1445 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1446 /* but the array should really be extended up to 33 resolution levels */
1447 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1448 if (orient == 0 && level >= 10) {
1450 } else if (orient > 0 && level >= 9) {
1453 return opj_dwt_norms_real[orient][level];
1456 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1458 OPJ_UINT32 numbands, bandno;
1459 numbands = 3 * tccp->numresolutions - 2;
1460 for (bandno = 0; bandno < numbands; bandno++) {
1461 OPJ_FLOAT64 stepsize;
1462 OPJ_UINT32 resno, level, orient, gain;
1464 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1465 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1466 level = tccp->numresolutions - 1 - resno;
1467 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1468 (orient == 2)) ? 1 : 2));
1469 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1472 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1473 stepsize = (1 << (gain)) / norm;
1475 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1476 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1481 /* Determine maximum computed resolution level for inverse wavelet transform */
1483 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1490 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1493 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1504 OPJ_INT32 * OPJ_RESTRICT tiledp;
1507 } opj_dwt_decode_h_job_t;
1509 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1512 opj_dwt_decode_h_job_t* job;
1515 job = (opj_dwt_decode_h_job_t*)user_data;
1516 for (j = job->min_j; j < job->max_j; j++) {
1517 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1520 opj_aligned_free(job->h.mem);
1528 OPJ_INT32 * OPJ_RESTRICT tiledp;
1531 } opj_dwt_decode_v_job_t;
1533 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1536 opj_dwt_decode_v_job_t* job;
1539 job = (opj_dwt_decode_v_job_t*)user_data;
1540 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1541 j += PARALLEL_COLS_53) {
1542 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1546 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1547 (OPJ_INT32)(job->max_j - j));
1549 opj_aligned_free(job->v.mem);
1555 /* Inverse wavelet transform in 2-D. */
1557 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1558 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1563 opj_tcd_resolution_t* tr = tilec->resolutions;
1565 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1566 tr->x0); /* width of the resolution level computed */
1567 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1568 tr->y0); /* height of the resolution level computed */
1570 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1572 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1573 OPJ_SIZE_T h_mem_size;
1579 num_threads = opj_thread_pool_get_thread_count(tp);
1580 h_mem_size = opj_dwt_max_resolution(tr, numres);
1581 /* overflow check */
1582 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1583 /* FIXME event manager error callback */
1586 /* We need PARALLEL_COLS_53 times the height of the array, */
1587 /* since for the vertical pass */
1588 /* we process PARALLEL_COLS_53 columns at a time */
1589 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1590 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1592 /* FIXME event manager error callback */
1599 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1603 h.sn = (OPJ_INT32)rw;
1604 v.sn = (OPJ_INT32)rh;
1606 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1607 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1609 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1612 if (num_threads <= 1 || rh <= 1) {
1613 for (j = 0; j < rh; ++j) {
1614 opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
1617 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1620 if (rh < num_jobs) {
1623 step_j = (rh / num_jobs);
1625 for (j = 0; j < num_jobs; j++) {
1626 opj_dwt_decode_h_job_t* job;
1628 job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
1630 /* It would be nice to fallback to single thread case, but */
1631 /* unfortunately some jobs may be launched and have modified */
1632 /* tiledp, so it is not practical to recover from that error */
1633 /* FIXME event manager error callback */
1634 opj_thread_pool_wait_completion(tp, 0);
1635 opj_aligned_free(h.mem);
1641 job->tiledp = tiledp;
1642 job->min_j = j * step_j;
1643 job->max_j = (j + 1U) * step_j; /* this can overflow */
1644 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1647 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1649 /* FIXME event manager error callback */
1650 opj_thread_pool_wait_completion(tp, 0);
1652 opj_aligned_free(h.mem);
1655 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
1657 opj_thread_pool_wait_completion(tp, 0);
1660 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
1663 if (num_threads <= 1 || rw <= 1) {
1664 for (j = 0; j + PARALLEL_COLS_53 <= rw;
1665 j += PARALLEL_COLS_53) {
1666 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
1669 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
1672 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1675 if (rw < num_jobs) {
1678 step_j = (rw / num_jobs);
1680 for (j = 0; j < num_jobs; j++) {
1681 opj_dwt_decode_v_job_t* job;
1683 job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
1685 /* It would be nice to fallback to single thread case, but */
1686 /* unfortunately some jobs may be launched and have modified */
1687 /* tiledp, so it is not practical to recover from that error */
1688 /* FIXME event manager error callback */
1689 opj_thread_pool_wait_completion(tp, 0);
1690 opj_aligned_free(v.mem);
1696 job->tiledp = tiledp;
1697 job->min_j = j * step_j;
1698 job->max_j = (j + 1U) * step_j; /* this can overflow */
1699 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1702 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1704 /* FIXME event manager error callback */
1705 opj_thread_pool_wait_completion(tp, 0);
1707 opj_aligned_free(v.mem);
1710 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
1712 opj_thread_pool_wait_completion(tp, 0);
1715 opj_aligned_free(h.mem);
1719 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
1721 opj_sparse_array_int32_t* sa,
1724 OPJ_UINT32 win_l_x0,
1725 OPJ_UINT32 win_l_x1,
1726 OPJ_UINT32 win_h_x0,
1727 OPJ_UINT32 win_h_x1)
1730 ret = opj_sparse_array_int32_read(sa,
1732 win_l_x1, sa_line + 1,
1733 dest + cas + 2 * win_l_x0,
1736 ret = opj_sparse_array_int32_read(sa,
1737 sn + win_h_x0, sa_line,
1738 sn + win_h_x1, sa_line + 1,
1739 dest + 1 - cas + 2 * win_h_x0,
1746 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
1748 opj_sparse_array_int32_t* sa,
1752 OPJ_UINT32 win_l_y0,
1753 OPJ_UINT32 win_l_y1,
1754 OPJ_UINT32 win_h_y0,
1755 OPJ_UINT32 win_h_y1)
1758 ret = opj_sparse_array_int32_read(sa,
1760 sa_col + nb_cols, win_l_y1,
1761 dest + cas * 4 + 2 * 4 * win_l_y0,
1762 1, 2 * 4, OPJ_TRUE);
1764 ret = opj_sparse_array_int32_read(sa,
1765 sa_col, sn + win_h_y0,
1766 sa_col + nb_cols, sn + win_h_y1,
1767 dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
1768 1, 2 * 4, OPJ_TRUE);
1773 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
1783 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1785 /* Naive version is :
1786 for (i = win_l_x0; i < i_max; i++) {
1787 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1789 for (i = win_h_x0; i < win_h_x1; i++) {
1790 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1792 but the compiler doesn't manage to unroll it to avoid bound
1793 checking in OPJ_S_ and OPJ_D_ macros
1800 /* Left-most case */
1801 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1808 for (; i < i_max; i++) {
1809 /* No bound checking */
1810 OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
1812 for (; i < win_l_x1; i++) {
1813 /* Right-most case */
1814 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1820 OPJ_INT32 i_max = win_h_x1;
1824 for (; i < i_max; i++) {
1825 /* No bound checking */
1826 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
1828 for (; i < win_h_x1; i++) {
1829 /* Right-most case */
1830 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1835 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
1838 for (i = win_l_x0; i < win_l_x1; i++) {
1839 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
1841 for (i = win_h_x0; i < win_h_x1; i++) {
1842 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
1848 #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
1849 #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
1850 #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)))
1851 #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)))
1852 #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)))
1853 #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)))
1855 static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
1857 OPJ_INT32 dn, OPJ_INT32 sn,
1870 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
1872 /* Naive version is :
1873 for (i = win_l_x0; i < i_max; i++) {
1874 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
1876 for (i = win_h_x0; i < win_h_x1; i++) {
1877 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
1879 but the compiler doesn't manage to unroll it to avoid bound
1880 checking in OPJ_S_ and OPJ_D_ macros
1887 /* Left-most case */
1888 for (off = 0; off < 4; off++) {
1889 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1899 if (i + 1 < i_max) {
1900 const __m128i two = _mm_set1_epi32(2);
1901 __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
1902 for (; i + 1 < i_max; i += 2) {
1903 /* No bound checking */
1904 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
1905 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1906 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1907 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
1908 S = _mm_sub_epi32(S,
1909 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
1910 S1 = _mm_sub_epi32(S1,
1911 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
1912 _mm_store_si128((__m128i*)(a + i * 8), S);
1913 _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
1919 for (; i < i_max; i++) {
1920 /* No bound checking */
1921 for (off = 0; off < 4; off++) {
1922 OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
1925 for (; i < win_l_x1; i++) {
1926 /* Right-most case */
1927 for (off = 0; off < 4; off++) {
1928 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
1935 OPJ_INT32 i_max = win_h_x1;
1941 if (i + 1 < i_max) {
1942 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
1943 for (; i + 1 < i_max; i += 2) {
1944 /* No bound checking */
1945 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
1946 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
1947 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
1948 __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
1949 D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
1950 D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
1951 _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
1952 _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
1958 for (; i < i_max; i++) {
1959 /* No bound checking */
1960 for (off = 0; off < 4; off++) {
1961 OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
1964 for (; i < win_h_x1; i++) {
1965 /* Right-most case */
1966 for (off = 0; off < 4; off++) {
1967 OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
1973 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
1974 for (off = 0; off < 4; off++) {
1975 OPJ_S_off(0, off) /= 2;
1978 for (i = win_l_x0; i < win_l_x1; i++) {
1979 for (off = 0; off < 4; off++) {
1980 OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2;
1983 for (i = win_h_x0; i < win_h_x1; i++) {
1984 for (off = 0; off < 4; off++) {
1985 OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1;
1992 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
2004 /* Compute number of decomposition for this band. See table F-1 */
2005 OPJ_UINT32 nb = (resno == 0) ?
2006 tilec->numresolutions - 1 :
2007 tilec->numresolutions - resno;
2008 /* Map above tile-based coordinates to sub-band-based coordinates per */
2009 /* equation B-15 of the standard */
2010 OPJ_UINT32 x0b = bandno & 1;
2011 OPJ_UINT32 y0b = bandno >> 1;
2013 *tbx0 = (nb == 0) ? tcx0 :
2014 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2015 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2018 *tby0 = (nb == 0) ? tcy0 :
2019 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2020 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2023 *tbx1 = (nb == 0) ? tcx1 :
2024 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2025 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2028 *tby1 = (nb == 0) ? tcy1 :
2029 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2030 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2034 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2035 OPJ_UINT32 max_size,
2039 *start = opj_uint_subs(*start, filter_width);
2040 *end = opj_uint_adds(*end, filter_width);
2041 *end = opj_uint_min(*end, max_size);
2045 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2046 opj_tcd_tilecomp_t* tilec,
2049 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2050 OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2051 OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2052 OPJ_UINT32 resno, bandno, precno, cblkno;
2053 opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2054 w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2059 for (resno = 0; resno < numres; ++resno) {
2060 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2062 for (bandno = 0; bandno < res->numbands; ++bandno) {
2063 opj_tcd_band_t* band = &res->bands[bandno];
2065 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2066 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2067 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2068 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2069 if (cblk->decoded_data != NULL) {
2070 OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2071 OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2072 OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2073 OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2075 if (band->bandno & 1) {
2076 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2077 x += (OPJ_UINT32)(pres->x1 - pres->x0);
2079 if (band->bandno & 2) {
2080 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2081 y += (OPJ_UINT32)(pres->y1 - pres->y0);
2084 if (!opj_sparse_array_int32_write(sa, x, y,
2085 x + cblk_w, y + cblk_h,
2087 1, cblk_w, OPJ_TRUE)) {
2088 opj_sparse_array_int32_free(sa);
2101 static OPJ_BOOL opj_dwt_decode_partial_tile(
2102 opj_tcd_tilecomp_t* tilec,
2105 opj_sparse_array_int32_t* sa;
2109 /* This value matches the maximum left/right extension given in tables */
2110 /* F.2 and F.3 of the standard. */
2111 const OPJ_UINT32 filter_width = 2U;
2113 opj_tcd_resolution_t* tr = tilec->resolutions;
2114 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2116 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2117 tr->x0); /* width of the resolution level computed */
2118 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2119 tr->y0); /* height of the resolution level computed */
2121 OPJ_SIZE_T h_mem_size;
2123 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2124 /* with the tile coordinates */
2125 OPJ_UINT32 win_tcx0 = tilec->win_x0;
2126 OPJ_UINT32 win_tcy0 = tilec->win_y0;
2127 OPJ_UINT32 win_tcx1 = tilec->win_x1;
2128 OPJ_UINT32 win_tcy1 = tilec->win_y1;
2130 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2134 sa = opj_dwt_init_sparse_array(tilec, numres);
2140 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2141 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2142 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2143 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2144 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2146 1, tr_max->win_x1 - tr_max->win_x0,
2150 opj_sparse_array_int32_free(sa);
2153 h_mem_size = opj_dwt_max_resolution(tr, numres);
2154 /* overflow check */
2155 /* in vertical pass, we process 4 columns at a time */
2156 if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2157 /* FIXME event manager error callback */
2158 opj_sparse_array_int32_free(sa);
2162 h_mem_size *= 4 * sizeof(OPJ_INT32);
2163 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2165 /* FIXME event manager error callback */
2166 opj_sparse_array_int32_free(sa);
2172 for (resno = 1; resno < numres; resno ++) {
2174 /* Window of interest subband-based coordinates */
2175 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2176 OPJ_UINT32 win_hl_x0, win_hl_x1;
2177 OPJ_UINT32 win_lh_y0, win_lh_y1;
2178 /* Window of interest tile-resolution-based coordinates */
2179 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2180 /* Tile-resolution subband-based coordinates */
2181 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2185 h.sn = (OPJ_INT32)rw;
2186 v.sn = (OPJ_INT32)rh;
2188 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2189 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2191 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2194 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2197 /* Get the subband coordinates for the window of interest */
2199 opj_dwt_get_band_coordinates(tilec, resno, 0,
2200 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2201 &win_ll_x0, &win_ll_y0,
2202 &win_ll_x1, &win_ll_y1);
2205 opj_dwt_get_band_coordinates(tilec, resno, 1,
2206 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2207 &win_hl_x0, NULL, &win_hl_x1, NULL);
2210 opj_dwt_get_band_coordinates(tilec, resno, 2,
2211 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2212 NULL, &win_lh_y0, NULL, &win_lh_y1);
2214 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2215 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2216 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2217 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2218 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2220 /* Subtract the origin of the bands for this tile, to the subwindow */
2221 /* of interest band coordinates, so as to get them relative to the */
2223 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2224 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2225 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2226 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2227 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2228 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2229 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2230 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2232 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2233 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2235 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2236 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2238 /* Compute the tile-resolution-based coordinates for the window of interest */
2240 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2241 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2243 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2244 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2248 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2249 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2251 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2252 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2255 for (j = 0; j < rh; ++j) {
2256 if ((j >= win_ll_y0 && j < win_ll_y1) ||
2257 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2259 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2260 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2261 /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2262 /* This is less extreme than memsetting the whole buffer to 0 */
2263 /* although we could potentially do better with better handling of edge conditions */
2264 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2265 h.mem[win_tr_x1 - 1] = 0;
2267 if (win_tr_x1 < rw) {
2268 h.mem[win_tr_x1] = 0;
2271 opj_dwt_interleave_partial_h(h.mem,
2280 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
2281 (OPJ_INT32)win_ll_x0,
2282 (OPJ_INT32)win_ll_x1,
2283 (OPJ_INT32)win_hl_x0,
2284 (OPJ_INT32)win_hl_x1);
2285 if (!opj_sparse_array_int32_write(sa,
2290 /* FIXME event manager error callback */
2291 opj_sparse_array_int32_free(sa);
2292 opj_aligned_free(h.mem);
2298 for (i = win_tr_x0; i < win_tr_x1;) {
2299 OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
2300 opj_dwt_interleave_partial_v(v.mem,
2310 opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
2311 (OPJ_INT32)win_ll_y0,
2312 (OPJ_INT32)win_ll_y1,
2313 (OPJ_INT32)win_lh_y0,
2314 (OPJ_INT32)win_lh_y1);
2315 if (!opj_sparse_array_int32_write(sa,
2317 i + nb_cols, win_tr_y1,
2318 v.mem + 4 * win_tr_y0,
2320 /* FIXME event manager error callback */
2321 opj_sparse_array_int32_free(sa);
2322 opj_aligned_free(h.mem);
2329 opj_aligned_free(h.mem);
2332 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2333 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2334 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2335 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2336 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2338 1, tr_max->win_x1 - tr_max->win_x0,
2343 opj_sparse_array_int32_free(sa);
2347 static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
2348 OPJ_FLOAT32* OPJ_RESTRICT a,
2350 OPJ_UINT32 remaining_height)
2352 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2354 OPJ_UINT32 x0 = dwt->win_l_x0;
2355 OPJ_UINT32 x1 = dwt->win_l_x1;
2357 for (k = 0; k < 2; ++k) {
2358 if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
2359 ((OPJ_SIZE_T) bi & 0x0f) == 0) {
2360 /* Fast code path */
2361 for (i = x0; i < x1; ++i) {
2363 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2381 /* Slow code path */
2382 for (i = x0; i < x1; ++i) {
2384 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2387 if (remaining_height == 1) {
2392 if (remaining_height == 2) {
2397 if (remaining_height == 3) {
2402 if (remaining_height == 4) {
2407 if (remaining_height == 5) {
2412 if (remaining_height == 6) {
2417 if (remaining_height == 7) {
2424 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2431 static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
2432 opj_sparse_array_int32_t* sa,
2434 OPJ_UINT32 remaining_height)
2437 for (i = 0; i < remaining_height; i++) {
2439 ret = opj_sparse_array_int32_read(sa,
2440 dwt->win_l_x0, sa_line + i,
2441 dwt->win_l_x1, sa_line + i + 1,
2442 /* Nasty cast from float* to int32* */
2443 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2444 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2446 ret = opj_sparse_array_int32_read(sa,
2447 (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2448 (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2449 /* Nasty cast from float* to int32* */
2450 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2451 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2457 static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2458 OPJ_FLOAT32* OPJ_RESTRICT a,
2460 OPJ_UINT32 nb_elts_read)
2462 opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2465 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2466 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2467 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2470 a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
2471 bi = dwt->wavelet + 1 - dwt->cas;
2473 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2474 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2475 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2479 static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2480 opj_sparse_array_int32_t* sa,
2482 OPJ_UINT32 nb_elts_read)
2485 ret = opj_sparse_array_int32_read(sa,
2486 sa_col, dwt->win_l_x0,
2487 sa_col + nb_elts_read, dwt->win_l_x1,
2488 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
2489 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2491 ret = opj_sparse_array_int32_read(sa,
2492 sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2493 sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2494 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
2495 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2502 static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
2507 __m128* OPJ_RESTRICT vw = (__m128*) w;
2508 OPJ_UINT32 i = start;
2509 /* To be adapted if NB_ELTS_V8 changes */
2511 /* Note: attempt at loop unrolling x2 doesn't help */
2512 for (; i < end; ++i, vw += 4) {
2513 vw[0] = _mm_mul_ps(vw[0], c);
2514 vw[1] = _mm_mul_ps(vw[1], c);
2518 static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
2524 __m128* OPJ_RESTRICT vl = (__m128*) l;
2525 __m128* OPJ_RESTRICT vw = (__m128*) w;
2526 /* To be adapted if NB_ELTS_V8 changes */
2528 OPJ_UINT32 imax = opj_uint_min(end, m);
2531 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
2532 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
2541 /* Note: attempt at loop unrolling x2 doesn't help */
2542 for (; i < imax; ++i) {
2543 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
2544 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
2548 assert(m + 1 == end);
2549 c = _mm_add_ps(c, c);
2550 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
2551 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
2557 static void opj_v8dwt_decode_step1(opj_v8_t* w,
2560 const OPJ_FLOAT32 c)
2562 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2564 /* To be adapted if NB_ELTS_V8 changes */
2565 for (i = start; i < end; ++i) {
2566 fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c;
2567 fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
2568 fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
2569 fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
2570 fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
2571 fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
2572 fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
2573 fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
2577 static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
2583 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2584 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2586 OPJ_UINT32 imax = opj_uint_min(end, m);
2588 fw += 2 * NB_ELTS_V8 * start;
2589 fl = fw - 2 * NB_ELTS_V8;
2591 /* To be adapted if NB_ELTS_V8 changes */
2592 for (i = start; i < imax; ++i) {
2593 fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
2594 fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
2595 fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
2596 fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
2597 fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
2598 fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
2599 fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
2600 fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
2602 fw += 2 * NB_ELTS_V8;
2605 assert(m + 1 == end);
2607 fw[-8] = fw[-8] + fl[0] * c;
2608 fw[-7] = fw[-7] + fl[1] * c;
2609 fw[-6] = fw[-6] + fl[2] * c;
2610 fw[-5] = fw[-5] + fl[3] * c;
2611 fw[-4] = fw[-4] + fl[4] * c;
2612 fw[-3] = fw[-3] + fl[5] * c;
2613 fw[-2] = fw[-2] + fl[6] * c;
2614 fw[-1] = fw[-1] + fl[7] * c;
2621 /* Inverse 9-7 wavelet transform in 1-D. */
2623 static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
2626 /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
2627 /* Historic value for 2 / opj_invK */
2628 /* Normally, we should use invK, but if we do so, we have failures in the */
2629 /* conformance test, due to MSE and peak errors significantly higher than */
2630 /* accepted value */
2631 /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
2632 /* the computation of the stepsize for the non LL subbands */
2633 const float two_invK = 1.625732422f;
2634 if (dwt->cas == 0) {
2635 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2641 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2648 opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2649 _mm_set1_ps(opj_K));
2650 opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2651 _mm_set1_ps(two_invK));
2652 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2653 dwt->win_l_x0, dwt->win_l_x1,
2654 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2655 _mm_set1_ps(-opj_dwt_delta));
2656 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2657 dwt->win_h_x0, dwt->win_h_x1,
2658 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2659 _mm_set1_ps(-opj_dwt_gamma));
2660 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2661 dwt->win_l_x0, dwt->win_l_x1,
2662 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2663 _mm_set1_ps(-opj_dwt_beta));
2664 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
2665 dwt->win_h_x0, dwt->win_h_x1,
2666 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2667 _mm_set1_ps(-opj_dwt_alpha));
2669 opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2671 opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2673 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2674 dwt->win_l_x0, dwt->win_l_x1,
2675 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2677 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2678 dwt->win_h_x0, dwt->win_h_x1,
2679 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2681 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
2682 dwt->win_l_x0, dwt->win_l_x1,
2683 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2685 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
2686 dwt->win_h_x0, dwt->win_h_x1,
2687 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
2696 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2698 } opj_dwt97_decode_h_job_t;
2700 static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
2703 opj_dwt97_decode_h_job_t* job;
2704 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2708 job = (opj_dwt97_decode_h_job_t*)user_data;
2711 assert((job->nb_rows % NB_ELTS_V8) == 0);
2714 for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
2716 opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
2717 opj_v8dwt_decode(&job->h);
2719 /* To be adapted if NB_ELTS_V8 changes */
2720 for (k = 0; k < job->rw; k++) {
2721 aj[k ] = job->h.wavelet[k].f[0];
2722 aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1];
2723 aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
2724 aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
2726 for (k = 0; k < job->rw; k++) {
2727 aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
2728 aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
2729 aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
2730 aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
2733 aj += w * NB_ELTS_V8;
2736 opj_aligned_free(job->h.wavelet);
2745 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2746 OPJ_UINT32 nb_columns;
2747 } opj_dwt97_decode_v_job_t;
2749 static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
2752 opj_dwt97_decode_v_job_t* job;
2753 OPJ_FLOAT32 * OPJ_RESTRICT aj;
2756 job = (opj_dwt97_decode_v_job_t*)user_data;
2758 assert((job->nb_columns % NB_ELTS_V8) == 0);
2761 for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
2764 opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
2765 opj_v8dwt_decode(&job->v);
2767 for (k = 0; k < job->rh; ++k) {
2768 memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
2769 NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2774 opj_aligned_free(job->v.wavelet);
2780 /* Inverse 9-7 wavelet transform in 2-D. */
2783 OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
2784 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
2790 opj_tcd_resolution_t* res = tilec->resolutions;
2792 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
2793 res->x0); /* width of the resolution level computed */
2794 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
2795 res->y0); /* height of the resolution level computed */
2797 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2799 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2801 OPJ_SIZE_T l_data_size;
2802 const int num_threads = opj_thread_pool_get_thread_count(tp);
2808 l_data_size = opj_dwt_max_resolution(res, numres);
2809 /* overflow check */
2810 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
2811 /* FIXME event manager error callback */
2814 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2816 /* FIXME event manager error callback */
2819 v.wavelet = h.wavelet;
2822 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
2825 h.sn = (OPJ_INT32)rw;
2826 v.sn = (OPJ_INT32)rh;
2830 rw = (OPJ_UINT32)(res->x1 -
2831 res->x0); /* width of the resolution level computed */
2832 rh = (OPJ_UINT32)(res->y1 -
2833 res->y0); /* height of the resolution level computed */
2835 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2836 h.cas = res->x0 % 2;
2839 h.win_l_x1 = (OPJ_UINT32)h.sn;
2841 h.win_h_x1 = (OPJ_UINT32)h.dn;
2843 if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
2844 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
2846 opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
2847 opj_v8dwt_decode(&h);
2849 /* To be adapted if NB_ELTS_V8 changes */
2850 for (k = 0; k < rw; k++) {
2851 aj[k ] = h.wavelet[k].f[0];
2852 aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1];
2853 aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
2854 aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
2856 for (k = 0; k < rw; k++) {
2857 aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
2858 aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
2859 aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
2860 aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
2863 aj += w * NB_ELTS_V8;
2866 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2869 if ((rh / NB_ELTS_V8) < num_jobs) {
2870 num_jobs = rh / NB_ELTS_V8;
2872 step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2873 for (j = 0; j < num_jobs; j++) {
2874 opj_dwt97_decode_h_job_t* job;
2876 job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
2878 opj_thread_pool_wait_completion(tp, 0);
2879 opj_aligned_free(h.wavelet);
2882 job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2883 if (!job->h.wavelet) {
2884 opj_thread_pool_wait_completion(tp, 0);
2886 opj_aligned_free(h.wavelet);
2892 job->h.win_l_x0 = h.win_l_x0;
2893 job->h.win_l_x1 = h.win_l_x1;
2894 job->h.win_h_x0 = h.win_h_x0;
2895 job->h.win_h_x1 = h.win_h_x1;
2899 job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
2900 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
2901 aj += w * job->nb_rows;
2902 opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
2904 opj_thread_pool_wait_completion(tp, 0);
2905 j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
2910 opj_v8dwt_interleave_h(&h, aj, w, rh - j);
2911 opj_v8dwt_decode(&h);
2912 for (k = 0; k < rw; k++) {
2914 for (l = 0; l < rh - j; l++) {
2915 aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l];
2920 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2921 v.cas = res->y0 % 2;
2923 v.win_l_x1 = (OPJ_UINT32)v.sn;
2925 v.win_h_x1 = (OPJ_UINT32)v.dn;
2927 aj = (OPJ_FLOAT32*) tilec->data;
2928 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
2929 for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
2932 opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
2933 opj_v8dwt_decode(&v);
2935 for (k = 0; k < rh; ++k) {
2936 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
2941 /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
2942 transfer being the limiting factor. So limit the number of
2945 OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
2948 if ((rw / NB_ELTS_V8) < num_jobs) {
2949 num_jobs = rw / NB_ELTS_V8;
2951 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2952 for (j = 0; j < num_jobs; j++) {
2953 opj_dwt97_decode_v_job_t* job;
2955 job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
2957 opj_thread_pool_wait_completion(tp, 0);
2958 opj_aligned_free(h.wavelet);
2961 job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
2962 if (!job->v.wavelet) {
2963 opj_thread_pool_wait_completion(tp, 0);
2965 opj_aligned_free(h.wavelet);
2971 job->v.win_l_x0 = v.win_l_x0;
2972 job->v.win_l_x1 = v.win_l_x1;
2973 job->v.win_h_x0 = v.win_h_x0;
2974 job->v.win_h_x1 = v.win_h_x1;
2978 job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
2979 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
2980 aj += job->nb_columns;
2981 opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
2983 opj_thread_pool_wait_completion(tp, 0);
2986 if (rw & (NB_ELTS_V8 - 1)) {
2989 j = rw & (NB_ELTS_V8 - 1);
2991 opj_v8dwt_interleave_v(&v, aj, w, j);
2992 opj_v8dwt_decode(&v);
2994 for (k = 0; k < rh; ++k) {
2995 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
2996 (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
3001 opj_aligned_free(h.wavelet);
3006 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3009 opj_sparse_array_int32_t* sa;
3013 /* This value matches the maximum left/right extension given in tables */
3014 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3015 /* we currently use 3. */
3016 const OPJ_UINT32 filter_width = 4U;
3018 opj_tcd_resolution_t* tr = tilec->resolutions;
3019 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3021 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3022 tr->x0); /* width of the resolution level computed */
3023 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3024 tr->y0); /* height of the resolution level computed */
3026 OPJ_SIZE_T l_data_size;
3028 /* Compute the intersection of the area of interest, expressed in tile coordinates */
3029 /* with the tile coordinates */
3030 OPJ_UINT32 win_tcx0 = tilec->win_x0;
3031 OPJ_UINT32 win_tcy0 = tilec->win_y0;
3032 OPJ_UINT32 win_tcx1 = tilec->win_x1;
3033 OPJ_UINT32 win_tcy1 = tilec->win_y1;
3035 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3039 sa = opj_dwt_init_sparse_array(tilec, numres);
3045 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3046 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3047 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3048 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3049 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3051 1, tr_max->win_x1 - tr_max->win_x0,
3055 opj_sparse_array_int32_free(sa);
3059 l_data_size = opj_dwt_max_resolution(tr, numres);
3060 /* overflow check */
3061 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3062 /* FIXME event manager error callback */
3063 opj_sparse_array_int32_free(sa);
3066 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3068 /* FIXME event manager error callback */
3069 opj_sparse_array_int32_free(sa);
3072 v.wavelet = h.wavelet;
3074 for (resno = 1; resno < numres; resno ++) {
3076 /* Window of interest subband-based coordinates */
3077 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3078 OPJ_UINT32 win_hl_x0, win_hl_x1;
3079 OPJ_UINT32 win_lh_y0, win_lh_y1;
3080 /* Window of interest tile-resolution-based coordinates */
3081 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3082 /* Tile-resolution subband-based coordinates */
3083 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3087 h.sn = (OPJ_INT32)rw;
3088 v.sn = (OPJ_INT32)rh;
3090 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3091 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3093 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3096 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3099 /* Get the subband coordinates for the window of interest */
3101 opj_dwt_get_band_coordinates(tilec, resno, 0,
3102 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3103 &win_ll_x0, &win_ll_y0,
3104 &win_ll_x1, &win_ll_y1);
3107 opj_dwt_get_band_coordinates(tilec, resno, 1,
3108 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3109 &win_hl_x0, NULL, &win_hl_x1, NULL);
3112 opj_dwt_get_band_coordinates(tilec, resno, 2,
3113 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3114 NULL, &win_lh_y0, NULL, &win_lh_y1);
3116 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3117 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3118 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3119 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3120 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3122 /* Subtract the origin of the bands for this tile, to the subwindow */
3123 /* of interest band coordinates, so as to get them relative to the */
3125 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3126 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3127 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3128 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3129 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3130 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3131 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3132 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3134 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3135 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3137 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3138 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3140 /* Compute the tile-resolution-based coordinates for the window of interest */
3142 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3143 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3145 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3146 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3150 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3151 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3153 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3154 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3157 h.win_l_x0 = win_ll_x0;
3158 h.win_l_x1 = win_ll_x1;
3159 h.win_h_x0 = win_hl_x0;
3160 h.win_h_x1 = win_hl_x1;
3161 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3162 if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3163 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3164 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3165 opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3166 opj_v8dwt_decode(&h);
3167 if (!opj_sparse_array_int32_write(sa,
3169 win_tr_x1, j + NB_ELTS_V8,
3170 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3171 NB_ELTS_V8, 1, OPJ_TRUE)) {
3172 /* FIXME event manager error callback */
3173 opj_sparse_array_int32_free(sa);
3174 opj_aligned_free(h.wavelet);
3181 ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3182 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3183 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3184 opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3185 opj_v8dwt_decode(&h);
3186 if (!opj_sparse_array_int32_write(sa,
3189 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3190 NB_ELTS_V8, 1, OPJ_TRUE)) {
3191 /* FIXME event manager error callback */
3192 opj_sparse_array_int32_free(sa);
3193 opj_aligned_free(h.wavelet);
3198 v.win_l_x0 = win_ll_y0;
3199 v.win_l_x1 = win_ll_y1;
3200 v.win_h_x0 = win_lh_y0;
3201 v.win_h_x1 = win_lh_y1;
3202 for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3203 OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3205 opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3206 opj_v8dwt_decode(&v);
3208 if (!opj_sparse_array_int32_write(sa,
3210 j + nb_elts, win_tr_y1,
3211 (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3212 1, NB_ELTS_V8, OPJ_TRUE)) {
3213 /* FIXME event manager error callback */
3214 opj_sparse_array_int32_free(sa);
3215 opj_aligned_free(h.wavelet);
3222 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3223 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3224 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3225 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3226 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3228 1, tr_max->win_x1 - tr_max->win_x0,
3233 opj_sparse_array_int32_free(sa);
3235 opj_aligned_free(h.wavelet);
3240 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3241 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3244 if (p_tcd->whole_tile_decoding) {
3245 return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3247 return opj_dwt_decode_partial_97(tilec, numres);