Coverage Report

Created: 2025-08-28 07:06

/src/ghostpdl/openjpeg/src/lib/openjp2/dwt.c
Line
Count
Source (jump to first uncovered line)
1
/*
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.
6
 *
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.
18
 *
19
 * Redistribution and use in source and binary forms, with or without
20
 * modification, are permitted provided that the following conditions
21
 * are met:
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.
27
 *
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.
39
 */
40
41
#include <assert.h>
42
43
#define OPJ_SKIP_POISON
44
#include "opj_includes.h"
45
46
#ifdef __SSE__
47
#include <xmmintrin.h>
48
#endif
49
#ifdef __SSE2__
50
#include <emmintrin.h>
51
#endif
52
#ifdef __SSSE3__
53
#include <tmmintrin.h>
54
#endif
55
#if (defined(__AVX2__) || defined(__AVX512F__))
56
#include <immintrin.h>
57
#endif
58
59
#if defined(__GNUC__)
60
#pragma GCC poison malloc calloc realloc free
61
#endif
62
63
/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
64
/*@{*/
65
66
#define OPJ_WS(i) v->mem[(i)*2]
67
#define OPJ_WD(i) v->mem[(1+(i)*2)]
68
69
#if defined(__AVX512F__)
70
/** Number of int32 values in a AVX512 register */
71
#define VREG_INT_COUNT       16
72
#elif defined(__AVX2__)
73
/** Number of int32 values in a AVX2 register */
74
#define VREG_INT_COUNT       8
75
#else
76
/** Number of int32 values in a SSE2 register */
77
384k
#define VREG_INT_COUNT       4
78
#endif
79
80
/** Number of columns that we can process in parallel in the vertical pass */
81
384k
#define PARALLEL_COLS_53     (2*VREG_INT_COUNT)
82
83
/** @name Local data structures */
84
/*@{*/
85
86
typedef struct dwt_local {
87
    OPJ_INT32* mem;
88
    OPJ_INT32 dn;   /* number of elements in high pass band */
89
    OPJ_INT32 sn;   /* number of elements in low pass band */
90
    OPJ_INT32 cas;  /* 0 = start on even coord, 1 = start on odd coord */
91
} opj_dwt_t;
92
93
1.06G
#define NB_ELTS_V8  8
94
95
typedef union {
96
    OPJ_FLOAT32 f[NB_ELTS_V8];
97
} opj_v8_t;
98
99
typedef struct v8dwt_local {
100
    opj_v8_t*   wavelet ;
101
    OPJ_INT32       dn ;  /* number of elements in high pass band */
102
    OPJ_INT32       sn ;  /* number of elements in low pass band */
103
    OPJ_INT32       cas ; /* 0 = start on even coord, 1 = start on odd coord */
104
    OPJ_UINT32      win_l_x0; /* start coord in low pass band */
105
    OPJ_UINT32      win_l_x1; /* end coord in low pass band */
106
    OPJ_UINT32      win_h_x0; /* start coord in high pass band */
107
    OPJ_UINT32      win_h_x1; /* end coord in high pass band */
108
} opj_v8dwt_t ;
109
110
/* From table F.4 from the standard */
111
static const OPJ_FLOAT32 opj_dwt_alpha =  -1.586134342f;
112
static const OPJ_FLOAT32 opj_dwt_beta  =  -0.052980118f;
113
static const OPJ_FLOAT32 opj_dwt_gamma = 0.882911075f;
114
static const OPJ_FLOAT32 opj_dwt_delta = 0.443506852f;
115
116
static const OPJ_FLOAT32 opj_K      = 1.230174105f;
117
static const OPJ_FLOAT32 opj_invK   = (OPJ_FLOAT32)(1.0 / 1.230174105);
118
119
/*@}*/
120
121
/** @name Local static functions */
122
/*@{*/
123
124
/**
125
Forward lazy transform (horizontal)
126
*/
127
static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
128
                                   OPJ_INT32 * OPJ_RESTRICT b,
129
                                   OPJ_INT32 dn,
130
                                   OPJ_INT32 sn, OPJ_INT32 cas);
131
132
/**
133
Forward 9-7 wavelet transform in 1-D
134
*/
135
static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
136
                                  OPJ_INT32 cas);
137
/**
138
Explicit calculation of the Quantization Stepsizes
139
*/
140
static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
141
                                    opj_stepsize_t *bandno_stepsize);
142
/**
143
Inverse wavelet transform in 2-D.
144
*/
145
static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
146
                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
147
148
static OPJ_BOOL opj_dwt_decode_partial_tile(
149
    opj_tcd_tilecomp_t* tilec,
150
    OPJ_UINT32 numres);
151
152
/* Forward transform, for the vertical pass, processing cols columns */
153
/* where cols <= NB_ELTS_V8 */
154
/* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
155
typedef void (*opj_encode_and_deinterleave_v_fnptr_type)(
156
    void *array,
157
    void *tmp,
158
    OPJ_UINT32 height,
159
    OPJ_BOOL even,
160
    OPJ_UINT32 stride_width,
161
    OPJ_UINT32 cols);
162
163
/* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
164
typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
165
    void *row,
166
    void *tmp,
167
    OPJ_UINT32 width,
168
    OPJ_BOOL even);
169
170
static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
171
        opj_tcd_tilecomp_t * tilec,
172
        opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
173
        opj_encode_and_deinterleave_h_one_row_fnptr_type
174
        p_encode_and_deinterleave_h_one_row);
175
176
static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
177
        OPJ_UINT32 i);
178
179
/* <summary>                             */
180
/* Inverse 9-7 wavelet transform in 1-D. */
181
/* </summary>                            */
182
183
/*@}*/
184
185
/*@}*/
186
187
468M
#define OPJ_S(i) a[(i)*2]
188
468M
#define OPJ_D(i) a[(1+(i)*2)]
189
311M
#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
190
312M
#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
191
/* new */
192
0
#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
193
0
#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
194
195
/* <summary>                                                              */
196
/* This table contains the norms of the 5-3 wavelets for different bands. */
197
/* </summary>                                                             */
198
/* FIXME! the array should really be extended up to 33 resolution levels */
199
/* See https://github.com/uclouvain/openjpeg/issues/493 */
200
static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
201
    {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
202
    {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
203
    {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
204
    {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
205
};
206
207
/* <summary>                                                              */
208
/* This table contains the norms of the 9-7 wavelets for different bands. */
209
/* </summary>                                                             */
210
/* FIXME! the array should really be extended up to 33 resolution levels */
211
/* See https://github.com/uclouvain/openjpeg/issues/493 */
212
static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
213
    {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
214
    {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
215
    {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
216
    {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
217
};
218
219
/*
220
==========================================================
221
   local functions
222
==========================================================
223
*/
224
225
/* <summary>                             */
226
/* Forward lazy transform (horizontal).  */
227
/* </summary>                            */
228
static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
229
                                   OPJ_INT32 * OPJ_RESTRICT b,
230
                                   OPJ_INT32 dn,
231
                                   OPJ_INT32 sn, OPJ_INT32 cas)
232
0
{
233
0
    OPJ_INT32 i;
234
0
    OPJ_INT32 * OPJ_RESTRICT l_dest = b;
235
0
    const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
236
237
0
    for (i = 0; i < sn; ++i) {
238
0
        *l_dest++ = *l_src;
239
0
        l_src += 2;
240
0
    }
241
242
0
    l_dest = b + sn;
243
0
    l_src = a + 1 - cas;
244
245
0
    for (i = 0; i < dn; ++i)  {
246
0
        *l_dest++ = *l_src;
247
0
        l_src += 2;
248
0
    }
249
0
}
250
251
#ifdef STANDARD_SLOW_VERSION
252
/* <summary>                             */
253
/* Inverse lazy transform (horizontal).  */
254
/* </summary>                            */
255
static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
256
1.10M
{
257
1.10M
    const OPJ_INT32 *ai = a;
258
1.10M
    OPJ_INT32 *bi = h->mem + h->cas;
259
1.10M
    OPJ_INT32  i    = h->sn;
260
79.4M
    while (i--) {
261
78.3M
        *bi = *(ai++);
262
78.3M
        bi += 2;
263
78.3M
    }
264
1.10M
    ai  = a + h->sn;
265
1.10M
    bi  = h->mem + 1 - h->cas;
266
1.10M
    i   = h->dn ;
267
78.9M
    while (i--) {
268
77.8M
        *bi = *(ai++);
269
77.8M
        bi += 2;
270
77.8M
    }
271
1.10M
}
272
273
/* <summary>                             */
274
/* Inverse lazy transform (vertical).    */
275
/* </summary>                            */
276
static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
277
997k
{
278
997k
    const OPJ_INT32 *ai = a;
279
997k
    OPJ_INT32 *bi = v->mem + v->cas;
280
997k
    OPJ_INT32  i = v->sn;
281
79.1M
    while (i--) {
282
78.1M
        *bi = *ai;
283
78.1M
        bi += 2;
284
78.1M
        ai += x;
285
78.1M
    }
286
997k
    ai = a + (v->sn * (OPJ_SIZE_T)x);
287
997k
    bi = v->mem + 1 - v->cas;
288
997k
    i = v->dn ;
289
78.9M
    while (i--) {
290
77.9M
        *bi = *ai;
291
77.9M
        bi += 2;
292
77.9M
        ai += x;
293
77.9M
    }
294
997k
}
295
296
#endif /* STANDARD_SLOW_VERSION */
297
298
#ifdef STANDARD_SLOW_VERSION
299
/* <summary>                            */
300
/* Inverse 5-3 wavelet transform in 1-D. */
301
/* </summary>                           */
302
static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
303
                              OPJ_INT32 cas)
304
2.10M
{
305
2.10M
    OPJ_INT32 i;
306
307
2.10M
    if (!cas) {
308
2.10M
        if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
309
158M
            for (i = 0; i < sn; i++) {
310
156M
                OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
311
156M
            }
312
157M
            for (i = 0; i < dn; i++) {
313
155M
                OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
314
155M
            }
315
2.10M
        }
316
2.10M
    } else {
317
0
        if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
318
0
            OPJ_S(0) /= 2;
319
0
        } else {
320
0
            for (i = 0; i < sn; i++) {
321
0
                OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
322
0
            }
323
0
            for (i = 0; i < dn; i++) {
324
0
                OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
325
0
            }
326
0
        }
327
0
    }
328
2.10M
}
329
330
static void opj_dwt_decode_1(const opj_dwt_t *v)
331
2.10M
{
332
2.10M
    opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
333
2.10M
}
334
335
#endif /* STANDARD_SLOW_VERSION */
336
337
#if defined(__AVX512F__)
338
static int32_t loop_short_sse(int32_t len, const int32_t** lf_ptr,
339
                              const int32_t** hf_ptr, int32_t** out_ptr,
340
                              int32_t* prev_even)
341
{
342
    int32_t next_even;
343
    __m128i odd, even_m1, unpack1, unpack2;
344
    const int32_t batch = (len - 2) / 8;
345
    const __m128i two = _mm_set1_epi32(2);
346
347
    for (int32_t i = 0; i < batch; i++) {
348
        const __m128i lf_ = _mm_loadu_si128((__m128i*)(*lf_ptr + 1));
349
        const __m128i hf1_ = _mm_loadu_si128((__m128i*)(*hf_ptr));
350
        const __m128i hf2_ = _mm_loadu_si128((__m128i*)(*hf_ptr + 1));
351
352
        __m128i even = _mm_add_epi32(hf1_, hf2_);
353
        even = _mm_add_epi32(even, two);
354
        even = _mm_srai_epi32(even, 2);
355
        even = _mm_sub_epi32(lf_, even);
356
357
        next_even = _mm_extract_epi32(even, 3);
358
        even_m1 = _mm_bslli_si128(even, 4);
359
        even_m1 = _mm_insert_epi32(even_m1, *prev_even, 0);
360
361
        //out[0] + out[2]
362
        odd = _mm_add_epi32(even_m1, even);
363
        odd = _mm_srai_epi32(odd, 1);
364
        odd = _mm_add_epi32(odd, hf1_);
365
366
        unpack1 = _mm_unpacklo_epi32(even_m1, odd);
367
        unpack2 = _mm_unpackhi_epi32(even_m1, odd);
368
369
        _mm_storeu_si128((__m128i*)(*out_ptr + 0), unpack1);
370
        _mm_storeu_si128((__m128i*)(*out_ptr + 4), unpack2);
371
372
        *prev_even = next_even;
373
374
        *out_ptr += 8;
375
        *lf_ptr += 4;
376
        *hf_ptr += 4;
377
    }
378
    return batch;
379
}
380
#endif
381
382
#if !defined(STANDARD_SLOW_VERSION)
383
static void  opj_idwt53_h_cas0(OPJ_INT32* tmp,
384
                               const OPJ_INT32 sn,
385
                               const OPJ_INT32 len,
386
                               OPJ_INT32* tiledp)
387
{
388
    OPJ_INT32 i, j;
389
    const OPJ_INT32* in_even = &tiledp[0];
390
    const OPJ_INT32* in_odd = &tiledp[sn];
391
392
#ifdef TWO_PASS_VERSION
393
    /* For documentation purpose: performs lifting in two iterations, */
394
    /* but without explicit interleaving */
395
396
    assert(len > 1);
397
398
    /* Even */
399
    tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
400
    for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
401
        tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
402
    }
403
    if (len & 1) { /* if len is odd */
404
        tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
405
    }
406
407
    /* Odd */
408
    for (i = 1, j = 0; i < len - 1; i += 2, j++) {
409
        tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
410
    }
411
    if (!(len & 1)) { /* if len is even */
412
        tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
413
    }
414
#else
415
#if defined(__AVX512F__)
416
    OPJ_INT32* out_ptr = tmp;
417
    int32_t prev_even = in_even[0] - ((in_odd[0] + 1) >> 1);
418
419
    const __m512i permutevar_mask = _mm512_setr_epi32(
420
                                        0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
421
                                        0x0c, 0x0d, 0x0e);
422
    const __m512i store1_perm = _mm512_setr_epi64(0x00, 0x01, 0x08, 0x09, 0x02,
423
                                0x03, 0x0a, 0x0b);
424
    const __m512i store2_perm = _mm512_setr_epi64(0x04, 0x05, 0x0c, 0x0d, 0x06,
425
                                0x07, 0x0e, 0x0f);
426
427
    const __m512i two = _mm512_set1_epi32(2);
428
429
    int32_t simd_batch_512 = (len - 2) / 32;
430
    int32_t leftover;
431
432
    for (i = 0; i < simd_batch_512; i++) {
433
        const __m512i lf_avx2 = _mm512_loadu_si512((__m512i*)(in_even + 1));
434
        const __m512i hf1_avx2 = _mm512_loadu_si512((__m512i*)(in_odd));
435
        const __m512i hf2_avx2 = _mm512_loadu_si512((__m512i*)(in_odd + 1));
436
        int32_t next_even;
437
        __m512i duplicate, even_m1, odd, unpack1, unpack2, store1, store2;
438
439
        __m512i even = _mm512_add_epi32(hf1_avx2, hf2_avx2);
440
        even = _mm512_add_epi32(even, two);
441
        even = _mm512_srai_epi32(even, 2);
442
        even = _mm512_sub_epi32(lf_avx2, even);
443
444
        next_even = _mm_extract_epi32(_mm512_extracti32x4_epi32(even, 3), 3);
445
446
        duplicate = _mm512_set1_epi32(prev_even);
447
        even_m1 = _mm512_permutex2var_epi32(even, permutevar_mask, duplicate);
448
449
        //out[0] + out[2]
450
        odd = _mm512_add_epi32(even_m1, even);
451
        odd = _mm512_srai_epi32(odd, 1);
452
        odd = _mm512_add_epi32(odd, hf1_avx2);
453
454
        unpack1 = _mm512_unpacklo_epi32(even_m1, odd);
455
        unpack2 = _mm512_unpackhi_epi32(even_m1, odd);
456
457
        store1 = _mm512_permutex2var_epi64(unpack1, store1_perm, unpack2);
458
        store2 = _mm512_permutex2var_epi64(unpack1, store2_perm, unpack2);
459
460
        _mm512_storeu_si512(out_ptr, store1);
461
        _mm512_storeu_si512(out_ptr + 16, store2);
462
463
        prev_even = next_even;
464
465
        out_ptr += 32;
466
        in_even += 16;
467
        in_odd += 16;
468
    }
469
470
    leftover = len - simd_batch_512 * 32;
471
    if (leftover > 8) {
472
        leftover -= 8 * loop_short_sse(leftover, &in_even, &in_odd, &out_ptr,
473
                                       &prev_even);
474
    }
475
    out_ptr[0] = prev_even;
476
477
    for (j = 1; j < (leftover - 2); j += 2) {
478
        out_ptr[2] = in_even[1] - ((in_odd[0] + (in_odd[1]) + 2) >> 2);
479
        out_ptr[1] = in_odd[0] + ((out_ptr[0] + out_ptr[2]) >> 1);
480
        in_even++;
481
        in_odd++;
482
        out_ptr += 2;
483
    }
484
485
    if (len & 1) {
486
        out_ptr[2] = in_even[1] - ((in_odd[0] + 1) >> 1);
487
        out_ptr[1] = in_odd[0] + ((out_ptr[0] + out_ptr[2]) >> 1);
488
    } else { //!(len & 1)
489
        out_ptr[1] = in_odd[0] + out_ptr[0];
490
    }
491
#elif  defined(__AVX2__)
492
    OPJ_INT32* out_ptr = tmp;
493
    int32_t prev_even = in_even[0] - ((in_odd[0] + 1) >> 1);
494
495
    const __m256i reg_permutevar_mask_move_right = _mm256_setr_epi32(0x00, 0x00,
496
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06);
497
    const __m256i two = _mm256_set1_epi32(2);
498
499
    int32_t simd_batch = (len - 2) / 16;
500
    int32_t next_even;
501
    __m256i even_m1, odd, unpack1_avx2, unpack2_avx2;
502
503
    for (i = 0; i < simd_batch; i++) {
504
        const __m256i lf_avx2 = _mm256_loadu_si256((__m256i*)(in_even + 1));
505
        const __m256i hf1_avx2 = _mm256_loadu_si256((__m256i*)(in_odd));
506
        const __m256i hf2_avx2 = _mm256_loadu_si256((__m256i*)(in_odd + 1));
507
508
        __m256i even = _mm256_add_epi32(hf1_avx2, hf2_avx2);
509
        even = _mm256_add_epi32(even, two);
510
        even = _mm256_srai_epi32(even, 2);
511
        even = _mm256_sub_epi32(lf_avx2, even);
512
513
        next_even = _mm_extract_epi32(_mm256_extracti128_si256(even, 1), 3);
514
        even_m1 = _mm256_permutevar8x32_epi32(even, reg_permutevar_mask_move_right);
515
        even_m1 = _mm256_blend_epi32(even_m1, _mm256_set1_epi32(prev_even), (1 << 0));
516
517
        //out[0] + out[2]
518
        odd = _mm256_add_epi32(even_m1, even);
519
        odd = _mm256_srai_epi32(odd, 1);
520
        odd = _mm256_add_epi32(odd, hf1_avx2);
521
522
        unpack1_avx2 = _mm256_unpacklo_epi32(even_m1, odd);
523
        unpack2_avx2 = _mm256_unpackhi_epi32(even_m1, odd);
524
525
        _mm_storeu_si128((__m128i*)(out_ptr + 0), _mm256_castsi256_si128(unpack1_avx2));
526
        _mm_storeu_si128((__m128i*)(out_ptr + 4), _mm256_castsi256_si128(unpack2_avx2));
527
        _mm_storeu_si128((__m128i*)(out_ptr + 8), _mm256_extracti128_si256(unpack1_avx2,
528
                         0x1));
529
        _mm_storeu_si128((__m128i*)(out_ptr + 12),
530
                         _mm256_extracti128_si256(unpack2_avx2, 0x1));
531
532
        prev_even = next_even;
533
534
        out_ptr += 16;
535
        in_even += 8;
536
        in_odd += 8;
537
    }
538
    out_ptr[0] = prev_even;
539
    for (j = simd_batch * 16 + 1; j < (len - 2); j += 2) {
540
        out_ptr[2] = in_even[1] - ((in_odd[0] + in_odd[1] + 2) >> 2);
541
        out_ptr[1] = in_odd[0] + ((out_ptr[0] + out_ptr[2]) >> 1);
542
        in_even++;
543
        in_odd++;
544
        out_ptr += 2;
545
    }
546
547
    if (len & 1) {
548
        out_ptr[2] = in_even[1] - ((in_odd[0] + 1) >> 1);
549
        out_ptr[1] = in_odd[0] + ((out_ptr[0] + out_ptr[2]) >> 1);
550
    } else { //!(len & 1)
551
        out_ptr[1] = in_odd[0] + out_ptr[0];
552
    }
553
#else
554
    OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
555
556
    assert(len > 1);
557
558
    /* Improved version of the TWO_PASS_VERSION: */
559
    /* Performs lifting in one single iteration. Saves memory */
560
    /* accesses and explicit interleaving. */
561
    s1n = in_even[0];
562
    d1n = in_odd[0];
563
    s0n = s1n - ((d1n + 1) >> 1);
564
565
    for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
566
        d1c = d1n;
567
        s0c = s0n;
568
569
        s1n = in_even[j];
570
        d1n = in_odd[j];
571
572
        s0n = s1n - ((d1c + d1n + 2) >> 2);
573
574
        tmp[i  ] = s0c;
575
        tmp[i + 1] = opj_int_add_no_overflow(d1c, opj_int_add_no_overflow(s0c,
576
                                             s0n) >> 1);
577
    }
578
579
    tmp[i] = s0n;
580
581
    if (len & 1) {
582
        tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
583
        tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
584
    } else {
585
        tmp[len - 1] = d1n + s0n;
586
    }
587
#endif /*(__AVX512F__ || __AVX2__)*/
588
#endif /*TWO_PASS_VERSION*/
589
    memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
590
}
591
592
static void  opj_idwt53_h_cas1(OPJ_INT32* tmp,
593
                               const OPJ_INT32 sn,
594
                               const OPJ_INT32 len,
595
                               OPJ_INT32* tiledp)
