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);
118 /** @name Local static functions */
122 Forward lazy transform (horizontal)
124 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
125 OPJ_INT32 * OPJ_RESTRICT b,
127 OPJ_INT32 sn, OPJ_INT32 cas);
129 Forward lazy transform (vertical)
131 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
132 OPJ_INT32 * OPJ_RESTRICT b,
134 OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas);
137 Forward 9-7 wavelet transform in 1-D
139 static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
142 Explicit calculation of the Quantization Stepsizes
144 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
145 opj_stepsize_t *bandno_stepsize);
147 Inverse wavelet transform in 2-D.
149 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
150 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
152 static OPJ_BOOL opj_dwt_decode_partial_tile(
153 opj_tcd_tilecomp_t* tilec,
156 /* Forward transform, for the vertical pass, processing cols columns */
157 /* where cols <= NB_ELTS_V8 */
158 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
159 typedef void (*opj_encode_and_deinterleave_v_fnptr_type)(
164 OPJ_UINT32 stride_width,
167 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
168 typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
174 static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
175 opj_tcd_tilecomp_t * tilec,
176 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
177 opj_encode_and_deinterleave_h_one_row_fnptr_type
178 p_encode_and_deinterleave_h_one_row);
180 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
184 /* Inverse 9-7 wavelet transform in 1-D. */
191 #define OPJ_S(i) a[(i)*2]
192 #define OPJ_D(i) a[(1+(i)*2)]
193 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
194 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
196 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
197 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
200 /* This table contains the norms of the 5-3 wavelets for different bands. */
202 /* FIXME! the array should really be extended up to 33 resolution levels */
203 /* See https://github.com/uclouvain/openjpeg/issues/493 */
204 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
205 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
206 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
207 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
208 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
212 /* This table contains the norms of the 9-7 wavelets for different bands. */
214 /* FIXME! the array should really be extended up to 33 resolution levels */
215 /* See https://github.com/uclouvain/openjpeg/issues/493 */
216 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
217 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
218 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
219 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
220 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
224 ==========================================================
226 ==========================================================
230 /* Forward lazy transform (horizontal). */
232 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
233 OPJ_INT32 * OPJ_RESTRICT b,
235 OPJ_INT32 sn, OPJ_INT32 cas)
238 OPJ_INT32 * OPJ_RESTRICT l_dest = b;
239 const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
241 for (i = 0; i < sn; ++i) {
249 for (i = 0; i < dn; ++i) {
256 /* Forward lazy transform (vertical). */
258 static void opj_dwt_deinterleave_v(const OPJ_INT32 * OPJ_RESTRICT a,
259 OPJ_INT32 * OPJ_RESTRICT b,
261 OPJ_INT32 sn, OPJ_UINT32 x, OPJ_INT32 cas)
264 OPJ_INT32 * OPJ_RESTRICT l_dest = b;
265 const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
271 } /* b[i*x]=a[2*i+cas]; */
273 l_dest = b + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)x;
281 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
284 #ifdef STANDARD_SLOW_VERSION
286 /* Inverse lazy transform (horizontal). */
288 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
290 const OPJ_INT32 *ai = a;
291 OPJ_INT32 *bi = h->mem + h->cas;
298 bi = h->mem + 1 - h->cas;
307 /* Inverse lazy transform (vertical). */
309 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
311 const OPJ_INT32 *ai = a;
312 OPJ_INT32 *bi = v->mem + v->cas;
319 ai = a + (v->sn * (OPJ_SIZE_T)x);
320 bi = v->mem + 1 - v->cas;
329 #endif /* STANDARD_SLOW_VERSION */
331 #ifdef STANDARD_SLOW_VERSION
333 /* Inverse 5-3 wavelet transform in 1-D. */
335 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
341 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
342 for (i = 0; i < sn; i++) {
343 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
345 for (i = 0; i < dn; i++) {
346 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
350 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
353 for (i = 0; i < sn; i++) {
354 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
356 for (i = 0; i < dn; i++) {
357 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
363 static void opj_dwt_decode_1(const opj_dwt_t *v)
365 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
368 #endif /* STANDARD_SLOW_VERSION */
370 #if !defined(STANDARD_SLOW_VERSION)
371 static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
377 const OPJ_INT32* in_even = &tiledp[0];
378 const OPJ_INT32* in_odd = &tiledp[sn];
380 #ifdef TWO_PASS_VERSION
381 /* For documentation purpose: performs lifting in two iterations, */
382 /* but without explicit interleaving */
387 tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
388 for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
389 tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
391 if (len & 1) { /* if len is odd */
392 tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
396 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
397 tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
399 if (!(len & 1)) { /* if len is even */
400 tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
403 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
407 /* Improved version of the TWO_PASS_VERSION: */
408 /* Performs lifting in one single iteration. Saves memory */
409 /* accesses and explicit interleaving. */
412 s0n = s1n - ((d1n + 1) >> 1);
414 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
421 s0n = s1n - ((d1c + d1n + 2) >> 2);
424 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
430 tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
431 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
433 tmp[len - 1] = d1n + s0n;
436 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
439 static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
445 const OPJ_INT32* in_even = &tiledp[sn];
446 const OPJ_INT32* in_odd = &tiledp[0];
448 #ifdef TWO_PASS_VERSION
449 /* For documentation purpose: performs lifting in two iterations, */
450 /* but without explicit interleaving */
455 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
456 tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
459 tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
463 tmp[0] = in_even[0] + tmp[1];
464 for (i = 2, j = 1; i < len - 1; i += 2, j++) {
465 tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
468 tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
471 OPJ_INT32 s1, s2, dc, dn;
475 /* Improved version of the TWO_PASS_VERSION: */
476 /* Performs lifting in one single iteration. Saves memory */
477 /* accesses and explicit interleaving. */
480 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
481 tmp[0] = in_even[0] + dc;
483 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
487 dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
489 tmp[i + 1] = s1 + ((dn + dc) >> 1);
498 dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
499 tmp[len - 2] = s1 + ((dn + dc) >> 1);
502 tmp[len - 1] = s1 + dc;
505 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
509 #endif /* !defined(STANDARD_SLOW_VERSION) */
512 /* Inverse 5-3 wavelet transform in 1-D for one row. */
514 /* Performs interleave, inverse wavelet transform and copy back to buffer */
515 static void opj_idwt53_h(const opj_dwt_t *dwt,
518 #ifdef STANDARD_SLOW_VERSION
519 /* For documentation purpose */
520 opj_dwt_interleave_h(dwt, tiledp);
521 opj_dwt_decode_1(dwt);
522 memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
524 const OPJ_INT32 sn = dwt->sn;
525 const OPJ_INT32 len = sn + dwt->dn;
526 if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
528 opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
530 /* Unmodified value */
532 } else { /* Left-most sample is on odd coordinate */
535 } else if (len == 2) {
536 OPJ_INT32* out = dwt->mem;
537 const OPJ_INT32* in_even = &tiledp[sn];
538 const OPJ_INT32* in_odd = &tiledp[0];
539 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
540 out[0] = in_even[0] + out[1];
541 memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
542 } else if (len > 2) {
543 opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
549 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
551 /* Conveniency macros to improve the readabilty of the formulas */
554 #define LOAD_CST(x) _mm256_set1_epi32(x)
555 #define LOAD(x) _mm256_load_si256((const VREG*)(x))
556 #define LOADU(x) _mm256_loadu_si256((const VREG*)(x))
557 #define STORE(x,y) _mm256_store_si256((VREG*)(x),(y))
558 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
559 #define ADD(x,y) _mm256_add_epi32((x),(y))
560 #define SUB(x,y) _mm256_sub_epi32((x),(y))
561 #define SAR(x,y) _mm256_srai_epi32((x),(y))
564 #define LOAD_CST(x) _mm_set1_epi32(x)
565 #define LOAD(x) _mm_load_si128((const VREG*)(x))
566 #define LOADU(x) _mm_loadu_si128((const VREG*)(x))
567 #define STORE(x,y) _mm_store_si128((VREG*)(x),(y))
568 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
569 #define ADD(x,y) _mm_add_epi32((x),(y))
570 #define SUB(x,y) _mm_sub_epi32((x),(y))
571 #define SAR(x,y) _mm_srai_epi32((x),(y))
573 #define ADD3(x,y,z) ADD(ADD(x,y),z)
576 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
577 const OPJ_INT32* tmp,
582 for (i = 0; i < len; ++i) {
583 /* A memcpy(&tiledp_col[i * stride + 0],
584 &tmp[PARALLEL_COLS_53 * i + 0],
585 PARALLEL_COLS_53 * sizeof(OPJ_INT32))
586 would do but would be a tiny bit slower.
587 We can take here advantage of our knowledge of alignment */
588 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
589 LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
590 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
591 LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
595 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
596 * 16 in AVX2, when top-most pixel is on even coordinate */
597 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
601 OPJ_INT32* tiledp_col,
602 const OPJ_SIZE_T stride)
604 const OPJ_INT32* in_even = &tiledp_col[0];
605 const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
609 VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
610 VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
611 const VREG two = LOAD_CST(2);
615 assert(PARALLEL_COLS_53 == 16);
616 assert(VREG_INT_COUNT == 8);
618 assert(PARALLEL_COLS_53 == 8);
619 assert(VREG_INT_COUNT == 4);
622 /* Note: loads of input even/odd values must be done in a unaligned */
623 /* fashion. But stores in tmp can be done with aligned store, since */
624 /* the temporary buffer is properly aligned */
625 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
627 s1n_0 = LOADU(in_even + 0);
628 s1n_1 = LOADU(in_even + VREG_INT_COUNT);
629 d1n_0 = LOADU(in_odd);
630 d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
632 /* s0n = s1n - ((d1n + 1) >> 1); <==> */
633 /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
634 s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
635 s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
637 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
643 s1n_0 = LOADU(in_even + j * stride);
644 s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
645 d1n_0 = LOADU(in_odd + j * stride);
646 d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
648 /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
649 s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
650 s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
652 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
653 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
655 /* d1c + ((s0c + s0n) >> 1) */
656 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
657 ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
658 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
659 ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
662 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
663 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
666 VREG tmp_len_minus_1;
667 s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
668 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
669 tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
670 STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
671 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
672 STORE(tmp + PARALLEL_COLS_53 * (len - 2),
673 ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
675 s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
676 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
677 tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
678 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
680 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
681 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
682 ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
686 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
688 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
692 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
696 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
697 * 16 in AVX2, when top-most pixel is on odd coordinate */
698 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
702 OPJ_INT32* tiledp_col,
703 const OPJ_SIZE_T stride)
708 VREG s1_0, s2_0, dc_0, dn_0;
709 VREG s1_1, s2_1, dc_1, dn_1;
710 const VREG two = LOAD_CST(2);
712 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
713 const OPJ_INT32* in_odd = &tiledp_col[0];
717 assert(PARALLEL_COLS_53 == 16);
718 assert(VREG_INT_COUNT == 8);
720 assert(PARALLEL_COLS_53 == 8);
721 assert(VREG_INT_COUNT == 4);
724 /* Note: loads of input even/odd values must be done in a unaligned */
725 /* fashion. But stores in tmp can be done with aligned store, since */
726 /* the temporary buffer is properly aligned */
727 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
729 s1_0 = LOADU(in_even + stride);
730 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
731 dc_0 = SUB(LOADU(in_odd + 0),
732 SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
733 STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
735 s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
736 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
737 dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
738 SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
739 STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
740 ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
742 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
744 s2_0 = LOADU(in_even + (j + 1) * stride);
745 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
747 /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
748 dn_0 = SUB(LOADU(in_odd + j * stride),
749 SAR(ADD3(s1_0, s2_0, two), 2));
750 dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
751 SAR(ADD3(s1_1, s2_1, two), 2));
753 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
754 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
756 /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
757 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
758 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
759 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
760 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
767 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
768 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
771 /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
772 dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
773 SAR(ADD3(s1_0, s1_0, two), 2));
774 dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
775 SAR(ADD3(s1_1, s1_1, two), 2));
777 /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
778 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
779 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
780 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
781 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
783 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
784 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
786 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
787 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
791 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
805 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
807 #if !defined(STANDARD_SLOW_VERSION)
808 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
809 * pixel is on even coordinate */
810 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
813 OPJ_INT32* tiledp_col,
814 const OPJ_SIZE_T stride)
817 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
821 /* Performs lifting in one single iteration. Saves memory */
822 /* accesses and explicit interleaving. */
825 d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
826 s0n = s1n - ((d1n + 1) >> 1);
828 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
832 s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
833 d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
835 s0n = s1n - ((d1c + d1n + 2) >> 2);
838 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
845 tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
847 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
849 tmp[len - 1] = d1n + s0n;
852 for (i = 0; i < len; ++i) {
853 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
858 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
859 * pixel is on odd coordinate */
860 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
863 OPJ_INT32* tiledp_col,
864 const OPJ_SIZE_T stride)
867 OPJ_INT32 s1, s2, dc, dn;
868 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
869 const OPJ_INT32* in_odd = &tiledp_col[0];
873 /* Performs lifting in one single iteration. Saves memory */
874 /* accesses and explicit interleaving. */
876 s1 = in_even[stride];
877 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
878 tmp[0] = in_even[0] + dc;
879 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
881 s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
883 dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
885 tmp[i + 1] = s1 + ((dn + dc) >> 1);
892 dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
893 tmp[len - 2] = s1 + ((dn + dc) >> 1);
896 tmp[len - 1] = s1 + dc;
899 for (i = 0; i < len; ++i) {
900 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
903 #endif /* !defined(STANDARD_SLOW_VERSION) */
906 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
908 /* Performs interleave, inverse wavelet transform and copy back to buffer */
909 static void opj_idwt53_v(const opj_dwt_t *dwt,
910 OPJ_INT32* tiledp_col,
914 #ifdef STANDARD_SLOW_VERSION
915 /* For documentation purpose */
917 for (c = 0; c < nb_cols; c ++) {
918 opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
919 opj_dwt_decode_1(dwt);
920 for (k = 0; k < dwt->sn + dwt->dn; ++k) {
921 tiledp_col[c + k * stride] = dwt->mem[k];
925 const OPJ_INT32 sn = dwt->sn;
926 const OPJ_INT32 len = sn + dwt->dn;
928 /* If len == 1, unmodified value */
930 #if (defined(__SSE2__) || defined(__AVX2__))
931 if (len > 1 && nb_cols == PARALLEL_COLS_53) {
932 /* Same as below general case, except that thanks to SSE2/AVX2 */
933 /* we can efficiently process 8/16 columns in parallel */
934 opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
940 for (c = 0; c < nb_cols; c++, tiledp_col++) {
941 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
948 for (c = 0; c < nb_cols; c++, tiledp_col++) {
956 OPJ_INT32* out = dwt->mem;
957 for (c = 0; c < nb_cols; c++, tiledp_col++) {
959 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
960 const OPJ_INT32* in_odd = &tiledp_col[0];
962 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
963 out[0] = in_even[0] + out[1];
965 for (i = 0; i < len; ++i) {
966 tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
973 #if (defined(__SSE2__) || defined(__AVX2__))
974 if (len > 2 && nb_cols == PARALLEL_COLS_53) {
975 /* Same as below general case, except that thanks to SSE2/AVX2 */
976 /* we can efficiently process 8/16 columns in parallel */
977 opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
983 for (c = 0; c < nb_cols; c++, tiledp_col++) {
984 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
992 static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
998 for (i = start; i < end; ++i) {
1002 static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1009 OPJ_UINT32 imax = opj_uint_min(end, m);
1014 for (i = start; i < imax; ++i) {
1015 fw[-1] += (fl[0] + fw[0]) * c;
1020 assert(m + 1 == end);
1021 fw[-1] += (2 * fl[0]) * c;
1025 static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1028 OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1031 if (!((dn > 0) || (sn > 1))) {
1037 if (!((sn > 0) || (dn > 1))) {
1043 opj_dwt_encode_step2(w + a, w + b + 1,
1045 (OPJ_UINT32)opj_int_min(dn, sn - b),
1047 opj_dwt_encode_step2(w + b, w + a + 1,
1049 (OPJ_UINT32)opj_int_min(sn, dn - a),
1051 opj_dwt_encode_step2(w + a, w + b + 1,
1053 (OPJ_UINT32)opj_int_min(dn, sn - b),
1055 opj_dwt_encode_step2(w + b, w + a + 1,
1057 (OPJ_UINT32)opj_int_min(sn, dn - a),
1059 opj_dwt_encode_step1(w + b, 0, (OPJ_UINT32)dn,
1061 opj_dwt_encode_step1(w + a, 0, (OPJ_UINT32)sn,
1065 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1066 opj_stepsize_t *bandno_stepsize)
1069 p = opj_int_floorlog2(stepsize) - 13;
1070 n = 11 - opj_int_floorlog2(stepsize);
1071 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1072 bandno_stepsize->expn = numbps - p;
1076 ==========================================================
1078 ==========================================================
1081 /** Process one line for the horizontal pass of the 5x3 forward transform */
1083 void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1088 OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1089 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1090 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1091 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1096 for (i = 0; i < sn - 1; i++) {
1097 tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1);
1099 if ((width % 2) == 0) {
1100 tmp[sn + i] = row[2 * i + 1] - row[(i) * 2];
1102 row[0] += (tmp[sn] + tmp[sn] + 2) >> 2;
1103 for (i = 1; i < dn; i++) {
1104 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2);
1106 if ((width % 2) == 1) {
1107 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2);
1109 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1116 tmp[sn + 0] = row[0] - row[1];
1117 for (i = 1; i < sn; i++) {
1118 tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1);
1120 if ((width % 2) == 1) {
1121 tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1];
1124 for (i = 0; i < dn - 1; i++) {
1125 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2);
1127 if ((width % 2) == 0) {
1128 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2);
1130 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1135 /** Process one line for the horizontal pass of the 9x7 forward transform */
1137 void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1142 OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1143 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1144 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1145 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1146 memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1147 opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1148 opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1149 (OPJ_INT32 * OPJ_RESTRICT)row,
1150 dn, sn, even ? 0 : 1);
1155 OPJ_UINT32 rw; /* Width of the resolution to process */
1156 OPJ_UINT32 w; /* Width of tiledp */
1157 OPJ_INT32 * OPJ_RESTRICT tiledp;
1160 opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1161 } opj_dwt_encode_h_job_t;
1163 static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1166 opj_dwt_encode_h_job_t* job;
1169 job = (opj_dwt_encode_h_job_t*)user_data;
1170 for (j = job->min_j; j < job->max_j; j++) {
1171 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1172 (*job->p_function)(aj, job->h.mem, job->rw,
1173 job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1176 opj_aligned_free(job->h.mem);
1184 OPJ_INT32 * OPJ_RESTRICT tiledp;
1187 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v;
1188 } opj_dwt_encode_v_job_t;
1190 static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1193 opj_dwt_encode_v_job_t* job;
1196 job = (opj_dwt_encode_v_job_t*)user_data;
1197 for (j = job->min_j; j + NB_ELTS_V8 - 1 < job->max_j; j += NB_ELTS_V8) {
1198 (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1205 if (j < job->max_j) {
1206 (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1214 opj_aligned_free(job->v.mem);
1218 /** Fetch up to cols <= NB_ELTS_V8 for each line, and put them in tmpOut */
1219 /* that has a NB_ELTS_V8 interleave factor. */
1220 static void opj_dwt_fetch_cols_vertical_pass(const void *arrayIn,
1223 OPJ_UINT32 stride_width,
1226 const OPJ_INT32* OPJ_RESTRICT array = (const OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1227 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpOut;
1228 if (cols == NB_ELTS_V8) {
1230 for (k = 0; k < height; ++k) {
1231 memcpy(tmp + NB_ELTS_V8 * k,
1232 array + k * stride_width,
1233 NB_ELTS_V8 * sizeof(OPJ_INT32));
1237 for (k = 0; k < height; ++k) {
1239 for (c = 0; c < cols; c++) {
1240 tmp[NB_ELTS_V8 * k + c] = array[c + k * stride_width];
1242 for (; c < NB_ELTS_V8; c++) {
1243 tmp[NB_ELTS_V8 * k + c] = 0;
1249 /* Deinterleave result of forward transform, where cols <= NB_ELTS_V8 */
1250 /* and src contains NB_ELTS_V8 consecutive values for up to NB_ELTS_V8 */
1252 static INLINE void opj_dwt_deinterleave_v_cols(
1253 const OPJ_INT32 * OPJ_RESTRICT src,
1254 OPJ_INT32 * OPJ_RESTRICT dst,
1257 OPJ_UINT32 stride_width,
1262 OPJ_INT32 * OPJ_RESTRICT l_dest = dst;
1263 const OPJ_INT32 * OPJ_RESTRICT l_src = src + cas * NB_ELTS_V8;
1267 for (c = 0; c < cols; c++) {
1268 l_dest[c] = l_src[c];
1270 l_dest += stride_width;
1271 l_src += 2 * NB_ELTS_V8;
1274 l_dest = dst + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)stride_width;
1275 l_src = src + (1 - cas) * NB_ELTS_V8;
1279 for (c = 0; c < cols; c++) {
1280 l_dest[c] = l_src[c];
1282 l_dest += stride_width;
1283 l_src += 2 * NB_ELTS_V8;
1288 /* Forward 5-3 transform, for the vertical pass, processing cols columns */
1289 /* where cols <= NB_ELTS_V8 */
1290 static void opj_dwt_encode_and_deinterleave_v(
1295 OPJ_UINT32 stride_width,
1298 OPJ_INT32* OPJ_RESTRICT array = (OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1299 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpIn;
1300 const OPJ_UINT32 sn = (height + (even ? 1 : 0)) >> 1;
1301 const OPJ_UINT32 dn = height - sn;
1303 opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
1305 #define OPJ_Sc(i) tmp[(i)*2* NB_ELTS_V8 + c]
1306 #define OPJ_Dc(i) tmp[((1+(i)*2))* NB_ELTS_V8 + c]
1312 for (c = 0; c < NB_ELTS_V8; c++) {
1321 __m128i xmm_Si_0 = *(const __m128i*)(tmp + 4 * 0);
1322 __m128i xmm_Si_1 = *(const __m128i*)(tmp + 4 * 1);
1323 for (; i + 1 < sn; i++) {
1324 __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1325 (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1326 __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1327 (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1328 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1329 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1330 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1331 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1332 xmm_Di_0 = _mm_sub_epi32(xmm_Di_0,
1333 _mm_srai_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), 1));
1334 xmm_Di_1 = _mm_sub_epi32(xmm_Di_1,
1335 _mm_srai_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), 1));
1336 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
1337 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
1338 xmm_Si_0 = xmm_Sip1_0;
1339 xmm_Si_1 = xmm_Sip1_1;
1342 if (((height) % 2) == 0) {
1343 for (c = 0; c < NB_ELTS_V8; c++) {
1344 OPJ_Dc(i) -= OPJ_Sc(i);
1347 for (c = 0; c < NB_ELTS_V8; c++) {
1348 OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1352 __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1353 (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1354 __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1355 (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1356 const __m128i xmm_two = _mm_set1_epi32(2);
1357 for (; i < dn; i++) {
1358 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1359 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1360 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1361 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1362 __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1363 (i * 2) * NB_ELTS_V8 + 4 * 0);
1364 __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1365 (i * 2) * NB_ELTS_V8 + 4 * 1);
1366 xmm_Si_0 = _mm_add_epi32(xmm_Si_0,
1367 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_0, xmm_Di_0), xmm_two), 2));
1368 xmm_Si_1 = _mm_add_epi32(xmm_Si_1,
1369 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_1, xmm_Di_1), xmm_two), 2));
1370 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1371 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1372 xmm_Dim1_0 = xmm_Di_0;
1373 xmm_Dim1_1 = xmm_Di_1;
1376 if (((height) % 2) == 1) {
1377 for (c = 0; c < NB_ELTS_V8; c++) {
1378 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1384 for (c = 0; c < NB_ELTS_V8; c++) {
1385 OPJ_Sc(0) -= OPJ_Dc(0);
1389 __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1390 (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1391 __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1392 (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1393 for (; i < sn; i++) {
1394 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1395 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1396 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1397 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1398 __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1399 (i * 2) * NB_ELTS_V8 + 4 * 0);
1400 __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1401 (i * 2) * NB_ELTS_V8 + 4 * 1);
1402 xmm_Si_0 = _mm_sub_epi32(xmm_Si_0,
1403 _mm_srai_epi32(_mm_add_epi32(xmm_Di_0, xmm_Dim1_0), 1));
1404 xmm_Si_1 = _mm_sub_epi32(xmm_Si_1,
1405 _mm_srai_epi32(_mm_add_epi32(xmm_Di_1, xmm_Dim1_1), 1));
1406 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1407 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1408 xmm_Dim1_0 = xmm_Di_0;
1409 xmm_Dim1_1 = xmm_Di_1;
1412 if (((height) % 2) == 1) {
1413 for (c = 0; c < NB_ELTS_V8; c++) {
1414 OPJ_Sc(i) -= OPJ_Dc(i - 1);
1419 __m128i xmm_Si_0 = *((const __m128i*)(tmp + 4 * 0));
1420 __m128i xmm_Si_1 = *((const __m128i*)(tmp + 4 * 1));
1421 const __m128i xmm_two = _mm_set1_epi32(2);
1422 for (; i + 1 < dn; i++) {
1423 __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1424 (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1425 __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1426 (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1427 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1428 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1429 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1430 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1431 xmm_Di_0 = _mm_add_epi32(xmm_Di_0,
1432 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), xmm_two), 2));
1433 xmm_Di_1 = _mm_add_epi32(xmm_Di_1,
1434 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), xmm_two), 2));
1435 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
1436 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
1437 xmm_Si_0 = xmm_Sip1_0;
1438 xmm_Si_1 = xmm_Sip1_1;
1441 if (((height) % 2) == 0) {
1442 for (c = 0; c < NB_ELTS_V8; c++) {
1443 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1452 for (i = 0; i + 1 < sn; i++) {
1453 for (c = 0; c < NB_ELTS_V8; c++) {
1454 OPJ_Dc(i) -= (OPJ_Sc(i) + OPJ_Sc(i + 1)) >> 1;
1457 if (((height) % 2) == 0) {
1458 for (c = 0; c < NB_ELTS_V8; c++) {
1459 OPJ_Dc(i) -= OPJ_Sc(i);
1462 for (c = 0; c < NB_ELTS_V8; c++) {
1463 OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1465 for (i = 1; i < dn; i++) {
1466 for (c = 0; c < NB_ELTS_V8; c++) {
1467 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i) + 2) >> 2;
1470 if (((height) % 2) == 1) {
1471 for (c = 0; c < NB_ELTS_V8; c++) {
1472 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1479 for (c = 0; c < NB_ELTS_V8; c++) {
1484 for (c = 0; c < NB_ELTS_V8; c++) {
1485 OPJ_Sc(0) -= OPJ_Dc(0);
1487 for (i = 1; i < sn; i++) {
1488 for (c = 0; c < NB_ELTS_V8; c++) {
1489 OPJ_Sc(i) -= (OPJ_Dc(i) + OPJ_Dc(i - 1)) >> 1;
1492 if (((height) % 2) == 1) {
1493 for (c = 0; c < NB_ELTS_V8; c++) {
1494 OPJ_Sc(i) -= OPJ_Dc(i - 1);
1497 for (i = 0; i + 1 < dn; i++) {
1498 for (c = 0; c < NB_ELTS_V8; c++) {
1499 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i + 1) + 2) >> 2;
1502 if (((height) % 2) == 0) {
1503 for (c = 0; c < NB_ELTS_V8; c++) {
1504 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1511 if (cols == NB_ELTS_V8) {
1512 opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1513 stride_width, even ? 0 : 1, NB_ELTS_V8);
1515 opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1516 stride_width, even ? 0 : 1, cols);
1520 /* Forward 9-7 transform, for the vertical pass, processing cols columns */
1521 /* where cols <= NB_ELTS_V8 */
1522 static void opj_dwt_encode_and_deinterleave_v_real(
1527 OPJ_UINT32 stride_width,
1530 OPJ_FLOAT32* OPJ_RESTRICT array = (OPJ_FLOAT32 * OPJ_RESTRICT)arrayIn;
1531 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32 * OPJ_RESTRICT)tmpIn;
1533 const OPJ_INT32 sn = (OPJ_INT32)((height + (even ? 1 : 0)) >> 1);
1534 const OPJ_INT32 dn = (OPJ_INT32)(height - (OPJ_UINT32)sn);
1535 for (c = 0; c < cols; c++) {
1537 for (k = 0; k < height; ++k) {
1538 tmp[k] = array[c + k * stride_width];
1541 opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1543 opj_dwt_deinterleave_v((OPJ_INT32*)tmpIn,
1544 ((OPJ_INT32*)(arrayIn)) + c,
1545 dn, sn, stride_width, even ? 0 : 1);
1551 /* Forward 5-3 wavelet transform in 2-D. */
1553 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1554 opj_tcd_tilecomp_t * tilec,
1555 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
1556 opj_encode_and_deinterleave_h_one_row_fnptr_type
1557 p_encode_and_deinterleave_h_one_row)
1564 OPJ_SIZE_T l_data_size;
1566 opj_tcd_resolution_t * l_cur_res = 0;
1567 opj_tcd_resolution_t * l_last_res = 0;
1568 const int num_threads = opj_thread_pool_get_thread_count(tp);
1569 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1571 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1572 l = (OPJ_INT32)tilec->numresolutions - 1;
1574 l_cur_res = tilec->resolutions + l;
1575 l_last_res = l_cur_res - 1;
1577 l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1578 /* overflow check */
1579 if (l_data_size > (SIZE_MAX / (NB_ELTS_V8 * sizeof(OPJ_INT32)))) {
1580 /* FIXME event manager error callback */
1583 l_data_size *= NB_ELTS_V8 * sizeof(OPJ_INT32);
1584 bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1585 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1586 /* in that case, so do not error out */
1587 if (l_data_size != 0 && ! bj) {
1594 OPJ_UINT32 rw; /* width of the resolution level computed */
1595 OPJ_UINT32 rh; /* height of the resolution level computed */
1597 rw1; /* width of the resolution level once lower than computed one */
1599 rh1; /* height of the resolution level once lower than computed one */
1600 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1601 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
1604 rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1605 rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1606 rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1607 rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1609 cas_row = l_cur_res->x0 & 1;
1610 cas_col = l_cur_res->y0 & 1;
1612 sn = (OPJ_INT32)rh1;
1613 dn = (OPJ_INT32)(rh - rh1);
1615 /* Perform vertical pass */
1616 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
1617 for (j = 0; j + NB_ELTS_V8 - 1 < rw; j += NB_ELTS_V8) {
1618 p_encode_and_deinterleave_v(tiledp + j,
1626 p_encode_and_deinterleave_v(tiledp + j,
1634 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1637 if (rw < num_jobs) {
1640 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
1642 for (j = 0; j < num_jobs; j++) {
1643 opj_dwt_encode_v_job_t* job;
1645 job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
1647 opj_thread_pool_wait_completion(tp, 0);
1648 opj_aligned_free(bj);
1651 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1653 opj_thread_pool_wait_completion(tp, 0);
1655 opj_aligned_free(bj);
1660 job->v.cas = cas_col;
1663 job->tiledp = tiledp;
1664 job->min_j = j * step_j;
1665 job->max_j = (j + 1 == num_jobs) ? rw : (j + 1) * step_j;
1666 job->p_encode_and_deinterleave_v = p_encode_and_deinterleave_v;
1667 opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
1669 opj_thread_pool_wait_completion(tp, 0);
1672 sn = (OPJ_INT32)rw1;
1673 dn = (OPJ_INT32)(rw - rw1);
1675 /* Perform horizontal pass */
1676 if (num_threads <= 1 || rh <= 1) {
1677 for (j = 0; j < rh; j++) {
1678 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
1679 (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
1680 cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
1683 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1686 if (rh < num_jobs) {
1689 step_j = (rh / num_jobs);
1691 for (j = 0; j < num_jobs; j++) {
1692 opj_dwt_encode_h_job_t* job;
1694 job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
1696 opj_thread_pool_wait_completion(tp, 0);
1697 opj_aligned_free(bj);
1700 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1702 opj_thread_pool_wait_completion(tp, 0);
1704 opj_aligned_free(bj);
1709 job->h.cas = cas_row;
1712 job->tiledp = tiledp;
1713 job->min_j = j * step_j;
1714 job->max_j = (j + 1U) * step_j; /* this can overflow */
1715 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1718 job->p_function = p_encode_and_deinterleave_h_one_row;
1719 opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
1721 opj_thread_pool_wait_completion(tp, 0);
1724 l_cur_res = l_last_res;
1729 opj_aligned_free(bj);
1733 /* Forward 5-3 wavelet transform in 2-D. */
1735 OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
1736 opj_tcd_tilecomp_t * tilec)
1738 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1739 opj_dwt_encode_and_deinterleave_v,
1740 opj_dwt_encode_and_deinterleave_h_one_row);
1744 /* Inverse 5-3 wavelet transform in 2-D. */
1746 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1749 if (p_tcd->whole_tile_decoding) {
1750 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1752 return opj_dwt_decode_partial_tile(tilec, numres);
1757 /* Get norm of 5-3 wavelet. */
1759 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1761 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1762 /* but the array should really be extended up to 33 resolution levels */
1763 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1764 if (orient == 0 && level >= 10) {
1766 } else if (orient > 0 && level >= 9) {
1769 return opj_dwt_norms[orient][level];
1773 /* Forward 9-7 wavelet transform in 2-D. */
1775 OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
1776 opj_tcd_tilecomp_t * tilec)
1778 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1779 opj_dwt_encode_and_deinterleave_v_real,
1780 opj_dwt_encode_and_deinterleave_h_one_row_real);
1784 /* Get norm of 9-7 wavelet. */
1786 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1788 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1789 /* but the array should really be extended up to 33 resolution levels */
1790 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1791 if (orient == 0 && level >= 10) {
1793 } else if (orient > 0 && level >= 9) {
1796 return opj_dwt_norms_real[orient][level];
1799 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1801 OPJ_UINT32 numbands, bandno;
1802 numbands = 3 * tccp->numresolutions - 2;
1803 for (bandno = 0; bandno < numbands; bandno++) {
1804 OPJ_FLOAT64 stepsize;
1805 OPJ_UINT32 resno, level, orient, gain;
1807 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1808 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1809 level = tccp->numresolutions - 1 - resno;
1810 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1811 (orient == 2)) ? 1 : 2));
1812 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1815 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
1816 stepsize = (1 << (gain)) / norm;
1818 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1819 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1824 /* Determine maximum computed resolution level for inverse wavelet transform */
1826 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1833 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
1836 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
1847 OPJ_INT32 * OPJ_RESTRICT tiledp;
1850 } opj_dwt_decode_h_job_t;
1852 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
1855 opj_dwt_decode_h_job_t* job;
1858 job = (opj_dwt_decode_h_job_t*)user_data;
1859 for (j = job->min_j; j < job->max_j; j++) {
1860 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
1863 opj_aligned_free(job->h.mem);
1871 OPJ_INT32 * OPJ_RESTRICT tiledp;
1874 } opj_dwt_decode_v_job_t;
1876 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
1879 opj_dwt_decode_v_job_t* job;
1882 job = (opj_dwt_decode_v_job_t*)user_data;
1883 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
1884 j += PARALLEL_COLS_53) {
1885 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1889 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
1890 (OPJ_INT32)(job->max_j - j));
1892 opj_aligned_free(job->v.mem);
1898 /* Inverse wavelet transform in 2-D. */
1900 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
1901 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
1906 opj_tcd_resolution_t* tr = tilec->resolutions;
1908 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
1909 tr->x0); /* width of the resolution level computed */
1910 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
1911 tr->y0); /* height of the resolution level computed */
1913 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
1915 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
1916 OPJ_SIZE_T h_mem_size;
1922 num_threads = opj_thread_pool_get_thread_count(tp);
1923 h_mem_size = opj_dwt_max_resolution(tr, numres);
1924 /* overflow check */
1925 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
1926 /* FIXME event manager error callback */
1929 /* We need PARALLEL_COLS_53 times the height of the array, */
1930 /* since for the vertical pass */
1931 /* we process PARALLEL_COLS_53 columns at a time */
1932 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
1933 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1935 /* FIXME event manager error callback */
1942 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1946 h.sn = (OPJ_INT32)rw;
1947 v.sn = (OPJ_INT32)rh;
1949 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
1950 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
1952 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
1955 if (num_threads <= 1 || rh <= 1) {
1956 for (j = 0; j < rh; ++j) {
1957 opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
1960 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1963 if (rh < num_jobs) {
1966 step_j = (rh / num_jobs);
1968 for (j = 0; j < num_jobs; j++) {
1969 opj_dwt_decode_h_job_t* job;
1971 job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
1973 /* It would be nice to fallback to single thread case, but */
1974 /* unfortunately some jobs may be launched and have modified */
1975 /* tiledp, so it is not practical to recover from that error */
1976 /* FIXME event manager error callback */
1977 opj_thread_pool_wait_completion(tp, 0);
1978 opj_aligned_free(h.mem);
1984 job->tiledp = tiledp;
1985 job->min_j = j * step_j;
1986 job->max_j = (j + 1U) * step_j; /* this can overflow */
1987 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1990 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
1992 /* FIXME event manager error callback */
1993 opj_thread_pool_wait_completion(tp, 0);
1995 opj_aligned_free(h.mem);
1998 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
2000 opj_thread_pool_wait_completion(tp, 0);
2003 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2006 if (num_threads <= 1 || rw <= 1) {
2007 for (j = 0; j + PARALLEL_COLS_53 <= rw;
2008 j += PARALLEL_COLS_53) {
2009 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
2012 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
2015 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2018 if (rw < num_jobs) {
2021 step_j = (rw / num_jobs);
2023 for (j = 0; j < num_jobs; j++) {
2024 opj_dwt_decode_v_job_t* job;
2026 job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
2028 /* It would be nice to fallback to single thread case, but */
2029 /* unfortunately some jobs may be launched and have modified */
2030 /* tiledp, so it is not practical to recover from that error */
2031 /* FIXME event manager error callback */
2032 opj_thread_pool_wait_completion(tp, 0);
2033 opj_aligned_free(v.mem);
2039 job->tiledp = tiledp;
2040 job->min_j = j * step_j;
2041 job->max_j = (j + 1U) * step_j; /* this can overflow */
2042 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
2045 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2047 /* FIXME event manager error callback */
2048 opj_thread_pool_wait_completion(tp, 0);
2050 opj_aligned_free(v.mem);
2053 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
2055 opj_thread_pool_wait_completion(tp, 0);
2058 opj_aligned_free(h.mem);
2062 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
2064 opj_sparse_array_int32_t* sa,
2067 OPJ_UINT32 win_l_x0,
2068 OPJ_UINT32 win_l_x1,
2069 OPJ_UINT32 win_h_x0,
2070 OPJ_UINT32 win_h_x1)
2073 ret = opj_sparse_array_int32_read(sa,
2075 win_l_x1, sa_line + 1,
2076 dest + cas + 2 * win_l_x0,
2079 ret = opj_sparse_array_int32_read(sa,
2080 sn + win_h_x0, sa_line,
2081 sn + win_h_x1, sa_line + 1,
2082 dest + 1 - cas + 2 * win_h_x0,
2089 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
2091 opj_sparse_array_int32_t* sa,
2095 OPJ_UINT32 win_l_y0,
2096 OPJ_UINT32 win_l_y1,
2097 OPJ_UINT32 win_h_y0,
2098 OPJ_UINT32 win_h_y1)
2101 ret = opj_sparse_array_int32_read(sa,
2103 sa_col + nb_cols, win_l_y1,
2104 dest + cas * 4 + 2 * 4 * win_l_y0,
2105 1, 2 * 4, OPJ_TRUE);
2107 ret = opj_sparse_array_int32_read(sa,
2108 sa_col, sn + win_h_y0,
2109 sa_col + nb_cols, sn + win_h_y1,
2110 dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
2111 1, 2 * 4, OPJ_TRUE);
2116 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
2126 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
2128 /* Naive version is :
2129 for (i = win_l_x0; i < i_max; i++) {
2130 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2132 for (i = win_h_x0; i < win_h_x1; i++) {
2133 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2135 but the compiler doesn't manage to unroll it to avoid bound
2136 checking in OPJ_S_ and OPJ_D_ macros
2143 /* Left-most case */
2144 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2151 for (; i < i_max; i++) {
2152 /* No bound checking */
2153 OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
2155 for (; i < win_l_x1; i++) {
2156 /* Right-most case */
2157 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2163 OPJ_INT32 i_max = win_h_x1;
2167 for (; i < i_max; i++) {
2168 /* No bound checking */
2169 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
2171 for (; i < win_h_x1; i++) {
2172 /* Right-most case */
2173 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2178 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
2181 for (i = win_l_x0; i < win_l_x1; i++) {
2182 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
2184 for (i = win_h_x0; i < win_h_x1; i++) {
2185 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
2191 #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
2192 #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
2193 #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)))
2194 #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)))
2195 #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)))
2196 #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)))
2198 static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
2200 OPJ_INT32 dn, OPJ_INT32 sn,
2213 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
2215 /* Naive version is :
2216 for (i = win_l_x0; i < i_max; i++) {
2217 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2219 for (i = win_h_x0; i < win_h_x1; i++) {
2220 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2222 but the compiler doesn't manage to unroll it to avoid bound
2223 checking in OPJ_S_ and OPJ_D_ macros
2230 /* Left-most case */
2231 for (off = 0; off < 4; off++) {
2232 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2242 if (i + 1 < i_max) {
2243 const __m128i two = _mm_set1_epi32(2);
2244 __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
2245 for (; i + 1 < i_max; i += 2) {
2246 /* No bound checking */
2247 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
2248 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2249 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2250 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2251 S = _mm_sub_epi32(S,
2252 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
2253 S1 = _mm_sub_epi32(S1,
2254 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
2255 _mm_store_si128((__m128i*)(a + i * 8), S);
2256 _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
2262 for (; i < i_max; i++) {
2263 /* No bound checking */
2264 for (off = 0; off < 4; off++) {
2265 OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
2268 for (; i < win_l_x1; i++) {
2269 /* Right-most case */
2270 for (off = 0; off < 4; off++) {
2271 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2278 OPJ_INT32 i_max = win_h_x1;
2284 if (i + 1 < i_max) {
2285 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
2286 for (; i + 1 < i_max; i += 2) {
2287 /* No bound checking */
2288 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2289 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2290 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2291 __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
2292 D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
2293 D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
2294 _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
2295 _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
2301 for (; i < i_max; i++) {
2302 /* No bound checking */
2303 for (off = 0; off < 4; off++) {
2304 OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
2307 for (; i < win_h_x1; i++) {
2308 /* Right-most case */
2309 for (off = 0; off < 4; off++) {
2310 OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
2316 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
2317 for (off = 0; off < 4; off++) {
2318 OPJ_S_off(0, off) /= 2;
2321 for (i = win_l_x0; i < win_l_x1; i++) {
2322 for (off = 0; off < 4; off++) {
2323 OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2;
2326 for (i = win_h_x0; i < win_h_x1; i++) {
2327 for (off = 0; off < 4; off++) {
2328 OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1;
2335 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
2347 /* Compute number of decomposition for this band. See table F-1 */
2348 OPJ_UINT32 nb = (resno == 0) ?
2349 tilec->numresolutions - 1 :
2350 tilec->numresolutions - resno;
2351 /* Map above tile-based coordinates to sub-band-based coordinates per */
2352 /* equation B-15 of the standard */
2353 OPJ_UINT32 x0b = bandno & 1;
2354 OPJ_UINT32 y0b = bandno >> 1;
2356 *tbx0 = (nb == 0) ? tcx0 :
2357 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2358 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2361 *tby0 = (nb == 0) ? tcy0 :
2362 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2363 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2366 *tbx1 = (nb == 0) ? tcx1 :
2367 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2368 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2371 *tby1 = (nb == 0) ? tcy1 :
2372 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2373 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2377 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2378 OPJ_UINT32 max_size,
2382 *start = opj_uint_subs(*start, filter_width);
2383 *end = opj_uint_adds(*end, filter_width);
2384 *end = opj_uint_min(*end, max_size);
2388 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2389 opj_tcd_tilecomp_t* tilec,
2392 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2393 OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2394 OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2395 OPJ_UINT32 resno, bandno, precno, cblkno;
2396 opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2397 w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2402 for (resno = 0; resno < numres; ++resno) {
2403 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2405 for (bandno = 0; bandno < res->numbands; ++bandno) {
2406 opj_tcd_band_t* band = &res->bands[bandno];
2408 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2409 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2410 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2411 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2412 if (cblk->decoded_data != NULL) {
2413 OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2414 OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2415 OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2416 OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2418 if (band->bandno & 1) {
2419 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2420 x += (OPJ_UINT32)(pres->x1 - pres->x0);
2422 if (band->bandno & 2) {
2423 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2424 y += (OPJ_UINT32)(pres->y1 - pres->y0);
2427 if (!opj_sparse_array_int32_write(sa, x, y,
2428 x + cblk_w, y + cblk_h,
2430 1, cblk_w, OPJ_TRUE)) {
2431 opj_sparse_array_int32_free(sa);
2444 static OPJ_BOOL opj_dwt_decode_partial_tile(
2445 opj_tcd_tilecomp_t* tilec,
2448 opj_sparse_array_int32_t* sa;
2452 /* This value matches the maximum left/right extension given in tables */
2453 /* F.2 and F.3 of the standard. */
2454 const OPJ_UINT32 filter_width = 2U;
2456 opj_tcd_resolution_t* tr = tilec->resolutions;
2457 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2459 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2460 tr->x0); /* width of the resolution level computed */
2461 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2462 tr->y0); /* height of the resolution level computed */
2464 OPJ_SIZE_T h_mem_size;
2466 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2467 /* with the tile coordinates */
2468 OPJ_UINT32 win_tcx0 = tilec->win_x0;
2469 OPJ_UINT32 win_tcy0 = tilec->win_y0;
2470 OPJ_UINT32 win_tcx1 = tilec->win_x1;
2471 OPJ_UINT32 win_tcy1 = tilec->win_y1;
2473 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2477 sa = opj_dwt_init_sparse_array(tilec, numres);
2483 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2484 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2485 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2486 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2487 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2489 1, tr_max->win_x1 - tr_max->win_x0,
2493 opj_sparse_array_int32_free(sa);
2496 h_mem_size = opj_dwt_max_resolution(tr, numres);
2497 /* overflow check */
2498 /* in vertical pass, we process 4 columns at a time */
2499 if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2500 /* FIXME event manager error callback */
2501 opj_sparse_array_int32_free(sa);
2505 h_mem_size *= 4 * sizeof(OPJ_INT32);
2506 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2508 /* FIXME event manager error callback */
2509 opj_sparse_array_int32_free(sa);
2515 for (resno = 1; resno < numres; resno ++) {
2517 /* Window of interest subband-based coordinates */
2518 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2519 OPJ_UINT32 win_hl_x0, win_hl_x1;
2520 OPJ_UINT32 win_lh_y0, win_lh_y1;
2521 /* Window of interest tile-resolution-based coordinates */
2522 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2523 /* Tile-resolution subband-based coordinates */
2524 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2528 h.sn = (OPJ_INT32)rw;
2529 v.sn = (OPJ_INT32)rh;
2531 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2532 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2534 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2537 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2540 /* Get the subband coordinates for the window of interest */
2542 opj_dwt_get_band_coordinates(tilec, resno, 0,
2543 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2544 &win_ll_x0, &win_ll_y0,
2545 &win_ll_x1, &win_ll_y1);
2548 opj_dwt_get_band_coordinates(tilec, resno, 1,
2549 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2550 &win_hl_x0, NULL, &win_hl_x1, NULL);
2553 opj_dwt_get_band_coordinates(tilec, resno, 2,
2554 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2555 NULL, &win_lh_y0, NULL, &win_lh_y1);
2557 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2558 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2559 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2560 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2561 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2563 /* Subtract the origin of the bands for this tile, to the subwindow */
2564 /* of interest band coordinates, so as to get them relative to the */
2566 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2567 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2568 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2569 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2570 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2571 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2572 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2573 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2575 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2576 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2578 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2579 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2581 /* Compute the tile-resolution-based coordinates for the window of interest */
2583 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2584 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2586 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2587 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2591 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2592 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2594 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2595 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2598 for (j = 0; j < rh; ++j) {
2599 if ((j >= win_ll_y0 && j < win_ll_y1) ||
2600 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2602 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2603 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2604 /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2605 /* This is less extreme than memsetting the whole buffer to 0 */
2606 /* although we could potentially do better with better handling of edge conditions */
2607 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2608 h.mem[win_tr_x1 - 1] = 0;
2610 if (win_tr_x1 < rw) {
2611 h.mem[win_tr_x1] = 0;
2614 opj_dwt_interleave_partial_h(h.mem,
2623 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
2624 (OPJ_INT32)win_ll_x0,
2625 (OPJ_INT32)win_ll_x1,
2626 (OPJ_INT32)win_hl_x0,
2627 (OPJ_INT32)win_hl_x1);
2628 if (!opj_sparse_array_int32_write(sa,
2633 /* FIXME event manager error callback */
2634 opj_sparse_array_int32_free(sa);
2635 opj_aligned_free(h.mem);
2641 for (i = win_tr_x0; i < win_tr_x1;) {
2642 OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
2643 opj_dwt_interleave_partial_v(v.mem,
2653 opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
2654 (OPJ_INT32)win_ll_y0,
2655 (OPJ_INT32)win_ll_y1,
2656 (OPJ_INT32)win_lh_y0,
2657 (OPJ_INT32)win_lh_y1);
2658 if (!opj_sparse_array_int32_write(sa,
2660 i + nb_cols, win_tr_y1,
2661 v.mem + 4 * win_tr_y0,
2663 /* FIXME event manager error callback */
2664 opj_sparse_array_int32_free(sa);
2665 opj_aligned_free(h.mem);
2672 opj_aligned_free(h.mem);
2675 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2676 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2677 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2678 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2679 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2681 1, tr_max->win_x1 - tr_max->win_x0,
2686 opj_sparse_array_int32_free(sa);
2690 static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
2691 OPJ_FLOAT32* OPJ_RESTRICT a,
2693 OPJ_UINT32 remaining_height)
2695 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2697 OPJ_UINT32 x0 = dwt->win_l_x0;
2698 OPJ_UINT32 x1 = dwt->win_l_x1;
2700 for (k = 0; k < 2; ++k) {
2701 if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
2702 ((OPJ_SIZE_T) bi & 0x0f) == 0) {
2703 /* Fast code path */
2704 for (i = x0; i < x1; ++i) {
2706 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2724 /* Slow code path */
2725 for (i = x0; i < x1; ++i) {
2727 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2730 if (remaining_height == 1) {
2735 if (remaining_height == 2) {
2740 if (remaining_height == 3) {
2745 if (remaining_height == 4) {
2750 if (remaining_height == 5) {
2755 if (remaining_height == 6) {
2760 if (remaining_height == 7) {
2767 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2774 static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
2775 opj_sparse_array_int32_t* sa,
2777 OPJ_UINT32 remaining_height)
2780 for (i = 0; i < remaining_height; i++) {
2782 ret = opj_sparse_array_int32_read(sa,
2783 dwt->win_l_x0, sa_line + i,
2784 dwt->win_l_x1, sa_line + i + 1,
2785 /* Nasty cast from float* to int32* */
2786 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2787 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2789 ret = opj_sparse_array_int32_read(sa,
2790 (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2791 (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2792 /* Nasty cast from float* to int32* */
2793 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2794 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2800 static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2801 OPJ_FLOAT32* OPJ_RESTRICT a,
2803 OPJ_UINT32 nb_elts_read)
2805 opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2808 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2809 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2810 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2813 a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
2814 bi = dwt->wavelet + 1 - dwt->cas;
2816 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2817 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2818 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2822 static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2823 opj_sparse_array_int32_t* sa,
2825 OPJ_UINT32 nb_elts_read)
2828 ret = opj_sparse_array_int32_read(sa,
2829 sa_col, dwt->win_l_x0,
2830 sa_col + nb_elts_read, dwt->win_l_x1,
2831 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
2832 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2834 ret = opj_sparse_array_int32_read(sa,
2835 sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
2836 sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
2837 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
2838 1, 2 * NB_ELTS_V8, OPJ_TRUE);
2845 static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
2850 __m128* OPJ_RESTRICT vw = (__m128*) w;
2851 OPJ_UINT32 i = start;
2852 /* To be adapted if NB_ELTS_V8 changes */
2854 /* Note: attempt at loop unrolling x2 doesn't help */
2855 for (; i < end; ++i, vw += 4) {
2856 vw[0] = _mm_mul_ps(vw[0], c);
2857 vw[1] = _mm_mul_ps(vw[1], c);
2861 static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
2867 __m128* OPJ_RESTRICT vl = (__m128*) l;
2868 __m128* OPJ_RESTRICT vw = (__m128*) w;
2869 /* To be adapted if NB_ELTS_V8 changes */
2871 OPJ_UINT32 imax = opj_uint_min(end, m);
2874 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
2875 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
2884 /* Note: attempt at loop unrolling x2 doesn't help */
2885 for (; i < imax; ++i) {
2886 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
2887 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
2891 assert(m + 1 == end);
2892 c = _mm_add_ps(c, c);
2893 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
2894 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
2900 static void opj_v8dwt_decode_step1(opj_v8_t* w,
2903 const OPJ_FLOAT32 c)
2905 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
2907 /* To be adapted if NB_ELTS_V8 changes */
2908 for (i = start; i < end; ++i) {
2909 fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c;
2910 fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
2911 fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
2912 fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
2913 fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
2914 fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
2915 fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
2916 fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
2920 static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
2926 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
2927 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
2929 OPJ_UINT32 imax = opj_uint_min(end, m);
2931 fw += 2 * NB_ELTS_V8 * start;
2932 fl = fw - 2 * NB_ELTS_V8;
2934 /* To be adapted if NB_ELTS_V8 changes */
2935 for (i = start; i < imax; ++i) {
2936 fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
2937 fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
2938 fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
2939 fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
2940 fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
2941 fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
2942 fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
2943 fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
2945 fw += 2 * NB_ELTS_V8;
2948 assert(m + 1 == end);
2950 fw[-8] = fw[-8] + fl[0] * c;
2951 fw[-7] = fw[-7] + fl[1] * c;
2952 fw[-6] = fw[-6] + fl[2] * c;
2953 fw[-5] = fw[-5] + fl[3] * c;
2954 fw[-4] = fw[-4] + fl[4] * c;
2955 fw[-3] = fw[-3] + fl[5] * c;
2956 fw[-2] = fw[-2] + fl[6] * c;
2957 fw[-1] = fw[-1] + fl[7] * c;
2964 /* Inverse 9-7 wavelet transform in 1-D. */
2966 static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
2969 /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
2970 /* Historic value for 2 / opj_invK */
2971 /* Normally, we should use invK, but if we do so, we have failures in the */
2972 /* conformance test, due to MSE and peak errors significantly higher than */
2973 /* accepted value */
2974 /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
2975 /* the computation of the stepsize for the non LL subbands */
2976 const float two_invK = 1.625732422f;
2977 if (dwt->cas == 0) {
2978 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
2984 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
2991 opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
2992 _mm_set1_ps(opj_K));
2993 opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
2994 _mm_set1_ps(two_invK));
2995 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
2996 dwt->win_l_x0, dwt->win_l_x1,
2997 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
2998 _mm_set1_ps(-opj_dwt_delta));
2999 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3000 dwt->win_h_x0, dwt->win_h_x1,
3001 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3002 _mm_set1_ps(-opj_dwt_gamma));
3003 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
3004 dwt->win_l_x0, dwt->win_l_x1,
3005 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3006 _mm_set1_ps(-opj_dwt_beta));
3007 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3008 dwt->win_h_x0, dwt->win_h_x1,
3009 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3010 _mm_set1_ps(-opj_dwt_alpha));
3012 opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
3014 opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
3016 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3017 dwt->win_l_x0, dwt->win_l_x1,
3018 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3020 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3021 dwt->win_h_x0, dwt->win_h_x1,
3022 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3024 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3025 dwt->win_l_x0, dwt->win_l_x1,
3026 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3028 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3029 dwt->win_h_x0, dwt->win_h_x1,
3030 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3039 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3041 } opj_dwt97_decode_h_job_t;
3043 static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
3046 opj_dwt97_decode_h_job_t* job;
3047 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3051 job = (opj_dwt97_decode_h_job_t*)user_data;
3054 assert((job->nb_rows % NB_ELTS_V8) == 0);
3057 for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
3059 opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
3060 opj_v8dwt_decode(&job->h);
3062 /* To be adapted if NB_ELTS_V8 changes */
3063 for (k = 0; k < job->rw; k++) {
3064 aj[k ] = job->h.wavelet[k].f[0];
3065 aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1];
3066 aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
3067 aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
3069 for (k = 0; k < job->rw; k++) {
3070 aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
3071 aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
3072 aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
3073 aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
3076 aj += w * NB_ELTS_V8;
3079 opj_aligned_free(job->h.wavelet);
3088 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3089 OPJ_UINT32 nb_columns;
3090 } opj_dwt97_decode_v_job_t;
3092 static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
3095 opj_dwt97_decode_v_job_t* job;
3096 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3099 job = (opj_dwt97_decode_v_job_t*)user_data;
3101 assert((job->nb_columns % NB_ELTS_V8) == 0);
3104 for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
3107 opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
3108 opj_v8dwt_decode(&job->v);
3110 for (k = 0; k < job->rh; ++k) {
3111 memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
3112 NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3117 opj_aligned_free(job->v.wavelet);
3123 /* Inverse 9-7 wavelet transform in 2-D. */
3126 OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
3127 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3133 opj_tcd_resolution_t* res = tilec->resolutions;
3135 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
3136 res->x0); /* width of the resolution level computed */
3137 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
3138 res->y0); /* height of the resolution level computed */
3140 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
3142 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
3144 OPJ_SIZE_T l_data_size;
3145 const int num_threads = opj_thread_pool_get_thread_count(tp);
3151 l_data_size = opj_dwt_max_resolution(res, numres);
3152 /* overflow check */
3153 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3154 /* FIXME event manager error callback */
3157 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3159 /* FIXME event manager error callback */
3162 v.wavelet = h.wavelet;
3165 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
3168 h.sn = (OPJ_INT32)rw;
3169 v.sn = (OPJ_INT32)rh;
3173 rw = (OPJ_UINT32)(res->x1 -
3174 res->x0); /* width of the resolution level computed */
3175 rh = (OPJ_UINT32)(res->y1 -
3176 res->y0); /* height of the resolution level computed */
3178 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3179 h.cas = res->x0 % 2;
3182 h.win_l_x1 = (OPJ_UINT32)h.sn;
3184 h.win_h_x1 = (OPJ_UINT32)h.dn;
3186 if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
3187 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3189 opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
3190 opj_v8dwt_decode(&h);
3192 /* To be adapted if NB_ELTS_V8 changes */
3193 for (k = 0; k < rw; k++) {
3194 aj[k ] = h.wavelet[k].f[0];
3195 aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1];
3196 aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
3197 aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
3199 for (k = 0; k < rw; k++) {
3200 aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
3201 aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
3202 aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
3203 aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
3206 aj += w * NB_ELTS_V8;
3209 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
3212 if ((rh / NB_ELTS_V8) < num_jobs) {
3213 num_jobs = rh / NB_ELTS_V8;
3215 step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3216 for (j = 0; j < num_jobs; j++) {
3217 opj_dwt97_decode_h_job_t* job;
3219 job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
3221 opj_thread_pool_wait_completion(tp, 0);
3222 opj_aligned_free(h.wavelet);
3225 job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3226 if (!job->h.wavelet) {
3227 opj_thread_pool_wait_completion(tp, 0);
3229 opj_aligned_free(h.wavelet);
3235 job->h.win_l_x0 = h.win_l_x0;
3236 job->h.win_l_x1 = h.win_l_x1;
3237 job->h.win_h_x0 = h.win_h_x0;
3238 job->h.win_h_x1 = h.win_h_x1;
3242 job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
3243 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3244 aj += w * job->nb_rows;
3245 opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
3247 opj_thread_pool_wait_completion(tp, 0);
3248 j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
3253 opj_v8dwt_interleave_h(&h, aj, w, rh - j);
3254 opj_v8dwt_decode(&h);
3255 for (k = 0; k < rw; k++) {
3257 for (l = 0; l < rh - j; l++) {
3258 aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l];
3263 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3264 v.cas = res->y0 % 2;
3266 v.win_l_x1 = (OPJ_UINT32)v.sn;
3268 v.win_h_x1 = (OPJ_UINT32)v.dn;
3270 aj = (OPJ_FLOAT32*) tilec->data;
3271 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
3272 for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
3275 opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
3276 opj_v8dwt_decode(&v);
3278 for (k = 0; k < rh; ++k) {
3279 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3284 /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
3285 transfer being the limiting factor. So limit the number of
3288 OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
3291 if ((rw / NB_ELTS_V8) < num_jobs) {
3292 num_jobs = rw / NB_ELTS_V8;
3294 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3295 for (j = 0; j < num_jobs; j++) {
3296 opj_dwt97_decode_v_job_t* job;
3298 job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
3300 opj_thread_pool_wait_completion(tp, 0);
3301 opj_aligned_free(h.wavelet);
3304 job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3305 if (!job->v.wavelet) {
3306 opj_thread_pool_wait_completion(tp, 0);
3308 opj_aligned_free(h.wavelet);
3314 job->v.win_l_x0 = v.win_l_x0;
3315 job->v.win_l_x1 = v.win_l_x1;
3316 job->v.win_h_x0 = v.win_h_x0;
3317 job->v.win_h_x1 = v.win_h_x1;
3321 job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
3322 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3323 aj += job->nb_columns;
3324 opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
3326 opj_thread_pool_wait_completion(tp, 0);
3329 if (rw & (NB_ELTS_V8 - 1)) {
3332 j = rw & (NB_ELTS_V8 - 1);
3334 opj_v8dwt_interleave_v(&v, aj, w, j);
3335 opj_v8dwt_decode(&v);
3337 for (k = 0; k < rh; ++k) {
3338 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
3339 (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
3344 opj_aligned_free(h.wavelet);
3349 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3352 opj_sparse_array_int32_t* sa;
3356 /* This value matches the maximum left/right extension given in tables */
3357 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3358 /* we currently use 3. */
3359 const OPJ_UINT32 filter_width = 4U;
3361 opj_tcd_resolution_t* tr = tilec->resolutions;
3362 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3364 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3365 tr->x0); /* width of the resolution level computed */
3366 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3367 tr->y0); /* height of the resolution level computed */
3369 OPJ_SIZE_T l_data_size;
3371 /* Compute the intersection of the area of interest, expressed in tile coordinates */
3372 /* with the tile coordinates */
3373 OPJ_UINT32 win_tcx0 = tilec->win_x0;
3374 OPJ_UINT32 win_tcy0 = tilec->win_y0;
3375 OPJ_UINT32 win_tcx1 = tilec->win_x1;
3376 OPJ_UINT32 win_tcy1 = tilec->win_y1;
3378 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3382 sa = opj_dwt_init_sparse_array(tilec, numres);
3388 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3389 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3390 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3391 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3392 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3394 1, tr_max->win_x1 - tr_max->win_x0,
3398 opj_sparse_array_int32_free(sa);
3402 l_data_size = opj_dwt_max_resolution(tr, numres);
3403 /* overflow check */
3404 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3405 /* FIXME event manager error callback */
3406 opj_sparse_array_int32_free(sa);
3409 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3411 /* FIXME event manager error callback */
3412 opj_sparse_array_int32_free(sa);
3415 v.wavelet = h.wavelet;
3417 for (resno = 1; resno < numres; resno ++) {
3419 /* Window of interest subband-based coordinates */
3420 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3421 OPJ_UINT32 win_hl_x0, win_hl_x1;
3422 OPJ_UINT32 win_lh_y0, win_lh_y1;
3423 /* Window of interest tile-resolution-based coordinates */
3424 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3425 /* Tile-resolution subband-based coordinates */
3426 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3430 h.sn = (OPJ_INT32)rw;
3431 v.sn = (OPJ_INT32)rh;
3433 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3434 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3436 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3439 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3442 /* Get the subband coordinates for the window of interest */
3444 opj_dwt_get_band_coordinates(tilec, resno, 0,
3445 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3446 &win_ll_x0, &win_ll_y0,
3447 &win_ll_x1, &win_ll_y1);
3450 opj_dwt_get_band_coordinates(tilec, resno, 1,
3451 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3452 &win_hl_x0, NULL, &win_hl_x1, NULL);
3455 opj_dwt_get_band_coordinates(tilec, resno, 2,
3456 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3457 NULL, &win_lh_y0, NULL, &win_lh_y1);
3459 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3460 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3461 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3462 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3463 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3465 /* Subtract the origin of the bands for this tile, to the subwindow */
3466 /* of interest band coordinates, so as to get them relative to the */
3468 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3469 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3470 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3471 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3472 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3473 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3474 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3475 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3477 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3478 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3480 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3481 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3483 /* Compute the tile-resolution-based coordinates for the window of interest */
3485 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3486 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3488 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3489 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3493 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3494 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3496 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3497 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3500 h.win_l_x0 = win_ll_x0;
3501 h.win_l_x1 = win_ll_x1;
3502 h.win_h_x0 = win_hl_x0;
3503 h.win_h_x1 = win_hl_x1;
3504 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3505 if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3506 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3507 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3508 opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3509 opj_v8dwt_decode(&h);
3510 if (!opj_sparse_array_int32_write(sa,
3512 win_tr_x1, j + NB_ELTS_V8,
3513 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3514 NB_ELTS_V8, 1, OPJ_TRUE)) {
3515 /* FIXME event manager error callback */
3516 opj_sparse_array_int32_free(sa);
3517 opj_aligned_free(h.wavelet);
3524 ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3525 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3526 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3527 opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3528 opj_v8dwt_decode(&h);
3529 if (!opj_sparse_array_int32_write(sa,
3532 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3533 NB_ELTS_V8, 1, OPJ_TRUE)) {
3534 /* FIXME event manager error callback */
3535 opj_sparse_array_int32_free(sa);
3536 opj_aligned_free(h.wavelet);
3541 v.win_l_x0 = win_ll_y0;
3542 v.win_l_x1 = win_ll_y1;
3543 v.win_h_x0 = win_lh_y0;
3544 v.win_h_x1 = win_lh_y1;
3545 for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3546 OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3548 opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3549 opj_v8dwt_decode(&v);
3551 if (!opj_sparse_array_int32_write(sa,
3553 j + nb_elts, win_tr_y1,
3554 (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3555 1, NB_ELTS_V8, OPJ_TRUE)) {
3556 /* FIXME event manager error callback */
3557 opj_sparse_array_int32_free(sa);
3558 opj_aligned_free(h.wavelet);
3565 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3566 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3567 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3568 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3569 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3571 1, tr_max->win_x1 - tr_max->win_x0,
3576 opj_sparse_array_int32_free(sa);
3578 opj_aligned_free(h.wavelet);
3583 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3584 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3587 if (p_tcd->whole_tile_decoding) {
3588 return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3590 return opj_dwt_decode_partial_97(tilec, numres);