Coverage Report

Created: 2026-05-16 06:41

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