596
{
597
    OPJ_INT32 i, j;
598
    const OPJ_INT32* in_even = &tiledp[sn];
599
    const OPJ_INT32* in_odd = &tiledp[0];
600
601
#ifdef TWO_PASS_VERSION
602
    /* For documentation purpose: performs lifting in two iterations, */
603
    /* but without explicit interleaving */
604
605
    assert(len > 2);
606
607
    /* Odd */
608
    for (i = 1, j = 0; i < len - 1; i += 2, j++) {
609
        tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
610
    }
611
    if (!(len & 1)) {
612
        tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
613
    }
614
615
    /* Even */
616
    tmp[0] = in_even[0] + tmp[1];
617
    for (i = 2, j = 1; i < len - 1; i += 2, j++) {
618
        tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
619
    }
620
    if (len & 1) {
621
        tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
622
    }
623
#else
624
    OPJ_INT32 s1, s2, dc, dn;
625
626
    assert(len > 2);
627
628
    /* Improved version of the TWO_PASS_VERSION: */
629
    /* Performs lifting in one single iteration. Saves memory */
630
    /* accesses and explicit interleaving. */
631
632
    s1 = in_even[1];
633
    dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
634
    tmp[0] = in_even[0] + dc;
635
636
    for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
637
638
        s2 = in_even[j + 1];
639
640
        dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
641
        tmp[i  ] = dc;
642
        tmp[i + 1] = opj_int_add_no_overflow(s1, opj_int_add_no_overflow(dn, dc) >> 1);
643
644
        dc = dn;
645
        s1 = s2;
646
    }
647
648
    tmp[i] = dc;
649
650
    if (!(len & 1)) {
651
        dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
652
        tmp[len - 2] = s1 + ((dn + dc) >> 1);
653
        tmp[len - 1] = dn;
654
    } else {
655
        tmp[len - 1] = s1 + dc;
656
    }
657
#endif
658
    memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
659
}
660
661
662
#endif /* !defined(STANDARD_SLOW_VERSION) */
663
664
/* <summary>                            */
665
/* Inverse 5-3 wavelet transform in 1-D for one row. */
666
/* </summary>                           */
667
/* Performs interleave, inverse wavelet transform and copy back to buffer */
668
static void opj_idwt53_h(const opj_dwt_t *dwt,
669
                         OPJ_INT32* tiledp)
670
1.10M
{
671
1.10M
#ifdef STANDARD_SLOW_VERSION
672
    /* For documentation purpose */
673
1.10M
    opj_dwt_interleave_h(dwt, tiledp);
674
1.10M
    opj_dwt_decode_1(dwt);
675
1.10M
    memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
676
#else
677
    const OPJ_INT32 sn = dwt->sn;
678
    const OPJ_INT32 len = sn + dwt->dn;
679
    if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
680
        if (len > 1) {
681
            opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
682
        } else {
683
            /* Unmodified value */
684
        }
685
    } else { /* Left-most sample is on odd coordinate */
686
        if (len == 1) {
687
            tiledp[0] /= 2;
688
        } else if (len == 2) {
689
            OPJ_INT32* out = dwt->mem;
690
            const OPJ_INT32* in_even = &tiledp[sn];
691
            const OPJ_INT32* in_odd = &tiledp[0];
692
            out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
693
            out[0] = in_even[0] + out[1];
694
            memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
695
        } else if (len > 2) {
696
            opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
697
        }
698
    }
699
#endif
700
1.10M
}
701
702
#if (defined(__SSE2__) || defined(__AVX2__) || defined(__AVX512F__)) && !defined(STANDARD_SLOW_VERSION)
703
704
/* Conveniency macros to improve the readability of the formulas */
705
#if defined(__AVX512F__)
706
#define VREG        __m512i
707
#define LOAD_CST(x) _mm512_set1_epi32(x)
708
#define LOAD(x)     _mm512_loadu_si512((const VREG*)(x))
709
#define LOADU(x)    _mm512_loadu_si512((const VREG*)(x))
710
#define STORE(x,y)  _mm512_storeu_si512((VREG*)(x),(y))
711
#define STOREU(x,y) _mm512_storeu_si512((VREG*)(x),(y))
712
#define ADD(x,y)    _mm512_add_epi32((x),(y))
713
#define SUB(x,y)    _mm512_sub_epi32((x),(y))
714
#define SAR(x,y)    _mm512_srai_epi32((x),(y))
715
#elif defined(__AVX2__)
716
#define VREG        __m256i
717
#define LOAD_CST(x) _mm256_set1_epi32(x)
718
#define LOAD(x)     _mm256_load_si256((const VREG*)(x))
719
#define LOADU(x)    _mm256_loadu_si256((const VREG*)(x))
720
#define STORE(x,y)  _mm256_store_si256((VREG*)(x),(y))
721
#define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
722
#define ADD(x,y)    _mm256_add_epi32((x),(y))
723
#define SUB(x,y)    _mm256_sub_epi32((x),(y))
724
#define SAR(x,y)    _mm256_srai_epi32((x),(y))
725
#else
726
#define VREG        __m128i
727
#define LOAD_CST(x) _mm_set1_epi32(x)
728
#define LOAD(x)     _mm_load_si128((const VREG*)(x))
729
#define LOADU(x)    _mm_loadu_si128((const VREG*)(x))
730
#define STORE(x,y)  _mm_store_si128((VREG*)(x),(y))
731
#define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
732
#define ADD(x,y)    _mm_add_epi32((x),(y))
733
#define SUB(x,y)    _mm_sub_epi32((x),(y))
734
#define SAR(x,y)    _mm_srai_epi32((x),(y))
735
#endif
736
#define ADD3(x,y,z) ADD(ADD(x,y),z)
737
738
static
739
void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
740
                               const OPJ_INT32* tmp,
741
                               OPJ_INT32 len,
742
                               OPJ_SIZE_T stride)
743
{
744
    OPJ_INT32 i;
745
    for (i = 0; i < len; ++i) {
746
        /* A memcpy(&tiledp_col[i * stride + 0],
747
                    &tmp[PARALLEL_COLS_53 * i + 0],
748
                    PARALLEL_COLS_53 * sizeof(OPJ_INT32))
749
           would do but would be a tiny bit slower.
750
           We can take here advantage of our knowledge of alignment */
751
        STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
752
               LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
753
        STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
754
               LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
755
    }
756
}
757
758
/** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
759
 * 16 in AVX2, when top-most pixel is on even coordinate */
760
static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
761
    OPJ_INT32* tmp,
762
    const OPJ_INT32 sn,
763
    const OPJ_INT32 len,
764
    OPJ_INT32* tiledp_col,
765
    const OPJ_SIZE_T stride)
766
{
767
    const OPJ_INT32* in_even = &tiledp_col[0];
768
    const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
769
770
    OPJ_INT32 i;
771
    OPJ_SIZE_T j;
772
    VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
773
    VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
774
    const VREG two = LOAD_CST(2);
775
776
    assert(len > 1);
777
#if defined(__AVX512F__)
778
    assert(PARALLEL_COLS_53 == 32);
779
    assert(VREG_INT_COUNT == 16);
780
#elif defined(__AVX2__)
781
    assert(PARALLEL_COLS_53 == 16);
782
    assert(VREG_INT_COUNT == 8);
783
#else
784
    assert(PARALLEL_COLS_53 == 8);
785
    assert(VREG_INT_COUNT == 4);
786
#endif
787
788
//For AVX512 code aligned load/store is set to it's unaligned equivalents
789
#if !defined(__AVX512F__)
790
    /* Note: loads of input even/odd values must be done in a unaligned */
791
    /* fashion. But stores in tmp can be done with aligned store, since */
792
    /* the temporary buffer is properly aligned */
793
    assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
794
#endif
795
796
    s1n_0 = LOADU(in_even + 0);
797
    s1n_1 = LOADU(in_even + VREG_INT_COUNT);
798
    d1n_0 = LOADU(in_odd);
799
    d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
800
801
    /* s0n = s1n - ((d1n + 1) >> 1); <==> */
802
    /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
803
    s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
804
    s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
805
806
    for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
807
        d1c_0 = d1n_0;
808
        s0c_0 = s0n_0;
809
        d1c_1 = d1n_1;
810
        s0c_1 = s0n_1;
811
812
        s1n_0 = LOADU(in_even + j * stride);
813
        s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
814
        d1n_0 = LOADU(in_odd + j * stride);
815
        d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
816
817
        /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
818
        s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
819
        s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
820
821
        STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
822
        STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
823
824
        /* d1c + ((s0c + s0n) >> 1) */
825
        STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
826
              ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
827
        STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
828
              ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
829
    }
830
831
    STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
832
    STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
833
834
    if (len & 1) {
835
        VREG tmp_len_minus_1;
836
        s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
837
        /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
838
        tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
839
        STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
840
        /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
841
        STORE(tmp + PARALLEL_COLS_53 * (len - 2),
842
              ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
843
844
        s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
845
        /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
846
        tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
847
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
848
              tmp_len_minus_1);
849
        /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
850
        STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
851
              ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
852
853
854
    } else {
855
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
856
              ADD(d1n_0, s0n_0));
857
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
858
              ADD(d1n_1, s0n_1));
859
    }
860
861
    opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
862
}
863
864
865
/** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
866
 * 16 in AVX2, when top-most pixel is on odd coordinate */
867
static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
868
    OPJ_INT32* tmp,
869
    const OPJ_INT32 sn,
870
    const OPJ_INT32 len,
871
    OPJ_INT32* tiledp_col,
872
    const OPJ_SIZE_T stride)
873
{
874
    OPJ_INT32 i;
875
    OPJ_SIZE_T j;
876
877
    VREG s1_0, s2_0, dc_0, dn_0;
878
    VREG s1_1, s2_1, dc_1, dn_1;
879
    const VREG two = LOAD_CST(2);
880
881
    const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
882
    const OPJ_INT32* in_odd = &tiledp_col[0];
883
884
    assert(len > 2);
885
#if defined(__AVX512F__)
886
    assert(PARALLEL_COLS_53 == 32);
887
    assert(VREG_INT_COUNT == 16);
888
#elif defined(__AVX2__)
889
    assert(PARALLEL_COLS_53 == 16);
890
    assert(VREG_INT_COUNT == 8);
891
#else
892
    assert(PARALLEL_COLS_53 == 8);
893
    assert(VREG_INT_COUNT == 4);
894
#endif
895
896
//For AVX512 code aligned load/store is set to it's unaligned equivalents
897
#if !defined(__AVX512F__)
898
    /* Note: loads of input even/odd values must be done in a unaligned */
899
    /* fashion. But stores in tmp can be done with aligned store, since */
900
    /* the temporary buffer is properly aligned */
901
    assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
902
#endif
903
904
    s1_0 = LOADU(in_even + stride);
905
    /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
906
    dc_0 = SUB(LOADU(in_odd + 0),
907
               SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
908
    STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
909
910
    s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
911
    /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
912
    dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
913
               SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
914
    STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
915
          ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
916
917
    for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
918
919
        s2_0 = LOADU(in_even + (j + 1) * stride);
920
        s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
921
922
        /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
923
        dn_0 = SUB(LOADU(in_odd + j * stride),
924
                   SAR(ADD3(s1_0, s2_0, two), 2));
925
        dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
926
                   SAR(ADD3(s1_1, s2_1, two), 2));
927
928
        STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
929
        STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
930
931
        /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
932
        STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
933
              ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
934
        STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
935
              ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
936
937
        dc_0 = dn_0;
938
        s1_0 = s2_0;
939
        dc_1 = dn_1;
940
        s1_1 = s2_1;
941
    }
942
    STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
943
    STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
944
945
    if (!(len & 1)) {
946
        /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
947
        dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
948
                   SAR(ADD3(s1_0, s1_0, two), 2));
949
        dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
950
                   SAR(ADD3(s1_1, s1_1, two), 2));
951
952
        /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
953
        STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
954
              ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
955
        STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
956
              ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
957
958
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
959
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
960
    } else {
961
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
962
        STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
963
              ADD(s1_1, dc_1));
964
    }
965
966
    opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
967
}
968
969
#undef VREG
970
#undef LOAD_CST
971
#undef LOADU
972
#undef LOAD
973
#undef STORE
974
#undef STOREU
975
#undef ADD
976
#undef ADD3
977
#undef SUB
978
#undef SAR
979
980
#endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
981
982
#if !defined(STANDARD_SLOW_VERSION)
983
/** Vertical inverse 5x3 wavelet transform for one column, when top-most
984
 * pixel is on even coordinate */
985
static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
986
                             const OPJ_INT32 sn,
987
                             const OPJ_INT32 len,
988
                             OPJ_INT32* tiledp_col,
989
                             const OPJ_SIZE_T stride)
990
{
991
    OPJ_INT32 i, j;
992
    OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
993
994
    assert(len > 1);
995
996
    /* Performs lifting in one single iteration. Saves memory */
997
    /* accesses and explicit interleaving. */
998
999
    s1n = tiledp_col[0];
1000
    d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
1001
    s0n = s1n - ((d1n + 1) >> 1);
1002
1003
    for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
1004
        d1c = d1n;
1005
        s0c = s0n;
1006
1007
        s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
1008
        d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
1009
1010
        s0n = opj_int_sub_no_overflow(s1n,
1011
                                      opj_int_add_no_overflow(opj_int_add_no_overflow(d1c, d1n), 2) >> 2);
1012
1013
        tmp[i  ] = s0c;
1014
        tmp[i + 1] = opj_int_add_no_overflow(d1c, opj_int_add_no_overflow(s0c,
1015
                                             s0n) >> 1);
1016
    }
1017
1018
    tmp[i] = s0n;
1019
1020
    if (len & 1) {
1021
        tmp[len - 1] =
1022
            tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
1023
            ((d1n + 1) >> 1);
1024
        tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
1025
    } else {
1026
        tmp[len - 1] = d1n + s0n;
1027
    }
1028
1029
    for (i = 0; i < len; ++i) {
1030
        tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
1031
    }
1032
}
1033
1034
1035
/** Vertical inverse 5x3 wavelet transform for one column, when top-most
1036
 * pixel is on odd coordinate */
1037
static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
1038
                             const OPJ_INT32 sn,
1039
                             const OPJ_INT32 len,
1040
                             OPJ_INT32* tiledp_col,
1041
                             const OPJ_SIZE_T stride)
1042
{
1043
    OPJ_INT32 i, j;
1044
    OPJ_INT32 s1, s2, dc, dn;
1045
    const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
1046
    const OPJ_INT32* in_odd = &tiledp_col[0];
1047
1048
    assert(len > 2);
1049
1050
    /* Performs lifting in one single iteration. Saves memory */
1051
    /* accesses and explicit interleaving. */
1052
1053
    s1 = in_even[stride];
1054
    dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
1055
    tmp[0] = in_even[0] + dc;
1056
    for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
1057
1058
        s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
1059
1060
        dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
1061
        tmp[i  ] = dc;
1062
        tmp[i + 1] = s1 + ((dn + dc) >> 1);
1063
1064
        dc = dn;
1065
        s1 = s2;
1066
    }
1067
    tmp[i] = dc;
1068
    if (!(len & 1)) {
1069
        dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
1070
        tmp[len - 2] = s1 + ((dn + dc) >> 1);
1071
        tmp[len - 1] = dn;
1072
    } else {
1073
        tmp[len - 1] = s1 + dc;
1074
    }
1075
1076
    for (i = 0; i < len; ++i) {
1077
        tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
1078
    }
1079
}
1080
#endif /* !defined(STANDARD_SLOW_VERSION) */
1081
1082
/* <summary>                            */
1083
/* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
1084
/* </summary>                           */
1085
/* Performs interleave, inverse wavelet transform and copy back to buffer */
1086
static void opj_idwt53_v(const opj_dwt_t *dwt,
1087
                         OPJ_INT32* tiledp_col,
1088
                         OPJ_SIZE_T stride,
1089
                         OPJ_INT32 nb_cols)
