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);
130 Forward 9-7 wavelet transform in 1-D
132 static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
135 Explicit calculation of the Quantization Stepsizes
137 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
138 opj_stepsize_t *bandno_stepsize);
140 Inverse wavelet transform in 2-D.
142 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
143 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
145 static OPJ_BOOL opj_dwt_decode_partial_tile(
146 opj_tcd_tilecomp_t* tilec,
149 /* Forward transform, for the vertical pass, processing cols columns */
150 /* where cols <= NB_ELTS_V8 */
151 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
152 typedef void (*opj_encode_and_deinterleave_v_fnptr_type)(
157 OPJ_UINT32 stride_width,
160 /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
161 typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
167 static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
168 opj_tcd_tilecomp_t * tilec,
169 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
170 opj_encode_and_deinterleave_h_one_row_fnptr_type
171 p_encode_and_deinterleave_h_one_row);
173 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
177 /* Inverse 9-7 wavelet transform in 1-D. */
184 #define OPJ_S(i) a[(i)*2]
185 #define OPJ_D(i) a[(1+(i)*2)]
186 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
187 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
189 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
190 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
193 /* This table contains the norms of the 5-3 wavelets for different bands. */
195 /* FIXME! the array should really be extended up to 33 resolution levels */
196 /* See https://github.com/uclouvain/openjpeg/issues/493 */
197 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
198 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
199 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
200 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
201 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
205 /* This table contains the norms of the 9-7 wavelets for different bands. */
207 /* FIXME! the array should really be extended up to 33 resolution levels */
208 /* See https://github.com/uclouvain/openjpeg/issues/493 */
209 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
210 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
211 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
212 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
213 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
217 ==========================================================
219 ==========================================================
223 /* Forward lazy transform (horizontal). */
225 static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
226 OPJ_INT32 * OPJ_RESTRICT b,
228 OPJ_INT32 sn, OPJ_INT32 cas)
231 OPJ_INT32 * OPJ_RESTRICT l_dest = b;
232 const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
234 for (i = 0; i < sn; ++i) {
242 for (i = 0; i < dn; ++i) {
248 #ifdef STANDARD_SLOW_VERSION
250 /* Inverse lazy transform (horizontal). */
252 static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
254 const OPJ_INT32 *ai = a;
255 OPJ_INT32 *bi = h->mem + h->cas;
262 bi = h->mem + 1 - h->cas;
271 /* Inverse lazy transform (vertical). */
273 static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
275 const OPJ_INT32 *ai = a;
276 OPJ_INT32 *bi = v->mem + v->cas;
283 ai = a + (v->sn * (OPJ_SIZE_T)x);
284 bi = v->mem + 1 - v->cas;
293 #endif /* STANDARD_SLOW_VERSION */
295 #ifdef STANDARD_SLOW_VERSION
297 /* Inverse 5-3 wavelet transform in 1-D. */
299 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
305 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
306 for (i = 0; i < sn; i++) {
307 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
309 for (i = 0; i < dn; i++) {
310 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
314 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
317 for (i = 0; i < sn; i++) {
318 OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
320 for (i = 0; i < dn; i++) {
321 OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
327 static void opj_dwt_decode_1(const opj_dwt_t *v)
329 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
332 #endif /* STANDARD_SLOW_VERSION */
334 #if !defined(STANDARD_SLOW_VERSION)
335 static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
341 const OPJ_INT32* in_even = &tiledp[0];
342 const OPJ_INT32* in_odd = &tiledp[sn];
344 #ifdef TWO_PASS_VERSION
345 /* For documentation purpose: performs lifting in two iterations, */
346 /* but without explicit interleaving */
351 tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
352 for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
353 tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
355 if (len & 1) { /* if len is odd */
356 tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
360 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
361 tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
363 if (!(len & 1)) { /* if len is even */
364 tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
367 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
371 /* Improved version of the TWO_PASS_VERSION: */
372 /* Performs lifting in one single iteration. Saves memory */
373 /* accesses and explicit interleaving. */
376 s0n = s1n - ((d1n + 1) >> 1);
378 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
385 s0n = s1n - ((d1c + d1n + 2) >> 2);
388 tmp[i + 1] = opj_int_add_no_overflow(d1c, opj_int_add_no_overflow(s0c,
395 tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
396 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
398 tmp[len - 1] = d1n + s0n;
401 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
404 static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
410 const OPJ_INT32* in_even = &tiledp[sn];
411 const OPJ_INT32* in_odd = &tiledp[0];
413 #ifdef TWO_PASS_VERSION
414 /* For documentation purpose: performs lifting in two iterations, */
415 /* but without explicit interleaving */
420 for (i = 1, j = 0; i < len - 1; i += 2, j++) {
421 tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
424 tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
428 tmp[0] = in_even[0] + tmp[1];
429 for (i = 2, j = 1; i < len - 1; i += 2, j++) {
430 tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
433 tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
436 OPJ_INT32 s1, s2, dc, dn;
440 /* Improved version of the TWO_PASS_VERSION: */
441 /* Performs lifting in one single iteration. Saves memory */
442 /* accesses and explicit interleaving. */
445 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
446 tmp[0] = in_even[0] + dc;
448 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
452 dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
454 tmp[i + 1] = opj_int_add_no_overflow(s1, opj_int_add_no_overflow(dn, dc) >> 1);
463 dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
464 tmp[len - 2] = s1 + ((dn + dc) >> 1);
467 tmp[len - 1] = s1 + dc;
470 memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
474 #endif /* !defined(STANDARD_SLOW_VERSION) */
477 /* Inverse 5-3 wavelet transform in 1-D for one row. */
479 /* Performs interleave, inverse wavelet transform and copy back to buffer */
480 static void opj_idwt53_h(const opj_dwt_t *dwt,
483 #ifdef STANDARD_SLOW_VERSION
484 /* For documentation purpose */
485 opj_dwt_interleave_h(dwt, tiledp);
486 opj_dwt_decode_1(dwt);
487 memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
489 const OPJ_INT32 sn = dwt->sn;
490 const OPJ_INT32 len = sn + dwt->dn;
491 if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
493 opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
495 /* Unmodified value */
497 } else { /* Left-most sample is on odd coordinate */
500 } else if (len == 2) {
501 OPJ_INT32* out = dwt->mem;
502 const OPJ_INT32* in_even = &tiledp[sn];
503 const OPJ_INT32* in_odd = &tiledp[0];
504 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
505 out[0] = in_even[0] + out[1];
506 memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
507 } else if (len > 2) {
508 opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
514 #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
516 /* Conveniency macros to improve the readabilty of the formulas */
519 #define LOAD_CST(x) _mm256_set1_epi32(x)
520 #define LOAD(x) _mm256_load_si256((const VREG*)(x))
521 #define LOADU(x) _mm256_loadu_si256((const VREG*)(x))
522 #define STORE(x,y) _mm256_store_si256((VREG*)(x),(y))
523 #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
524 #define ADD(x,y) _mm256_add_epi32((x),(y))
525 #define SUB(x,y) _mm256_sub_epi32((x),(y))
526 #define SAR(x,y) _mm256_srai_epi32((x),(y))
529 #define LOAD_CST(x) _mm_set1_epi32(x)
530 #define LOAD(x) _mm_load_si128((const VREG*)(x))
531 #define LOADU(x) _mm_loadu_si128((const VREG*)(x))
532 #define STORE(x,y) _mm_store_si128((VREG*)(x),(y))
533 #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
534 #define ADD(x,y) _mm_add_epi32((x),(y))
535 #define SUB(x,y) _mm_sub_epi32((x),(y))
536 #define SAR(x,y) _mm_srai_epi32((x),(y))
538 #define ADD3(x,y,z) ADD(ADD(x,y),z)
541 void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
542 const OPJ_INT32* tmp,
547 for (i = 0; i < len; ++i) {
548 /* A memcpy(&tiledp_col[i * stride + 0],
549 &tmp[PARALLEL_COLS_53 * i + 0],
550 PARALLEL_COLS_53 * sizeof(OPJ_INT32))
551 would do but would be a tiny bit slower.
552 We can take here advantage of our knowledge of alignment */
553 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
554 LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
555 STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
556 LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
560 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
561 * 16 in AVX2, when top-most pixel is on even coordinate */
562 static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
566 OPJ_INT32* tiledp_col,
567 const OPJ_SIZE_T stride)
569 const OPJ_INT32* in_even = &tiledp_col[0];
570 const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
574 VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
575 VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
576 const VREG two = LOAD_CST(2);
580 assert(PARALLEL_COLS_53 == 16);
581 assert(VREG_INT_COUNT == 8);
583 assert(PARALLEL_COLS_53 == 8);
584 assert(VREG_INT_COUNT == 4);
587 /* Note: loads of input even/odd values must be done in a unaligned */
588 /* fashion. But stores in tmp can be done with aligned store, since */
589 /* the temporary buffer is properly aligned */
590 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
592 s1n_0 = LOADU(in_even + 0);
593 s1n_1 = LOADU(in_even + VREG_INT_COUNT);
594 d1n_0 = LOADU(in_odd);
595 d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
597 /* s0n = s1n - ((d1n + 1) >> 1); <==> */
598 /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
599 s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
600 s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
602 for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
608 s1n_0 = LOADU(in_even + j * stride);
609 s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
610 d1n_0 = LOADU(in_odd + j * stride);
611 d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
613 /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
614 s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
615 s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
617 STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
618 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
620 /* d1c + ((s0c + s0n) >> 1) */
621 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
622 ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
623 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
624 ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
627 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
628 STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
631 VREG tmp_len_minus_1;
632 s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
633 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
634 tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
635 STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
636 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
637 STORE(tmp + PARALLEL_COLS_53 * (len - 2),
638 ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
640 s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
641 /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
642 tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
643 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
645 /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
646 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
647 ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
651 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
653 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
657 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
661 /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
662 * 16 in AVX2, when top-most pixel is on odd coordinate */
663 static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
667 OPJ_INT32* tiledp_col,
668 const OPJ_SIZE_T stride)
673 VREG s1_0, s2_0, dc_0, dn_0;
674 VREG s1_1, s2_1, dc_1, dn_1;
675 const VREG two = LOAD_CST(2);
677 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
678 const OPJ_INT32* in_odd = &tiledp_col[0];
682 assert(PARALLEL_COLS_53 == 16);
683 assert(VREG_INT_COUNT == 8);
685 assert(PARALLEL_COLS_53 == 8);
686 assert(VREG_INT_COUNT == 4);
689 /* Note: loads of input even/odd values must be done in a unaligned */
690 /* fashion. But stores in tmp can be done with aligned store, since */
691 /* the temporary buffer is properly aligned */
692 assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
694 s1_0 = LOADU(in_even + stride);
695 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
696 dc_0 = SUB(LOADU(in_odd + 0),
697 SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
698 STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
700 s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
701 /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
702 dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
703 SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
704 STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
705 ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
707 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
709 s2_0 = LOADU(in_even + (j + 1) * stride);
710 s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
712 /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
713 dn_0 = SUB(LOADU(in_odd + j * stride),
714 SAR(ADD3(s1_0, s2_0, two), 2));
715 dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
716 SAR(ADD3(s1_1, s2_1, two), 2));
718 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
719 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
721 /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
722 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
723 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
724 STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
725 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
732 STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
733 STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
736 /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
737 dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
738 SAR(ADD3(s1_0, s1_0, two), 2));
739 dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
740 SAR(ADD3(s1_1, s1_1, two), 2));
742 /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
743 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
744 ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
745 STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
746 ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
748 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
749 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
751 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
752 STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
756 opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
770 #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
772 #if !defined(STANDARD_SLOW_VERSION)
773 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
774 * pixel is on even coordinate */
775 static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
778 OPJ_INT32* tiledp_col,
779 const OPJ_SIZE_T stride)
782 OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
786 /* Performs lifting in one single iteration. Saves memory */
787 /* accesses and explicit interleaving. */
790 d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
791 s0n = s1n - ((d1n + 1) >> 1);
793 for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
797 s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
798 d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
800 s0n = opj_int_sub_no_overflow(s1n,
801 opj_int_add_no_overflow(opj_int_add_no_overflow(d1c, d1n), 2) >> 2);
804 tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
811 tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
813 tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
815 tmp[len - 1] = d1n + s0n;
818 for (i = 0; i < len; ++i) {
819 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
824 /** Vertical inverse 5x3 wavelet transform for one column, when top-most
825 * pixel is on odd coordinate */
826 static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
829 OPJ_INT32* tiledp_col,
830 const OPJ_SIZE_T stride)
833 OPJ_INT32 s1, s2, dc, dn;
834 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
835 const OPJ_INT32* in_odd = &tiledp_col[0];
839 /* Performs lifting in one single iteration. Saves memory */
840 /* accesses and explicit interleaving. */
842 s1 = in_even[stride];
843 dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
844 tmp[0] = in_even[0] + dc;
845 for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
847 s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
849 dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
851 tmp[i + 1] = s1 + ((dn + dc) >> 1);
858 dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
859 tmp[len - 2] = s1 + ((dn + dc) >> 1);
862 tmp[len - 1] = s1 + dc;
865 for (i = 0; i < len; ++i) {
866 tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
869 #endif /* !defined(STANDARD_SLOW_VERSION) */
872 /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
874 /* Performs interleave, inverse wavelet transform and copy back to buffer */
875 static void opj_idwt53_v(const opj_dwt_t *dwt,
876 OPJ_INT32* tiledp_col,
880 #ifdef STANDARD_SLOW_VERSION
881 /* For documentation purpose */
883 for (c = 0; c < nb_cols; c ++) {
884 opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
885 opj_dwt_decode_1(dwt);
886 for (k = 0; k < dwt->sn + dwt->dn; ++k) {
887 tiledp_col[c + k * stride] = dwt->mem[k];
891 const OPJ_INT32 sn = dwt->sn;
892 const OPJ_INT32 len = sn + dwt->dn;
894 /* If len == 1, unmodified value */
896 #if (defined(__SSE2__) || defined(__AVX2__))
897 if (len > 1 && nb_cols == PARALLEL_COLS_53) {
898 /* Same as below general case, except that thanks to SSE2/AVX2 */
899 /* we can efficiently process 8/16 columns in parallel */
900 opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
906 for (c = 0; c < nb_cols; c++, tiledp_col++) {
907 opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
914 for (c = 0; c < nb_cols; c++, tiledp_col++) {
922 OPJ_INT32* out = dwt->mem;
923 for (c = 0; c < nb_cols; c++, tiledp_col++) {
925 const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
926 const OPJ_INT32* in_odd = &tiledp_col[0];
928 out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
929 out[0] = in_even[0] + out[1];
931 for (i = 0; i < len; ++i) {
932 tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
939 #if (defined(__SSE2__) || defined(__AVX2__))
940 if (len > 2 && nb_cols == PARALLEL_COLS_53) {
941 /* Same as below general case, except that thanks to SSE2/AVX2 */
942 /* we can efficiently process 8/16 columns in parallel */
943 opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
949 for (c = 0; c < nb_cols; c++, tiledp_col++) {
950 opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
959 static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
964 for (; i < end; ++i) {
970 static void opj_dwt_encode_step1_combined(OPJ_FLOAT32* fw,
973 const OPJ_FLOAT32 c1,
974 const OPJ_FLOAT32 c2)
977 const OPJ_UINT32 iters_common = opj_uint_min(iters_c1, iters_c2);
978 assert((((OPJ_SIZE_T)fw) & 0xf) == 0);
979 assert(opj_int_abs((OPJ_INT32)iters_c1 - (OPJ_INT32)iters_c2) <= 1);
980 for (; i + 3 < iters_common; i += 4) {
982 const __m128 vcst = _mm_set_ps(c2, c1, c2, c1);
983 *(__m128*)fw = _mm_mul_ps(*(__m128*)fw, vcst);
984 *(__m128*)(fw + 4) = _mm_mul_ps(*(__m128*)(fw + 4), vcst);
997 for (; i < iters_common; i++) {
1004 } else if (i < iters_c2) {
1011 static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1017 OPJ_UINT32 imax = opj_uint_min(end, m);
1019 fw[-1] += (fl[0] + fw[0]) * c;
1022 for (; i + 3 < imax; i += 4) {
1023 fw[-1] += (fw[-2] + fw[0]) * c;
1024 fw[1] += (fw[0] + fw[2]) * c;
1025 fw[3] += (fw[2] + fw[4]) * c;
1026 fw[5] += (fw[4] + fw[6]) * c;
1029 for (; i < imax; ++i) {
1030 fw[-1] += (fw[-2] + fw[0]) * c;
1035 assert(m + 1 == end);
1036 fw[-1] += (2 * fw[-2]) * c;
1040 static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1043 OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1045 assert(dn + sn > 1);
1053 opj_dwt_encode_step2(w + a, w + b + 1,
1055 (OPJ_UINT32)opj_int_min(dn, sn - b),
1057 opj_dwt_encode_step2(w + b, w + a + 1,
1059 (OPJ_UINT32)opj_int_min(sn, dn - a),
1061 opj_dwt_encode_step2(w + a, w + b + 1,
1063 (OPJ_UINT32)opj_int_min(dn, sn - b),
1065 opj_dwt_encode_step2(w + b, w + a + 1,
1067 (OPJ_UINT32)opj_int_min(sn, dn - a),
1070 opj_dwt_encode_step1(w + b, (OPJ_UINT32)dn,
1072 opj_dwt_encode_step1(w + a, (OPJ_UINT32)sn,
1076 opj_dwt_encode_step1_combined(w,
1082 opj_dwt_encode_step1_combined(w,
1091 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1092 opj_stepsize_t *bandno_stepsize)
1095 p = opj_int_floorlog2(stepsize) - 13;
1096 n = 11 - opj_int_floorlog2(stepsize);
1097 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1098 bandno_stepsize->expn = numbps - p;
1102 ==========================================================
1104 ==========================================================
1107 /** Process one line for the horizontal pass of the 5x3 forward transform */
1109 void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1114 OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1115 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1116 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1117 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1122 for (i = 0; i < sn - 1; i++) {
1123 tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1);
1125 if ((width % 2) == 0) {
1126 tmp[sn + i] = row[2 * i + 1] - row[(i) * 2];
1128 row[0] += (tmp[sn] + tmp[sn] + 2) >> 2;
1129 for (i = 1; i < dn; i++) {
1130 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2);
1132 if ((width % 2) == 1) {
1133 row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2);
1135 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1142 tmp[sn + 0] = row[0] - row[1];
1143 for (i = 1; i < sn; i++) {
1144 tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1);
1146 if ((width % 2) == 1) {
1147 tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1];
1150 for (i = 0; i < dn - 1; i++) {
1151 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2);
1153 if ((width % 2) == 0) {
1154 row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2);
1156 memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1161 /** Process one line for the horizontal pass of the 9x7 forward transform */
1163 void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1168 OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1169 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1170 const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1171 const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1175 memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1176 opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1177 opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1178 (OPJ_INT32 * OPJ_RESTRICT)row,
1179 dn, sn, even ? 0 : 1);
1184 OPJ_UINT32 rw; /* Width of the resolution to process */
1185 OPJ_UINT32 w; /* Width of tiledp */
1186 OPJ_INT32 * OPJ_RESTRICT tiledp;
1189 opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1190 } opj_dwt_encode_h_job_t;
1192 static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1195 opj_dwt_encode_h_job_t* job;
1198 job = (opj_dwt_encode_h_job_t*)user_data;
1199 for (j = job->min_j; j < job->max_j; j++) {
1200 OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1201 (*job->p_function)(aj, job->h.mem, job->rw,
1202 job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1205 opj_aligned_free(job->h.mem);
1213 OPJ_INT32 * OPJ_RESTRICT tiledp;
1216 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v;
1217 } opj_dwt_encode_v_job_t;
1219 static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1222 opj_dwt_encode_v_job_t* job;
1225 job = (opj_dwt_encode_v_job_t*)user_data;
1226 for (j = job->min_j; j + NB_ELTS_V8 - 1 < job->max_j; j += NB_ELTS_V8) {
1227 (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1234 if (j < job->max_j) {
1235 (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1243 opj_aligned_free(job->v.mem);
1247 /** Fetch up to cols <= NB_ELTS_V8 for each line, and put them in tmpOut */
1248 /* that has a NB_ELTS_V8 interleave factor. */
1249 static void opj_dwt_fetch_cols_vertical_pass(const void *arrayIn,
1252 OPJ_UINT32 stride_width,
1255 const OPJ_INT32* OPJ_RESTRICT array = (const OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1256 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpOut;
1257 if (cols == NB_ELTS_V8) {
1259 for (k = 0; k < height; ++k) {
1260 memcpy(tmp + NB_ELTS_V8 * k,
1261 array + k * stride_width,
1262 NB_ELTS_V8 * sizeof(OPJ_INT32));
1266 for (k = 0; k < height; ++k) {
1268 for (c = 0; c < cols; c++) {
1269 tmp[NB_ELTS_V8 * k + c] = array[c + k * stride_width];
1271 for (; c < NB_ELTS_V8; c++) {
1272 tmp[NB_ELTS_V8 * k + c] = 0;
1278 /* Deinterleave result of forward transform, where cols <= NB_ELTS_V8 */
1279 /* and src contains NB_ELTS_V8 consecutive values for up to NB_ELTS_V8 */
1281 static INLINE void opj_dwt_deinterleave_v_cols(
1282 const OPJ_INT32 * OPJ_RESTRICT src,
1283 OPJ_INT32 * OPJ_RESTRICT dst,
1286 OPJ_UINT32 stride_width,
1292 OPJ_INT32 * OPJ_RESTRICT l_dest = dst;
1293 const OPJ_INT32 * OPJ_RESTRICT l_src = src + cas * NB_ELTS_V8;
1296 for (k = 0; k < 2; k++) {
1298 if (cols == NB_ELTS_V8) {
1299 memcpy(l_dest, l_src, NB_ELTS_V8 * sizeof(OPJ_INT32));
1304 l_dest[c] = l_src[c];
1307 l_dest[c] = l_src[c];
1310 l_dest[c] = l_src[c];
1313 l_dest[c] = l_src[c];
1316 l_dest[c] = l_src[c];
1319 l_dest[c] = l_src[c];
1322 l_dest[c] = l_src[c];
1326 l_dest += stride_width;
1327 l_src += 2 * NB_ELTS_V8;
1330 l_dest = dst + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)stride_width;
1331 l_src = src + (1 - cas) * NB_ELTS_V8;
1337 /* Forward 5-3 transform, for the vertical pass, processing cols columns */
1338 /* where cols <= NB_ELTS_V8 */
1339 static void opj_dwt_encode_and_deinterleave_v(
1344 OPJ_UINT32 stride_width,
1347 OPJ_INT32* OPJ_RESTRICT array = (OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1348 OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpIn;
1349 const OPJ_UINT32 sn = (height + (even ? 1 : 0)) >> 1;
1350 const OPJ_UINT32 dn = height - sn;
1352 opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
1354 #define OPJ_Sc(i) tmp[(i)*2* NB_ELTS_V8 + c]
1355 #define OPJ_Dc(i) tmp[((1+(i)*2))* NB_ELTS_V8 + c]
1361 for (c = 0; c < NB_ELTS_V8; c++) {
1370 __m128i xmm_Si_0 = *(const __m128i*)(tmp + 4 * 0);
1371 __m128i xmm_Si_1 = *(const __m128i*)(tmp + 4 * 1);
1372 for (; i + 1 < sn; i++) {
1373 __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1374 (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1375 __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1376 (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1377 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1378 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1379 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1380 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1381 xmm_Di_0 = _mm_sub_epi32(xmm_Di_0,
1382 _mm_srai_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), 1));
1383 xmm_Di_1 = _mm_sub_epi32(xmm_Di_1,
1384 _mm_srai_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), 1));
1385 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
1386 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
1387 xmm_Si_0 = xmm_Sip1_0;
1388 xmm_Si_1 = xmm_Sip1_1;
1391 if (((height) % 2) == 0) {
1392 for (c = 0; c < NB_ELTS_V8; c++) {
1393 OPJ_Dc(i) -= OPJ_Sc(i);
1396 for (c = 0; c < NB_ELTS_V8; c++) {
1397 OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1401 __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1402 (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1403 __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1404 (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1405 const __m128i xmm_two = _mm_set1_epi32(2);
1406 for (; i < dn; i++) {
1407 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1408 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1409 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1410 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1411 __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1412 (i * 2) * NB_ELTS_V8 + 4 * 0);
1413 __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1414 (i * 2) * NB_ELTS_V8 + 4 * 1);
1415 xmm_Si_0 = _mm_add_epi32(xmm_Si_0,
1416 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_0, xmm_Di_0), xmm_two), 2));
1417 xmm_Si_1 = _mm_add_epi32(xmm_Si_1,
1418 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_1, xmm_Di_1), xmm_two), 2));
1419 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1420 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1421 xmm_Dim1_0 = xmm_Di_0;
1422 xmm_Dim1_1 = xmm_Di_1;
1425 if (((height) % 2) == 1) {
1426 for (c = 0; c < NB_ELTS_V8; c++) {
1427 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1433 for (c = 0; c < NB_ELTS_V8; c++) {
1434 OPJ_Sc(0) -= OPJ_Dc(0);
1438 __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1439 (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1440 __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1441 (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1442 for (; i < sn; i++) {
1443 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1444 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1445 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1446 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1447 __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1448 (i * 2) * NB_ELTS_V8 + 4 * 0);
1449 __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1450 (i * 2) * NB_ELTS_V8 + 4 * 1);
1451 xmm_Si_0 = _mm_sub_epi32(xmm_Si_0,
1452 _mm_srai_epi32(_mm_add_epi32(xmm_Di_0, xmm_Dim1_0), 1));
1453 xmm_Si_1 = _mm_sub_epi32(xmm_Si_1,
1454 _mm_srai_epi32(_mm_add_epi32(xmm_Di_1, xmm_Dim1_1), 1));
1455 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1456 *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1457 xmm_Dim1_0 = xmm_Di_0;
1458 xmm_Dim1_1 = xmm_Di_1;
1461 if (((height) % 2) == 1) {
1462 for (c = 0; c < NB_ELTS_V8; c++) {
1463 OPJ_Sc(i) -= OPJ_Dc(i - 1);
1468 __m128i xmm_Si_0 = *((const __m128i*)(tmp + 4 * 0));
1469 __m128i xmm_Si_1 = *((const __m128i*)(tmp + 4 * 1));
1470 const __m128i xmm_two = _mm_set1_epi32(2);
1471 for (; i + 1 < dn; i++) {
1472 __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1473 (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1474 __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1475 (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1476 __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1477 (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1478 __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1479 (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1480 xmm_Di_0 = _mm_add_epi32(xmm_Di_0,
1481 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), xmm_two), 2));
1482 xmm_Di_1 = _mm_add_epi32(xmm_Di_1,
1483 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), xmm_two), 2));
1484 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
1485 *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
1486 xmm_Si_0 = xmm_Sip1_0;
1487 xmm_Si_1 = xmm_Sip1_1;
1490 if (((height) % 2) == 0) {
1491 for (c = 0; c < NB_ELTS_V8; c++) {
1492 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1501 for (i = 0; i + 1 < sn; i++) {
1502 for (c = 0; c < NB_ELTS_V8; c++) {
1503 OPJ_Dc(i) -= (OPJ_Sc(i) + OPJ_Sc(i + 1)) >> 1;
1506 if (((height) % 2) == 0) {
1507 for (c = 0; c < NB_ELTS_V8; c++) {
1508 OPJ_Dc(i) -= OPJ_Sc(i);
1511 for (c = 0; c < NB_ELTS_V8; c++) {
1512 OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1514 for (i = 1; i < dn; i++) {
1515 for (c = 0; c < NB_ELTS_V8; c++) {
1516 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i) + 2) >> 2;
1519 if (((height) % 2) == 1) {
1520 for (c = 0; c < NB_ELTS_V8; c++) {
1521 OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1528 for (c = 0; c < NB_ELTS_V8; c++) {
1533 for (c = 0; c < NB_ELTS_V8; c++) {
1534 OPJ_Sc(0) -= OPJ_Dc(0);
1536 for (i = 1; i < sn; i++) {
1537 for (c = 0; c < NB_ELTS_V8; c++) {
1538 OPJ_Sc(i) -= (OPJ_Dc(i) + OPJ_Dc(i - 1)) >> 1;
1541 if (((height) % 2) == 1) {
1542 for (c = 0; c < NB_ELTS_V8; c++) {
1543 OPJ_Sc(i) -= OPJ_Dc(i - 1);
1546 for (i = 0; i + 1 < dn; i++) {
1547 for (c = 0; c < NB_ELTS_V8; c++) {
1548 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i + 1) + 2) >> 2;
1551 if (((height) % 2) == 0) {
1552 for (c = 0; c < NB_ELTS_V8; c++) {
1553 OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1560 if (cols == NB_ELTS_V8) {
1561 opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1562 stride_width, even ? 0 : 1, NB_ELTS_V8);
1564 opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1565 stride_width, even ? 0 : 1, cols);
1569 static void opj_v8dwt_encode_step1(OPJ_FLOAT32* fw,
1571 const OPJ_FLOAT32 cst)
1575 __m128* vw = (__m128*) fw;
1576 const __m128 vcst = _mm_set1_ps(cst);
1577 for (i = 0; i < end; ++i) {
1578 vw[0] = _mm_mul_ps(vw[0], vcst);
1579 vw[1] = _mm_mul_ps(vw[1], vcst);
1580 vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1584 for (i = 0; i < end; ++i) {
1585 for (c = 0; c < NB_ELTS_V8; c++) {
1586 fw[i * 2 * NB_ELTS_V8 + c] *= cst;
1592 static void opj_v8dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1598 OPJ_UINT32 imax = opj_uint_min(end, m);
1600 __m128* vw = (__m128*) fw;
1601 __m128 vcst = _mm_set1_ps(cst);
1603 __m128* vl = (__m128*) fl;
1604 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), vcst));
1605 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), vcst));
1606 vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1609 for (; i < imax; ++i) {
1610 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), vcst));
1611 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), vcst));
1612 vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1616 assert(m + 1 == end);
1617 vcst = _mm_add_ps(vcst, vcst);
1618 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(vw[-4], vcst));
1619 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(vw[-3], vcst));
1624 for (c = 0; c < NB_ELTS_V8; c++) {
1625 fw[-1 * NB_ELTS_V8 + c] += (fl[0 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
1628 fw += 2 * NB_ELTS_V8;
1630 for (; i < imax; ++i) {
1631 for (c = 0; c < NB_ELTS_V8; c++) {
1632 fw[-1 * NB_ELTS_V8 + c] += (fw[-2 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
1635 fw += 2 * NB_ELTS_V8;
1639 assert(m + 1 == end);
1640 for (c = 0; c < NB_ELTS_V8; c++) {
1641 fw[-1 * NB_ELTS_V8 + c] += (2 * fw[-2 * NB_ELTS_V8 + c]) * cst;
1647 /* Forward 9-7 transform, for the vertical pass, processing cols columns */
1648 /* where cols <= NB_ELTS_V8 */
1649 static void opj_dwt_encode_and_deinterleave_v_real(
1654 OPJ_UINT32 stride_width,
1657 OPJ_FLOAT32* OPJ_RESTRICT array = (OPJ_FLOAT32 * OPJ_RESTRICT)arrayIn;
1658 OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32 * OPJ_RESTRICT)tmpIn;
1659 const OPJ_INT32 sn = (OPJ_INT32)((height + (even ? 1 : 0)) >> 1);
1660 const OPJ_INT32 dn = (OPJ_INT32)(height - (OPJ_UINT32)sn);
1667 opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
1676 opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
1677 tmp + (b + 1) * NB_ELTS_V8,
1679 (OPJ_UINT32)opj_int_min(dn, sn - b),
1681 opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
1682 tmp + (a + 1) * NB_ELTS_V8,
1684 (OPJ_UINT32)opj_int_min(sn, dn - a),
1686 opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
1687 tmp + (b + 1) * NB_ELTS_V8,
1689 (OPJ_UINT32)opj_int_min(dn, sn - b),
1691 opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
1692 tmp + (a + 1) * NB_ELTS_V8,
1694 (OPJ_UINT32)opj_int_min(sn, dn - a),
1696 opj_v8dwt_encode_step1(tmp + b * NB_ELTS_V8, (OPJ_UINT32)dn,
1698 opj_v8dwt_encode_step1(tmp + a * NB_ELTS_V8, (OPJ_UINT32)sn,
1702 if (cols == NB_ELTS_V8) {
1703 opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
1705 (OPJ_INT32)dn, (OPJ_INT32)sn,
1706 stride_width, even ? 0 : 1, NB_ELTS_V8);
1708 opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
1710 (OPJ_INT32)dn, (OPJ_INT32)sn,
1711 stride_width, even ? 0 : 1, cols);
1717 /* Forward 5-3 wavelet transform in 2-D. */
1719 static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1720 opj_tcd_tilecomp_t * tilec,
1721 opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
1722 opj_encode_and_deinterleave_h_one_row_fnptr_type
1723 p_encode_and_deinterleave_h_one_row)
1730 OPJ_SIZE_T l_data_size;
1732 opj_tcd_resolution_t * l_cur_res = 0;
1733 opj_tcd_resolution_t * l_last_res = 0;
1734 const int num_threads = opj_thread_pool_get_thread_count(tp);
1735 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1737 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1738 l = (OPJ_INT32)tilec->numresolutions - 1;
1740 l_cur_res = tilec->resolutions + l;
1741 l_last_res = l_cur_res - 1;
1743 l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1744 /* overflow check */
1745 if (l_data_size > (SIZE_MAX / (NB_ELTS_V8 * sizeof(OPJ_INT32)))) {
1746 /* FIXME event manager error callback */
1749 l_data_size *= NB_ELTS_V8 * sizeof(OPJ_INT32);
1750 bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1751 /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1752 /* in that case, so do not error out */
1753 if (l_data_size != 0 && ! bj) {
1760 OPJ_UINT32 rw; /* width of the resolution level computed */
1761 OPJ_UINT32 rh; /* height of the resolution level computed */
1763 rw1; /* width of the resolution level once lower than computed one */
1765 rh1; /* height of the resolution level once lower than computed one */
1766 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1767 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
1770 rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1771 rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1772 rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1773 rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1775 cas_row = l_cur_res->x0 & 1;
1776 cas_col = l_cur_res->y0 & 1;
1778 sn = (OPJ_INT32)rh1;
1779 dn = (OPJ_INT32)(rh - rh1);
1781 /* Perform vertical pass */
1782 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
1783 for (j = 0; j + NB_ELTS_V8 - 1 < rw; j += NB_ELTS_V8) {
1784 p_encode_and_deinterleave_v(tiledp + j,
1792 p_encode_and_deinterleave_v(tiledp + j,
1800 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1803 if (rw < num_jobs) {
1806 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
1808 for (j = 0; j < num_jobs; j++) {
1809 opj_dwt_encode_v_job_t* job;
1811 job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
1813 opj_thread_pool_wait_completion(tp, 0);
1814 opj_aligned_free(bj);
1817 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1819 opj_thread_pool_wait_completion(tp, 0);
1821 opj_aligned_free(bj);
1826 job->v.cas = cas_col;
1829 job->tiledp = tiledp;
1830 job->min_j = j * step_j;
1831 job->max_j = (j + 1 == num_jobs) ? rw : (j + 1) * step_j;
1832 job->p_encode_and_deinterleave_v = p_encode_and_deinterleave_v;
1833 opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
1835 opj_thread_pool_wait_completion(tp, 0);
1838 sn = (OPJ_INT32)rw1;
1839 dn = (OPJ_INT32)(rw - rw1);
1841 /* Perform horizontal pass */
1842 if (num_threads <= 1 || rh <= 1) {
1843 for (j = 0; j < rh; j++) {
1844 OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
1845 (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
1846 cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
1849 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
1852 if (rh < num_jobs) {
1855 step_j = (rh / num_jobs);
1857 for (j = 0; j < num_jobs; j++) {
1858 opj_dwt_encode_h_job_t* job;
1860 job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
1862 opj_thread_pool_wait_completion(tp, 0);
1863 opj_aligned_free(bj);
1866 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1868 opj_thread_pool_wait_completion(tp, 0);
1870 opj_aligned_free(bj);
1875 job->h.cas = cas_row;
1878 job->tiledp = tiledp;
1879 job->min_j = j * step_j;
1880 job->max_j = (j + 1U) * step_j; /* this can overflow */
1881 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
1884 job->p_function = p_encode_and_deinterleave_h_one_row;
1885 opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
1887 opj_thread_pool_wait_completion(tp, 0);
1890 l_cur_res = l_last_res;
1895 opj_aligned_free(bj);
1899 /* Forward 5-3 wavelet transform in 2-D. */
1901 OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
1902 opj_tcd_tilecomp_t * tilec)
1904 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1905 opj_dwt_encode_and_deinterleave_v,
1906 opj_dwt_encode_and_deinterleave_h_one_row);
1910 /* Inverse 5-3 wavelet transform in 2-D. */
1912 OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
1915 if (p_tcd->whole_tile_decoding) {
1916 return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
1918 return opj_dwt_decode_partial_tile(tilec, numres);
1923 /* Get norm of 5-3 wavelet. */
1925 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
1927 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1928 /* but the array should really be extended up to 33 resolution levels */
1929 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1930 if (orient == 0 && level >= 10) {
1932 } else if (orient > 0 && level >= 9) {
1935 return opj_dwt_norms[orient][level];
1939 /* Forward 9-7 wavelet transform in 2-D. */
1941 OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
1942 opj_tcd_tilecomp_t * tilec)
1944 return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
1945 opj_dwt_encode_and_deinterleave_v_real,
1946 opj_dwt_encode_and_deinterleave_h_one_row_real);
1950 /* Get norm of 9-7 wavelet. */
1952 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
1954 /* FIXME ! This is just a band-aid to avoid a buffer overflow */
1955 /* but the array should really be extended up to 33 resolution levels */
1956 /* See https://github.com/uclouvain/openjpeg/issues/493 */
1957 if (orient == 0 && level >= 10) {
1959 } else if (orient > 0 && level >= 9) {
1962 return opj_dwt_norms_real[orient][level];
1965 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
1967 OPJ_UINT32 numbands, bandno;
1968 numbands = 3 * tccp->numresolutions - 2;
1969 for (bandno = 0; bandno < numbands; bandno++) {
1970 OPJ_FLOAT64 stepsize;
1971 OPJ_UINT32 resno, level, orient, gain;
1973 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
1974 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
1975 level = tccp->numresolutions - 1 - resno;
1976 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
1977 (orient == 2)) ? 1 : 2));
1978 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1981 OPJ_FLOAT64 norm = opj_dwt_getnorm_real(level, orient);
1982 stepsize = (1 << (gain)) / norm;
1984 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
1985 (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
1990 /* Determine maximum computed resolution level for inverse wavelet transform */
1992 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
1999 if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
2002 if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
2013 OPJ_INT32 * OPJ_RESTRICT tiledp;
2016 } opj_dwt_decode_h_job_t;
2018 static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
2021 opj_dwt_decode_h_job_t* job;
2024 job = (opj_dwt_decode_h_job_t*)user_data;
2025 for (j = job->min_j; j < job->max_j; j++) {
2026 opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
2029 opj_aligned_free(job->h.mem);
2037 OPJ_INT32 * OPJ_RESTRICT tiledp;
2040 } opj_dwt_decode_v_job_t;
2042 static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
2045 opj_dwt_decode_v_job_t* job;
2048 job = (opj_dwt_decode_v_job_t*)user_data;
2049 for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
2050 j += PARALLEL_COLS_53) {
2051 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
2055 opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
2056 (OPJ_INT32)(job->max_j - j));
2058 opj_aligned_free(job->v.mem);
2064 /* Inverse wavelet transform in 2-D. */
2066 static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
2067 opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
2072 opj_tcd_resolution_t* tr = tilec->resolutions;
2074 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2075 tr->x0); /* width of the resolution level computed */
2076 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2077 tr->y0); /* height of the resolution level computed */
2079 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2081 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2082 OPJ_SIZE_T h_mem_size;
2088 num_threads = opj_thread_pool_get_thread_count(tp);
2089 h_mem_size = opj_dwt_max_resolution(tr, numres);
2090 /* overflow check */
2091 if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
2092 /* FIXME event manager error callback */
2095 /* We need PARALLEL_COLS_53 times the height of the array, */
2096 /* since for the vertical pass */
2097 /* we process PARALLEL_COLS_53 columns at a time */
2098 h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
2099 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2101 /* FIXME event manager error callback */
2108 OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
2112 h.sn = (OPJ_INT32)rw;
2113 v.sn = (OPJ_INT32)rh;
2115 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2116 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2118 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2121 if (num_threads <= 1 || rh <= 1) {
2122 for (j = 0; j < rh; ++j) {
2123 opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
2126 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2129 if (rh < num_jobs) {
2132 step_j = (rh / num_jobs);
2134 for (j = 0; j < num_jobs; j++) {
2135 opj_dwt_decode_h_job_t* job;
2137 job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
2139 /* It would be nice to fallback to single thread case, but */
2140 /* unfortunately some jobs may be launched and have modified */
2141 /* tiledp, so it is not practical to recover from that error */
2142 /* FIXME event manager error callback */
2143 opj_thread_pool_wait_completion(tp, 0);
2144 opj_aligned_free(h.mem);
2150 job->tiledp = tiledp;
2151 job->min_j = j * step_j;
2152 job->max_j = (j + 1U) * step_j; /* this can overflow */
2153 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
2156 job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2158 /* FIXME event manager error callback */
2159 opj_thread_pool_wait_completion(tp, 0);
2161 opj_aligned_free(h.mem);
2164 opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
2166 opj_thread_pool_wait_completion(tp, 0);
2169 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2172 if (num_threads <= 1 || rw <= 1) {
2173 for (j = 0; j + PARALLEL_COLS_53 <= rw;
2174 j += PARALLEL_COLS_53) {
2175 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
2178 opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
2181 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2184 if (rw < num_jobs) {
2187 step_j = (rw / num_jobs);
2189 for (j = 0; j < num_jobs; j++) {
2190 opj_dwt_decode_v_job_t* job;
2192 job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
2194 /* It would be nice to fallback to single thread case, but */
2195 /* unfortunately some jobs may be launched and have modified */
2196 /* tiledp, so it is not practical to recover from that error */
2197 /* FIXME event manager error callback */
2198 opj_thread_pool_wait_completion(tp, 0);
2199 opj_aligned_free(v.mem);
2205 job->tiledp = tiledp;
2206 job->min_j = j * step_j;
2207 job->max_j = (j + 1U) * step_j; /* this can overflow */
2208 if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
2211 job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2213 /* FIXME event manager error callback */
2214 opj_thread_pool_wait_completion(tp, 0);
2216 opj_aligned_free(v.mem);
2219 opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
2221 opj_thread_pool_wait_completion(tp, 0);
2224 opj_aligned_free(h.mem);
2228 static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
2230 opj_sparse_array_int32_t* sa,
2233 OPJ_UINT32 win_l_x0,
2234 OPJ_UINT32 win_l_x1,
2235 OPJ_UINT32 win_h_x0,
2236 OPJ_UINT32 win_h_x1)
2239 ret = opj_sparse_array_int32_read(sa,
2241 win_l_x1, sa_line + 1,
2242 dest + cas + 2 * win_l_x0,
2245 ret = opj_sparse_array_int32_read(sa,
2246 sn + win_h_x0, sa_line,
2247 sn + win_h_x1, sa_line + 1,
2248 dest + 1 - cas + 2 * win_h_x0,
2255 static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
2257 opj_sparse_array_int32_t* sa,
2261 OPJ_UINT32 win_l_y0,
2262 OPJ_UINT32 win_l_y1,
2263 OPJ_UINT32 win_h_y0,
2264 OPJ_UINT32 win_h_y1)
2267 ret = opj_sparse_array_int32_read(sa,
2269 sa_col + nb_cols, win_l_y1,
2270 dest + cas * 4 + 2 * 4 * win_l_y0,
2271 1, 2 * 4, OPJ_TRUE);
2273 ret = opj_sparse_array_int32_read(sa,
2274 sa_col, sn + win_h_y0,
2275 sa_col + nb_cols, sn + win_h_y1,
2276 dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
2277 1, 2 * 4, OPJ_TRUE);
2282 static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
2292 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
2294 /* Naive version is :
2295 for (i = win_l_x0; i < i_max; i++) {
2296 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2298 for (i = win_h_x0; i < win_h_x1; i++) {
2299 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2301 but the compiler doesn't manage to unroll it to avoid bound
2302 checking in OPJ_S_ and OPJ_D_ macros
2309 /* Left-most case */
2310 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2317 for (; i < i_max; i++) {
2318 /* No bound checking */
2319 OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
2321 for (; i < win_l_x1; i++) {
2322 /* Right-most case */
2323 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2329 OPJ_INT32 i_max = win_h_x1;
2333 for (; i < i_max; i++) {
2334 /* No bound checking */
2335 OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
2337 for (; i < win_h_x1; i++) {
2338 /* Right-most case */
2339 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2344 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
2347 for (i = win_l_x0; i < win_l_x1; i++) {
2348 OPJ_D(i) = opj_int_sub_no_overflow(OPJ_D(i),
2349 opj_int_add_no_overflow(opj_int_add_no_overflow(OPJ_SS_(i), OPJ_SS_(i + 1)),
2352 for (i = win_h_x0; i < win_h_x1; i++) {
2353 OPJ_S(i) = opj_int_add_no_overflow(OPJ_S(i),
2354 opj_int_add_no_overflow(OPJ_DD_(i), OPJ_DD_(i - 1)) >> 1);
2360 #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
2361 #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
2362 #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)))
2363 #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)))
2364 #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)))
2365 #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)))
2367 static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
2369 OPJ_INT32 dn, OPJ_INT32 sn,
2382 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
2384 /* Naive version is :
2385 for (i = win_l_x0; i < i_max; i++) {
2386 OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2388 for (i = win_h_x0; i < win_h_x1; i++) {
2389 OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2391 but the compiler doesn't manage to unroll it to avoid bound
2392 checking in OPJ_S_ and OPJ_D_ macros
2399 /* Left-most case */
2400 for (off = 0; off < 4; off++) {
2401 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2411 if (i + 1 < i_max) {
2412 const __m128i two = _mm_set1_epi32(2);
2413 __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
2414 for (; i + 1 < i_max; i += 2) {
2415 /* No bound checking */
2416 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
2417 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2418 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2419 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2420 S = _mm_sub_epi32(S,
2421 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
2422 S1 = _mm_sub_epi32(S1,
2423 _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
2424 _mm_store_si128((__m128i*)(a + i * 8), S);
2425 _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
2431 for (; i < i_max; i++) {
2432 /* No bound checking */
2433 for (off = 0; off < 4; off++) {
2434 OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
2437 for (; i < win_l_x1; i++) {
2438 /* Right-most case */
2439 for (off = 0; off < 4; off++) {
2440 OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2447 OPJ_INT32 i_max = win_h_x1;
2453 if (i + 1 < i_max) {
2454 __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
2455 for (; i + 1 < i_max; i += 2) {
2456 /* No bound checking */
2457 __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2458 __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2459 __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2460 __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
2461 D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
2462 D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
2463 _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
2464 _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
2470 for (; i < i_max; i++) {
2471 /* No bound checking */
2472 for (off = 0; off < 4; off++) {
2473 OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
2476 for (; i < win_h_x1; i++) {
2477 /* Right-most case */
2478 for (off = 0; off < 4; off++) {
2479 OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
2485 if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
2486 for (off = 0; off < 4; off++) {
2487 OPJ_S_off(0, off) /= 2;
2490 for (i = win_l_x0; i < win_l_x1; i++) {
2491 for (off = 0; off < 4; off++) {
2492 OPJ_D_off(i, off) = opj_int_sub_no_overflow(
2494 opj_int_add_no_overflow(
2495 opj_int_add_no_overflow(OPJ_SS__off(i, off), OPJ_SS__off(i + 1, off)), 2) >> 2);
2498 for (i = win_h_x0; i < win_h_x1; i++) {
2499 for (off = 0; off < 4; off++) {
2500 OPJ_S_off(i, off) = opj_int_add_no_overflow(
2502 opj_int_add_no_overflow(OPJ_DD__off(i, off), OPJ_DD__off(i - 1, off)) >> 1);
2509 static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
2521 /* Compute number of decomposition for this band. See table F-1 */
2522 OPJ_UINT32 nb = (resno == 0) ?
2523 tilec->numresolutions - 1 :
2524 tilec->numresolutions - resno;
2525 /* Map above tile-based coordinates to sub-band-based coordinates per */
2526 /* equation B-15 of the standard */
2527 OPJ_UINT32 x0b = bandno & 1;
2528 OPJ_UINT32 y0b = bandno >> 1;
2530 *tbx0 = (nb == 0) ? tcx0 :
2531 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2532 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2535 *tby0 = (nb == 0) ? tcy0 :
2536 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2537 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2540 *tbx1 = (nb == 0) ? tcx1 :
2541 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2542 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2545 *tby1 = (nb == 0) ? tcy1 :
2546 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2547 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2551 static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2552 OPJ_UINT32 max_size,
2556 *start = opj_uint_subs(*start, filter_width);
2557 *end = opj_uint_adds(*end, filter_width);
2558 *end = opj_uint_min(*end, max_size);
2562 static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2563 opj_tcd_tilecomp_t* tilec,
2566 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2567 OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2568 OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2569 OPJ_UINT32 resno, bandno, precno, cblkno;
2570 opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2571 w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2576 for (resno = 0; resno < numres; ++resno) {
2577 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2579 for (bandno = 0; bandno < res->numbands; ++bandno) {
2580 opj_tcd_band_t* band = &res->bands[bandno];
2582 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2583 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2584 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2585 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2586 if (cblk->decoded_data != NULL) {
2587 OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2588 OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2589 OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2590 OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2592 if (band->bandno & 1) {
2593 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2594 x += (OPJ_UINT32)(pres->x1 - pres->x0);
2596 if (band->bandno & 2) {
2597 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2598 y += (OPJ_UINT32)(pres->y1 - pres->y0);
2601 if (!opj_sparse_array_int32_write(sa, x, y,
2602 x + cblk_w, y + cblk_h,
2604 1, cblk_w, OPJ_TRUE)) {
2605 opj_sparse_array_int32_free(sa);
2618 static OPJ_BOOL opj_dwt_decode_partial_tile(
2619 opj_tcd_tilecomp_t* tilec,
2622 opj_sparse_array_int32_t* sa;
2626 /* This value matches the maximum left/right extension given in tables */
2627 /* F.2 and F.3 of the standard. */
2628 const OPJ_UINT32 filter_width = 2U;
2630 opj_tcd_resolution_t* tr = tilec->resolutions;
2631 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2633 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2634 tr->x0); /* width of the resolution level computed */
2635 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2636 tr->y0); /* height of the resolution level computed */
2638 OPJ_SIZE_T h_mem_size;
2640 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2641 /* with the tile coordinates */
2642 OPJ_UINT32 win_tcx0 = tilec->win_x0;
2643 OPJ_UINT32 win_tcy0 = tilec->win_y0;
2644 OPJ_UINT32 win_tcx1 = tilec->win_x1;
2645 OPJ_UINT32 win_tcy1 = tilec->win_y1;
2647 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2651 sa = opj_dwt_init_sparse_array(tilec, numres);
2657 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2658 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2659 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2660 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2661 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2663 1, tr_max->win_x1 - tr_max->win_x0,
2667 opj_sparse_array_int32_free(sa);
2670 h_mem_size = opj_dwt_max_resolution(tr, numres);
2671 /* overflow check */
2672 /* in vertical pass, we process 4 columns at a time */
2673 if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2674 /* FIXME event manager error callback */
2675 opj_sparse_array_int32_free(sa);
2679 h_mem_size *= 4 * sizeof(OPJ_INT32);
2680 h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2682 /* FIXME event manager error callback */
2683 opj_sparse_array_int32_free(sa);
2689 for (resno = 1; resno < numres; resno ++) {
2691 /* Window of interest subband-based coordinates */
2692 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2693 OPJ_UINT32 win_hl_x0, win_hl_x1;
2694 OPJ_UINT32 win_lh_y0, win_lh_y1;
2695 /* Window of interest tile-resolution-based coordinates */
2696 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2697 /* Tile-resolution subband-based coordinates */
2698 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2702 h.sn = (OPJ_INT32)rw;
2703 v.sn = (OPJ_INT32)rh;
2705 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2706 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2708 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2711 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2714 /* Get the subband coordinates for the window of interest */
2716 opj_dwt_get_band_coordinates(tilec, resno, 0,
2717 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2718 &win_ll_x0, &win_ll_y0,
2719 &win_ll_x1, &win_ll_y1);
2722 opj_dwt_get_band_coordinates(tilec, resno, 1,
2723 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2724 &win_hl_x0, NULL, &win_hl_x1, NULL);
2727 opj_dwt_get_band_coordinates(tilec, resno, 2,
2728 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2729 NULL, &win_lh_y0, NULL, &win_lh_y1);
2731 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2732 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2733 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2734 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2735 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2737 /* Subtract the origin of the bands for this tile, to the subwindow */
2738 /* of interest band coordinates, so as to get them relative to the */
2740 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2741 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2742 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2743 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2744 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2745 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2746 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2747 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2749 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2750 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2752 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2753 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2755 /* Compute the tile-resolution-based coordinates for the window of interest */
2757 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2758 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2760 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2761 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2765 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2766 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2768 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2769 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2772 for (j = 0; j < rh; ++j) {
2773 if ((j >= win_ll_y0 && j < win_ll_y1) ||
2774 (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2776 /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2777 /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2778 /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2779 /* This is less extreme than memsetting the whole buffer to 0 */
2780 /* although we could potentially do better with better handling of edge conditions */
2781 if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2782 h.mem[win_tr_x1 - 1] = 0;
2784 if (win_tr_x1 < rw) {
2785 h.mem[win_tr_x1] = 0;
2788 opj_dwt_interleave_partial_h(h.mem,
2797 opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
2798 (OPJ_INT32)win_ll_x0,
2799 (OPJ_INT32)win_ll_x1,
2800 (OPJ_INT32)win_hl_x0,
2801 (OPJ_INT32)win_hl_x1);
2802 if (!opj_sparse_array_int32_write(sa,
2807 /* FIXME event manager error callback */
2808 opj_sparse_array_int32_free(sa);
2809 opj_aligned_free(h.mem);
2815 for (i = win_tr_x0; i < win_tr_x1;) {
2816 OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
2817 opj_dwt_interleave_partial_v(v.mem,
2827 opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
2828 (OPJ_INT32)win_ll_y0,
2829 (OPJ_INT32)win_ll_y1,
2830 (OPJ_INT32)win_lh_y0,
2831 (OPJ_INT32)win_lh_y1);
2832 if (!opj_sparse_array_int32_write(sa,
2834 i + nb_cols, win_tr_y1,
2835 v.mem + 4 * win_tr_y0,
2837 /* FIXME event manager error callback */
2838 opj_sparse_array_int32_free(sa);
2839 opj_aligned_free(h.mem);
2846 opj_aligned_free(h.mem);
2849 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2850 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2851 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2852 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2853 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2855 1, tr_max->win_x1 - tr_max->win_x0,
2860 opj_sparse_array_int32_free(sa);
2864 static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
2865 OPJ_FLOAT32* OPJ_RESTRICT a,
2867 OPJ_UINT32 remaining_height)
2869 OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
2871 OPJ_UINT32 x0 = dwt->win_l_x0;
2872 OPJ_UINT32 x1 = dwt->win_l_x1;
2874 for (k = 0; k < 2; ++k) {
2875 if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
2876 ((OPJ_SIZE_T) bi & 0x0f) == 0) {
2877 /* Fast code path */
2878 for (i = x0; i < x1; ++i) {
2880 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2898 /* Slow code path */
2899 for (i = x0; i < x1; ++i) {
2901 OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
2904 if (remaining_height == 1) {
2909 if (remaining_height == 2) {
2914 if (remaining_height == 3) {
2919 if (remaining_height == 4) {
2924 if (remaining_height == 5) {
2929 if (remaining_height == 6) {
2934 if (remaining_height == 7) {
2941 bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
2948 static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
2949 opj_sparse_array_int32_t* sa,
2951 OPJ_UINT32 remaining_height)
2954 for (i = 0; i < remaining_height; i++) {
2956 ret = opj_sparse_array_int32_read(sa,
2957 dwt->win_l_x0, sa_line + i,
2958 dwt->win_l_x1, sa_line + i + 1,
2959 /* Nasty cast from float* to int32* */
2960 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
2961 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2963 ret = opj_sparse_array_int32_read(sa,
2964 (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
2965 (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
2966 /* Nasty cast from float* to int32* */
2967 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
2968 2 * NB_ELTS_V8, 0, OPJ_TRUE);
2974 static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2975 OPJ_FLOAT32* OPJ_RESTRICT a,
2977 OPJ_UINT32 nb_elts_read)
2979 opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
2982 for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
2983 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2984 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2987 a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
2988 bi = dwt->wavelet + 1 - dwt->cas;
2990 for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
2991 memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
2992 (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
2996 static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
2997 opj_sparse_array_int32_t* sa,
2999 OPJ_UINT32 nb_elts_read)
3002 ret = opj_sparse_array_int32_read(sa,
3003 sa_col, dwt->win_l_x0,
3004 sa_col + nb_elts_read, dwt->win_l_x1,
3005 (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
3006 1, 2 * NB_ELTS_V8, OPJ_TRUE);
3008 ret = opj_sparse_array_int32_read(sa,
3009 sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
3010 sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
3011 (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
3012 1, 2 * NB_ELTS_V8, OPJ_TRUE);
3019 static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
3024 __m128* OPJ_RESTRICT vw = (__m128*) w;
3025 OPJ_UINT32 i = start;
3026 /* To be adapted if NB_ELTS_V8 changes */
3028 /* Note: attempt at loop unrolling x2 doesn't help */
3029 for (; i < end; ++i, vw += 4) {
3030 vw[0] = _mm_mul_ps(vw[0], c);
3031 vw[1] = _mm_mul_ps(vw[1], c);
3035 static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
3041 __m128* OPJ_RESTRICT vl = (__m128*) l;
3042 __m128* OPJ_RESTRICT vw = (__m128*) w;
3043 /* To be adapted if NB_ELTS_V8 changes */
3045 OPJ_UINT32 imax = opj_uint_min(end, m);
3048 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
3049 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
3058 /* Note: attempt at loop unrolling x2 doesn't help */
3059 for (; i < imax; ++i) {
3060 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
3061 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
3065 assert(m + 1 == end);
3066 c = _mm_add_ps(c, c);
3067 vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
3068 vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
3074 static void opj_v8dwt_decode_step1(opj_v8_t* w,
3077 const OPJ_FLOAT32 c)
3079 OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
3081 /* To be adapted if NB_ELTS_V8 changes */
3082 for (i = start; i < end; ++i) {
3083 fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c;
3084 fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
3085 fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
3086 fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
3087 fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
3088 fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
3089 fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
3090 fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
3094 static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
3100 OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
3101 OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
3103 OPJ_UINT32 imax = opj_uint_min(end, m);
3105 fw += 2 * NB_ELTS_V8 * start;
3106 fl = fw - 2 * NB_ELTS_V8;
3108 /* To be adapted if NB_ELTS_V8 changes */
3109 for (i = start; i < imax; ++i) {
3110 fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
3111 fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
3112 fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
3113 fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
3114 fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
3115 fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
3116 fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
3117 fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
3119 fw += 2 * NB_ELTS_V8;
3122 assert(m + 1 == end);
3124 fw[-8] = fw[-8] + fl[0] * c;
3125 fw[-7] = fw[-7] + fl[1] * c;
3126 fw[-6] = fw[-6] + fl[2] * c;
3127 fw[-5] = fw[-5] + fl[3] * c;
3128 fw[-4] = fw[-4] + fl[4] * c;
3129 fw[-3] = fw[-3] + fl[5] * c;
3130 fw[-2] = fw[-2] + fl[6] * c;
3131 fw[-1] = fw[-1] + fl[7] * c;
3138 /* Inverse 9-7 wavelet transform in 1-D. */
3140 static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
3143 /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
3144 /* Historic value for 2 / opj_invK */
3145 /* Normally, we should use invK, but if we do so, we have failures in the */
3146 /* conformance test, due to MSE and peak errors significantly higher than */
3147 /* accepted value */
3148 /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
3149 /* the computation of the stepsize for the non LL subbands */
3150 const float two_invK = 1.625732422f;
3151 if (dwt->cas == 0) {
3152 if (!((dwt->dn > 0) || (dwt->sn > 1))) {
3158 if (!((dwt->sn > 0) || (dwt->dn > 1))) {
3165 opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
3166 _mm_set1_ps(opj_K));
3167 opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
3168 _mm_set1_ps(two_invK));
3169 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
3170 dwt->win_l_x0, dwt->win_l_x1,
3171 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3172 _mm_set1_ps(-opj_dwt_delta));
3173 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3174 dwt->win_h_x0, dwt->win_h_x1,
3175 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3176 _mm_set1_ps(-opj_dwt_gamma));
3177 opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
3178 dwt->win_l_x0, dwt->win_l_x1,
3179 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3180 _mm_set1_ps(-opj_dwt_beta));
3181 opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3182 dwt->win_h_x0, dwt->win_h_x1,
3183 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3184 _mm_set1_ps(-opj_dwt_alpha));
3186 opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
3188 opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
3190 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3191 dwt->win_l_x0, dwt->win_l_x1,
3192 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3194 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3195 dwt->win_h_x0, dwt->win_h_x1,
3196 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3198 opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3199 dwt->win_l_x0, dwt->win_l_x1,
3200 (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3202 opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3203 dwt->win_h_x0, dwt->win_h_x1,
3204 (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3213 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3215 } opj_dwt97_decode_h_job_t;
3217 static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
3220 opj_dwt97_decode_h_job_t* job;
3221 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3225 job = (opj_dwt97_decode_h_job_t*)user_data;
3228 assert((job->nb_rows % NB_ELTS_V8) == 0);
3231 for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
3233 opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
3234 opj_v8dwt_decode(&job->h);
3236 /* To be adapted if NB_ELTS_V8 changes */
3237 for (k = 0; k < job->rw; k++) {
3238 aj[k ] = job->h.wavelet[k].f[0];
3239 aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1];
3240 aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
3241 aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
3243 for (k = 0; k < job->rw; k++) {
3244 aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
3245 aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
3246 aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
3247 aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
3250 aj += w * NB_ELTS_V8;
3253 opj_aligned_free(job->h.wavelet);
3262 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3263 OPJ_UINT32 nb_columns;
3264 } opj_dwt97_decode_v_job_t;
3266 static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
3269 opj_dwt97_decode_v_job_t* job;
3270 OPJ_FLOAT32 * OPJ_RESTRICT aj;
3273 job = (opj_dwt97_decode_v_job_t*)user_data;
3275 assert((job->nb_columns % NB_ELTS_V8) == 0);
3278 for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
3281 opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
3282 opj_v8dwt_decode(&job->v);
3284 for (k = 0; k < job->rh; ++k) {
3285 memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
3286 NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3291 opj_aligned_free(job->v.wavelet);
3297 /* Inverse 9-7 wavelet transform in 2-D. */
3300 OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
3301 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3307 opj_tcd_resolution_t* res = tilec->resolutions;
3309 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
3310 res->x0); /* width of the resolution level computed */
3311 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
3312 res->y0); /* height of the resolution level computed */
3314 OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
3316 tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
3318 OPJ_SIZE_T l_data_size;
3319 const int num_threads = opj_thread_pool_get_thread_count(tp);
3325 l_data_size = opj_dwt_max_resolution(res, numres);
3326 /* overflow check */
3327 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3328 /* FIXME event manager error callback */
3331 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3333 /* FIXME event manager error callback */
3336 v.wavelet = h.wavelet;
3339 OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
3342 h.sn = (OPJ_INT32)rw;
3343 v.sn = (OPJ_INT32)rh;
3347 rw = (OPJ_UINT32)(res->x1 -
3348 res->x0); /* width of the resolution level computed */
3349 rh = (OPJ_UINT32)(res->y1 -
3350 res->y0); /* height of the resolution level computed */
3352 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3353 h.cas = res->x0 % 2;
3356 h.win_l_x1 = (OPJ_UINT32)h.sn;
3358 h.win_h_x1 = (OPJ_UINT32)h.dn;
3360 if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
3361 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3363 opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
3364 opj_v8dwt_decode(&h);
3366 /* To be adapted if NB_ELTS_V8 changes */
3367 for (k = 0; k < rw; k++) {
3368 aj[k ] = h.wavelet[k].f[0];
3369 aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1];
3370 aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
3371 aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
3373 for (k = 0; k < rw; k++) {
3374 aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
3375 aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
3376 aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
3377 aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
3380 aj += w * NB_ELTS_V8;
3383 OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
3386 if ((rh / NB_ELTS_V8) < num_jobs) {
3387 num_jobs = rh / NB_ELTS_V8;
3389 step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3390 for (j = 0; j < num_jobs; j++) {
3391 opj_dwt97_decode_h_job_t* job;
3393 job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
3395 opj_thread_pool_wait_completion(tp, 0);
3396 opj_aligned_free(h.wavelet);
3399 job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3400 if (!job->h.wavelet) {
3401 opj_thread_pool_wait_completion(tp, 0);
3403 opj_aligned_free(h.wavelet);
3409 job->h.win_l_x0 = h.win_l_x0;
3410 job->h.win_l_x1 = h.win_l_x1;
3411 job->h.win_h_x0 = h.win_h_x0;
3412 job->h.win_h_x1 = h.win_h_x1;
3416 job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
3417 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3418 aj += w * job->nb_rows;
3419 opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
3421 opj_thread_pool_wait_completion(tp, 0);
3422 j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
3427 opj_v8dwt_interleave_h(&h, aj, w, rh - j);
3428 opj_v8dwt_decode(&h);
3429 for (k = 0; k < rw; k++) {
3431 for (l = 0; l < rh - j; l++) {
3432 aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l];
3437 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3438 v.cas = res->y0 % 2;
3440 v.win_l_x1 = (OPJ_UINT32)v.sn;
3442 v.win_h_x1 = (OPJ_UINT32)v.dn;
3444 aj = (OPJ_FLOAT32*) tilec->data;
3445 if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
3446 for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
3449 opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
3450 opj_v8dwt_decode(&v);
3452 for (k = 0; k < rh; ++k) {
3453 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3458 /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
3459 transfer being the limiting factor. So limit the number of
3462 OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
3465 if ((rw / NB_ELTS_V8) < num_jobs) {
3466 num_jobs = rw / NB_ELTS_V8;
3468 step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3469 for (j = 0; j < num_jobs; j++) {
3470 opj_dwt97_decode_v_job_t* job;
3472 job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
3474 opj_thread_pool_wait_completion(tp, 0);
3475 opj_aligned_free(h.wavelet);
3478 job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3479 if (!job->v.wavelet) {
3480 opj_thread_pool_wait_completion(tp, 0);
3482 opj_aligned_free(h.wavelet);
3488 job->v.win_l_x0 = v.win_l_x0;
3489 job->v.win_l_x1 = v.win_l_x1;
3490 job->v.win_h_x0 = v.win_h_x0;
3491 job->v.win_h_x1 = v.win_h_x1;
3495 job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
3496 (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3497 aj += job->nb_columns;
3498 opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
3500 opj_thread_pool_wait_completion(tp, 0);
3503 if (rw & (NB_ELTS_V8 - 1)) {
3506 j = rw & (NB_ELTS_V8 - 1);
3508 opj_v8dwt_interleave_v(&v, aj, w, j);
3509 opj_v8dwt_decode(&v);
3511 for (k = 0; k < rh; ++k) {
3512 memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
3513 (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
3518 opj_aligned_free(h.wavelet);
3523 OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3526 opj_sparse_array_int32_t* sa;
3530 /* This value matches the maximum left/right extension given in tables */
3531 /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3532 /* we currently use 3. */
3533 const OPJ_UINT32 filter_width = 4U;
3535 opj_tcd_resolution_t* tr = tilec->resolutions;
3536 opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3538 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3539 tr->x0); /* width of the resolution level computed */
3540 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3541 tr->y0); /* height of the resolution level computed */
3543 OPJ_SIZE_T l_data_size;
3545 /* Compute the intersection of the area of interest, expressed in tile coordinates */
3546 /* with the tile coordinates */
3547 OPJ_UINT32 win_tcx0 = tilec->win_x0;
3548 OPJ_UINT32 win_tcy0 = tilec->win_y0;
3549 OPJ_UINT32 win_tcx1 = tilec->win_x1;
3550 OPJ_UINT32 win_tcy1 = tilec->win_y1;
3552 if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3556 sa = opj_dwt_init_sparse_array(tilec, numres);
3562 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3563 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3564 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3565 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3566 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3568 1, tr_max->win_x1 - tr_max->win_x0,
3572 opj_sparse_array_int32_free(sa);
3576 l_data_size = opj_dwt_max_resolution(tr, numres);
3577 /* overflow check */
3578 if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3579 /* FIXME event manager error callback */
3580 opj_sparse_array_int32_free(sa);
3583 h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3585 /* FIXME event manager error callback */
3586 opj_sparse_array_int32_free(sa);
3589 v.wavelet = h.wavelet;
3591 for (resno = 1; resno < numres; resno ++) {
3593 /* Window of interest subband-based coordinates */
3594 OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3595 OPJ_UINT32 win_hl_x0, win_hl_x1;
3596 OPJ_UINT32 win_lh_y0, win_lh_y1;
3597 /* Window of interest tile-resolution-based coordinates */
3598 OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3599 /* Tile-resolution subband-based coordinates */
3600 OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3604 h.sn = (OPJ_INT32)rw;
3605 v.sn = (OPJ_INT32)rh;
3607 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3608 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3610 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3613 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3616 /* Get the subband coordinates for the window of interest */
3618 opj_dwt_get_band_coordinates(tilec, resno, 0,
3619 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3620 &win_ll_x0, &win_ll_y0,
3621 &win_ll_x1, &win_ll_y1);
3624 opj_dwt_get_band_coordinates(tilec, resno, 1,
3625 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3626 &win_hl_x0, NULL, &win_hl_x1, NULL);
3629 opj_dwt_get_band_coordinates(tilec, resno, 2,
3630 win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3631 NULL, &win_lh_y0, NULL, &win_lh_y1);
3633 /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3634 tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3635 tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3636 tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3637 tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3639 /* Subtract the origin of the bands for this tile, to the subwindow */
3640 /* of interest band coordinates, so as to get them relative to the */
3642 win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3643 win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3644 win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3645 win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3646 win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3647 win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3648 win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3649 win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3651 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3652 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3654 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3655 opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3657 /* Compute the tile-resolution-based coordinates for the window of interest */
3659 win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3660 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3662 win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3663 win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3667 win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3668 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3670 win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3671 win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3674 h.win_l_x0 = win_ll_x0;
3675 h.win_l_x1 = win_ll_x1;
3676 h.win_h_x0 = win_hl_x0;
3677 h.win_h_x1 = win_hl_x1;
3678 for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3679 if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3680 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3681 j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3682 opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3683 opj_v8dwt_decode(&h);
3684 if (!opj_sparse_array_int32_write(sa,
3686 win_tr_x1, j + NB_ELTS_V8,
3687 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3688 NB_ELTS_V8, 1, OPJ_TRUE)) {
3689 /* FIXME event manager error callback */
3690 opj_sparse_array_int32_free(sa);
3691 opj_aligned_free(h.wavelet);
3698 ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3699 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3700 j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3701 opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3702 opj_v8dwt_decode(&h);
3703 if (!opj_sparse_array_int32_write(sa,
3706 (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3707 NB_ELTS_V8, 1, OPJ_TRUE)) {
3708 /* FIXME event manager error callback */
3709 opj_sparse_array_int32_free(sa);
3710 opj_aligned_free(h.wavelet);
3715 v.win_l_x0 = win_ll_y0;
3716 v.win_l_x1 = win_ll_y1;
3717 v.win_h_x0 = win_lh_y0;
3718 v.win_h_x1 = win_lh_y1;
3719 for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3720 OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3722 opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3723 opj_v8dwt_decode(&v);
3725 if (!opj_sparse_array_int32_write(sa,
3727 j + nb_elts, win_tr_y1,
3728 (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3729 1, NB_ELTS_V8, OPJ_TRUE)) {
3730 /* FIXME event manager error callback */
3731 opj_sparse_array_int32_free(sa);
3732 opj_aligned_free(h.wavelet);
3739 OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3740 tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3741 tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3742 tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3743 tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3745 1, tr_max->win_x1 - tr_max->win_x0,
3750 opj_sparse_array_int32_free(sa);
3752 opj_aligned_free(h.wavelet);
3757 OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3758 opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3761 if (p_tcd->whole_tile_decoding) {
3762 return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3764 return opj_dwt_decode_partial_97(tilec, numres);