1090
129k
{
1091
129k
#ifdef STANDARD_SLOW_VERSION
1092
    /* For documentation purpose */
1093
129k
    OPJ_INT32 k, c;
1094
1.12M
    for (c = 0; c < nb_cols; c ++) {
1095
997k
        opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
1096
997k
        opj_dwt_decode_1(dwt);
1097
157M
        for (k = 0; k < dwt->sn + dwt->dn; ++k) {
1098
156M
            tiledp_col[c + k * stride] = dwt->mem[k];
1099
156M
        }
1100
997k
    }
1101
#else
1102
    const OPJ_INT32 sn = dwt->sn;
1103
    const OPJ_INT32 len = sn + dwt->dn;
1104
    if (dwt->cas == 0) {
1105
        /* If len == 1, unmodified value */
1106
1107
#if (defined(__SSE2__) || defined(__AVX2__))
1108
        if (len > 1 && nb_cols == PARALLEL_COLS_53) {
1109
            /* Same as below general case, except that thanks to SSE2/AVX2 */
1110
            /* we can efficiently process 8/16 columns in parallel */
1111
            opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1112
            return;
1113
        }
1114
#endif
1115
        if (len > 1) {
1116
            OPJ_INT32 c;
1117
            for (c = 0; c < nb_cols; c++, tiledp_col++) {
1118
                opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
1119
            }
1120
            return;
1121
        }
1122
    } else {
1123
        if (len == 1) {
1124
            OPJ_INT32 c;
1125
            for (c = 0; c < nb_cols; c++, tiledp_col++) {
1126
                tiledp_col[0] /= 2;
1127
            }
1128
            return;
1129
        }
1130
1131
        if (len == 2) {
1132
            OPJ_INT32 c;
1133
            OPJ_INT32* out = dwt->mem;
1134
            for (c = 0; c < nb_cols; c++, tiledp_col++) {
1135
                OPJ_INT32 i;
1136
                const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
1137
                const OPJ_INT32* in_odd = &tiledp_col[0];
1138
1139
                out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
1140
                out[0] = in_even[0] + out[1];
1141
1142
                for (i = 0; i < len; ++i) {
1143
                    tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
1144
                }
1145
            }
1146
1147
            return;
1148
        }
1149
1150
#if (defined(__SSE2__) || defined(__AVX2__))
1151
        if (len > 2 && nb_cols == PARALLEL_COLS_53) {
1152
            /* Same as below general case, except that thanks to SSE2/AVX2 */
1153
            /* we can efficiently process 8/16 columns in parallel */
1154
            opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
1155
            return;
1156
        }
1157
#endif
1158
        if (len > 2) {
1159
            OPJ_INT32 c;
1160
            for (c = 0; c < nb_cols; c++, tiledp_col++) {
1161
                opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
1162
            }
1163
            return;
1164
        }
1165
    }
1166
#endif
1167
129k
}
1168
1169
#if 0
1170
static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
1171
                                 OPJ_UINT32 end,
1172
                                 const OPJ_FLOAT32 c)
1173
{
1174
    OPJ_UINT32 i = 0;
1175
    for (; i < end; ++i) {
1176
        fw[0] *= c;
1177
        fw += 2;
1178
    }
1179
}
1180
#else
1181
static void opj_dwt_encode_step1_combined(OPJ_FLOAT32* fw,
1182
        OPJ_UINT32 iters_c1,
1183
        OPJ_UINT32 iters_c2,
1184
        const OPJ_FLOAT32 c1,
1185
        const OPJ_FLOAT32 c2)
1186
0
{
1187
0
    OPJ_UINT32 i = 0;
1188
0
    const OPJ_UINT32 iters_common =  opj_uint_min(iters_c1, iters_c2);
1189
0
    assert((((OPJ_SIZE_T)fw) & 0xf) == 0);
1190
0
    assert(opj_int_abs((OPJ_INT32)iters_c1 - (OPJ_INT32)iters_c2) <= 1);
1191
0
    for (; i + 3 < iters_common; i += 4) {
1192
0
#ifdef __SSE__
1193
0
        const __m128 vcst = _mm_set_ps(c2, c1, c2, c1);
1194
0
        *(__m128*)fw = _mm_mul_ps(*(__m128*)fw, vcst);
1195
0
        *(__m128*)(fw + 4) = _mm_mul_ps(*(__m128*)(fw + 4), vcst);
1196
#else
1197
        fw[0] *= c1;
1198
        fw[1] *= c2;
1199
        fw[2] *= c1;
1200
        fw[3] *= c2;
1201
        fw[4] *= c1;
1202
        fw[5] *= c2;
1203
        fw[6] *= c1;
1204
        fw[7] *= c2;
1205
#endif
1206
0
        fw += 8;
1207
0
    }
1208
0
    for (; i < iters_common; i++) {
1209
0
        fw[0] *= c1;
1210
0
        fw[1] *= c2;
1211
0
        fw += 2;
1212
0
    }
1213
0
    if (i < iters_c1) {
1214
0
        fw[0] *= c1;
1215
0
    } else if (i < iters_c2) {
1216
0
        fw[1] *= c2;
1217
0
    }
1218
0
}
1219
1220
#endif
1221
1222
static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1223
                                 OPJ_UINT32 end,
1224
                                 OPJ_UINT32 m,
1225
                                 OPJ_FLOAT32 c)
1226
0
{
1227
0
    OPJ_UINT32 i;
1228
0
    OPJ_UINT32 imax = opj_uint_min(end, m);
1229
0
    if (imax > 0) {
1230
0
        fw[-1] += (fl[0] + fw[0]) * c;
1231
0
        fw += 2;
1232
0
        i = 1;
1233
0
        for (; i + 3 < imax; i += 4) {
1234
0
            fw[-1] += (fw[-2] + fw[0]) * c;
1235
0
            fw[1] += (fw[0] + fw[2]) * c;
1236
0
            fw[3] += (fw[2] + fw[4]) * c;
1237
0
            fw[5] += (fw[4] + fw[6]) * c;
1238
0
            fw += 8;
1239
0
        }
1240
0
        for (; i < imax; ++i) {
1241
0
            fw[-1] += (fw[-2] + fw[0]) * c;
1242
0
            fw += 2;
1243
0
        }
1244
0
    }
1245
0
    if (m < end) {
1246
0
        assert(m + 1 == end);
1247
0
        fw[-1] += (2 * fw[-2]) * c;
1248
0
    }
1249
0
}
1250
1251
static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
1252
                                  OPJ_INT32 cas)
1253
0
{
1254
0
    OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
1255
0
    OPJ_INT32 a, b;
1256
0
    assert(dn + sn > 1);
1257
0
    if (cas == 0) {
1258
0
        a = 0;
1259
0
        b = 1;
1260
0
    } else {
1261
0
        a = 1;
1262
0
        b = 0;
1263
0
    }
1264
0
    opj_dwt_encode_step2(w + a, w + b + 1,
1265
0
                         (OPJ_UINT32)dn,
1266
0
                         (OPJ_UINT32)opj_int_min(dn, sn - b),
1267
0
                         opj_dwt_alpha);
1268
0
    opj_dwt_encode_step2(w + b, w + a + 1,
1269
0
                         (OPJ_UINT32)sn,
1270
0
                         (OPJ_UINT32)opj_int_min(sn, dn - a),
1271
0
                         opj_dwt_beta);
1272
0
    opj_dwt_encode_step2(w + a, w + b + 1,
1273
0
                         (OPJ_UINT32)dn,
1274
0
                         (OPJ_UINT32)opj_int_min(dn, sn - b),
1275
0
                         opj_dwt_gamma);
1276
0
    opj_dwt_encode_step2(w + b, w + a + 1,
1277
0
                         (OPJ_UINT32)sn,
1278
0
                         (OPJ_UINT32)opj_int_min(sn, dn - a),
1279
0
                         opj_dwt_delta);
1280
#if 0
1281
    opj_dwt_encode_step1(w + b, (OPJ_UINT32)dn,
1282
                         opj_K);
1283
    opj_dwt_encode_step1(w + a, (OPJ_UINT32)sn,
1284
                         opj_invK);
1285
#else
1286
0
    if (a == 0) {
1287
0
        opj_dwt_encode_step1_combined(w,
1288
0
                                      (OPJ_UINT32)sn,
1289
0
                                      (OPJ_UINT32)dn,
1290
0
                                      opj_invK,
1291
0
                                      opj_K);
1292
0
    } else {
1293
0
        opj_dwt_encode_step1_combined(w,
1294
0
                                      (OPJ_UINT32)dn,
1295
0
                                      (OPJ_UINT32)sn,
1296
0
                                      opj_K,
1297
0
                                      opj_invK);
1298
0
    }
1299
0
#endif
1300
0
}
1301
1302
static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
1303
                                    opj_stepsize_t *bandno_stepsize)
1304
0
{
1305
0
    OPJ_INT32 p, n;
1306
0
    p = opj_int_floorlog2(stepsize) - 13;
1307
0
    n = 11 - opj_int_floorlog2(stepsize);
1308
0
    bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
1309
0
    bandno_stepsize->expn = numbps - p;
1310
0
}
1311
1312
/*
1313
==========================================================
1314
   DWT interface
1315
==========================================================
1316
*/
1317
1318
/** Process one line for the horizontal pass of the 5x3 forward transform */
1319
static
1320
void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
1321
        void* tmpIn,
1322
        OPJ_UINT32 width,
1323
        OPJ_BOOL even)
1324
0
{
1325
0
    OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
1326
0
    OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
1327
0
    const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1328
0
    const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1329
1330
0
    if (even) {
1331
0
        if (width > 1) {
1332
0
            OPJ_INT32 i;
1333
0
            for (i = 0; i < sn - 1; i++) {
1334
0
                tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1);
1335
0
            }
1336
0
            if ((width % 2) == 0) {
1337
0
                tmp[sn + i] = row[2 * i + 1] - row[(i) * 2];
1338
0
            }
1339
0
            row[0] += (tmp[sn] + tmp[sn] + 2) >> 2;
1340
0
            for (i = 1; i < dn; i++) {
1341
0
                row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2);
1342
0
            }
1343
0
            if ((width % 2) == 1) {
1344
0
                row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2);
1345
0
            }
1346
0
            memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1347
0
        }
1348
0
    } else {
1349
0
        if (width == 1) {
1350
0
            row[0] *= 2;
1351
0
        } else {
1352
0
            OPJ_INT32 i;
1353
0
            tmp[sn + 0] = row[0] - row[1];
1354
0
            for (i = 1; i < sn; i++) {
1355
0
                tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1);
1356
0
            }
1357
0
            if ((width % 2) == 1) {
1358
0
                tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1];
1359
0
            }
1360
1361
0
            for (i = 0; i < dn - 1; i++) {
1362
0
                row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2);
1363
0
            }
1364
0
            if ((width % 2) == 0) {
1365
0
                row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2);
1366
0
            }
1367
0
            memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
1368
0
        }
1369
0
    }
1370
0
}
1371
1372
/** Process one line for the horizontal pass of the 9x7 forward transform */
1373
static
1374
void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
1375
        void* tmpIn,
1376
        OPJ_UINT32 width,
1377
        OPJ_BOOL even)
1378
0
{
1379
0
    OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
1380
0
    OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
1381
0
    const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
1382
0
    const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
1383
0
    if (width == 1) {
1384
0
        return;
1385
0
    }
1386
0
    memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
1387
0
    opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
1388
0
    opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
1389
0
                           (OPJ_INT32 * OPJ_RESTRICT)row,
1390
0
                           dn, sn, even ? 0 : 1);
1391
0
}
1392
1393
typedef struct {
1394
    opj_dwt_t h;
1395
    OPJ_UINT32 rw; /* Width of the resolution to process */
1396
    OPJ_UINT32 w; /* Width of tiledp */
1397
    OPJ_INT32 * OPJ_RESTRICT tiledp;
1398
    OPJ_UINT32 min_j;
1399
    OPJ_UINT32 max_j;
1400
    opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
1401
} opj_dwt_encode_h_job_t;
1402
1403
static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
1404
0
{
1405
0
    OPJ_UINT32 j;
1406
0
    opj_dwt_encode_h_job_t* job;
1407
0
    (void)tls;
1408
1409
0
    job = (opj_dwt_encode_h_job_t*)user_data;
1410
0
    for (j = job->min_j; j < job->max_j; j++) {
1411
0
        OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
1412
0
        (*job->p_function)(aj, job->h.mem, job->rw,
1413
0
                           job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
1414
0
    }
1415
1416
0
    opj_aligned_free(job->h.mem);
1417
0
    opj_free(job);
1418
0
}
1419
1420
typedef struct {
1421
    opj_dwt_t v;
1422
    OPJ_UINT32 rh;
1423
    OPJ_UINT32 w;
1424
    OPJ_INT32 * OPJ_RESTRICT tiledp;
1425
    OPJ_UINT32 min_j;
1426
    OPJ_UINT32 max_j;
1427
    opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v;
1428
} opj_dwt_encode_v_job_t;
1429
1430
static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
1431
0
{
1432
0
    OPJ_UINT32 j;
1433
0
    opj_dwt_encode_v_job_t* job;
1434
0
    (void)tls;
1435
1436
0
    job = (opj_dwt_encode_v_job_t*)user_data;
1437
0
    for (j = job->min_j; j + NB_ELTS_V8 - 1 < job->max_j; j += NB_ELTS_V8) {
1438
0
        (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1439
0
                                            job->v.mem,
1440
0
                                            job->rh,
1441
0
                                            job->v.cas == 0,
1442
0
                                            job->w,
1443
0
                                            NB_ELTS_V8);
1444
0
    }
1445
0
    if (j < job->max_j) {
1446
0
        (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
1447
0
                                            job->v.mem,
1448
0
                                            job->rh,
1449
0
                                            job->v.cas == 0,
1450
0
                                            job->w,
1451
0
                                            job->max_j - j);
1452
0
    }
1453
1454
0
    opj_aligned_free(job->v.mem);
1455
0
    opj_free(job);
1456
0
}
1457
1458
/** Fetch up to cols <= NB_ELTS_V8 for each line, and put them in tmpOut */
1459
/* that has a NB_ELTS_V8 interleave factor. */
1460
static void opj_dwt_fetch_cols_vertical_pass(const void *arrayIn,
1461
        void *tmpOut,
1462
        OPJ_UINT32 height,
1463
        OPJ_UINT32 stride_width,
1464
        OPJ_UINT32 cols)
1465
0
{
1466
0
    const OPJ_INT32* OPJ_RESTRICT array = (const OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1467
0
    OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpOut;
1468
0
    if (cols == NB_ELTS_V8) {
1469
0
        OPJ_UINT32 k;
1470
0
        for (k = 0; k < height; ++k) {
1471
0
            memcpy(tmp + NB_ELTS_V8 * k,
1472
0
                   array + k * stride_width,
1473
0
                   NB_ELTS_V8 * sizeof(OPJ_INT32));
1474
0
        }
1475
0
    } else {
1476
0
        OPJ_UINT32 k;
1477
0
        for (k = 0; k < height; ++k) {
1478
0
            OPJ_UINT32 c;
1479
0
            for (c = 0; c < cols; c++) {
1480
0
                tmp[NB_ELTS_V8 * k + c] = array[c + k * stride_width];
1481
0
            }
1482
0
            for (; c < NB_ELTS_V8; c++) {
1483
0
                tmp[NB_ELTS_V8 * k + c] = 0;
1484
0
            }
1485
0
        }
1486
0
    }
1487
0
}
1488
1489
/* Deinterleave result of forward transform, where cols <= NB_ELTS_V8 */
1490
/* and src contains NB_ELTS_V8 consecutive values for up to NB_ELTS_V8 */
1491
/* columns. */
1492
static INLINE void opj_dwt_deinterleave_v_cols(
1493
    const OPJ_INT32 * OPJ_RESTRICT src,
1494
    OPJ_INT32 * OPJ_RESTRICT dst,
1495
    OPJ_INT32 dn,
1496
    OPJ_INT32 sn,
1497
    OPJ_UINT32 stride_width,
1498
    OPJ_INT32 cas,
1499
    OPJ_UINT32 cols)
1500
0
{
1501
0
    OPJ_INT32 k;
1502
0
    OPJ_INT32 i = sn;
1503
0
    OPJ_INT32 * OPJ_RESTRICT l_dest = dst;
1504
0
    const OPJ_INT32 * OPJ_RESTRICT l_src = src + cas * NB_ELTS_V8;
1505
0
    OPJ_UINT32 c;
1506
1507
0
    for (k = 0; k < 2; k++) {
1508
0
        while (i--) {
1509
0
            if (cols == NB_ELTS_V8) {
1510
0
                memcpy(l_dest, l_src, NB_ELTS_V8 * sizeof(OPJ_INT32));
1511
0
            } else {
1512
0
                c = 0;
1513
0
                switch (cols) {
1514
0
                case 7:
1515
0
                    l_dest[c] = l_src[c];
1516
0
                    c++; /* fallthru */
1517
0
                case 6:
1518
0
                    l_dest[c] = l_src[c];
1519
0
                    c++; /* fallthru */
1520
0
                case 5:
1521
0
                    l_dest[c] = l_src[c];
1522
0
                    c++; /* fallthru */
1523
0
                case 4:
1524
0
                    l_dest[c] = l_src[c];
1525
0
                    c++; /* fallthru */
1526
0
                case 3:
1527
0
                    l_dest[c] = l_src[c];
1528
0
                    c++; /* fallthru */
1529
0
                case 2:
1530
0
                    l_dest[c] = l_src[c];
1531
0
                    c++; /* fallthru */
1532
0
                default:
1533
0
                    l_dest[c] = l_src[c];
1534
0
                    break;
1535
0
                }
1536
0
            }
1537
0
            l_dest += stride_width;
1538
0
            l_src += 2 * NB_ELTS_V8;
1539
0
        }
1540
1541
0
        l_dest = dst + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)stride_width;
1542
0
        l_src = src + (1 - cas) * NB_ELTS_V8;
1543
0
        i = dn;
1544
0
    }
1545
0
}
1546
1547
1548
/* Forward 5-3 transform, for the vertical pass, processing cols columns */
1549
/* where cols <= NB_ELTS_V8 */
1550
static void opj_dwt_encode_and_deinterleave_v(
1551
    void *arrayIn,
1552
    void *tmpIn,
1553
    OPJ_UINT32 height,
1554
    OPJ_BOOL even,
1555
    OPJ_UINT32 stride_width,
1556
    OPJ_UINT32 cols)
1557
0
{
1558
0
    OPJ_INT32* OPJ_RESTRICT array = (OPJ_INT32 * OPJ_RESTRICT)arrayIn;
1559
0
    OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpIn;
1560
0
    const OPJ_UINT32 sn = (height + (even ? 1 : 0)) >> 1;
1561
0
    const OPJ_UINT32 dn = height - sn;
1562
1563
0
    opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
1564
1565
0
#define OPJ_Sc(i) tmp[(i)*2* NB_ELTS_V8 + c]
1566
0
#define OPJ_Dc(i) tmp[((1+(i)*2))* NB_ELTS_V8 + c]
1567
1568
0
#ifdef __SSE2__
1569
0
    if (height == 1) {
1570
0
        if (!even) {
1571
0
            OPJ_UINT32 c;
1572
0
            for (c = 0; c < NB_ELTS_V8; c++) {
1573
0
                tmp[c] *= 2;
1574
0
            }
1575
0
        }
1576
0
    } else if (even) {
1577
0
        OPJ_UINT32 c;
1578
0
        OPJ_UINT32 i;
1579
0
        i = 0;
1580
0
        if (i + 1 < sn) {
1581
0
            __m128i xmm_Si_0 = *(const __m128i*)(tmp + 4 * 0);
1582
0
            __m128i xmm_Si_1 = *(const __m128i*)(tmp + 4 * 1);
1583
0
            for (; i + 1 < sn; i++) {
1584
0
                __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1585
0
                                                       (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1586
0
                __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1587
0
                                                       (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1588
0
                __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1589
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1590
0
                __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1591
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1592
0
                xmm_Di_0 = _mm_sub_epi32(xmm_Di_0,
1593
0
                                         _mm_srai_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), 1));
1594
0
                xmm_Di_1 = _mm_sub_epi32(xmm_Di_1,
1595
0
                                         _mm_srai_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), 1));
1596
0
                *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) =  xmm_Di_0;
1597
0
                *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) =  xmm_Di_1;
1598
0
                xmm_Si_0 = xmm_Sip1_0;
1599
0
                xmm_Si_1 = xmm_Sip1_1;
1600
0
            }
1601
0
        }
1602
0
        if (((height) % 2) == 0) {
1603
0
            for (c = 0; c < NB_ELTS_V8; c++) {
1604
0
                OPJ_Dc(i) -= OPJ_Sc(i);
1605
0
            }
1606
0
        }
1607
0
        for (c = 0; c < NB_ELTS_V8; c++) {
1608
0
            OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1609
0
        }
1610
0
        i = 1;
1611
0
        if (i < dn) {
1612
0
            __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1613
0
                                                   (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1614
0
            __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1615
0
                                                   (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1616
0
            const __m128i xmm_two = _mm_set1_epi32(2);
1617
0
            for (; i < dn; i++) {
1618
0
                __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1619
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1620
0
                __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1621
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1622
0
                __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1623
0
                                                     (i * 2) * NB_ELTS_V8 + 4 * 0);
1624
0
                __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1625
0
                                                     (i * 2) * NB_ELTS_V8 + 4 * 1);
1626
0
                xmm_Si_0 = _mm_add_epi32(xmm_Si_0,
1627
0
                                         _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_0, xmm_Di_0), xmm_two), 2));
1628
0
                xmm_Si_1 = _mm_add_epi32(xmm_Si_1,
1629
0
                                         _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_1, xmm_Di_1), xmm_two), 2));
1630
0
                *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1631
0
                *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1632
0
                xmm_Dim1_0 = xmm_Di_0;
1633
0
                xmm_Dim1_1 = xmm_Di_1;
1634
0
            }
1635
0
        }
1636
0
        if (((height) % 2) == 1) {
1637
0
            for (c = 0; c < NB_ELTS_V8; c++) {
1638
0
                OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1639
0
            }
1640
0
        }
1641
0
    } else {
1642
0
        OPJ_UINT32 c;
1643
0
        OPJ_UINT32 i;
1644
0
        for (c = 0; c < NB_ELTS_V8; c++) {
1645
0
            OPJ_Sc(0) -= OPJ_Dc(0);
1646
0
        }
1647
0
        i = 1;
1648
0
        if (i < sn) {
1649
0
            __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
1650
0
                                                   (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
1651
0
            __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
1652
0
                                                   (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
1653
0
            for (; i < sn; i++) {
1654
0
                __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1655
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1656
0
                __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1657
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1658
0
                __m128i xmm_Si_0 = *(const __m128i*)(tmp +
1659
0
                                                     (i * 2) * NB_ELTS_V8 + 4 * 0);
1660
0
                __m128i xmm_Si_1 = *(const __m128i*)(tmp +
1661
0
                                                     (i * 2) * NB_ELTS_V8 + 4 * 1);
1662
0
                xmm_Si_0 = _mm_sub_epi32(xmm_Si_0,
1663
0
                                         _mm_srai_epi32(_mm_add_epi32(xmm_Di_0, xmm_Dim1_0), 1));
1664
0
                xmm_Si_1 = _mm_sub_epi32(xmm_Si_1,
1665
0
                                         _mm_srai_epi32(_mm_add_epi32(xmm_Di_1, xmm_Dim1_1), 1));
1666
0
                *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
1667
0
                *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
1668
0
                xmm_Dim1_0 = xmm_Di_0;
1669
0
                xmm_Dim1_1 = xmm_Di_1;
1670
0
            }
1671
0
        }
1672
0
        if (((height) % 2) == 1) {
1673
0
            for (c = 0; c < NB_ELTS_V8; c++) {
1674
0
                OPJ_Sc(i) -= OPJ_Dc(i - 1);
1675
0
            }
1676
0
        }
1677
0
        i = 0;
1678
0
        if (i + 1 < dn) {
1679
0
            __m128i xmm_Si_0 = *((const __m128i*)(tmp + 4 * 0));
1680
0
            __m128i xmm_Si_1 = *((const __m128i*)(tmp + 4 * 1));
1681
0
            const __m128i xmm_two = _mm_set1_epi32(2);
1682
0
            for (; i + 1 < dn; i++) {
1683
0
                __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
1684
0
                                                       (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
1685
0
                __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
1686
0
                                                       (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
1687
0
                __m128i xmm_Di_0 = *(const __m128i*)(tmp +
1688
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
1689
0
                __m128i xmm_Di_1 = *(const __m128i*)(tmp +
1690
0
                                                     (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
1691
0
                xmm_Di_0 = _mm_add_epi32(xmm_Di_0,
1692
0
                                         _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), xmm_two), 2));
1693
0
                xmm_Di_1 = _mm_add_epi32(xmm_Di_1,
1694
0
                                         _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), xmm_two), 2));
1695
0
                *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
1696
0
                *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
1697
0
                xmm_Si_0 = xmm_Sip1_0;
1698
0
                xmm_Si_1 = xmm_Sip1_1;
1699
0
            }
1700
0
        }
1701
0
        if (((height) % 2) == 0) {
1702
0
            for (c = 0; c < NB_ELTS_V8; c++) {
1703
0
                OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1704
0
            }
1705
0
        }
1706
0
    }
1707
#else
1708
    if (even) {
1709
        OPJ_UINT32 c;
1710
        if (height > 1) {
1711
            OPJ_UINT32 i;
1712
            for (i = 0; i + 1 < sn; i++) {
1713
                for (c = 0; c < NB_ELTS_V8; c++) {
1714
                    OPJ_Dc(i) -= (OPJ_Sc(i) + OPJ_Sc(i + 1)) >> 1;
1715
                }
1716
            }
1717
            if (((height) % 2) == 0) {
1718
                for (c = 0; c < NB_ELTS_V8; c++) {
1719
                    OPJ_Dc(i) -= OPJ_Sc(i);
1720
                }
1721
            }
1722
            for (c = 0; c < NB_ELTS_V8; c++) {
1723
                OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
1724
            }
1725
            for (i = 1; i < dn; i++) {
1726
                for (c = 0; c < NB_ELTS_V8; c++) {
1727
                    OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i) + 2) >> 2;
1728
                }
1729
            }
1730
            if (((height) % 2) == 1) {
1731
                for (c = 0; c < NB_ELTS_V8; c++) {
1732
                    OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
1733
                }
1734
            }
1735
        }
1736
    } else {
1737
        OPJ_UINT32 c;
1738
        if (height == 1) {
1739
            for (c = 0; c < NB_ELTS_V8; c++) {
1740
                OPJ_Sc(0) *= 2;
1741
            }
1742
        } else {
1743
            OPJ_UINT32 i;
1744
            for (c = 0; c < NB_ELTS_V8; c++) {
1745
                OPJ_Sc(0) -= OPJ_Dc(0);
1746
            }
1747
            for (i = 1; i < sn; i++) {
1748
                for (c = 0; c < NB_ELTS_V8; c++) {
1749
                    OPJ_Sc(i) -= (OPJ_Dc(i) + OPJ_Dc(i - 1)) >> 1;
1750
                }
1751
            }
1752
            if (((height) % 2) == 1) {
1753
                for (c = 0; c < NB_ELTS_V8; c++) {
1754
                    OPJ_Sc(i) -= OPJ_Dc(i - 1);
1755
                }
1756
            }
1757
            for (i = 0; i + 1 < dn; i++) {
1758
                for (c = 0; c < NB_ELTS_V8; c++) {
1759
                    OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i + 1) + 2) >> 2;
1760
                }
1761
            }
1762
            if (((height) % 2) == 0) {
1763
                for (c = 0; c < NB_ELTS_V8; c++) {
1764
                    OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
1765
                }
1766
            }
1767
        }
1768
    }
1769
#endif
1770
1771
0
    if (cols == NB_ELTS_V8) {
1772
0
        opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1773
0
                                    stride_width, even ? 0 : 1, NB_ELTS_V8);
1774
0
    } else {
1775
0
        opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
1776
0
                                    stride_width, even ? 0 : 1, cols);
1777
0
    }
1778
0
}
1779
1780
static void opj_v8dwt_encode_step1(OPJ_FLOAT32* fw,
1781
                                   OPJ_UINT32 end,
1782
                                   const OPJ_FLOAT32 cst)
1783
0
{
1784
0
    OPJ_UINT32 i;
1785
0
#ifdef __SSE__
1786
0
    __m128* vw = (__m128*) fw;
1787
0
    const __m128 vcst = _mm_set1_ps(cst);
1788
0
    for (i = 0; i < end; ++i) {
1789
0
        vw[0] = _mm_mul_ps(vw[0], vcst);
1790
0
        vw[1] = _mm_mul_ps(vw[1], vcst);
1791
0
        vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1792
0
    }
1793
#else
1794
    OPJ_UINT32 c;
1795
    for (i = 0; i < end; ++i) {
1796
        for (c = 0; c < NB_ELTS_V8; c++) {
1797
            fw[i * 2 * NB_ELTS_V8 + c] *= cst;
1798
        }
1799
    }
1800
#endif
1801
0
}
1802
1803
static void opj_v8dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
1804
                                   OPJ_UINT32 end,
1805
                                   OPJ_UINT32 m,
1806
                                   OPJ_FLOAT32 cst)
1807
0
{
1808
0
    OPJ_UINT32 i;
1809
0
    OPJ_UINT32 imax = opj_uint_min(end, m);
1810
0
#ifdef __SSE__
1811
0
    __m128* vw = (__m128*) fw;
1812
0
    __m128 vcst = _mm_set1_ps(cst);
1813
0
    if (imax > 0) {
1814
0
        __m128* vl = (__m128*) fl;
1815
0
        vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), vcst));
1816
0
        vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), vcst));
1817
0
        vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1818
0
        i = 1;
1819
1820
0
        for (; i < imax; ++i) {
1821
0
            vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), vcst));
1822
0
            vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), vcst));
1823
0
            vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
1824
0
        }
1825
0
    }
1826
0
    if (m < end) {
1827
0
        assert(m + 1 == end);
1828
0
        vcst = _mm_add_ps(vcst, vcst);
1829
0
        vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(vw[-4], vcst));
1830
0
        vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(vw[-3], vcst));
1831
0
    }
1832
#else
1833
    OPJ_INT32 c;
1834
    if (imax > 0) {
1835
        for (c = 0; c < NB_ELTS_V8; c++) {
1836
            fw[-1 * NB_ELTS_V8 + c] += (fl[0 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
1837
                                       cst;
1838
        }
1839
        fw += 2 * NB_ELTS_V8;
1840
        i = 1;
1841
        for (; i < imax; ++i) {
1842
            for (c = 0; c < NB_ELTS_V8; c++) {
1843
                fw[-1 * NB_ELTS_V8 + c] += (fw[-2 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
1844
                                           cst;
1845
            }
1846
            fw += 2 * NB_ELTS_V8;
1847
        }
1848
    }
1849
    if (m < end) {
1850
        assert(m + 1 == end);
1851
        for (c = 0; c < NB_ELTS_V8; c++) {
1852
            fw[-1 * NB_ELTS_V8 + c] += (2 * fw[-2 * NB_ELTS_V8 + c]) * cst;
1853
        }
1854
    }
1855
#endif
1856
0
}
1857
1858
/* Forward 9-7 transform, for the vertical pass, processing cols columns */
1859
/* where cols <= NB_ELTS_V8 */
1860
static void opj_dwt_encode_and_deinterleave_v_real(
1861
    void *arrayIn,
1862
    void *tmpIn,
1863
    OPJ_UINT32 height,
1864
    OPJ_BOOL even,
1865
    OPJ_UINT32 stride_width,
1866
    OPJ_UINT32 cols)
1867
0
{
1868
0
    OPJ_FLOAT32* OPJ_RESTRICT array = (OPJ_FLOAT32 * OPJ_RESTRICT)arrayIn;
1869
0
    OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32 * OPJ_RESTRICT)tmpIn;
1870
0
    const OPJ_INT32 sn = (OPJ_INT32)((height + (even ? 1 : 0)) >> 1);
1871
0
    const OPJ_INT32 dn = (OPJ_INT32)(height - (OPJ_UINT32)sn);
1872
0
    OPJ_INT32 a, b;
1873
1874
0
    if (height == 1) {
1875
0
        return;
1876
0
    }
1877
1878
0
    opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
1879
1880
0
    if (even) {
1881
0
        a = 0;
1882
0
        b = 1;
1883
0
    } else {
1884
0
        a = 1;
1885
0
        b = 0;
1886
0
    }
1887
0
    opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
1888
0
                           tmp + (b + 1) * NB_ELTS_V8,
1889
0
                           (OPJ_UINT32)dn,
1890
0
                           (OPJ_UINT32)opj_int_min(dn, sn - b),
1891
0
                           opj_dwt_alpha);
1892
0
    opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
1893
0
                           tmp + (a + 1) * NB_ELTS_V8,
1894
0
                           (OPJ_UINT32)sn,
1895
0
                           (OPJ_UINT32)opj_int_min(sn, dn - a),
1896
0
                           opj_dwt_beta);
1897
0
    opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
1898
0
                           tmp + (b + 1) * NB_ELTS_V8,
1899
0
                           (OPJ_UINT32)dn,
1900
0
                           (OPJ_UINT32)opj_int_min(dn, sn - b),
1901
0
                           opj_dwt_gamma);
1902
0
    opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
1903
0
                           tmp + (a + 1) * NB_ELTS_V8,
1904
0
                           (OPJ_UINT32)sn,
1905
0
                           (OPJ_UINT32)opj_int_min(sn, dn - a),
1906
0
                           opj_dwt_delta);
1907
0
    opj_v8dwt_encode_step1(tmp + b * NB_ELTS_V8, (OPJ_UINT32)dn,
1908
0
                           opj_K);
1909
0
    opj_v8dwt_encode_step1(tmp + a * NB_ELTS_V8, (OPJ_UINT32)sn,
1910
0
                           opj_invK);
1911
1912
1913
0
    if (cols == NB_ELTS_V8) {
1914
0
        opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
1915
0
                                    (OPJ_INT32*)array,
1916
0
                                    (OPJ_INT32)dn, (OPJ_INT32)sn,
1917
0
                                    stride_width, even ? 0 : 1, NB_ELTS_V8);
1918
0
    } else {
1919
0
        opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
1920
0
                                    (OPJ_INT32*)array,
1921
0
                                    (OPJ_INT32)dn, (OPJ_INT32)sn,
1922
0
                                    stride_width, even ? 0 : 1, cols);
1923
0
    }
1924
0
}
1925
1926
1927
/* <summary>                            */
1928
/* Forward 5-3 wavelet transform in 2-D. */
1929
/* </summary>                           */
1930
static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
1931
        opj_tcd_tilecomp_t * tilec,
1932
        opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
1933
        opj_encode_and_deinterleave_h_one_row_fnptr_type
1934
        p_encode_and_deinterleave_h_one_row)
1935
0
{
1936
0
    OPJ_INT32 i;
1937
0
    OPJ_INT32 *bj = 00;
1938
0
    OPJ_UINT32 w;
1939
0
    OPJ_INT32 l;
1940
1941
0
    OPJ_SIZE_T l_data_size;
1942
1943
0
    opj_tcd_resolution_t * l_cur_res = 0;
1944
0
    opj_tcd_resolution_t * l_last_res = 0;
1945
0
    const int num_threads = opj_thread_pool_get_thread_count(tp);
1946
0
    OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
1947
1948
0
    w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1949
0
    l = (OPJ_INT32)tilec->numresolutions - 1;
1950
1951
0
    l_cur_res = tilec->resolutions + l;
1952
0
    l_last_res = l_cur_res - 1;
1953
1954
0
    l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
1955
    /* overflow check */
1956
0
    if (l_data_size > (SIZE_MAX / (NB_ELTS_V8 * sizeof(OPJ_INT32)))) {
1957
        /* FIXME event manager error callback */
1958
0
        return OPJ_FALSE;
1959
0
    }
1960
0
    l_data_size *= NB_ELTS_V8 * sizeof(OPJ_INT32);
1961
0
    bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
1962
    /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
1963
    /* in that case, so do not error out */
1964
0
    if (l_data_size != 0 && ! bj) {
1965
0
        return OPJ_FALSE;
1966
0
    }
1967
0
    i = l;
1968
1969
0
    while (i--) {
1970
0
        OPJ_UINT32 j;
1971
0
        OPJ_UINT32 rw;           /* width of the resolution level computed   */
1972
0
        OPJ_UINT32 rh;           /* height of the resolution level computed  */
1973
0
        OPJ_UINT32
1974
0
        rw1;      /* width of the resolution level once lower than computed one                                       */
1975
0
        OPJ_UINT32
1976
0
        rh1;      /* height of the resolution level once lower than computed one                                      */
1977
0
        OPJ_INT32 cas_col;  /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
1978
0
        OPJ_INT32 cas_row;  /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
1979
0
        OPJ_INT32 dn, sn;
1980
1981
0
        rw  = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
1982
0
        rh  = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
1983
0
        rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
1984
0
        rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
1985
1986
0
        cas_row = l_cur_res->x0 & 1;
1987
0
        cas_col = l_cur_res->y0 & 1;
1988
1989
0
        sn = (OPJ_INT32)rh1;
1990
0
        dn = (OPJ_INT32)(rh - rh1);
1991
1992
        /* Perform vertical pass */
1993
0
        if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
1994
0
            for (j = 0; j + NB_ELTS_V8 - 1 < rw; j += NB_ELTS_V8) {
1995
0
                p_encode_and_deinterleave_v(tiledp + j,
1996
0
                                            bj,
1997
0
                                            rh,
1998
0
                                            cas_col == 0,
1999
0
                                            w,
2000
0
                                            NB_ELTS_V8);
2001
0
            }
2002
0
            if (j < rw) {
2003
0
                p_encode_and_deinterleave_v(tiledp + j,
2004
0
                                            bj,
2005
0
                                            rh,
2006
0
                                            cas_col == 0,
2007
0
                                            w,
2008
0
                                            rw - j);
2009
0
            }
2010
0
        }  else {
2011
0
            OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2012
0
            OPJ_UINT32 step_j;
2013
2014
0
            if (rw < num_jobs) {
2015
0
                num_jobs = rw;
2016
0
            }
2017
0
            step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
2018
2019
0
            for (j = 0; j < num_jobs; j++) {
2020
0
                opj_dwt_encode_v_job_t* job;
2021
2022
0
                job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
2023
0
                if (!job) {
2024
0
                    opj_thread_pool_wait_completion(tp, 0);
2025
0
                    opj_aligned_free(bj);
2026
0
                    return OPJ_FALSE;
2027
0
                }
2028
0
                job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
2029
0
                if (!job->v.mem) {
2030
0
                    opj_thread_pool_wait_completion(tp, 0);
2031
0
                    opj_free(job);
2032
0
                    opj_aligned_free(bj);
2033
0
                    return OPJ_FALSE;
2034
0
                }
2035
0
                job->v.dn = dn;
2036
0
                job->v.sn = sn;
2037
0
                job->v.cas = cas_col;
2038
0
                job->rh = rh;
2039
0
                job->w = w;
2040
0
                job->tiledp = tiledp;
2041
0
                job->min_j = j * step_j;
2042
0
                job->max_j = (j + 1 == num_jobs) ? rw : (j + 1) * step_j;
2043
0
                job->p_encode_and_deinterleave_v = p_encode_and_deinterleave_v;
2044
0
                opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
2045
0
            }
2046
0
            opj_thread_pool_wait_completion(tp, 0);
2047
0
        }
2048
2049
0
        sn = (OPJ_INT32)rw1;
2050
0
        dn = (OPJ_INT32)(rw - rw1);
2051
2052
        /* Perform horizontal pass */
2053
0
        if (num_threads <= 1 || rh <= 1) {
2054
0
            for (j = 0; j < rh; j++) {
2055
0
                OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
2056
0
                (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
2057
0
                                                       cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
2058
0
            }
2059
0
        }  else {
2060
0
            OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2061
0
            OPJ_UINT32 step_j;
2062
2063
0
            if (rh < num_jobs) {
2064
0
                num_jobs = rh;
2065
0
            }
2066
0
            step_j = (rh / num_jobs);
2067
2068
0
            for (j = 0; j < num_jobs; j++) {
2069
0
                opj_dwt_encode_h_job_t* job;
2070
2071
0
                job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
2072
0
                if (!job) {
2073
0
                    opj_thread_pool_wait_completion(tp, 0);
2074
0
                    opj_aligned_free(bj);
2075
0
                    return OPJ_FALSE;
2076
0
                }
2077
0
                job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
2078
0
                if (!job->h.mem) {
2079
0
                    opj_thread_pool_wait_completion(tp, 0);
2080
0
                    opj_free(job);
2081
0
                    opj_aligned_free(bj);
2082
0
                    return OPJ_FALSE;
2083
0
                }
2084
0
                job->h.dn = dn;
2085
0
                job->h.sn = sn;
2086
0
                job->h.cas = cas_row;
2087
0
                job->rw = rw;
2088
0
                job->w = w;
2089
0
                job->tiledp = tiledp;
2090
0
                job->min_j = j * step_j;
2091
0
                job->max_j = (j + 1U) * step_j; /* this can overflow */
2092
0
                if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
2093
0
                    job->max_j = rh;
2094
0
                }
2095
0
                job->p_function = p_encode_and_deinterleave_h_one_row;
2096
0
                opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
2097
0
            }
2098
0
            opj_thread_pool_wait_completion(tp, 0);
2099
0
        }
2100
2101
0
        l_cur_res = l_last_res;
2102
2103
0
        --l_last_res;
2104
0
    }
2105
2106
0
    opj_aligned_free(bj);
2107
0
    return OPJ_TRUE;
2108
0
}
2109
2110
/* Forward 5-3 wavelet transform in 2-D. */
2111
/* </summary>                           */
2112
OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
2113
                        opj_tcd_tilecomp_t * tilec)
2114
0
{
2115
0
    return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
2116
0
                                    opj_dwt_encode_and_deinterleave_v,
2117
0
                                    opj_dwt_encode_and_deinterleave_h_one_row);
2118
0
}
2119
2120
/* <summary>                            */
2121
/* Inverse 5-3 wavelet transform in 2-D. */
2122
/* </summary>                           */
2123
OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
2124
                        OPJ_UINT32 numres)
2125
3.20k
{
2126
3.20k
    if (p_tcd->whole_tile_decoding) {
2127
3.20k
        return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
2128
3.20k
    } else {
2129
0
        return opj_dwt_decode_partial_tile(tilec, numres);
2130
0
    }
2131
3.20k
}
2132
2133
/* <summary>                */
2134
/* Get norm of 5-3 wavelet. */
2135
/* </summary>               */
2136
OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
2137
0
{
2138
    /* FIXME ! This is just a band-aid to avoid a buffer overflow */
2139
    /* but the array should really be extended up to 33 resolution levels */
2140
    /* See https://github.com/uclouvain/openjpeg/issues/493 */
2141
0
    if (orient == 0 && level >= 10) {
2142
0
        level = 9;
2143
0
    } else if (orient > 0 && level >= 9) {
2144
0
        level = 8;
2145
0
    }
2146
0
    return opj_dwt_norms[orient][level];
2147
0
}
2148
2149
/* <summary>                             */
2150
/* Forward 9-7 wavelet transform in 2-D. */
2151
/* </summary>                            */
2152
OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
2153
                             opj_tcd_tilecomp_t * tilec)
2154
0
{
2155
0
    return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
2156
0
                                    opj_dwt_encode_and_deinterleave_v_real,
2157
0
                                    opj_dwt_encode_and_deinterleave_h_one_row_real);
2158
0
}
2159
2160
/* <summary>                */
2161
/* Get norm of 9-7 wavelet. */
2162
/* </summary>               */
2163
OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
2164
0
{
2165
    /* FIXME ! This is just a band-aid to avoid a buffer overflow */
2166
    /* but the array should really be extended up to 33 resolution levels */
2167
    /* See https://github.com/uclouvain/openjpeg/issues/493 */
2168
0
    if (orient == 0 && level >= 10) {
2169
0
        level = 9;
2170
0
    } else if (orient > 0 && level >= 9) {
2171
0
        level = 8;
2172
0
    }
2173
0
    return opj_dwt_norms_real[orient][level];
2174
0
}
2175
2176
void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
2177
0
{
2178
0
    OPJ_UINT32 numbands, bandno;
2179
0
    numbands = 3 * tccp->numresolutions - 2;
2180
0
    for (bandno = 0; bandno < numbands; bandno++) {
2181
0
        OPJ_FLOAT64 stepsize;
2182
0
        OPJ_UINT32 resno, level, orient, gain;
2183
2184
0
        resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
2185
0
        orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
2186
0
        level = tccp->numresolutions - 1 - resno;
2187
0
        gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
2188
0
                                          (orient == 2)) ? 1 : 2));
2189
0
        if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2190
0
            stepsize = 1.0;
2191
0
        } else {
2192
0
            OPJ_FLOAT64 norm = opj_dwt_getnorm_real(level, orient);
2193
0
            stepsize = (1 << (gain)) / norm;
2194
0
        }
2195
0
        opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
2196
0
                                (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
2197
0
    }
2198
0
}
2199
2200
/* <summary>                             */
2201
/* Determine maximum computed resolution level for inverse wavelet transform */
2202
/* </summary>                            */
2203
static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
2204
        OPJ_UINT32 i)
2205
60.1k
{
2206
60.1k
    OPJ_UINT32 mr   = 0;
2207
60.1k
    OPJ_UINT32 w;
2208
341k
    while (--i) {
2209
281k
        ++r;
2210
281k
        if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
2211
224k
            mr = w ;
2212
224k
        }
2213
281k
        if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
2214
130k
            mr = w ;
2215
130k
        }
2216
281k
    }
2217
60.1k
    return mr ;
2218
60.1k
}
2219
2220
typedef struct {
2221
    opj_dwt_t h;
2222
    OPJ_UINT32 rw;
2223
    OPJ_UINT32 w;
2224
    OPJ_INT32 * OPJ_RESTRICT tiledp;
2225
    OPJ_UINT32 min_j;
2226
    OPJ_UINT32 max_j;
2227
} opj_dwt_decode_h_job_t;
2228
2229
static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
2230
0
{
2231
0
    OPJ_UINT32 j;
2232
0
    opj_dwt_decode_h_job_t* job;
2233
0
    (void)tls;
2234
2235
0
    job = (opj_dwt_decode_h_job_t*)user_data;
2236
0
    for (j = job->min_j; j < job->max_j; j++) {
2237
0
        opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
2238
0
    }
2239
2240
0
    opj_aligned_free(job->h.mem);
2241
0
    opj_free(job);
2242
0
}
2243
2244
typedef struct {
2245
    opj_dwt_t v;
2246
    OPJ_UINT32 rh;
2247
    OPJ_UINT32 w;
2248
    OPJ_INT32 * OPJ_RESTRICT tiledp;
2249
    OPJ_UINT32 min_j;
2250
    OPJ_UINT32 max_j;
2251
} opj_dwt_decode_v_job_t;
2252
2253
static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
2254
0
{
2255
0
    OPJ_UINT32 j;
2256
0
    opj_dwt_decode_v_job_t* job;
2257
0
    (void)tls;
2258
2259
0
    job = (opj_dwt_decode_v_job_t*)user_data;
2260
0
    for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
2261
0
            j += PARALLEL_COLS_53) {
2262
0
        opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
2263
0
                     PARALLEL_COLS_53);
2264
0
    }
2265
0
    if (j < job->max_j)
2266
0
        opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
2267
0
                     (OPJ_INT32)(job->max_j - j));
2268
2269
0
    opj_aligned_free(job->v.mem);
2270
0
    opj_free(job);
2271
0
}
2272
2273
2274
/* <summary>                            */
2275
/* Inverse wavelet transform in 2-D.    */
2276
/* </summary>                           */
2277
static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
2278
                                    opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
2279
3.20k
{
2280
3.20k
    opj_dwt_t h;
2281
3.20k
    opj_dwt_t v;
2282
2283
3.20k
    opj_tcd_resolution_t* tr = tilec->resolutions;
2284
2285
3.20k
    OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2286
3.20k
                                 tr->x0);  /* width of the resolution level computed */
2287
3.20k
    OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2288
3.20k
                                 tr->y0);  /* height of the resolution level computed */
2289
2290
3.20k
    OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
2291
3.20k
                                                               1].x1 -
2292
3.20k
                                tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
2293
3.20k
    OPJ_SIZE_T h_mem_size;
2294
3.20k
    int num_threads;
2295
2296
    /* Not entirely sure for the return code of w == 0 which is triggered per */
2297
    /* https://github.com/uclouvain/openjpeg/issues/1505 */
2298
3.20k
    if (numres == 1U || w == 0) {
2299
0
        return OPJ_TRUE;
2300
0
    }
2301
3.20k
    num_threads = opj_thread_pool_get_thread_count(tp);
2302
3.20k
    h_mem_size = opj_dwt_max_resolution(tr, numres);
2303
    /* overflow check */
2304
3.20k
    if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
2305
        /* FIXME event manager error callback */
2306
0
        return OPJ_FALSE;
2307
0
    }
2308
    /* We need PARALLEL_COLS_53 times the height of the array, */
2309
    /* since for the vertical pass */
2310
    /* we process PARALLEL_COLS_53 columns at a time */
2311
3.20k
    h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
2312
3.20k
    h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2313
3.20k
    if (! h.mem) {
2314
        /* FIXME event manager error callback */
2315
0
        return OPJ_FALSE;
2316
0
    }
2317
2318
3.20k
    v.mem = h.mem;
2319
2320
19.2k
    while (--numres) {
2321
16.0k
        OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
2322
16.0k
        OPJ_UINT32 j;
2323
2324
16.0k
        ++tr;
2325
16.0k
        h.sn = (OPJ_INT32)rw;
2326
16.0k
        v.sn = (OPJ_INT32)rh;
2327
2328
16.0k
        rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2329
16.0k
        rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2330
2331
16.0k
        h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2332
16.0k
        h.cas = tr->x0 % 2;
2333
2334
16.0k
        if (num_threads <= 1 || rh <= 1) {
2335
1.12M
            for (j = 0; j < rh; ++j) {
2336
1.10M
                opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
2337
1.10M
            }
2338
16.0k
        } else {
2339
0
            OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2340
0
            OPJ_UINT32 step_j;
2341
2342
0
            if (rh < num_jobs) {
2343
0
                num_jobs = rh;
2344
0
            }
2345
0
            step_j = (rh / num_jobs);
2346
2347
0
            for (j = 0; j < num_jobs; j++) {
2348
0
                opj_dwt_decode_h_job_t* job;
2349
2350
0
                job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
2351
0
                if (!job) {
2352
                    /* It would be nice to fallback to single thread case, but */
2353
                    /* unfortunately some jobs may be launched and have modified */
2354
                    /* tiledp, so it is not practical to recover from that error */
2355
                    /* FIXME event manager error callback */
2356
0
                    opj_thread_pool_wait_completion(tp, 0);
2357
0
                    opj_aligned_free(h.mem);
2358
0
                    return OPJ_FALSE;
2359
0
                }
2360
0
                job->h = h;
2361
0
                job->rw = rw;
2362
0
                job->w = w;
2363
0
                job->tiledp = tiledp;
2364
0
                job->min_j = j * step_j;
2365
0
                job->max_j = (j + 1U) * step_j; /* this can overflow */
2366
0
                if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
2367
0
                    job->max_j = rh;
2368
0
                }
2369
0
                job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2370
0
                if (!job->h.mem) {
2371
                    /* FIXME event manager error callback */
2372
0
                    opj_thread_pool_wait_completion(tp, 0);
2373
0
                    opj_free(job);
2374
0
                    opj_aligned_free(h.mem);
2375
0
                    return OPJ_FALSE;
2376
0
                }
2377
0
                opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
2378
0
            }
2379
0
            opj_thread_pool_wait_completion(tp, 0);
2380
0
        }
2381
2382
16.0k
        v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2383
16.0k
        v.cas = tr->y0 % 2;
2384
2385
16.0k
        if (num_threads <= 1 || rw <= 1) {
2386
136k
            for (j = 0; j + PARALLEL_COLS_53 <= rw;
2387
120k
                    j += PARALLEL_COLS_53) {
2388
120k
                opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
2389
120k
            }
2390
16.0k
            if (j < rw) {
2391
8.65k
                opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
2392
8.65k
            }
2393
16.0k
        } else {
2394
0
            OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
2395
0
            OPJ_UINT32 step_j;
2396
2397
0
            if (rw < num_jobs) {
2398
0
                num_jobs = rw;
2399
0
            }
2400
0
            step_j = (rw / num_jobs);
2401
2402
0
            for (j = 0; j < num_jobs; j++) {
2403
0
                opj_dwt_decode_v_job_t* job;
2404
2405
0
                job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
2406
0
                if (!job) {
2407
                    /* It would be nice to fallback to single thread case, but */
2408
                    /* unfortunately some jobs may be launched and have modified */
2409
                    /* tiledp, so it is not practical to recover from that error */
2410
                    /* FIXME event manager error callback */
2411
0
                    opj_thread_pool_wait_completion(tp, 0);
2412
0
                    opj_aligned_free(v.mem);
2413
0
                    return OPJ_FALSE;
2414
0
                }
2415
0
                job->v = v;
2416
0
                job->rh = rh;
2417
0
                job->w = w;
2418
0
                job->tiledp = tiledp;
2419
0
                job->min_j = j * step_j;
2420
0
                job->max_j = (j + 1U) * step_j; /* this can overflow */
2421
0
                if (j == (num_jobs - 1U)) {  /* this will take care of the overflow */
2422
0
                    job->max_j = rw;
2423
0
                }
2424
0
                job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2425
0
                if (!job->v.mem) {
2426
                    /* FIXME event manager error callback */
2427
0
                    opj_thread_pool_wait_completion(tp, 0);
2428
0
                    opj_free(job);
2429
0
                    opj_aligned_free(v.mem);
2430
0
                    return OPJ_FALSE;
2431
0
                }
2432
0
                opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
2433
0
            }
2434
0
            opj_thread_pool_wait_completion(tp, 0);
2435
0
        }
2436
16.0k
    }
2437
3.20k
    opj_aligned_free(h.mem);
2438
3.20k
    return OPJ_TRUE;
2439
3.20k
}
2440
2441
static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
2442
        OPJ_INT32 cas,
2443
        opj_sparse_array_int32_t* sa,
2444
        OPJ_UINT32 sa_line,
2445
        OPJ_UINT32 sn,
2446
        OPJ_UINT32 win_l_x0,
2447
        OPJ_UINT32 win_l_x1,
2448
        OPJ_UINT32 win_h_x0,
2449
        OPJ_UINT32 win_h_x1)
2450
0
{
2451
0
    OPJ_BOOL ret;
2452
0
    ret = opj_sparse_array_int32_read(sa,
2453
0
                                      win_l_x0, sa_line,
2454
0
                                      win_l_x1, sa_line + 1,
2455
0
                                      dest + cas + 2 * win_l_x0,
2456
0
                                      2, 0, OPJ_TRUE);
2457
0
    assert(ret);
2458
0
    ret = opj_sparse_array_int32_read(sa,
2459
0
                                      sn + win_h_x0, sa_line,
2460
0
                                      sn + win_h_x1, sa_line + 1,
2461
0
                                      dest + 1 - cas + 2 * win_h_x0,
2462
0
                                      2, 0, OPJ_TRUE);
2463
0
    assert(ret);
2464
0
    OPJ_UNUSED(ret);
2465
0
}
2466
2467
2468
static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
2469
        OPJ_INT32 cas,
2470
        opj_sparse_array_int32_t* sa,
2471
        OPJ_UINT32 sa_col,
2472
        OPJ_UINT32 nb_cols,
2473
        OPJ_UINT32 sn,
2474
        OPJ_UINT32 win_l_y0,
2475
        OPJ_UINT32 win_l_y1,
2476
        OPJ_UINT32 win_h_y0,
2477
        OPJ_UINT32 win_h_y1)
2478
0
{
2479
0
    OPJ_BOOL ret;
2480
0
    ret  = opj_sparse_array_int32_read(sa,
2481
0
                                       sa_col, win_l_y0,
2482
0
                                       sa_col + nb_cols, win_l_y1,
2483
0
                                       dest + cas * 4 + 2 * 4 * win_l_y0,
2484
0
                                       1, 2 * 4, OPJ_TRUE);
2485
0
    assert(ret);
2486
0
    ret = opj_sparse_array_int32_read(sa,
2487
0
                                      sa_col, sn + win_h_y0,
2488
0
                                      sa_col + nb_cols, sn + win_h_y1,
2489
0
                                      dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
2490
0
                                      1, 2 * 4, OPJ_TRUE);
2491
0
    assert(ret);
2492
0
    OPJ_UNUSED(ret);
2493
0
}
2494
2495
static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
2496
                                     OPJ_INT32 cas,
2497
                                     OPJ_INT32 win_l_x0,
2498
                                     OPJ_INT32 win_l_x1,
2499
                                     OPJ_INT32 win_h_x0,
2500
                                     OPJ_INT32 win_h_x1)
2501
0
{
2502
0
    OPJ_INT32 i;
2503
2504
0
    if (!cas) {
2505
0
        if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
2506
2507
            /* Naive version is :
2508
            for (i = win_l_x0; i < i_max; i++) {
2509
                OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2510
            }
2511
            for (i = win_h_x0; i < win_h_x1; i++) {
2512
                OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2513
            }
2514
            but the compiler doesn't manage to unroll it to avoid bound
2515
            checking in OPJ_S_ and OPJ_D_ macros
2516
            */
2517
2518
0
            i = win_l_x0;
2519
0
            if (i < win_l_x1) {
2520
0
                OPJ_INT32 i_max;
2521
2522
                /* Left-most case */
2523
0
                OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2524
0
                i ++;
2525
2526
0
                i_max = win_l_x1;
2527
0
                if (i_max > dn) {
2528
0
                    i_max = dn;
2529
0
                }
2530
0
                for (; i < i_max; i++) {
2531
                    /* No bound checking */
2532
0
                    OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
2533
0
                }
2534
0
                for (; i < win_l_x1; i++) {
2535
                    /* Right-most case */
2536
0
                    OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2537
0
                }
2538
0
            }
2539
2540
0
            i = win_h_x0;
2541
0
            if (i < win_h_x1) {
2542
0
                OPJ_INT32 i_max = win_h_x1;
2543
0
                if (i_max >= sn) {
2544
0
                    i_max = sn - 1;
2545
0
                }
2546
0
                for (; i < i_max; i++) {
2547
                    /* No bound checking */
2548
0
                    OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
2549
0
                }
2550
0
                for (; i < win_h_x1; i++) {
2551
                    /* Right-most case */
2552
0
                    OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2553
0
                }
2554
0
            }
2555
0
        }
2556
0
    } else {
2557
0
        if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
2558
0
            OPJ_S(0) /= 2;
2559
0
        } else {
2560
0
            for (i = win_l_x0; i < win_l_x1; i++) {
2561
0
                OPJ_D(i) = opj_int_sub_no_overflow(OPJ_D(i),
2562
0
                                                   opj_int_add_no_overflow(opj_int_add_no_overflow(OPJ_SS_(i), OPJ_SS_(i + 1)),
2563
0
                                                           2) >> 2);
2564
0
            }
2565
0
            for (i = win_h_x0; i < win_h_x1; i++) {
2566
0
                OPJ_S(i) = opj_int_add_no_overflow(OPJ_S(i),
2567
0
                                                   opj_int_add_no_overflow(OPJ_DD_(i), OPJ_DD_(i - 1)) >> 1);
2568
0
            }
2569
0
        }
2570
0
    }
2571
0
}
2572
2573
0
#define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
2574
0
#define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
2575
0
#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)))
2576
0
#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)))
2577
0
#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)))
2578
0
#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)))
2579
2580
static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
2581
        OPJ_UINT32 nb_cols,
2582
        OPJ_INT32 dn, OPJ_INT32 sn,
2583
        OPJ_INT32 cas,
2584
        OPJ_INT32 win_l_x0,
2585
        OPJ_INT32 win_l_x1,
2586
        OPJ_INT32 win_h_x0,
2587
        OPJ_INT32 win_h_x1)
2588
0
{
2589
0
    OPJ_INT32 i;
2590
0
    OPJ_UINT32 off;
2591
2592
0
    (void)nb_cols;
2593
2594
0
    if (!cas) {
2595
0
        if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
2596
2597
            /* Naive version is :
2598
            for (i = win_l_x0; i < i_max; i++) {
2599
                OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
2600
            }
2601
            for (i = win_h_x0; i < win_h_x1; i++) {
2602
                OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
2603
            }
2604
            but the compiler doesn't manage to unroll it to avoid bound
2605
            checking in OPJ_S_ and OPJ_D_ macros
2606
            */
2607
2608
0
            i = win_l_x0;
2609
0
            if (i < win_l_x1) {
2610
0
                OPJ_INT32 i_max;
2611
2612
                /* Left-most case */
2613
0
                for (off = 0; off < 4; off++) {
2614
0
                    OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2615
0
                }
2616
0
                i ++;
2617
2618
0
                i_max = win_l_x1;
2619
0
                if (i_max > dn) {
2620
0
                    i_max = dn;
2621
0
                }
2622
2623
0
#ifdef __SSE2__
2624
0
                if (i + 1 < i_max) {
2625
0
                    const __m128i two = _mm_set1_epi32(2);
2626
0
                    __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
2627
0
                    for (; i + 1 < i_max; i += 2) {
2628
                        /* No bound checking */
2629
0
                        __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
2630
0
                        __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2631
0
                        __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2632
0
                        __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2633
0
                        S = _mm_sub_epi32(S,
2634
0
                                          _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
2635
0
                        S1 = _mm_sub_epi32(S1,
2636
0
                                           _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
2637
0
                        _mm_store_si128((__m128i*)(a + i * 8), S);
2638
0
                        _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
2639
0
                        Dm1 = D1;
2640
0
                    }
2641
0
                }
2642
0
#endif
2643
2644
0
                for (; i < i_max; i++) {
2645
                    /* No bound checking */
2646
0
                    for (off = 0; off < 4; off++) {
2647
0
                        OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
2648
0
                    }
2649
0
                }
2650
0
                for (; i < win_l_x1; i++) {
2651
                    /* Right-most case */
2652
0
                    for (off = 0; off < 4; off++) {
2653
0
                        OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
2654
0
                    }
2655
0
                }
2656
0
            }
2657
2658
0
            i = win_h_x0;
2659
0
            if (i < win_h_x1) {
2660
0
                OPJ_INT32 i_max = win_h_x1;
2661
0
                if (i_max >= sn) {
2662
0
                    i_max = sn - 1;
2663
0
                }
2664
2665
0
#ifdef __SSE2__
2666
0
                if (i + 1 < i_max) {
2667
0
                    __m128i S =  _mm_load_si128((__m128i * const)(a + i * 8));
2668
0
                    for (; i + 1 < i_max; i += 2) {
2669
                        /* No bound checking */
2670
0
                        __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
2671
0
                        __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
2672
0
                        __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
2673
0
                        __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
2674
0
                        D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
2675
0
                        D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
2676
0
                        _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
2677
0
                        _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
2678
0
                        S = S2;
2679
0
                    }
2680
0
                }
2681
0
#endif
2682
2683
0
                for (; i < i_max; i++) {
2684
                    /* No bound checking */
2685
0
                    for (off = 0; off < 4; off++) {
2686
0
                        OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
2687
0
                    }
2688
0
                }
2689
0
                for (; i < win_h_x1; i++) {
2690
                    /* Right-most case */
2691
0
                    for (off = 0; off < 4; off++) {
2692
0
                        OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
2693
0
                    }
2694
0
                }
2695
0
            }
2696
0
        }
2697
0
    } else {
2698
0
        if (!sn  && dn == 1) {        /* NEW :  CASE ONE ELEMENT */
2699
0
            for (off = 0; off < 4; off++) {
2700
0
                OPJ_S_off(0, off) /= 2;
2701
0
            }
2702
0
        } else {
2703
0
            for (i = win_l_x0; i < win_l_x1; i++) {
2704
0
                for (off = 0; off < 4; off++) {
2705
0
                    OPJ_D_off(i, off) = opj_int_sub_no_overflow(
2706
0
                                            OPJ_D_off(i, off),
2707
0
                                            opj_int_add_no_overflow(
2708
0
                                                opj_int_add_no_overflow(OPJ_SS__off(i, off), OPJ_SS__off(i + 1, off)), 2) >> 2);
2709
0
                }
2710
0
            }
2711
0
            for (i = win_h_x0; i < win_h_x1; i++) {
2712
0
                for (off = 0; off < 4; off++) {
2713
0
                    OPJ_S_off(i, off) = opj_int_add_no_overflow(
2714
0
                                            OPJ_S_off(i, off),
2715
0
                                            opj_int_add_no_overflow(OPJ_DD__off(i, off), OPJ_DD__off(i - 1, off)) >> 1);
2716
0
                }
2717
0
            }
2718
0
        }
2719
0
    }
2720
0
}
2721
2722
static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
2723
        OPJ_UINT32 resno,
2724
        OPJ_UINT32 bandno,
2725
        OPJ_UINT32 tcx0,
2726
        OPJ_UINT32 tcy0,
2727
        OPJ_UINT32 tcx1,
2728
        OPJ_UINT32 tcy1,
2729
        OPJ_UINT32* tbx0,
2730
        OPJ_UINT32* tby0,
2731
        OPJ_UINT32* tbx1,
2732
        OPJ_UINT32* tby1)
2733
0
{
2734
    /* Compute number of decomposition for this band. See table F-1 */
2735
0
    OPJ_UINT32 nb = (resno == 0) ?
2736
0
                    tilec->numresolutions - 1 :
2737
0
                    tilec->numresolutions - resno;
2738
    /* Map above tile-based coordinates to sub-band-based coordinates per */
2739
    /* equation B-15 of the standard */
2740
0
    OPJ_UINT32 x0b = bandno & 1;
2741
0
    OPJ_UINT32 y0b = bandno >> 1;
2742
0
    if (tbx0) {
2743
0
        *tbx0 = (nb == 0) ? tcx0 :
2744
0
                (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2745
0
                opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2746
0
    }
2747
0
    if (tby0) {
2748
0
        *tby0 = (nb == 0) ? tcy0 :
2749
0
                (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2750
0
                opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2751
0
    }
2752
0
    if (tbx1) {
2753
0
        *tbx1 = (nb == 0) ? tcx1 :
2754
0
                (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2755
0
                opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2756
0
    }
2757
0
    if (tby1) {
2758
0
        *tby1 = (nb == 0) ? tcy1 :
2759
0
                (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2760
0
                opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2761
0
    }
2762
0
}
2763
2764
static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
2765
                                 OPJ_UINT32 max_size,
2766
                                 OPJ_UINT32* start,
2767
                                 OPJ_UINT32* end)
2768
0
{
2769
0
    *start = opj_uint_subs(*start, filter_width);
2770
0
    *end = opj_uint_adds(*end, filter_width);
2771
0
    *end = opj_uint_min(*end, max_size);
2772
0
}
2773
2774
2775
static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
2776
    opj_tcd_tilecomp_t* tilec,
2777
    OPJ_UINT32 numres)
2778
0
{
2779
0
    opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2780
0
    OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
2781
0
    OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
2782
0
    OPJ_UINT32 resno, bandno, precno, cblkno;
2783
0
    opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
2784
0
                                       w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
2785
0
    if (sa == NULL) {
2786
0
        return NULL;
2787
0
    }
2788
2789
0
    for (resno = 0; resno < numres; ++resno) {
2790
0
        opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2791
2792
0
        for (bandno = 0; bandno < res->numbands; ++bandno) {
2793
0
            opj_tcd_band_t* band = &res->bands[bandno];
2794
2795
0
            for (precno = 0; precno < res->pw * res->ph; ++precno) {
2796
0
                opj_tcd_precinct_t* precinct = &band->precincts[precno];
2797
0
                for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2798
0
                    opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2799
0
                    if (cblk->decoded_data != NULL) {
2800
0
                        OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
2801
0
                        OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
2802
0
                        OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
2803
0
                        OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
2804
2805
0
                        if (band->bandno & 1) {
2806
0
                            opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2807
0
                            x += (OPJ_UINT32)(pres->x1 - pres->x0);
2808
0
                        }
2809
0
                        if (band->bandno & 2) {
2810
0
                            opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2811
0
                            y += (OPJ_UINT32)(pres->y1 - pres->y0);
2812
0
                        }
2813
2814
0
                        if (!opj_sparse_array_int32_write(sa, x, y,
2815
0
                                                          x + cblk_w, y + cblk_h,
2816
0
                                                          cblk->decoded_data,
2817
0
                                                          1, cblk_w, OPJ_TRUE)) {
2818
0
                            opj_sparse_array_int32_free(sa);
2819
0
                            return NULL;
2820
0
                        }
2821
0
                    }
2822
0
                }
2823
0
            }
2824
0
        }
2825
0
    }
2826
2827
0
    return sa;
2828
0
}
2829
2830
2831
static OPJ_BOOL opj_dwt_decode_partial_tile(
2832
    opj_tcd_tilecomp_t* tilec,
2833
    OPJ_UINT32 numres)
2834
0
{
2835
0
    opj_sparse_array_int32_t* sa;
2836
0
    opj_dwt_t h;
2837
0
    opj_dwt_t v;
2838
0
    OPJ_UINT32 resno;
2839
    /* This value matches the maximum left/right extension given in tables */
2840
    /* F.2 and F.3 of the standard. */
2841
0
    const OPJ_UINT32 filter_width = 2U;
2842
2843
0
    opj_tcd_resolution_t* tr = tilec->resolutions;
2844
0
    opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
2845
2846
0
    OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
2847
0
                                 tr->x0);  /* width of the resolution level computed */
2848
0
    OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
2849
0
                                 tr->y0);  /* height of the resolution level computed */
2850
2851
0
    OPJ_SIZE_T h_mem_size;
2852
2853
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
2854
    /* with the tile coordinates */
2855
0
    OPJ_UINT32 win_tcx0 = tilec->win_x0;
2856
0
    OPJ_UINT32 win_tcy0 = tilec->win_y0;
2857
0
    OPJ_UINT32 win_tcx1 = tilec->win_x1;
2858
0
    OPJ_UINT32 win_tcy1 = tilec->win_y1;
2859
2860
0
    if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
2861
0
        return OPJ_TRUE;
2862
0
    }
2863
2864
0
    sa = opj_dwt_init_sparse_array(tilec, numres);
2865
0
    if (sa == NULL) {
2866
0
        return OPJ_FALSE;
2867
0
    }
2868
2869
0
    if (numres == 1U) {
2870
0
        OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
2871
0
                       tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
2872
0
                       tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
2873
0
                       tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
2874
0
                       tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
2875
0
                       tilec->data_win,
2876
0
                       1, tr_max->win_x1 - tr_max->win_x0,
2877
0
                       OPJ_TRUE);
2878
0
        assert(ret);
2879
0
        OPJ_UNUSED(ret);
2880
0
        opj_sparse_array_int32_free(sa);
2881
0
        return OPJ_TRUE;
2882
0
    }
2883
0
    h_mem_size = opj_dwt_max_resolution(tr, numres);
2884
    /* overflow check */
2885
    /* in vertical pass, we process 4 columns at a time */
2886
0
    if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
2887
        /* FIXME event manager error callback */
2888
0
        opj_sparse_array_int32_free(sa);
2889
0
        return OPJ_FALSE;
2890
0
    }
2891
2892
0
    h_mem_size *= 4 * sizeof(OPJ_INT32);
2893
0
    h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
2894
0
    if (! h.mem) {
2895
        /* FIXME event manager error callback */
2896
0
        opj_sparse_array_int32_free(sa);
2897
0
        return OPJ_FALSE;
2898
0
    }
2899
2900
0
    v.mem = h.mem;
2901
2902
0
    for (resno = 1; resno < numres; resno ++) {
2903
0
        OPJ_UINT32 i, j;
2904
        /* Window of interest subband-based coordinates */
2905
0
        OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
2906
0
        OPJ_UINT32 win_hl_x0, win_hl_x1;
2907
0
        OPJ_UINT32 win_lh_y0, win_lh_y1;
2908
        /* Window of interest tile-resolution-based coordinates */
2909
0
        OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
2910
        /* Tile-resolution subband-based coordinates */
2911
0
        OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
2912
2913
0
        ++tr;
2914
2915
0
        h.sn = (OPJ_INT32)rw;
2916
0
        v.sn = (OPJ_INT32)rh;
2917
2918
0
        rw = (OPJ_UINT32)(tr->x1 - tr->x0);
2919
0
        rh = (OPJ_UINT32)(tr->y1 - tr->y0);
2920
2921
0
        h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
2922
0
        h.cas = tr->x0 % 2;
2923
2924
0
        v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
2925
0
        v.cas = tr->y0 % 2;
2926
2927
        /* Get the subband coordinates for the window of interest */
2928
        /* LL band */
2929
0
        opj_dwt_get_band_coordinates(tilec, resno, 0,
2930
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2931
0
                                     &win_ll_x0, &win_ll_y0,
2932
0
                                     &win_ll_x1, &win_ll_y1);
2933
2934
        /* HL band */
2935
0
        opj_dwt_get_band_coordinates(tilec, resno, 1,
2936
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2937
0
                                     &win_hl_x0, NULL, &win_hl_x1, NULL);
2938
2939
        /* LH band */
2940
0
        opj_dwt_get_band_coordinates(tilec, resno, 2,
2941
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
2942
0
                                     NULL, &win_lh_y0, NULL, &win_lh_y1);
2943
2944
        /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
2945
0
        tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
2946
0
        tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
2947
0
        tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
2948
0
        tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
2949
2950
        /* Subtract the origin of the bands for this tile, to the subwindow */
2951
        /* of interest band coordinates, so as to get them relative to the */
2952
        /* tile */
2953
0
        win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
2954
0
        win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
2955
0
        win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
2956
0
        win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
2957
0
        win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
2958
0
        win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
2959
0
        win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
2960
0
        win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
2961
2962
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
2963
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
2964
2965
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
2966
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
2967
2968
        /* Compute the tile-resolution-based coordinates for the window of interest */
2969
0
        if (h.cas == 0) {
2970
0
            win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
2971
0
            win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
2972
0
        } else {
2973
0
            win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
2974
0
            win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
2975
0
        }
2976
2977
0
        if (v.cas == 0) {
2978
0
            win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
2979
0
            win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
2980
0
        } else {
2981
0
            win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
2982
0
            win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
2983
0
        }
2984
2985
0
        for (j = 0; j < rh; ++j) {
2986
0
            if ((j >= win_ll_y0 && j < win_ll_y1) ||
2987
0
                    (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
2988
2989
                /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
2990
                /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
2991
                /* on opj_decompress -i  ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
2992
                /* This is less extreme than memsetting the whole buffer to 0 */
2993
                /* although we could potentially do better with better handling of edge conditions */
2994
0
                if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
2995
0
                    h.mem[win_tr_x1 - 1] = 0;
2996
0
                }
2997
0
                if (win_tr_x1 < rw) {
2998
0
                    h.mem[win_tr_x1] = 0;
2999
0
                }
3000
3001
0
                opj_dwt_interleave_partial_h(h.mem,
3002
0
                                             h.cas,
3003
0
                                             sa,
3004
0
                                             j,
3005
0
                                             (OPJ_UINT32)h.sn,
3006
0
                                             win_ll_x0,
3007
0
                                             win_ll_x1,
3008
0
                                             win_hl_x0,
3009
0
                                             win_hl_x1);
3010
0
                opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
3011
0
                                         (OPJ_INT32)win_ll_x0,
3012
0
                                         (OPJ_INT32)win_ll_x1,
3013
0
                                         (OPJ_INT32)win_hl_x0,
3014
0
                                         (OPJ_INT32)win_hl_x1);
3015
0
                if (!opj_sparse_array_int32_write(sa,
3016
0
                                                  win_tr_x0, j,
3017
0
                                                  win_tr_x1, j + 1,
3018
0
                                                  h.mem + win_tr_x0,
3019
0
                                                  1, 0, OPJ_TRUE)) {
3020
                    /* FIXME event manager error callback */
3021
0
                    opj_sparse_array_int32_free(sa);
3022
0
                    opj_aligned_free(h.mem);
3023
0
                    return OPJ_FALSE;
3024
0
                }
3025
0
            }
3026
0
        }
3027
3028
0
        for (i = win_tr_x0; i < win_tr_x1;) {
3029
0
            OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
3030
0
            opj_dwt_interleave_partial_v(v.mem,
3031
0
                                         v.cas,
3032
0
                                         sa,
3033
0
                                         i,
3034
0
                                         nb_cols,
3035
0
                                         (OPJ_UINT32)v.sn,
3036
0
                                         win_ll_y0,
3037
0
                                         win_ll_y1,
3038
0
                                         win_lh_y0,
3039
0
                                         win_lh_y1);
3040
0
            opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
3041
0
                                              (OPJ_INT32)win_ll_y0,
3042
0
                                              (OPJ_INT32)win_ll_y1,
3043
0
                                              (OPJ_INT32)win_lh_y0,
3044
0
                                              (OPJ_INT32)win_lh_y1);
3045
0
            if (!opj_sparse_array_int32_write(sa,
3046
0
                                              i, win_tr_y0,
3047
0
                                              i + nb_cols, win_tr_y1,
3048
0
                                              v.mem + 4 * win_tr_y0,
3049
0
                                              1, 4, OPJ_TRUE)) {
3050
                /* FIXME event manager error callback */
3051
0
                opj_sparse_array_int32_free(sa);
3052
0
                opj_aligned_free(h.mem);
3053
0
                return OPJ_FALSE;
3054
0
            }
3055
3056
0
            i += nb_cols;
3057
0
        }
3058
0
    }
3059
0
    opj_aligned_free(h.mem);
3060
3061
0
    {
3062
0
        OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3063
0
                       tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3064
0
                       tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3065
0
                       tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3066
0
                       tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3067
0
                       tilec->data_win,
3068
0
                       1, tr_max->win_x1 - tr_max->win_x0,
3069
0
                       OPJ_TRUE);
3070
0
        assert(ret);
3071
0
        OPJ_UNUSED(ret);
3072
0
    }
3073
0
    opj_sparse_array_int32_free(sa);
3074
0
    return OPJ_TRUE;
3075
0
}
3076
3077
static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
3078
                                   OPJ_FLOAT32* OPJ_RESTRICT a,
3079
                                   OPJ_UINT32 width,
3080
                                   OPJ_UINT32 remaining_height)
3081
3.00M
{
3082
3.00M
    OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
3083
3.00M
    OPJ_UINT32 i, k;
3084
3.00M
    OPJ_UINT32 x0 = dwt->win_l_x0;
3085
3.00M
    OPJ_UINT32 x1 = dwt->win_l_x1;
3086
3087
9.01M
    for (k = 0; k < 2; ++k) {
3088
6.01M
        if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
3089
6.01M
                ((OPJ_SIZE_T) bi & 0x0f) == 0) {
3090
            /* Fast code path */
3091
486M
            for (i = x0; i < x1; ++i) {
3092
481M
                OPJ_UINT32 j = i;
3093
481M
                OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
3094
481M
                dst[0] = a[j];
3095
481M
                j += width;
3096
481M
                dst[1] = a[j];
3097
481M
                j += width;
3098
481M
                dst[2] = a[j];
3099
481M
                j += width;
3100
481M
                dst[3] = a[j];
3101
481M
                j += width;
3102
481M
                dst[4] = a[j];
3103
481M
                j += width;
3104
481M
                dst[5] = a[j];
3105
481M
                j += width;
3106
481M
                dst[6] = a[j];
3107
481M
                j += width;
3108
481M
                dst[7] = a[j];
3109
481M
            }
3110
4.77M
        } else {
3111
            /* Slow code path */
3112
38.3M
            for (i = x0; i < x1; ++i) {
3113
37.1M
                OPJ_UINT32 j = i;
3114
37.1M
                OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
3115
37.1M
                dst[0] = a[j];
3116
37.1M
                j += width;
3117
37.1M
                if (remaining_height == 1) {
3118
1.25M
                    continue;
3119
1.25M
                }
3120
35.9M
                dst[1] = a[j];
3121
35.9M
                j += width;
3122
35.9M
                if (remaining_height == 2) {
3123
842k
                    continue;
3124
842k
                }
3125
35.0M
                dst[2] = a[j];
3126
35.0M
                j += width;
3127
35.0M
                if (remaining_height == 3) {
3128
2.01M
                    continue;
3129
2.01M
                }
3130
33.0M
                dst[3] = a[j];
3131
33.0M
                j += width;
3132
33.0M
                if (remaining_height == 4) {
3133
1.29M
                    continue;
3134
1.29M
                }
3135
31.7M
                dst[4] = a[j];
3136
31.7M
                j += width;
3137
31.7M
                if (remaining_height == 5) {
3138
2.68M
                    continue;
3139
2.68M
                }
3140
29.0M
                dst[5] = a[j];
3141
29.0M
                j += width;
3142
29.0M
                if (remaining_height == 6) {
3143
809k
                    continue;
3144
809k
                }
3145
28.2M
                dst[6] = a[j];
3146
28.2M
                j += width;
3147
28.2M
                if (remaining_height == 7) {
3148
440k
                    continue;
3149
440k
                }
3150
27.8M
                dst[7] = a[j];
3151
27.8M
            }
3152
1.23M
        }
3153
3154
6.01M
        bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
3155
6.01M
        a += dwt->sn;
3156
6.01M
        x0 = dwt->win_h_x0;
3157
6.01M
        x1 = dwt->win_h_x1;
3158
6.01M
    }
3159
3.00M
}
3160
3161
static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
3162
        opj_sparse_array_int32_t* sa,
3163
        OPJ_UINT32 sa_line,
3164
        OPJ_UINT32 remaining_height)
3165
0
{
3166
0
    OPJ_UINT32 i;
3167
0
    for (i = 0; i < remaining_height; i++) {
3168
0
        OPJ_BOOL ret;
3169
0
        ret = opj_sparse_array_int32_read(sa,
3170
0
                                          dwt->win_l_x0, sa_line + i,
3171
0
                                          dwt->win_l_x1, sa_line + i + 1,
3172
                                          /* Nasty cast from float* to int32* */
3173
0
                                          (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
3174
0
                                          2 * NB_ELTS_V8, 0, OPJ_TRUE);
3175
0
        assert(ret);
3176
0
        ret = opj_sparse_array_int32_read(sa,
3177
0
                                          (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
3178
0
                                          (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
3179
                                          /* Nasty cast from float* to int32* */
3180
0
                                          (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
3181
0
                                          2 * NB_ELTS_V8, 0, OPJ_TRUE);
3182
0
        assert(ret);
3183
0
        OPJ_UNUSED(ret);
3184
0
    }
3185
0
}
3186
3187
static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
3188
        OPJ_FLOAT32* OPJ_RESTRICT a,
3189
        OPJ_UINT32 width,
3190
        OPJ_UINT32 nb_elts_read)
3191
2.78M
{
3192
2.78M
    opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
3193
2.78M
    OPJ_UINT32 i;
3194
3195
262M
    for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
3196
259M
        memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
3197
259M
               (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
3198
259M
    }
3199
3200
2.78M
    a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
3201
2.78M
    bi = dwt->wavelet + 1 - dwt->cas;
3202
3203
261M
    for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
3204
258M
        memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
3205
258M
               (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
3206
258M
    }
3207
2.78M
}
3208
3209
static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
3210
        opj_sparse_array_int32_t* sa,
3211
        OPJ_UINT32 sa_col,
3212
        OPJ_UINT32 nb_elts_read)
3213
0
{
3214
0
    OPJ_BOOL ret;
3215
0
    ret = opj_sparse_array_int32_read(sa,
3216
0
                                      sa_col, dwt->win_l_x0,
3217
0
                                      sa_col + nb_elts_read, dwt->win_l_x1,
3218
0
                                      (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
3219
0
                                      1, 2 * NB_ELTS_V8, OPJ_TRUE);
3220
0
    assert(ret);
3221
0
    ret = opj_sparse_array_int32_read(sa,
3222
0
                                      sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
3223
0
                                      sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
3224
0
                                      (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
3225
0
                                      1, 2 * NB_ELTS_V8, OPJ_TRUE);
3226
0
    assert(ret);
3227
0
    OPJ_UNUSED(ret);
3228
0
}
3229
3230
#ifdef __SSE__
3231
3232
static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
3233
                                       OPJ_UINT32 start,
3234
                                       OPJ_UINT32 end,
3235
                                       const __m128 c)
3236
11.5M
{
3237
11.5M
    __m128* OPJ_RESTRICT vw = (__m128*) w;
3238
11.5M
    OPJ_UINT32 i = start;
3239
    /* To be adapted if NB_ELTS_V8 changes */
3240
11.5M
    vw += 4 * start;
3241
    /* Note: attempt at loop unrolling x2 doesn't help */
3242
1.04G
    for (; i < end; ++i, vw += 4) {
3243
1.03G
        vw[0] = _mm_mul_ps(vw[0], c);
3244
1.03G
        vw[1] = _mm_mul_ps(vw[1], c);
3245
1.03G
    }
3246
11.5M
}
3247
3248
static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
3249
                                       OPJ_UINT32 start,
3250
                                       OPJ_UINT32 end,
3251
                                       OPJ_UINT32 m,
3252
                                       __m128 c)
3253
23.1M
{
3254
23.1M
    __m128* OPJ_RESTRICT vl = (__m128*) l;
3255
23.1M
    __m128* OPJ_RESTRICT vw = (__m128*) w;
3256
    /* To be adapted if NB_ELTS_V8 changes */
3257
23.1M
    OPJ_UINT32 i;
3258
23.1M
    OPJ_UINT32 imax = opj_uint_min(end, m);
3259
23.1M
    if (start == 0) {
3260
23.1M
        if (imax >= 1) {
3261
23.1M
            vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
3262
23.1M
            vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
3263
23.1M
            vw += 4;
3264
23.1M
            start = 1;
3265
23.1M
        }
3266
23.1M
    } else {
3267
0
        vw += start * 4;
3268
0
    }
3269
3270
23.1M
    i = start;
3271
    /* Note: attempt at loop unrolling x2 doesn't help */
3272
2.06G
    for (; i < imax; ++i) {
3273
2.04G
        vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
3274
2.04G
        vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
3275
2.04G
        vw += 4;
3276
2.04G
    }
3277
23.1M
    if (m < end) {
3278
11.5M
        assert(m + 1 == end);
3279
11.5M
        c = _mm_add_ps(c, c);
3280
11.5M
        vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
3281
11.5M
        vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
3282
11.5M
    }
3283
23.1M
}
3284
3285
#else
3286
3287
static void opj_v8dwt_decode_step1(opj_v8_t* w,
3288
                                   OPJ_UINT32 start,
3289
                                   OPJ_UINT32 end,
3290
                                   const OPJ_FLOAT32 c)
3291
{
3292
    OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
3293
    OPJ_UINT32 i;
3294
    /* To be adapted if NB_ELTS_V8 changes */
3295
    for (i = start; i < end; ++i) {
3296
        fw[i * 2 * 8    ] = fw[i * 2 * 8    ] * c;
3297
        fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
3298
        fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
3299
        fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
3300
        fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
3301
        fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
3302
        fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
3303
        fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
3304
    }
3305
}
3306
3307
static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
3308
                                   OPJ_UINT32 start,
3309
                                   OPJ_UINT32 end,
3310
                                   OPJ_UINT32 m,
3311
                                   OPJ_FLOAT32 c)
3312
{
3313
    OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
3314
    OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
3315
    OPJ_UINT32 i;
3316
    OPJ_UINT32 imax = opj_uint_min(end, m);
3317
    if (start > 0) {
3318
        fw += 2 * NB_ELTS_V8 * start;
3319
        fl = fw - 2 * NB_ELTS_V8;
3320
    }
3321
    /* To be adapted if NB_ELTS_V8 changes */
3322
    for (i = start; i < imax; ++i) {
3323
        fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
3324
        fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
3325
        fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
3326
        fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
3327
        fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
3328
        fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
3329
        fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
3330
        fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
3331
        fl = fw;
3332
        fw += 2 * NB_ELTS_V8;
3333
    }
3334
    if (m < end) {
3335
        assert(m + 1 == end);
3336
        c += c;
3337
        fw[-8] = fw[-8] + fl[0] * c;
3338
        fw[-7] = fw[-7] + fl[1] * c;
3339
        fw[-6] = fw[-6] + fl[2] * c;
3340
        fw[-5] = fw[-5] + fl[3] * c;
3341
        fw[-4] = fw[-4] + fl[4] * c;
3342
        fw[-3] = fw[-3] + fl[5] * c;
3343
        fw[-2] = fw[-2] + fl[6] * c;
3344
        fw[-1] = fw[-1] + fl[7] * c;
3345
    }
3346
}
3347
3348
#endif
3349
3350
/* <summary>                             */
3351
/* Inverse 9-7 wavelet transform in 1-D. */
3352
/* </summary>                            */
3353
static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
3354
5.78M
{
3355
5.78M
    OPJ_INT32 a, b;
3356
    /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
3357
    /* Historic value for 2 / opj_invK */
3358
    /* Normally, we should use invK, but if we do so, we have failures in the */
3359
    /* conformance test, due to MSE and peak errors significantly higher than */
3360
    /* accepted value */
3361
    /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
3362
    /* the computation of the stepsize for the non LL subbands */
3363
5.78M
    const float two_invK = 1.625732422f;
3364
5.78M
    if (dwt->cas == 0) {
3365
5.78M
        if (!((dwt->dn > 0) || (dwt->sn > 1))) {
3366
0
            return;
3367
0
        }
3368
5.78M
        a = 0;
3369
5.78M
        b = 1;
3370
5.78M
    } else {
3371
372
        if (!((dwt->sn > 0) || (dwt->dn > 1))) {
3372
0
            return;
3373
0
        }
3374
372
        a = 1;
3375
372
        b = 0;
3376
372
    }
3377
5.78M
#ifdef __SSE__
3378
5.78M
    opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
3379
5.78M
                               _mm_set1_ps(opj_K));
3380
5.78M
    opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
3381
5.78M
                               _mm_set1_ps(two_invK));
3382
5.78M
    opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
3383
5.78M
                               dwt->win_l_x0, dwt->win_l_x1,
3384
5.78M
                               (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3385
5.78M
                               _mm_set1_ps(-opj_dwt_delta));
3386
5.78M
    opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3387
5.78M
                               dwt->win_h_x0, dwt->win_h_x1,
3388
5.78M
                               (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3389
5.78M
                               _mm_set1_ps(-opj_dwt_gamma));
3390
5.78M
    opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
3391
5.78M
                               dwt->win_l_x0, dwt->win_l_x1,
3392
5.78M
                               (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3393
5.78M
                               _mm_set1_ps(-opj_dwt_beta));
3394
5.78M
    opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
3395
5.78M
                               dwt->win_h_x0, dwt->win_h_x1,
3396
5.78M
                               (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3397
5.78M
                               _mm_set1_ps(-opj_dwt_alpha));
3398
#else
3399
    opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
3400
                           opj_K);
3401
    opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
3402
                           two_invK);
3403
    opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3404
                           dwt->win_l_x0, dwt->win_l_x1,
3405
                           (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3406
                           -opj_dwt_delta);
3407
    opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3408
                           dwt->win_h_x0, dwt->win_h_x1,
3409
                           (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3410
                           -opj_dwt_gamma);
3411
    opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
3412
                           dwt->win_l_x0, dwt->win_l_x1,
3413
                           (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
3414
                           -opj_dwt_beta);
3415
    opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
3416
                           dwt->win_h_x0, dwt->win_h_x1,
3417
                           (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
3418
                           -opj_dwt_alpha);
3419
#endif
3420
5.78M
}
3421
3422
typedef struct {
3423
    opj_v8dwt_t h;
3424
    OPJ_UINT32 rw;
3425
    OPJ_UINT32 w;
3426
    OPJ_FLOAT32 * OPJ_RESTRICT aj;
3427
    OPJ_UINT32 nb_rows;
3428
} opj_dwt97_decode_h_job_t;
3429
3430
static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
3431
0
{
3432
0
    OPJ_UINT32 j;
3433
0
    opj_dwt97_decode_h_job_t* job;
3434
0
    OPJ_FLOAT32 * OPJ_RESTRICT aj;
3435
0
    OPJ_UINT32 w;
3436
0
    (void)tls;
3437
3438
0
    job = (opj_dwt97_decode_h_job_t*)user_data;
3439
0
    w = job->w;
3440
3441
0
    assert((job->nb_rows % NB_ELTS_V8) == 0);
3442
3443
0
    aj = job->aj;
3444
0
    for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
3445
0
        OPJ_UINT32 k;
3446
0
        opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
3447
0
        opj_v8dwt_decode(&job->h);
3448
3449
        /* To be adapted if NB_ELTS_V8 changes */
3450
0
        for (k = 0; k < job->rw; k++) {
3451
0
            aj[k      ] = job->h.wavelet[k].f[0];
3452
0
            aj[k + (OPJ_SIZE_T)w  ] = job->h.wavelet[k].f[1];
3453
0
            aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
3454
0
            aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
3455
0
        }
3456
0
        for (k = 0; k < job->rw; k++) {
3457
0
            aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
3458
0
            aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
3459
0
            aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
3460
0
            aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
3461
0
        }
3462
3463
0
        aj += w * NB_ELTS_V8;
3464
0
    }
3465
3466
0
    opj_aligned_free(job->h.wavelet);
3467
0
    opj_free(job);
3468
0
}
3469
3470
3471
typedef struct {
3472
    opj_v8dwt_t v;
3473
    OPJ_UINT32 rh;
3474
    OPJ_UINT32 w;
3475
    OPJ_FLOAT32 * OPJ_RESTRICT aj;
3476
    OPJ_UINT32 nb_columns;
3477
} opj_dwt97_decode_v_job_t;
3478
3479
static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
3480
0
{
3481
0
    OPJ_UINT32 j;
3482
0
    opj_dwt97_decode_v_job_t* job;
3483
0
    OPJ_FLOAT32 * OPJ_RESTRICT aj;
3484
0
    (void)tls;
3485
3486
0
    job = (opj_dwt97_decode_v_job_t*)user_data;
3487
3488
0
    assert((job->nb_columns % NB_ELTS_V8) == 0);
3489
3490
0
    aj = job->aj;
3491
0
    for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
3492
0
        OPJ_UINT32 k;
3493
3494
0
        opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
3495
0
        opj_v8dwt_decode(&job->v);
3496
3497
0
        for (k = 0; k < job->rh; ++k) {
3498
0
            memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
3499
0
                   NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3500
0
        }
3501
0
        aj += NB_ELTS_V8;
3502
0
    }
3503
3504
0
    opj_aligned_free(job->v.wavelet);
3505
0
    opj_free(job);
3506
0
}
3507
3508
3509
/* <summary>                             */
3510
/* Inverse 9-7 wavelet transform in 2-D. */
3511
/* </summary>                            */
3512
static
3513
OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
3514
                                opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3515
                                OPJ_UINT32 numres)
3516
56.9k
{
3517
56.9k
    opj_v8dwt_t h;
3518
56.9k
    opj_v8dwt_t v;
3519
3520
56.9k
    opj_tcd_resolution_t* res = tilec->resolutions;
3521
3522
56.9k
    OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
3523
56.9k
                                 res->x0);    /* width of the resolution level computed */
3524
56.9k
    OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
3525
56.9k
                                 res->y0);    /* height of the resolution level computed */
3526
3527
56.9k
    OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
3528
56.9k
                                                               1].x1 -
3529
56.9k
                                tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
3530
3531
56.9k
    OPJ_SIZE_T l_data_size;
3532
56.9k
    const int num_threads = opj_thread_pool_get_thread_count(tp);
3533
3534
56.9k
    if (numres == 1) {
3535
0
        return OPJ_TRUE;
3536
0
    }
3537
3538
56.9k
    l_data_size = opj_dwt_max_resolution(res, numres);
3539
    /* overflow check */
3540
56.9k
    if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3541
        /* FIXME event manager error callback */
3542
0
        return OPJ_FALSE;
3543
0
    }
3544
56.9k
    h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3545
56.9k
    if (!h.wavelet) {
3546
        /* FIXME event manager error callback */
3547
0
        return OPJ_FALSE;
3548
0
    }
3549
56.9k
    v.wavelet = h.wavelet;
3550
3551
321k
    while (--numres) {
3552
265k
        OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
3553
265k
        OPJ_UINT32 j;
3554
3555
265k
        h.sn = (OPJ_INT32)rw;
3556
265k
        v.sn = (OPJ_INT32)rh;
3557
3558
265k
        ++res;
3559
3560
265k
        rw = (OPJ_UINT32)(res->x1 -
3561
265k
                          res->x0);   /* width of the resolution level computed */
3562
265k
        rh = (OPJ_UINT32)(res->y1 -
3563
265k
                          res->y0);   /* height of the resolution level computed */
3564
3565
265k
        h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3566
265k
        h.cas = res->x0 % 2;
3567
3568
265k
        h.win_l_x0 = 0;
3569
265k
        h.win_l_x1 = (OPJ_UINT32)h.sn;
3570
265k
        h.win_h_x0 = 0;
3571
265k
        h.win_h_x1 = (OPJ_UINT32)h.dn;
3572
3573
265k
        if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
3574
3.14M
            for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3575
2.88M
                OPJ_UINT32 k;
3576
2.88M
                opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
3577
2.88M
                opj_v8dwt_decode(&h);
3578
3579
                /* To be adapted if NB_ELTS_V8 changes */
3580
512M
                for (k = 0; k < rw; k++) {
3581
509M
                    aj[k      ] = h.wavelet[k].f[0];
3582
509M
                    aj[k + (OPJ_SIZE_T)w  ] = h.wavelet[k].f[1];
3583
509M
                    aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
3584
509M
                    aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
3585
509M
                }
3586
512M
                for (k = 0; k < rw; k++) {
3587
509M
                    aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
3588
509M
                    aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
3589
509M
                    aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
3590
509M
                    aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
3591
509M
                }
3592
3593
2.88M
                aj += w * NB_ELTS_V8;
3594
2.88M
            }
3595
265k
        } else {
3596
0
            OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
3597
0
            OPJ_UINT32 step_j;
3598
3599
0
            if ((rh / NB_ELTS_V8) < num_jobs) {
3600
0
                num_jobs = rh / NB_ELTS_V8;
3601
0
            }
3602
0
            step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3603
0
            for (j = 0; j < num_jobs; j++) {
3604
0
                opj_dwt97_decode_h_job_t* job;
3605
3606
0
                job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
3607
0
                if (!job) {
3608
0
                    opj_thread_pool_wait_completion(tp, 0);
3609
0
                    opj_aligned_free(h.wavelet);
3610
0
                    return OPJ_FALSE;
3611
0
                }
3612
0
                job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3613
0
                if (!job->h.wavelet) {
3614
0
                    opj_thread_pool_wait_completion(tp, 0);
3615
0
                    opj_free(job);
3616
0
                    opj_aligned_free(h.wavelet);
3617
0
                    return OPJ_FALSE;
3618
0
                }
3619
0
                job->h.dn = h.dn;
3620
0
                job->h.sn = h.sn;
3621
0
                job->h.cas = h.cas;
3622
0
                job->h.win_l_x0 = h.win_l_x0;
3623
0
                job->h.win_l_x1 = h.win_l_x1;
3624
0
                job->h.win_h_x0 = h.win_h_x0;
3625
0
                job->h.win_h_x1 = h.win_h_x1;
3626
0
                job->rw = rw;
3627
0
                job->w = w;
3628
0
                job->aj = aj;
3629
0
                job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
3630
0
                                                      (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3631
0
                aj += w * job->nb_rows;
3632
0
                opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
3633
0
            }
3634
0
            opj_thread_pool_wait_completion(tp, 0);
3635
0
            j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
3636
0
        }
3637
3638
265k
        if (j < rh) {
3639
124k
            OPJ_UINT32 k;
3640
124k
            opj_v8dwt_interleave_h(&h, aj, w, rh - j);
3641
124k
            opj_v8dwt_decode(&h);
3642
9.46M
            for (k = 0; k < rw; k++) {
3643
9.34M
                OPJ_UINT32 l;
3644
44.8M
                for (l = 0; l < rh - j; l++) {
3645
35.5M
                    aj[k + (OPJ_SIZE_T)w  * l ] = h.wavelet[k].f[l];
3646
35.5M
                }
3647
9.34M
            }
3648
124k
        }
3649
3650
265k
        v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3651
265k
        v.cas = res->y0 % 2;
3652
265k
        v.win_l_x0 = 0;
3653
265k
        v.win_l_x1 = (OPJ_UINT32)v.sn;
3654
265k
        v.win_h_x0 = 0;
3655
265k
        v.win_h_x1 = (OPJ_UINT32)v.dn;
3656
3657
265k
        aj = (OPJ_FLOAT32*) tilec->data;
3658
265k
        if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
3659
2.93M
            for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
3660
2.66M
                OPJ_UINT32 k;
3661
3662
2.66M
                opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
3663
2.66M
                opj_v8dwt_decode(&v);
3664
3665
512M
                for (k = 0; k < rh; ++k) {
3666
509M
                    memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
3667
509M
                }
3668
2.66M
                aj += NB_ELTS_V8;
3669
2.66M
            }
3670
265k
        } else {
3671
            /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
3672
                transfer being the limiting factor. So limit the number of
3673
                threads.
3674
             */
3675
0
            OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
3676
0
            OPJ_UINT32 step_j;
3677
3678
0
            if ((rw / NB_ELTS_V8) < num_jobs) {
3679
0
                num_jobs = rw / NB_ELTS_V8;
3680
0
            }
3681
0
            step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
3682
0
            for (j = 0; j < num_jobs; j++) {
3683
0
                opj_dwt97_decode_v_job_t* job;
3684
3685
0
                job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
3686
0
                if (!job) {
3687
0
                    opj_thread_pool_wait_completion(tp, 0);
3688
0
                    opj_aligned_free(h.wavelet);
3689
0
                    return OPJ_FALSE;
3690
0
                }
3691
0
                job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3692
0
                if (!job->v.wavelet) {
3693
0
                    opj_thread_pool_wait_completion(tp, 0);
3694
0
                    opj_free(job);
3695
0
                    opj_aligned_free(h.wavelet);
3696
0
                    return OPJ_FALSE;
3697
0
                }
3698
0
                job->v.dn = v.dn;
3699
0
                job->v.sn = v.sn;
3700
0
                job->v.cas = v.cas;
3701
0
                job->v.win_l_x0 = v.win_l_x0;
3702
0
                job->v.win_l_x1 = v.win_l_x1;
3703
0
                job->v.win_h_x0 = v.win_h_x0;
3704
0
                job->v.win_h_x1 = v.win_h_x1;
3705
0
                job->rh = rh;
3706
0
                job->w = w;
3707
0
                job->aj = aj;
3708
0
                job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
3709
0
                                  (NB_ELTS_V8 - 1)) - j * step_j : step_j;
3710
0
                aj += job->nb_columns;
3711
0
                opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
3712
0
            }
3713
0
            opj_thread_pool_wait_completion(tp, 0);
3714
0
        }
3715
3716
265k
        if (rw & (NB_ELTS_V8 - 1)) {
3717
115k
            OPJ_UINT32 k;
3718
3719
115k
            j = rw & (NB_ELTS_V8 - 1);
3720
3721
115k
            opj_v8dwt_interleave_v(&v, aj, w, j);
3722
115k
            opj_v8dwt_decode(&v);
3723
3724
8.69M
            for (k = 0; k < rh; ++k) {
3725
8.57M
                memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
3726
8.57M
                       (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
3727
8.57M
            }
3728
115k
        }
3729
265k
    }
3730
3731
56.9k
    opj_aligned_free(h.wavelet);
3732
56.9k
    return OPJ_TRUE;
3733
56.9k
}
3734
3735
static
3736
OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3737
                                   OPJ_UINT32 numres)
3738
0
{
3739
0
    opj_sparse_array_int32_t* sa;
3740
0
    opj_v8dwt_t h;
3741
0
    opj_v8dwt_t v;
3742
0
    OPJ_UINT32 resno;
3743
    /* This value matches the maximum left/right extension given in tables */
3744
    /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
3745
    /* we currently use 3. */
3746
0
    const OPJ_UINT32 filter_width = 4U;
3747
3748
0
    opj_tcd_resolution_t* tr = tilec->resolutions;
3749
0
    opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
3750
3751
0
    OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
3752
0
                                 tr->x0);    /* width of the resolution level computed */
3753
0
    OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
3754
0
                                 tr->y0);    /* height of the resolution level computed */
3755
3756
0
    OPJ_SIZE_T l_data_size;
3757
3758
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
3759
    /* with the tile coordinates */
3760
0
    OPJ_UINT32 win_tcx0 = tilec->win_x0;
3761
0
    OPJ_UINT32 win_tcy0 = tilec->win_y0;
3762
0
    OPJ_UINT32 win_tcx1 = tilec->win_x1;
3763
0
    OPJ_UINT32 win_tcy1 = tilec->win_y1;
3764
3765
0
    if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
3766
0
        return OPJ_TRUE;
3767
0
    }
3768
3769
0
    sa = opj_dwt_init_sparse_array(tilec, numres);
3770
0
    if (sa == NULL) {
3771
0
        return OPJ_FALSE;
3772
0
    }
3773
3774
0
    if (numres == 1U) {
3775
0
        OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3776
0
                       tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3777
0
                       tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3778
0
                       tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3779
0
                       tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3780
0
                       tilec->data_win,
3781
0
                       1, tr_max->win_x1 - tr_max->win_x0,
3782
0
                       OPJ_TRUE);
3783
0
        assert(ret);
3784
0
        OPJ_UNUSED(ret);
3785
0
        opj_sparse_array_int32_free(sa);
3786
0
        return OPJ_TRUE;
3787
0
    }
3788
3789
0
    l_data_size = opj_dwt_max_resolution(tr, numres);
3790
    /* overflow check */
3791
0
    if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
3792
        /* FIXME event manager error callback */
3793
0
        opj_sparse_array_int32_free(sa);
3794
0
        return OPJ_FALSE;
3795
0
    }
3796
0
    h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
3797
0
    if (!h.wavelet) {
3798
        /* FIXME event manager error callback */
3799
0
        opj_sparse_array_int32_free(sa);
3800
0
        return OPJ_FALSE;
3801
0
    }
3802
0
    v.wavelet = h.wavelet;
3803
3804
0
    for (resno = 1; resno < numres; resno ++) {
3805
0
        OPJ_UINT32 j;
3806
        /* Window of interest subband-based coordinates */
3807
0
        OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
3808
0
        OPJ_UINT32 win_hl_x0, win_hl_x1;
3809
0
        OPJ_UINT32 win_lh_y0, win_lh_y1;
3810
        /* Window of interest tile-resolution-based coordinates */
3811
0
        OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
3812
        /* Tile-resolution subband-based coordinates */
3813
0
        OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
3814
3815
0
        ++tr;
3816
3817
0
        h.sn = (OPJ_INT32)rw;
3818
0
        v.sn = (OPJ_INT32)rh;
3819
3820
0
        rw = (OPJ_UINT32)(tr->x1 - tr->x0);
3821
0
        rh = (OPJ_UINT32)(tr->y1 - tr->y0);
3822
3823
0
        h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
3824
0
        h.cas = tr->x0 % 2;
3825
3826
0
        v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
3827
0
        v.cas = tr->y0 % 2;
3828
3829
        /* Get the subband coordinates for the window of interest */
3830
        /* LL band */
3831
0
        opj_dwt_get_band_coordinates(tilec, resno, 0,
3832
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3833
0
                                     &win_ll_x0, &win_ll_y0,
3834
0
                                     &win_ll_x1, &win_ll_y1);
3835
3836
        /* HL band */
3837
0
        opj_dwt_get_band_coordinates(tilec, resno, 1,
3838
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3839
0
                                     &win_hl_x0, NULL, &win_hl_x1, NULL);
3840
3841
        /* LH band */
3842
0
        opj_dwt_get_band_coordinates(tilec, resno, 2,
3843
0
                                     win_tcx0, win_tcy0, win_tcx1, win_tcy1,
3844
0
                                     NULL, &win_lh_y0, NULL, &win_lh_y1);
3845
3846
        /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
3847
0
        tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
3848
0
        tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
3849
0
        tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
3850
0
        tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
3851
3852
        /* Subtract the origin of the bands for this tile, to the subwindow */
3853
        /* of interest band coordinates, so as to get them relative to the */
3854
        /* tile */
3855
0
        win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
3856
0
        win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
3857
0
        win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
3858
0
        win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
3859
0
        win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
3860
0
        win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
3861
0
        win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
3862
0
        win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
3863
3864
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
3865
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
3866
3867
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
3868
0
        opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
3869
3870
        /* Compute the tile-resolution-based coordinates for the window of interest */
3871
0
        if (h.cas == 0) {
3872
0
            win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
3873
0
            win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
3874
0
        } else {
3875
0
            win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
3876
0
            win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
3877
0
        }
3878
3879
0
        if (v.cas == 0) {
3880
0
            win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
3881
0
            win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
3882
0
        } else {
3883
0
            win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
3884
0
            win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
3885
0
        }
3886
3887
0
        h.win_l_x0 = win_ll_x0;
3888
0
        h.win_l_x1 = win_ll_x1;
3889
0
        h.win_h_x0 = win_hl_x0;
3890
0
        h.win_h_x1 = win_hl_x1;
3891
0
        for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
3892
0
            if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3893
0
                    (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3894
0
                     j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
3895
0
                opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
3896
0
                opj_v8dwt_decode(&h);
3897
0
                if (!opj_sparse_array_int32_write(sa,
3898
0
                                                  win_tr_x0, j,
3899
0
                                                  win_tr_x1, j + NB_ELTS_V8,
3900
0
                                                  (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3901
0
                                                  NB_ELTS_V8, 1, OPJ_TRUE)) {
3902
                    /* FIXME event manager error callback */
3903
0
                    opj_sparse_array_int32_free(sa);
3904
0
                    opj_aligned_free(h.wavelet);
3905
0
                    return OPJ_FALSE;
3906
0
                }
3907
0
            }
3908
0
        }
3909
3910
0
        if (j < rh &&
3911
0
                ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
3912
0
                 (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
3913
0
                  j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
3914
0
            opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
3915
0
            opj_v8dwt_decode(&h);
3916
0
            if (!opj_sparse_array_int32_write(sa,
3917
0
                                              win_tr_x0, j,
3918
0
                                              win_tr_x1, rh,
3919
0
                                              (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
3920
0
                                              NB_ELTS_V8, 1, OPJ_TRUE)) {
3921
                /* FIXME event manager error callback */
3922
0
                opj_sparse_array_int32_free(sa);
3923
0
                opj_aligned_free(h.wavelet);
3924
0
                return OPJ_FALSE;
3925
0
            }
3926
0
        }
3927
3928
0
        v.win_l_x0 = win_ll_y0;
3929
0
        v.win_l_x1 = win_ll_y1;
3930
0
        v.win_h_x0 = win_lh_y0;
3931
0
        v.win_h_x1 = win_lh_y1;
3932
0
        for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
3933
0
            OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
3934
3935
0
            opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
3936
0
            opj_v8dwt_decode(&v);
3937
3938
0
            if (!opj_sparse_array_int32_write(sa,
3939
0
                                              j, win_tr_y0,
3940
0
                                              j + nb_elts, win_tr_y1,
3941
0
                                              (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
3942
0
                                              1, NB_ELTS_V8, OPJ_TRUE)) {
3943
                /* FIXME event manager error callback */
3944
0
                opj_sparse_array_int32_free(sa);
3945
0
                opj_aligned_free(h.wavelet);
3946
0
                return OPJ_FALSE;
3947
0
            }
3948
0
        }
3949
0
    }
3950
3951
0
    {
3952
0
        OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
3953
0
                       tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
3954
0
                       tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
3955
0
                       tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
3956
0
                       tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
3957
0
                       tilec->data_win,
3958
0
                       1, tr_max->win_x1 - tr_max->win_x0,
3959
0
                       OPJ_TRUE);
3960
0
        assert(ret);
3961
0
        OPJ_UNUSED(ret);
3962
0
    }
3963
0
    opj_sparse_array_int32_free(sa);
3964
3965
0
    opj_aligned_free(h.wavelet);
3966
0
    return OPJ_TRUE;
3967
0
}
3968
3969
3970
OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
3971
                             opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
3972
                             OPJ_UINT32 numres)
3973
56.9k
{
3974
56.9k
    if (p_tcd->whole_tile_decoding) {
3975
56.9k
        return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
3976
56.9k
    } else {
3977
0
        return opj_dwt_decode_partial_97(tilec, numres);
3978
0
    }
3979
56.9k
}