Coverage Report

Created: 2026-03-31 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openjpeg/src/lib/openjp2/ht_dec.c
Line
Count
Source
1
//***************************************************************************/
2
// This software is released under the 2-Clause BSD license, included
3
// below.
4
//
5
// Copyright (c) 2021, Aous Naman
6
// Copyright (c) 2021, Kakadu Software Pty Ltd, Australia
7
// Copyright (c) 2021, The University of New South Wales, Australia
8
//
9
// Redistribution and use in source and binary forms, with or without
10
// modification, are permitted provided that the following conditions are
11
// met:
12
//
13
// 1. Redistributions of source code must retain the above copyright
14
// notice, this list of conditions and the following disclaimer.
15
//
16
// 2. Redistributions in binary form must reproduce the above copyright
17
// notice, this list of conditions and the following disclaimer in the
18
// documentation and/or other materials provided with the distribution.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
//***************************************************************************/
32
// This file is part of the OpenJpeg software implementation.
33
// File: ht_dec.c
34
// Author: Aous Naman
35
// Date: 01 September 2021
36
//***************************************************************************/
37
38
//***************************************************************************/
39
/** @file ht_dec.c
40
 *  @brief implements HTJ2K block decoder
41
 */
42
43
#include <assert.h>
44
#include <string.h>
45
#include "opj_includes.h"
46
47
#include "t1_ht_luts.h"
48
49
/////////////////////////////////////////////////////////////////////////////
50
// compiler detection
51
/////////////////////////////////////////////////////////////////////////////
52
#ifdef _MSC_VER
53
#define OPJ_COMPILER_MSVC
54
#elif (defined __GNUC__)
55
#define OPJ_COMPILER_GNUC
56
#endif
57
58
#if defined(OPJ_COMPILER_MSVC) && defined(_M_ARM64) \
59
    && !defined(_M_ARM64EC) && !defined(_M_CEE_PURE) && !defined(__CUDACC__) \
60
    && !defined(__INTEL_COMPILER) && !defined(__clang__)
61
#define MSVC_NEON_INTRINSICS
62
#endif
63
64
#ifdef MSVC_NEON_INTRINSICS
65
#include <arm64_neon.h>
66
#endif
67
68
//************************************************************************/
69
/** @brief Displays the error message for disabling the decoding of SPP and
70
  * MRP passes
71
  */
72
static OPJ_BOOL only_cleanup_pass_is_decoded = OPJ_FALSE;
73
74
//************************************************************************/
75
/** @brief Generates population count (i.e., the number of set bits)
76
  *
77
  *   @param [in]  val is the value for which population count is sought
78
  */
79
static INLINE
80
OPJ_UINT32 population_count(OPJ_UINT32 val)
81
54.5k
{
82
#if defined(OPJ_COMPILER_MSVC) && (defined(_M_IX86) || defined(_M_AMD64))
83
    return (OPJ_UINT32)__popcnt(val);
84
#elif defined(OPJ_COMPILER_MSVC) && defined(MSVC_NEON_INTRINSICS)
85
    const __n64 temp = neon_cnt(__uint64ToN64_v(val));
86
    return neon_addv8(temp).n8_i8[0];
87
#elif (defined OPJ_COMPILER_GNUC)
88
    return (OPJ_UINT32)__builtin_popcount(val);
89
#else
90
    val -= ((val >> 1) & 0x55555555);
91
    val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
92
    val = (((val >> 4) + val) & 0x0f0f0f0f);
93
    val += (val >> 8);
94
    val += (val >> 16);
95
    return (OPJ_UINT32)(val & 0x0000003f);
96
#endif
97
54.5k
}
98
99
//************************************************************************/
100
/** @brief Counts the number of leading zeros
101
  *
102
  *   @param [in]  val is the value for which leading zero count is sought
103
  */
104
#ifdef OPJ_COMPILER_MSVC
105
#pragma intrinsic(_BitScanReverse)
106
#endif
107
static INLINE
108
OPJ_UINT32 count_leading_zeros(OPJ_UINT32 val)
109
114k
{
110
#ifdef OPJ_COMPILER_MSVC
111
    unsigned long result = 0;
112
    _BitScanReverse(&result, val);
113
    return 31U ^ (OPJ_UINT32)result;
114
#elif (defined OPJ_COMPILER_GNUC)
115
    return (OPJ_UINT32)__builtin_clz(val);
116
#else
117
    val |= (val >> 1);
118
    val |= (val >> 2);
119
    val |= (val >> 4);
120
    val |= (val >> 8);
121
    val |= (val >> 16);
122
    return 32U - population_count(val);
123
#endif
124
114k
}
125
126
//************************************************************************/
127
/** @brief Read a little-endian serialized UINT32.
128
  *
129
  *   @param [in]  dataIn pointer to byte stream to read from
130
  */
131
static INLINE OPJ_UINT32 read_le_uint32(const void* dataIn)
132
74.9k
{
133
#if defined(OPJ_BIG_ENDIAN)
134
    const OPJ_UINT8* data = (const OPJ_UINT8*)dataIn;
135
    return ((OPJ_UINT32)data[0]) | (OPJ_UINT32)(data[1] << 8) | (OPJ_UINT32)(
136
               data[2] << 16) | (((
137
                                      OPJ_UINT32)data[3]) <<
138
                                 24U);
139
#else
140
74.9k
    return *(OPJ_UINT32*)dataIn;
141
74.9k
#endif
142
74.9k
}
143
144
//************************************************************************/
145
/** @brief MEL state structure for reading and decoding the MEL bitstream
146
  *
147
  *  A number of events is decoded from the MEL bitstream ahead of time
148
  *  and stored in run/num_runs.
149
  *  Each run represents the number of zero events before a one event.
150
  */
151
typedef struct dec_mel {
152
    // data decoding machinery
153
    OPJ_UINT8* data;  //!<the address of data (or bitstream)
154
    OPJ_UINT64 tmp;   //!<temporary buffer for read data
155
    int bits;         //!<number of bits stored in tmp
156
    int size;         //!<number of bytes in MEL code
157
    OPJ_BOOL unstuff; //!<true if the next bit needs to be unstuffed
158
    int k;            //!<state of MEL decoder
159
160
    // queue of decoded runs
161
    int num_runs;    //!<number of decoded runs left in runs (maximum 8)
162
    OPJ_UINT64 runs; //!<runs of decoded MEL codewords (7 bits/run)
163
} dec_mel_t;
164
165
//************************************************************************/
166
/** @brief Reads and unstuffs the MEL bitstream
167
  *
168
  *  This design needs more bytes in the codeblock buffer than the length
169
  *  of the cleanup pass by up to 2 bytes.
170
  *
171
  *  Unstuffing removes the MSB of the byte following a byte whose
172
  *  value is 0xFF; this prevents sequences larger than 0xFF7F in value
173
  *  from appearing the bitstream.
174
  *
175
  *  @param [in]  melp is a pointer to dec_mel_t structure
176
  */
177
static INLINE
178
void mel_read(dec_mel_t *melp)
179
9.97k
{
180
9.97k
    OPJ_UINT32 val;
181
9.97k
    int bits;
182
9.97k
    OPJ_UINT32 t;
183
9.97k
    OPJ_BOOL unstuff;
184
185
9.97k
    if (melp->bits > 32) { //there are enough bits in the tmp variable
186
0
        return;    // return without reading new data
187
0
    }
188
189
9.97k
    val = 0xFFFFFFFF;      // feed in 0xFF if buffer is exhausted
190
9.97k
    if (melp->size > 4) {  // if there is more than 4 bytes the MEL segment
191
7.07k
        val = read_le_uint32(melp->data);  // read 32 bits from MEL data
192
7.07k
        melp->data += 4;           // advance pointer
193
7.07k
        melp->size -= 4;           // reduce counter
194
7.07k
    } else if (melp->size > 0) { // 4 or less
195
1.57k
        OPJ_UINT32 m, v;
196
1.57k
        int i = 0;
197
3.15k
        while (melp->size > 1) {
198
1.57k
            OPJ_UINT32 v = *melp->data++; // read one byte at a time
199
1.57k
            OPJ_UINT32 m = ~(0xFFu << i); // mask of location
200
1.57k
            val = (val & m) | (v << i);   // put byte in its correct location
201
1.57k
            --melp->size;
202
1.57k
            i += 8;
203
1.57k
        }
204
        // size equal to 1
205
1.57k
        v = *melp->data++;  // the one before the last is different
206
1.57k
        v |= 0xF;                         // MEL and VLC segments can overlap
207
1.57k
        m = ~(0xFFu << i);
208
1.57k
        val = (val & m) | (v << i);
209
1.57k
        --melp->size;
210
1.57k
    }
211
212
    // next we unstuff them before adding them to the buffer
213
9.97k
    bits = 32 - melp->unstuff;      // number of bits in val, subtract 1 if
214
    // the previously read byte requires
215
    // unstuffing
216
217
    // data is unstuffed and accumulated in t
218
    // bits has the number of bits in t
219
9.97k
    t = val & 0xFF;
220
9.97k
    unstuff = ((val & 0xFF) == 0xFF); // true if the byte needs unstuffing
221
9.97k
    bits -= unstuff; // there is one less bit in t if unstuffing is needed
222
9.97k
    t = t << (8 - unstuff); // move up to make room for the next byte
223
224
    //this is a repeat of the above
225
9.97k
    t |= (val >> 8) & 0xFF;
226
9.97k
    unstuff = (((val >> 8) & 0xFF) == 0xFF);
227
9.97k
    bits -= unstuff;
228
9.97k
    t = t << (8 - unstuff);
229
230
9.97k
    t |= (val >> 16) & 0xFF;
231
9.97k
    unstuff = (((val >> 16) & 0xFF) == 0xFF);
232
9.97k
    bits -= unstuff;
233
9.97k
    t = t << (8 - unstuff);
234
235
9.97k
    t |= (val >> 24) & 0xFF;
236
9.97k
    melp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
237
238
    // move t to tmp, and push the result all the way up, so we read from
239
    // the MSB
240
9.97k
    melp->tmp |= ((OPJ_UINT64)t) << (64 - bits - melp->bits);
241
9.97k
    melp->bits += bits; //increment the number of bits in tmp
242
9.97k
}
243
244
//************************************************************************/
245
/** @brief Decodes unstuffed MEL segment bits stored in tmp to runs
246
  *
247
  *  Runs are stored in "runs" and the number of runs in "num_runs".
248
  *  Each run represents a number of zero events that may or may not
249
  *  terminate in a 1 event.
250
  *  Each run is stored in 7 bits.  The LSB is 1 if the run terminates in
251
  *  a 1 event, 0 otherwise.  The next 6 bits, for the case terminating
252
  *  with 1, contain the number of consecutive 0 zero events * 2; for the
253
  *  case terminating with 0, they store (number of consecutive 0 zero
254
  *  events - 1) * 2.
255
  *  A total of 6 bits (made up of 1 + 5) should have been enough.
256
  *
257
  *  @param [in]  melp is a pointer to dec_mel_t structure
258
  */
259
static INLINE
260
void mel_decode(dec_mel_t *melp)
261
40.7k
{
262
40.7k
    static const int mel_exp[13] = { //MEL exponents
263
40.7k
        0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5
264
40.7k
    };
265
266
40.7k
    if (melp->bits < 6) { // if there are less than 6 bits in tmp
267
9.97k
        mel_read(melp);    // then read from the MEL bitstream
268
9.97k
    }
269
    // 6 bits is the largest decodable MEL cwd
270
271
    //repeat so long that there is enough decodable bits in tmp,
272
    // and the runs store is not full (num_runs < 8)
273
322k
    while (melp->bits >= 6 && melp->num_runs < 8) {
274
281k
        int eval = mel_exp[melp->k]; // number of bits associated with state
275
281k
        int run = 0;
276
281k
        if (melp->tmp & (1ull << 63)) { //The next bit to decode (stored in MSB)
277
            //one is found
278
143k
            run = 1 << eval;
279
143k
            run--; // consecutive runs of 0 events - 1
280
143k
            melp->k = melp->k + 1 < 12 ? melp->k + 1 : 12;//increment, max is 12
281
143k
            melp->tmp <<= 1; // consume one bit from tmp
282
143k
            melp->bits -= 1;
283
143k
            run = run << 1; // a stretch of zeros not terminating in one
284
143k
        } else {
285
            //0 is found
286
138k
            run = (int)(melp->tmp >> (63 - eval)) & ((1 << eval) - 1);
287
138k
            melp->k = melp->k - 1 > 0 ? melp->k - 1 : 0; //decrement, min is 0
288
138k
            melp->tmp <<= eval + 1; //consume eval + 1 bits (max is 6)
289
138k
            melp->bits -= eval + 1;
290
138k
            run = (run << 1) + 1; // a stretch of zeros terminating with one
291
138k
        }
292
281k
        eval = melp->num_runs * 7;                 // 7 bits per run
293
281k
        melp->runs &= ~((OPJ_UINT64)0x3F << eval); // 6 bits are sufficient
294
281k
        melp->runs |= ((OPJ_UINT64)run) << eval;   // store the value in runs
295
281k
        melp->num_runs++;                          // increment count
296
281k
    }
297
40.7k
}
298
299
//************************************************************************/
300
/** @brief Initiates a dec_mel_t structure for MEL decoding and reads
301
  *         some bytes in order to get the read address to a multiple
302
  *         of 4
303
  *
304
  *  @param [in]  melp is a pointer to dec_mel_t structure
305
  *  @param [in]  bbuf is a pointer to byte buffer
306
  *  @param [in]  lcup is the length of MagSgn+MEL+VLC segments
307
  *  @param [in]  scup is the length of MEL+VLC segments
308
  */
309
static INLINE
310
OPJ_BOOL mel_init(dec_mel_t *melp, OPJ_UINT8* bbuf, int lcup, int scup)
311
10.3k
{
312
10.3k
    int num;
313
10.3k
    int i;
314
315
10.3k
    melp->data = bbuf + lcup - scup; // move the pointer to the start of MEL
316
10.3k
    melp->bits = 0;                  // 0 bits in tmp
317
10.3k
    melp->tmp = 0;                   //
318
10.3k
    melp->unstuff = OPJ_FALSE;       // no unstuffing
319
10.3k
    melp->size = scup - 1;           // size is the length of MEL+VLC-1
320
10.3k
    melp->k = 0;                     // 0 for state
321
10.3k
    melp->num_runs = 0;              // num_runs is 0
322
10.3k
    melp->runs = 0;                  //
323
324
    //This code is borrowed; original is for a different architecture
325
    //These few lines take care of the case where data is not at a multiple
326
    // of 4 boundary.  It reads 1,2,3 up to 4 bytes from the MEL segment
327
10.3k
    num = 4 - (int)((intptr_t)(melp->data) & 0x3);
328
35.5k
    for (i = 0; i < num; ++i) { // this code is similar to mel_read
329
25.4k
        OPJ_UINT64 d;
330
25.4k
        int d_bits;
331
332
25.4k
        if (melp->unstuff == OPJ_TRUE && melp->data[0] > 0x8F) {
333
276
            return OPJ_FALSE;
334
276
        }
335
25.1k
        d = (melp->size > 0) ? *melp->data : 0xFF; // if buffer is consumed
336
        // set data to 0xFF
337
25.1k
        if (melp->size == 1) {
338
1.31k
            d |= 0xF;    //if this is MEL+VLC-1, set LSBs to 0xF
339
1.31k
        }
340
        // see the standard
341
25.1k
        melp->data += melp->size-- > 0; //increment if the end is not reached
342
25.1k
        d_bits = 8 - melp->unstuff; //if unstuffing is needed, reduce by 1
343
25.1k
        melp->tmp = (melp->tmp << d_bits) | d; //store bits in tmp
344
25.1k
        melp->bits += d_bits;  //increment tmp by number of bits
345
25.1k
        melp->unstuff = ((d & 0xFF) == 0xFF); //true of next byte needs
346
        //unstuffing
347
25.1k
    }
348
10.0k
    melp->tmp <<= (64 - melp->bits); //push all the way up so the first bit
349
    // is the MSB
350
10.0k
    return OPJ_TRUE;
351
10.3k
}
352
353
//************************************************************************/
354
/** @brief Retrieves one run from dec_mel_t; if there are no runs stored
355
  *         MEL segment is decoded
356
  *
357
  * @param [in]  melp is a pointer to dec_mel_t structure
358
  */
359
static INLINE
360
int mel_get_run(dec_mel_t *melp)
361
244k
{
362
244k
    int t;
363
244k
    if (melp->num_runs == 0) { //if no runs, decode more bit from MEL segment
364
40.7k
        mel_decode(melp);
365
40.7k
    }
366
367
244k
    t = melp->runs & 0x7F; //retrieve one run
368
244k
    melp->runs >>= 7;  // remove the retrieved run
369
244k
    melp->num_runs--;
370
244k
    return t; // return run
371
244k
}
372
373
//************************************************************************/
374
/** @brief A structure for reading and unstuffing a segment that grows
375
  *         backward, such as VLC and MRP
376
  */
377
typedef struct rev_struct {
378
    //storage
379
    OPJ_UINT8* data;  //!<pointer to where to read data
380
    OPJ_UINT64 tmp;     //!<temporary buffer of read data
381
    OPJ_UINT32 bits;  //!<number of bits stored in tmp
382
    int size;         //!<number of bytes left
383
    OPJ_BOOL unstuff; //!<true if the last byte is more than 0x8F
384
    //!<then the current byte is unstuffed if it is 0x7F
385
} rev_struct_t;
386
387
//************************************************************************/
388
/** @brief Read and unstuff data from a backwardly-growing segment
389
  *
390
  *  This reader can read up to 8 bytes from before the VLC segment.
391
  *  Care must be taken not read from unreadable memory, causing a
392
  *  segmentation fault.
393
  *
394
  *  Note that there is another subroutine rev_read_mrp that is slightly
395
  *  different.  The other one fills zeros when the buffer is exhausted.
396
  *  This one basically does not care if the bytes are consumed, because
397
  *  any extra data should not be used in the actual decoding.
398
  *
399
  *  Unstuffing is needed to prevent sequences more than 0xFF8F from
400
  *  appearing in the bits stream; since we are reading backward, we keep
401
  *  watch when a value larger than 0x8F appears in the bitstream.
402
  *  If the byte following this is 0x7F, we unstuff this byte (ignore the
403
  *  MSB of that byte, which should be 0).
404
  *
405
  *  @param [in]  vlcp is a pointer to rev_struct_t structure
406
  */
407
static INLINE
408
void rev_read(rev_struct_t *vlcp)
409
41.1k
{
410
41.1k
    OPJ_UINT32 val;
411
41.1k
    OPJ_UINT32 tmp;
412
41.1k
    OPJ_UINT32 bits;
413
41.1k
    OPJ_BOOL unstuff;
414
415
    //process 4 bytes at a time
416
41.1k
    if (vlcp->bits > 32) { // if there are more than 32 bits in tmp, then
417
2.62k
        return;    // reading 32 bits can overflow vlcp->tmp
418
2.62k
    }
419
38.5k
    val = 0;
420
    //the next line (the if statement) needs to be tested first
421
38.5k
    if (vlcp->size > 3) { // if there are more than 3 bytes left in VLC
422
        // (vlcp->data - 3) move pointer back to read 32 bits at once
423
20.3k
        val = read_le_uint32(vlcp->data - 3); // then read 32 bits
424
20.3k
        vlcp->data -= 4;                // move data pointer back by 4
425
20.3k
        vlcp->size -= 4;                // reduce available byte by 4
426
20.3k
    } else if (vlcp->size > 0) { // 4 or less
427
2.27k
        int i = 24;
428
7.47k
        while (vlcp->size > 0) {
429
5.19k
            OPJ_UINT32 v = *vlcp->data--; // read one byte at a time
430
5.19k
            val |= (v << i);              // put byte in its correct location
431
5.19k
            --vlcp->size;
432
5.19k
            i -= 8;
433
5.19k
        }
434
2.27k
    }
435
436
    //accumulate in tmp, number of bits in tmp are stored in bits
437
38.5k
    tmp = val >> 24;  //start with the MSB byte
438
439
    // test unstuff (previous byte is >0x8F), and this byte is 0x7F
440
38.5k
    bits = 8u - ((vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
441
38.5k
    unstuff = (val >> 24) > 0x8F; //this is for the next byte
442
443
38.5k
    tmp |= ((val >> 16) & 0xFF) << bits; //process the next byte
444
38.5k
    bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
445
38.5k
    unstuff = ((val >> 16) & 0xFF) > 0x8F;
446
447
38.5k
    tmp |= ((val >> 8) & 0xFF) << bits;
448
38.5k
    bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
449
38.5k
    unstuff = ((val >> 8) & 0xFF) > 0x8F;
450
451
38.5k
    tmp |= (val & 0xFF) << bits;
452
38.5k
    bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
453
38.5k
    unstuff = (val & 0xFF) > 0x8F;
454
455
    // now move the read and unstuffed bits into vlcp->tmp
456
38.5k
    vlcp->tmp |= (OPJ_UINT64)tmp << vlcp->bits;
457
38.5k
    vlcp->bits += bits;
458
38.5k
    vlcp->unstuff = unstuff; // this for the next read
459
38.5k
}
460
461
//************************************************************************/
462
/** @brief Initiates the rev_struct_t structure and reads a few bytes to
463
  *         move the read address to multiple of 4
464
  *
465
  *  There is another similar rev_init_mrp subroutine.  The difference is
466
  *  that this one, rev_init, discards the first 12 bits (they have the
467
  *  sum of the lengths of VLC and MEL segments), and first unstuff depends
468
  *  on first 4 bits.
469
  *
470
  *  @param [in]  vlcp is a pointer to rev_struct_t structure
471
  *  @param [in]  data is a pointer to byte at the start of the cleanup pass
472
  *  @param [in]  lcup is the length of MagSgn+MEL+VLC segments
473
  *  @param [in]  scup is the length of MEL+VLC segments
474
  */
475
static INLINE
476
void rev_init(rev_struct_t *vlcp, OPJ_UINT8* data, int lcup, int scup)
477
10.0k
{
478
10.0k
    OPJ_UINT32 d;
479
10.0k
    int num, tnum, i;
480
481
    //first byte has only the upper 4 bits
482
10.0k
    vlcp->data = data + lcup - 2;
483
484
    //size can not be larger than this, in fact it should be smaller
485
10.0k
    vlcp->size = scup - 2;
486
487
10.0k
    d = *vlcp->data--;            // read one byte (this is a half byte)
488
10.0k
    vlcp->tmp = d >> 4;           // both initialize and set
489
10.0k
    vlcp->bits = 4 - ((vlcp->tmp & 7) == 7); //check standard
490
10.0k
    vlcp->unstuff = (d | 0xF) > 0x8F; //this is useful for the next byte
491
492
    //This code is designed for an architecture that read address should
493
    // align to the read size (address multiple of 4 if read size is 4)
494
    //These few lines take care of the case where data is not at a multiple
495
    // of 4 boundary. It reads 1,2,3 up to 4 bytes from the VLC bitstream.
496
    // To read 32 bits, read from (vlcp->data - 3)
497
10.0k
    num = 1 + (int)((intptr_t)(vlcp->data) & 0x3);
498
10.0k
    tnum = num < vlcp->size ? num : vlcp->size;
499
29.8k
    for (i = 0; i < tnum; ++i) {
500
19.8k
        OPJ_UINT64 d;
501
19.8k
        OPJ_UINT32 d_bits;
502
19.8k
        d = *vlcp->data--;  // read one byte and move read pointer
503
        //check if the last byte was >0x8F (unstuff == true) and this is 0x7F
504
19.8k
        d_bits = 8u - ((vlcp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
505
19.8k
        vlcp->tmp |= d << vlcp->bits; // move data to vlcp->tmp
506
19.8k
        vlcp->bits += d_bits;
507
19.8k
        vlcp->unstuff = d > 0x8F; // for next byte
508
19.8k
    }
509
10.0k
    vlcp->size -= tnum;
510
10.0k
    rev_read(vlcp);  // read another 32 buts
511
10.0k
}
512
513
//************************************************************************/
514
/** @brief Retrieves 32 bits from the head of a rev_struct structure
515
  *
516
  *  By the end of this call, vlcp->tmp must have no less than 33 bits
517
  *
518
  *  @param [in]  vlcp is a pointer to rev_struct structure
519
  */
520
static INLINE
521
OPJ_UINT32 rev_fetch(rev_struct_t *vlcp)
522
434k
{
523
434k
    if (vlcp->bits < 32) { // if there are less then 32 bits, read more
524
31.0k
        rev_read(vlcp);     // read 32 bits, but unstuffing might reduce this
525
31.0k
        if (vlcp->bits < 32) { // if there is still space in vlcp->tmp for 32 bits
526
0
            rev_read(vlcp);    // read another 32
527
0
        }
528
31.0k
    }
529
434k
    return (OPJ_UINT32)vlcp->tmp; // return the head (bottom-most) of vlcp->tmp
530
434k
}
531
532
//************************************************************************/
533
/** @brief Consumes num_bits from a rev_struct structure
534
  *
535
  *  @param [in]  vlcp is a pointer to rev_struct structure
536
  *  @param [in]  num_bits is the number of bits to be removed
537
  */
538
static INLINE
539
OPJ_UINT32 rev_advance(rev_struct_t *vlcp, OPJ_UINT32 num_bits)
540
1.27M
{
541
1.27M
    assert(num_bits <= vlcp->bits); // vlcp->tmp must have more than num_bits
542
1.27M
    vlcp->tmp >>= num_bits;         // remove bits
543
1.27M
    vlcp->bits -= num_bits;         // decrement the number of bits
544
1.27M
    return (OPJ_UINT32)vlcp->tmp;
545
1.27M
}
546
547
//************************************************************************/
548
/** @brief Reads and unstuffs from rev_struct
549
  *
550
  *  This is different than rev_read in that this fills in zeros when the
551
  *  the available data is consumed.  The other does not care about the
552
  *  values when all data is consumed.
553
  *
554
  *  See rev_read for more information about unstuffing
555
  *
556
  *  @param [in]  mrp is a pointer to rev_struct structure
557
  */
558
static INLINE
559
void rev_read_mrp(rev_struct_t *mrp)
560
8.42k
{
561
8.42k
    OPJ_UINT32 val;
562
8.42k
    OPJ_UINT32 tmp;
563
8.42k
    OPJ_UINT32 bits;
564
8.42k
    OPJ_BOOL unstuff;
565
566
    //process 4 bytes at a time
567
8.42k
    if (mrp->bits > 32) {
568
0
        return;
569
0
    }
570
8.42k
    val = 0;
571
8.42k
    if (mrp->size > 3) { // If there are 3 byte or more
572
        // (mrp->data - 3) move pointer back to read 32 bits at once
573
7.49k
        val = read_le_uint32(mrp->data - 3); // read 32 bits
574
7.49k
        mrp->data -= 4;                      // move back pointer
575
7.49k
        mrp->size -= 4;                      // reduce count
576
7.49k
    } else if (mrp->size > 0) {
577
268
        int i = 24;
578
799
        while (mrp->size > 0) {
579
531
            OPJ_UINT32 v = *mrp->data--; // read one byte at a time
580
531
            val |= (v << i);             // put byte in its correct location
581
531
            --mrp->size;
582
531
            i -= 8;
583
531
        }
584
268
    }
585
586
587
    //accumulate in tmp, and keep count in bits
588
8.42k
    tmp = val >> 24;
589
590
    //test if the last byte > 0x8F (unstuff must be true) and this is 0x7F
591
8.42k
    bits = 8u - ((mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
592
8.42k
    unstuff = (val >> 24) > 0x8F;
593
594
    //process the next byte
595
8.42k
    tmp |= ((val >> 16) & 0xFF) << bits;
596
8.42k
    bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
597
8.42k
    unstuff = ((val >> 16) & 0xFF) > 0x8F;
598
599
8.42k
    tmp |= ((val >> 8) & 0xFF) << bits;
600
8.42k
    bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
601
8.42k
    unstuff = ((val >> 8) & 0xFF) > 0x8F;
602
603
8.42k
    tmp |= (val & 0xFF) << bits;
604
8.42k
    bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
605
8.42k
    unstuff = (val & 0xFF) > 0x8F;
606
607
8.42k
    mrp->tmp |= (OPJ_UINT64)tmp << mrp->bits; // move data to mrp pointer
608
8.42k
    mrp->bits += bits;
609
8.42k
    mrp->unstuff = unstuff;                   // next byte
610
8.42k
}
611
612
//************************************************************************/
613
/** @brief Initialized rev_struct structure for MRP segment, and reads
614
  *         a number of bytes such that the next 32 bits read are from
615
  *         an address that is a multiple of 4. Note this is designed for
616
  *         an architecture that read size must be compatible with the
617
  *         alignment of the read address
618
  *
619
  *  There is another similar subroutine rev_init.  This subroutine does
620
  *  NOT skip the first 12 bits, and starts with unstuff set to true.
621
  *
622
  *  @param [in]  mrp is a pointer to rev_struct structure
623
  *  @param [in]  data is a pointer to byte at the start of the cleanup pass
624
  *  @param [in]  lcup is the length of MagSgn+MEL+VLC segments
625
  *  @param [in]  len2 is the length of SPP+MRP segments
626
  */
627
static INLINE
628
void rev_init_mrp(rev_struct_t *mrp, OPJ_UINT8* data, int lcup, int len2)
629
3.86k
{
630
3.86k
    int num, i;
631
632
3.86k
    mrp->data = data + lcup + len2 - 1;
633
3.86k
    mrp->size = len2;
634
3.86k
    mrp->unstuff = OPJ_TRUE;
635
3.86k
    mrp->bits = 0;
636
3.86k
    mrp->tmp = 0;
637
638
    //This code is designed for an architecture that read address should
639
    // align to the read size (address multiple of 4 if read size is 4)
640
    //These few lines take care of the case where data is not at a multiple
641
    // of 4 boundary.  It reads 1,2,3 up to 4 bytes from the MRP stream
642
3.86k
    num = 1 + (int)((intptr_t)(mrp->data) & 0x3);
643
11.5k
    for (i = 0; i < num; ++i) {
644
7.64k
        OPJ_UINT64 d;
645
7.64k
        OPJ_UINT32 d_bits;
646
647
        //read a byte, 0 if no more data
648
7.64k
        d = (mrp->size-- > 0) ? *mrp->data-- : 0;
649
        //check if unstuffing is needed
650
7.64k
        d_bits = 8u - ((mrp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
651
7.64k
        mrp->tmp |= d << mrp->bits; // move data to vlcp->tmp
652
7.64k
        mrp->bits += d_bits;
653
7.64k
        mrp->unstuff = d > 0x8F; // for next byte
654
7.64k
    }
655
3.86k
    rev_read_mrp(mrp);
656
3.86k
}
657
658
//************************************************************************/
659
/** @brief Retrieves 32 bits from the head of a rev_struct structure
660
  *
661
  *  By the end of this call, mrp->tmp must have no less than 33 bits
662
  *
663
  *  @param [in]  mrp is a pointer to rev_struct structure
664
  */
665
static INLINE
666
OPJ_UINT32 rev_fetch_mrp(rev_struct_t *mrp)
667
54.5k
{
668
54.5k
    if (mrp->bits < 32) { // if there are less than 32 bits in mrp->tmp
669
4.53k
        rev_read_mrp(mrp);    // read 30-32 bits from mrp
670
4.53k
        if (mrp->bits < 32) { // if there is a space of 32 bits
671
36
            rev_read_mrp(mrp);    // read more
672
36
        }
673
4.53k
    }
674
54.5k
    return (OPJ_UINT32)mrp->tmp;  // return the head of mrp->tmp
675
54.5k
}
676
677
//************************************************************************/
678
/** @brief Consumes num_bits from a rev_struct structure
679
  *
680
  *  @param [in]  mrp is a pointer to rev_struct structure
681
  *  @param [in]  num_bits is the number of bits to be removed
682
  */
683
static INLINE
684
OPJ_UINT32 rev_advance_mrp(rev_struct_t *mrp, OPJ_UINT32 num_bits)
685
54.5k
{
686
54.5k
    assert(num_bits <= mrp->bits); // we must not consume more than mrp->bits
687
54.5k
    mrp->tmp >>= num_bits;         // discard the lowest num_bits bits
688
54.5k
    mrp->bits -= num_bits;
689
54.5k
    return (OPJ_UINT32)mrp->tmp;   // return data after consumption
690
54.5k
}
691
692
//************************************************************************/
693
/** @brief Decode initial UVLC to get the u value (or u_q)
694
  *
695
  *  @param [in]  vlc is the head of the VLC bitstream
696
  *  @param [in]  mode is 0, 1, 2, 3, or 4. Values in 0 to 3 are composed of
697
  *               u_off of 1st quad and 2nd quad of a quad pair.  The value
698
  *               4 occurs when both bits are 1, and the event decoded
699
  *               from MEL bitstream is also 1.
700
  *  @param [out] u is the u value (or u_q) + 1.  Note: we produce u + 1;
701
  *               this value is a partial calculation of u + kappa.
702
  */
703
static INLINE
704
OPJ_UINT32 decode_init_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
705
56.1k
{
706
    //table stores possible decoding three bits from vlc
707
    // there are 8 entries for xx1, x10, 100, 000, where x means do not care
708
    // table value is made up of
709
    // 2 bits in the LSB for prefix length
710
    // 3 bits for suffix length
711
    // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
712
56.1k
    static const OPJ_UINT8 dec[8] = { // the index is the prefix codeword
713
56.1k
        3 | (5 << 2) | (5 << 5),        //000 == 000, prefix codeword "000"
714
56.1k
        1 | (0 << 2) | (1 << 5),        //001 == xx1, prefix codeword "1"
715
56.1k
        2 | (0 << 2) | (2 << 5),        //010 == x10, prefix codeword "01"
716
56.1k
        1 | (0 << 2) | (1 << 5),        //011 == xx1, prefix codeword "1"
717
56.1k
        3 | (1 << 2) | (3 << 5),        //100 == 100, prefix codeword "001"
718
56.1k
        1 | (0 << 2) | (1 << 5),        //101 == xx1, prefix codeword "1"
719
56.1k
        2 | (0 << 2) | (2 << 5),        //110 == x10, prefix codeword "01"
720
56.1k
        1 | (0 << 2) | (1 << 5)         //111 == xx1, prefix codeword "1"
721
56.1k
    };
722
723
56.1k
    OPJ_UINT32 consumed_bits = 0;
724
56.1k
    if (mode == 0) { // both u_off are 0
725
38.3k
        u[0] = u[1] = 1; //Kappa is 1 for initial line
726
38.3k
    } else if (mode <= 2) { // u_off are either 01 or 10
727
8.88k
        OPJ_UINT32 d;
728
8.88k
        OPJ_UINT32 suffix_len;
729
730
8.88k
        d = dec[vlc & 0x7];   //look at the least significant 3 bits
731
8.88k
        vlc >>= d & 0x3;                 //prefix length
732
8.88k
        consumed_bits += d & 0x3;
733
734
8.88k
        suffix_len = ((d >> 2) & 0x7);
735
8.88k
        consumed_bits += suffix_len;
736
737
8.88k
        d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
738
8.88k
        u[0] = (mode == 1) ? d + 1 : 1; // kappa is 1 for initial line
739
8.88k
        u[1] = (mode == 1) ? 1 : d + 1; // kappa is 1 for initial line
740
8.91k
    } else if (mode == 3) { // both u_off are 1, and MEL event is 0
741
4.42k
        OPJ_UINT32 d1 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
742
4.42k
        vlc >>= d1 & 0x3;                // Consume bits
743
4.42k
        consumed_bits += d1 & 0x3;
744
745
4.42k
        if ((d1 & 0x3) > 2) {
746
1.68k
            OPJ_UINT32 suffix_len;
747
748
            //u_{q_2} prefix
749
1.68k
            u[1] = (vlc & 1) + 1 + 1; //Kappa is 1 for initial line
750
1.68k
            ++consumed_bits;
751
1.68k
            vlc >>= 1;
752
753
1.68k
            suffix_len = ((d1 >> 2) & 0x7);
754
1.68k
            consumed_bits += suffix_len;
755
1.68k
            d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
756
1.68k
            u[0] = d1 + 1; //Kappa is 1 for initial line
757
2.73k
        } else {
758
2.73k
            OPJ_UINT32 d2;
759
2.73k
            OPJ_UINT32 suffix_len;
760
761
2.73k
            d2 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
762
2.73k
            vlc >>= d2 & 0x3;                // Consume bits
763
2.73k
            consumed_bits += d2 & 0x3;
764
765
2.73k
            suffix_len = ((d1 >> 2) & 0x7);
766
2.73k
            consumed_bits += suffix_len;
767
768
2.73k
            d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
769
2.73k
            u[0] = d1 + 1; //Kappa is 1 for initial line
770
2.73k
            vlc >>= suffix_len;
771
772
2.73k
            suffix_len = ((d2 >> 2) & 0x7);
773
2.73k
            consumed_bits += suffix_len;
774
775
2.73k
            d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
776
2.73k
            u[1] = d2 + 1; //Kappa is 1 for initial line
777
2.73k
        }
778
4.49k
    } else if (mode == 4) { // both u_off are 1, and MEL event is 1
779
4.49k
        OPJ_UINT32 d1;
780
4.49k
        OPJ_UINT32 d2;
781
4.49k
        OPJ_UINT32 suffix_len;
782
783
4.49k
        d1 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
784
4.49k
        vlc >>= d1 & 0x3;                // Consume bits
785
4.49k
        consumed_bits += d1 & 0x3;
786
787
4.49k
        d2 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
788
4.49k
        vlc >>= d2 & 0x3;                // Consume bits
789
4.49k
        consumed_bits += d2 & 0x3;
790
791
4.49k
        suffix_len = ((d1 >> 2) & 0x7);
792
4.49k
        consumed_bits += suffix_len;
793
794
4.49k
        d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
795
4.49k
        u[0] = d1 + 3; // add 2+kappa
796
4.49k
        vlc >>= suffix_len;
797
798
4.49k
        suffix_len = ((d2 >> 2) & 0x7);
799
4.49k
        consumed_bits += suffix_len;
800
801
4.49k
        d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
802
4.49k
        u[1] = d2 + 3; // add 2+kappa
803
4.49k
    }
804
56.1k
    return consumed_bits;
805
56.1k
}
806
807
//************************************************************************/
808
/** @brief Decode non-initial UVLC to get the u value (or u_q)
809
  *
810
  *  @param [in]  vlc is the head of the VLC bitstream
811
  *  @param [in]  mode is 0, 1, 2, or 3. The 1st bit is u_off of 1st quad
812
  *               and 2nd for 2nd quad of a quad pair
813
  *  @param [out] u is the u value (or u_q) + 1.  Note: we produce u + 1;
814
  *               this value is a partial calculation of u + kappa.
815
  */
816
static INLINE
817
OPJ_UINT32 decode_noninit_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
818
378k
{
819
    //table stores possible decoding three bits from vlc
820
    // there are 8 entries for xx1, x10, 100, 000, where x means do not care
821
    // table value is made up of
822
    // 2 bits in the LSB for prefix length
823
    // 3 bits for suffix length
824
    // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
825
378k
    static const OPJ_UINT8 dec[8] = {
826
378k
        3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
827
378k
        1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
828
378k
        2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
829
378k
        1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
830
378k
        3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
831
378k
        1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
832
378k
        2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
833
378k
        1 | (0 << 2) | (1 << 5)  //111 == xx1, prefix codeword "1"
834
378k
    };
835
836
378k
    OPJ_UINT32 consumed_bits = 0;
837
378k
    if (mode == 0) {
838
363k
        u[0] = u[1] = 1; //for kappa
839
363k
    } else if (mode <= 2) { //u_off are either 01 or 10
840
12.3k
        OPJ_UINT32 d;
841
12.3k
        OPJ_UINT32 suffix_len;
842
843
12.3k
        d = dec[vlc & 0x7];  //look at the least significant 3 bits
844
12.3k
        vlc >>= d & 0x3;                //prefix length
845
12.3k
        consumed_bits += d & 0x3;
846
847
12.3k
        suffix_len = ((d >> 2) & 0x7);
848
12.3k
        consumed_bits += suffix_len;
849
850
12.3k
        d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
851
12.3k
        u[0] = (mode == 1) ? d + 1 : 1; //for kappa
852
12.3k
        u[1] = (mode == 1) ? 1 : d + 1; //for kappa
853
12.3k
    } else if (mode == 3) { // both u_off are 1
854
3.05k
        OPJ_UINT32 d1;
855
3.05k
        OPJ_UINT32 d2;
856
3.05k
        OPJ_UINT32 suffix_len;
857
858
3.05k
        d1 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
859
3.05k
        vlc >>= d1 & 0x3;                // Consume bits
860
3.05k
        consumed_bits += d1 & 0x3;
861
862
3.05k
        d2 = dec[vlc & 0x7];  // LSBs of VLC are prefix codeword
863
3.05k
        vlc >>= d2 & 0x3;                // Consume bits
864
3.05k
        consumed_bits += d2 & 0x3;
865
866
3.05k
        suffix_len = ((d1 >> 2) & 0x7);
867
3.05k
        consumed_bits += suffix_len;
868
869
3.05k
        d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
870
3.05k
        u[0] = d1 + 1;  //1 for kappa
871
3.05k
        vlc >>= suffix_len;
872
873
3.05k
        suffix_len = ((d2 >> 2) & 0x7);
874
3.05k
        consumed_bits += suffix_len;
875
876
3.05k
        d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
877
3.05k
        u[1] = d2 + 1;  //1 for kappa
878
3.05k
    }
879
378k
    return consumed_bits;
880
378k
}
881
882
//************************************************************************/
883
/** @brief State structure for reading and unstuffing of forward-growing
884
  *         bitstreams; these are: MagSgn and SPP bitstreams
885
  */
886
typedef struct frwd_struct {
887
    const OPJ_UINT8* data; //!<pointer to bitstream
888
    OPJ_UINT64 tmp;        //!<temporary buffer of read data
889
    OPJ_UINT32 bits;       //!<number of bits stored in tmp
890
    OPJ_BOOL unstuff;      //!<true if a bit needs to be unstuffed from next byte
891
    int size;              //!<size of data
892
    OPJ_UINT32 X;          //!<0 or 0xFF, X's are inserted at end of bitstream
893
} frwd_struct_t;
894
895
//************************************************************************/
896
/** @brief Read and unstuffs 32 bits from forward-growing bitstream
897
  *
898
  *  A subroutine to read from both the MagSgn or SPP bitstreams;
899
  *  in particular, when MagSgn bitstream is consumed, 0xFF's are fed,
900
  *  while when SPP is exhausted 0's are fed in.
901
  *  X controls this value.
902
  *
903
  *  Unstuffing prevent sequences that are more than 0xFF7F from appearing
904
  *  in the compressed sequence.  So whenever a value of 0xFF is coded, the
905
  *  MSB of the next byte is set 0 and must be ignored during decoding.
906
  *
907
  *  Reading can go beyond the end of buffer by up to 3 bytes.
908
  *
909
  *  @param  [in]  msp is a pointer to frwd_struct_t structure
910
  *
911
  */
912
static INLINE
913
void frwd_read(frwd_struct_t *msp)
914
61.0k
{
915
61.0k
    OPJ_UINT32 val;
916
61.0k
    OPJ_UINT32 bits;
917
61.0k
    OPJ_UINT32 t;
918
61.0k
    OPJ_BOOL unstuff;
919
920
61.0k
    assert(msp->bits <= 32); // assert that there is a space for 32 bits
921
922
61.0k
    val = 0u;
923
61.0k
    if (msp->size > 3) {
924
39.9k
        val = read_le_uint32(msp->data);  // read 32 bits
925
39.9k
        msp->data += 4;           // increment pointer
926
39.9k
        msp->size -= 4;           // reduce size
927
39.9k
    } else if (msp->size > 0) {
928
2.71k
        int i = 0;
929
2.71k
        val = msp->X != 0 ? 0xFFFFFFFFu : 0;
930
7.53k
        while (msp->size > 0) {
931
4.81k
            OPJ_UINT32 v = *msp->data++;  // read one byte at a time
932
4.81k
            OPJ_UINT32 m = ~(0xFFu << i); // mask of location
933
4.81k
            val = (val & m) | (v << i);   // put one byte in its correct location
934
4.81k
            --msp->size;
935
4.81k
            i += 8;
936
4.81k
        }
937
18.3k
    } else {
938
18.3k
        val = msp->X != 0 ? 0xFFFFFFFFu : 0;
939
18.3k
    }
940
941
    // we accumulate in t and keep a count of the number of bits in bits
942
61.0k
    bits = 8u - (msp->unstuff ? 1u : 0u);
943
61.0k
    t = val & 0xFF;
944
61.0k
    unstuff = ((val & 0xFF) == 0xFF);  // Do we need unstuffing next?
945
946
61.0k
    t |= ((val >> 8) & 0xFF) << bits;
947
61.0k
    bits += 8u - (unstuff ? 1u : 0u);
948
61.0k
    unstuff = (((val >> 8) & 0xFF) == 0xFF);
949
950
61.0k
    t |= ((val >> 16) & 0xFF) << bits;
951
61.0k
    bits += 8u - (unstuff ? 1u : 0u);
952
61.0k
    unstuff = (((val >> 16) & 0xFF) == 0xFF);
953
954
61.0k
    t |= ((val >> 24) & 0xFF) << bits;
955
61.0k
    bits += 8u - (unstuff ? 1u : 0u);
956
61.0k
    msp->unstuff = (((val >> 24) & 0xFF) == 0xFF); // for next byte
957
958
61.0k
    msp->tmp |= ((OPJ_UINT64)t) << msp->bits;  // move data to msp->tmp
959
61.0k
    msp->bits += bits;
960
61.0k
}
961
962
//************************************************************************/
963
/** @brief Initialize frwd_struct_t struct and reads some bytes
964
  *
965
  *  @param [in]  msp is a pointer to frwd_struct_t
966
  *  @param [in]  data is a pointer to the start of data
967
  *  @param [in]  size is the number of byte in the bitstream
968
  *  @param [in]  X is the value fed in when the bitstream is exhausted.
969
  *               See frwd_read.
970
  */
971
static INLINE
972
void frwd_init(frwd_struct_t *msp, const OPJ_UINT8* data, int size,
973
               OPJ_UINT32 X)
974
17.2k
{
975
17.2k
    int num, i;
976
977
17.2k
    msp->data = data;
978
17.2k
    msp->tmp = 0;
979
17.2k
    msp->bits = 0;
980
17.2k
    msp->unstuff = OPJ_FALSE;
981
17.2k
    msp->size = size;
982
17.2k
    msp->X = X;
983
17.2k
    assert(msp->X == 0 || msp->X == 0xFF);
984
985
    //This code is designed for an architecture that read address should
986
    // align to the read size (address multiple of 4 if read size is 4)
987
    //These few lines take care of the case where data is not at a multiple
988
    // of 4 boundary.  It reads 1,2,3 up to 4 bytes from the bitstream
989
17.2k
    num = 4 - (int)((intptr_t)(msp->data) & 0x3);
990
62.5k
    for (i = 0; i < num; ++i) {
991
45.3k
        OPJ_UINT64 d;
992
        //read a byte if the buffer is not exhausted, otherwise set it to X
993
45.3k
        d = msp->size-- > 0 ? *msp->data++ : msp->X;
994
45.3k
        msp->tmp |= (d << msp->bits);      // store data in msp->tmp
995
45.3k
        msp->bits += 8u - (msp->unstuff ? 1u : 0u); // number of bits added to msp->tmp
996
45.3k
        msp->unstuff = ((d & 0xFF) == 0xFF); // unstuffing for next byte
997
45.3k
    }
998
17.2k
    frwd_read(msp); // read 32 bits more
999
17.2k
}
1000
1001
//************************************************************************/
1002
/** @brief Consume num_bits bits from the bitstream of frwd_struct_t
1003
  *
1004
  *  @param [in]  msp is a pointer to frwd_struct_t
1005
  *  @param [in]  num_bits is the number of bit to consume
1006
  */
1007
static INLINE
1008
void frwd_advance(frwd_struct_t *msp, OPJ_UINT32 num_bits)
1009
390k
{
1010
390k
    assert(num_bits <= msp->bits);
1011
390k
    msp->tmp >>= num_bits;  // consume num_bits
1012
390k
    msp->bits -= num_bits;
1013
390k
}
1014
1015
//************************************************************************/
1016
/** @brief Fetches 32 bits from the frwd_struct_t bitstream
1017
  *
1018
  *  @param [in]  msp is a pointer to frwd_struct_t
1019
  */
1020
static INLINE
1021
OPJ_UINT32 frwd_fetch(frwd_struct_t *msp)
1022
390k
{
1023
390k
    if (msp->bits < 32) {
1024
43.5k
        frwd_read(msp);
1025
43.5k
        if (msp->bits < 32) { //need to test
1026
329
            frwd_read(msp);
1027
329
        }
1028
43.5k
    }
1029
390k
    return (OPJ_UINT32)msp->tmp;
1030
390k
}
1031
1032
//************************************************************************/
1033
/** @brief Allocates T1 buffers
1034
  *
1035
  *  @param [in, out]  t1 is codeblock coefficients storage
1036
  *  @param [in]       w is codeblock width
1037
  *  @param [in]       h is codeblock height
1038
  */
1039
static OPJ_BOOL opj_t1_allocate_buffers(
1040
    opj_t1_t *t1,
1041
    OPJ_UINT32 w,
1042
    OPJ_UINT32 h)
1043
153k
{
1044
153k
    OPJ_UINT32 flagssize;
1045
1046
    /* No risk of overflow. Prior checks ensure those assert are met */
1047
    /* They are per the specification */
1048
153k
    assert(w <= 1024);
1049
153k
    assert(h <= 1024);
1050
153k
    assert(w * h <= 4096);
1051
1052
    /* encoder uses tile buffer, so no need to allocate */
1053
153k
    {
1054
153k
        OPJ_UINT32 datasize = w * h;
1055
1056
153k
        if (datasize > t1->datasize) {
1057
18.1k
            opj_aligned_free(t1->data);
1058
18.1k
            t1->data = (OPJ_INT32*)
1059
18.1k
                       opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1060
18.1k
            if (!t1->data) {
1061
                /* FIXME event manager error callback */
1062
0
                return OPJ_FALSE;
1063
0
            }
1064
18.1k
            t1->datasize = datasize;
1065
18.1k
        }
1066
        /* memset first arg is declared to never be null by gcc */
1067
153k
        if (t1->data != NULL) {
1068
152k
            memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1069
152k
        }
1070
153k
    }
1071
1072
    // We expand these buffers to multiples of 16 bytes.
1073
    // We need 4 buffers of 129 integers each, expanded to 132 integers each
1074
    // We also need 514 bytes of buffer, expanded to 528 bytes
1075
0
    flagssize = 132U * sizeof(OPJ_UINT32) * 4U; // expanded to multiple of 16
1076
153k
    flagssize += 528U; // 514 expanded to multiples of 16
1077
1078
153k
    {
1079
153k
        if (flagssize > t1->flagssize) {
1080
1081
8.51k
            opj_aligned_free(t1->flags);
1082
8.51k
            t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1083
8.51k
            if (!t1->flags) {
1084
                /* FIXME event manager error callback */
1085
0
                return OPJ_FALSE;
1086
0
            }
1087
8.51k
        }
1088
153k
        t1->flagssize = flagssize;
1089
1090
153k
        memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1091
153k
    }
1092
1093
0
    t1->w = w;
1094
153k
    t1->h = h;
1095
1096
153k
    return OPJ_TRUE;
1097
153k
}
1098
1099
/**
1100
Decode 1 HT code-block
1101
@param t1 T1 handle
1102
@param cblk Code-block coding parameters
1103
@param orient
1104
@param roishift Region of interest shifting value
1105
@param cblksty Code-block style
1106
@param p_manager the event manager
1107
@param p_manager_mutex mutex for the event manager
1108
@param check_pterm whether PTERM correct termination should be checked
1109
*/
1110
OPJ_BOOL opj_t1_ht_decode_cblk(opj_t1_t *t1,
1111
                               opj_tcd_cblk_dec_t* cblk,
1112
                               OPJ_UINT32 orient,
1113
                               OPJ_UINT32 roishift,
1114
                               OPJ_UINT32 cblksty,
1115
                               opj_event_mgr_t *p_manager,
1116
                               opj_mutex_t* p_manager_mutex,
1117
                               OPJ_BOOL check_pterm);
1118
1119
//************************************************************************/
1120
/** @brief Decodes one codeblock, processing the cleanup, siginificance
1121
  *         propagation, and magnitude refinement pass
1122
  *
1123
  *  @param [in, out]  t1 is codeblock coefficients storage
1124
  *  @param [in]       cblk is codeblock properties
1125
  *  @param [in]       orient is the subband to which the codeblock belongs (not needed)
1126
  *  @param [in]       roishift is region of interest shift
1127
  *  @param [in]       cblksty is codeblock style
1128
  *  @param [in]       p_manager is events print manager
1129
  *  @param [in]       p_manager_mutex a mutex to control access to p_manager
1130
  *  @param [in]       check_pterm: check termination (not used)
1131
  */
1132
OPJ_BOOL opj_t1_ht_decode_cblk(opj_t1_t *t1,
1133
                               opj_tcd_cblk_dec_t* cblk,
1134
                               OPJ_UINT32 orient,
1135
                               OPJ_UINT32 roishift,
1136
                               OPJ_UINT32 cblksty,
1137
                               opj_event_mgr_t *p_manager,
1138
                               opj_mutex_t* p_manager_mutex,
1139
                               OPJ_BOOL check_pterm)
1140
153k
{
1141
153k
    OPJ_BYTE* cblkdata = NULL;
1142
153k
    OPJ_UINT8* coded_data;
1143
153k
    OPJ_UINT32* decoded_data;
1144
153k
    OPJ_UINT32 zero_bplanes;
1145
153k
    OPJ_UINT32 num_passes;
1146
153k
    OPJ_UINT32 lengths1;
1147
153k
    OPJ_UINT32 lengths2;
1148
153k
    OPJ_INT32 width;
1149
153k
    OPJ_INT32 height;
1150
153k
    OPJ_INT32 stride;
1151
153k
    OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
1152
153k
    OPJ_UINT32 p;
1153
153k
    OPJ_UINT32 zero_bplanes_p1;
1154
153k
    int lcup, scup;
1155
153k
    dec_mel_t mel;
1156
153k
    rev_struct_t vlc;
1157
153k
    frwd_struct_t magsgn;
1158
153k
    frwd_struct_t sigprop;
1159
153k
    rev_struct_t magref;
1160
153k
    OPJ_UINT8 *lsp, *line_state;
1161
153k
    int run;
1162
153k
    OPJ_UINT32 vlc_val;              // fetched data from VLC bitstream
1163
153k
    OPJ_UINT32 qinf[2];
1164
153k
    OPJ_UINT32 c_q;
1165
153k
    OPJ_UINT32* sp;
1166
153k
    OPJ_INT32 x, y; // loop indices
1167
153k
    OPJ_BOOL stripe_causal = (cblksty & J2K_CCP_CBLKSTY_VSC) != 0;
1168
153k
    OPJ_UINT32 cblk_len = 0;
1169
1170
153k
    (void)(orient);      // stops unused parameter message
1171
153k
    (void)(check_pterm); // stops unused parameter message
1172
1173
    // We ignor orient, because the same decoder is used for all subbands
1174
    // We also ignore check_pterm, because I am not sure how it applies
1175
153k
    if (roishift != 0) {
1176
0
        if (p_manager_mutex) {
1177
0
            opj_mutex_lock(p_manager_mutex);
1178
0
        }
1179
0
        opj_event_msg(p_manager, EVT_ERROR, "We do not support ROI in decoding "
1180
0
                      "HT codeblocks\n");
1181
0
        if (p_manager_mutex) {
1182
0
            opj_mutex_unlock(p_manager_mutex);
1183
0
        }
1184
0
        return OPJ_FALSE;
1185
0
    }
1186
1187
153k
    if (!opj_t1_allocate_buffers(
1188
153k
                t1,
1189
153k
                (OPJ_UINT32)(cblk->x1 - cblk->x0),
1190
153k
                (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1191
0
        return OPJ_FALSE;
1192
0
    }
1193
1194
153k
    if (cblk->Mb == 0) {
1195
138k
        return OPJ_TRUE;
1196
138k
    }
1197
1198
    /* numbps = Mb + 1 - zero_bplanes, Mb = Kmax, zero_bplanes = missing_msbs */
1199
14.1k
    zero_bplanes = (cblk->Mb + 1) - cblk->numbps;
1200
1201
    /* Compute whole codeblock length from chunk lengths */
1202
14.1k
    cblk_len = 0;
1203
14.1k
    {
1204
14.1k
        OPJ_UINT32 i;
1205
41.5k
        for (i = 0; i < cblk->numchunks; i++) {
1206
27.4k
            cblk_len += cblk->chunks[i].len;
1207
27.4k
        }
1208
14.1k
    }
1209
1210
14.1k
    if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
1211
9.66k
        OPJ_UINT32 i;
1212
1213
        /* Allocate temporary memory if needed */
1214
9.66k
        if (cblk_len > t1->cblkdatabuffersize) {
1215
7.07k
            cblkdata = (OPJ_BYTE*)opj_realloc(
1216
7.07k
                           t1->cblkdatabuffer, cblk_len);
1217
7.07k
            if (cblkdata == NULL) {
1218
0
                return OPJ_FALSE;
1219
0
            }
1220
7.07k
            t1->cblkdatabuffer = cblkdata;
1221
7.07k
            t1->cblkdatabuffersize = cblk_len;
1222
7.07k
        }
1223
1224
        /* Concatenate all chunks */
1225
9.66k
        cblkdata = t1->cblkdatabuffer;
1226
9.66k
        if (cblkdata == NULL) {
1227
235
            return OPJ_FALSE;
1228
235
        }
1229
9.42k
        cblk_len = 0;
1230
31.9k
        for (i = 0; i < cblk->numchunks; i++) {
1231
22.4k
            memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
1232
22.4k
            cblk_len += cblk->chunks[i].len;
1233
22.4k
        }
1234
9.42k
    } else if (cblk->numchunks == 1) {
1235
4.48k
        cblkdata = cblk->chunks[0].data;
1236
4.48k
    } else {
1237
        /* Not sure if that can happen in practice, but avoid Coverity to */
1238
        /* think we will dereference a null cblkdta pointer */
1239
0
        return OPJ_TRUE;
1240
0
    }
1241
1242
    // OPJ_BYTE* coded_data is a pointer to bitstream
1243
13.9k
    coded_data = cblkdata;
1244
    // OPJ_UINT32* decoded_data is a pointer to decoded codeblock data buf.
1245
13.9k
    decoded_data = (OPJ_UINT32*)t1->data;
1246
    // OPJ_UINT32 num_passes is the number of passes: 1 if CUP only, 2 for
1247
    // CUP+SPP, and 3 for CUP+SPP+MRP
1248
13.9k
    num_passes = cblk->numsegs > 0 ? cblk->segs[0].real_num_passes : 0;
1249
13.9k
    num_passes += cblk->numsegs > 1 ? cblk->segs[1].real_num_passes : 0;
1250
    // OPJ_UINT32 lengths1 is the length of cleanup pass
1251
13.9k
    lengths1 = num_passes > 0 ? cblk->segs[0].len : 0;
1252
    // OPJ_UINT32 lengths2 is the length of refinement passes (either SPP only or SPP+MRP)
1253
13.9k
    lengths2 = num_passes > 1 ? cblk->segs[1].len : 0;
1254
    // OPJ_INT32 width is the decoded codeblock width
1255
13.9k
    width = cblk->x1 - cblk->x0;
1256
    // OPJ_INT32 height is the decoded codeblock height
1257
13.9k
    height = cblk->y1 - cblk->y0;
1258
    // OPJ_INT32 stride is the decoded codeblock buffer stride
1259
13.9k
    stride = width;
1260
1261
    /*  sigma1 and sigma2 contains significant (i.e., non-zero) pixel
1262
     *  locations.  The buffers are used interchangeably, because we need
1263
     *  more than 4 rows of significance information at a given time.
1264
     *  Each 32 bits contain significance information for 4 rows of 8
1265
     *  columns each.  If we denote 32 bits by 0xaaaaaaaa, the each "a" is
1266
     *  called a nibble and has significance information for 4 rows.
1267
     *  The least significant nibble has information for the first column,
1268
     *  and so on. The nibble's LSB is for the first row, and so on.
1269
     *  Since, at most, we can have 1024 columns in a quad, we need 128
1270
     *  entries; we added 1 for convenience when propagation of signifcance
1271
     *  goes outside the structure
1272
     *  To work in OpenJPEG these buffers has been expanded to 132.
1273
     */
1274
    // OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
1275
13.9k
    pflags = (OPJ_UINT32 *)t1->flags;
1276
13.9k
    sigma1 = pflags;
1277
13.9k
    sigma2 = sigma1 + 132;
1278
    // mbr arrangement is similar to sigma; mbr contains locations
1279
    // that become significant during significance propagation pass
1280
13.9k
    mbr1 = sigma2 + 132;
1281
13.9k
    mbr2 = mbr1 + 132;
1282
    //a pointer to sigma
1283
13.9k
    sip = sigma1;  //pointers to arrays to be used interchangeably
1284
13.9k
    sip_shift = 0; //the amount of shift needed for sigma
1285
1286
13.9k
    if (num_passes > 1 && lengths2 == 0) {
1287
484
        if (p_manager_mutex) {
1288
484
            opj_mutex_lock(p_manager_mutex);
1289
484
        }
1290
484
        opj_event_msg(p_manager, EVT_WARNING, "A malformed codeblock that has "
1291
484
                      "more than one coding pass, but zero length for "
1292
484
                      "2nd and potentially the 3rd pass in an HT codeblock.\n");
1293
484
        if (p_manager_mutex) {
1294
484
            opj_mutex_unlock(p_manager_mutex);
1295
484
        }
1296
484
        num_passes = 1;
1297
484
    }
1298
13.9k
    if (num_passes > 3) {
1299
573
        if (p_manager_mutex) {
1300
573
            opj_mutex_lock(p_manager_mutex);
1301
573
        }
1302
573
        opj_event_msg(p_manager, EVT_ERROR, "We do not support more than 3 "
1303
573
                      "coding passes in an HT codeblock; This codeblocks has "
1304
573
                      "%d passes.\n", num_passes);
1305
573
        if (p_manager_mutex) {
1306
573
            opj_mutex_unlock(p_manager_mutex);
1307
573
        }
1308
573
        return OPJ_FALSE;
1309
573
    }
1310
1311
13.3k
    if (cblk->Mb > 30) {
1312
        /* This check is better moved to opj_t2_read_packet_header() in t2.c
1313
           We do not have enough precision to decode any passes
1314
           The design of openjpeg assumes that the bits of a 32-bit integer are
1315
           assigned as follows:
1316
           bit 31 is for sign
1317
           bits 30-1 are for magnitude
1318
           bit 0 is for the center of the quantization bin
1319
           Therefore we can only do values of cblk->Mb <= 30
1320
         */
1321
31
        if (p_manager_mutex) {
1322
31
            opj_mutex_lock(p_manager_mutex);
1323
31
        }
1324
31
        opj_event_msg(p_manager, EVT_ERROR, "32 bits are not enough to "
1325
31
                      "decode this codeblock, since the number of "
1326
31
                      "bitplane, %d, is larger than 30.\n", cblk->Mb);
1327
31
        if (p_manager_mutex) {
1328
31
            opj_mutex_unlock(p_manager_mutex);
1329
31
        }
1330
31
        return OPJ_FALSE;
1331
31
    }
1332
13.3k
    if (zero_bplanes > cblk->Mb) {
1333
        /* This check is better moved to opj_t2_read_packet_header() in t2.c,
1334
           in the line "l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;"
1335
           where i is the zero bitplanes, and should be no larger than cblk->Mb
1336
           We cannot have more zero bitplanes than there are planes. */
1337
785
        if (p_manager_mutex) {
1338
785
            opj_mutex_lock(p_manager_mutex);
1339
785
        }
1340
785
        opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1341
785
                      "Decoding this codeblock is stopped. There are "
1342
785
                      "%d zero bitplanes in %d bitplanes.\n",
1343
785
                      zero_bplanes, cblk->Mb);
1344
1345
785
        if (p_manager_mutex) {
1346
785
            opj_mutex_unlock(p_manager_mutex);
1347
785
        }
1348
785
        return OPJ_FALSE;
1349
12.5k
    } else if (zero_bplanes == cblk->Mb && num_passes > 1) {
1350
        /* When the number of zero bitplanes is equal to the number of bitplanes,
1351
           only the cleanup pass makes sense*/
1352
22
        if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
1353
6
            if (p_manager_mutex) {
1354
6
                opj_mutex_lock(p_manager_mutex);
1355
6
            }
1356
            /* We have a second check to prevent the possibility of an overrun condition,
1357
               in the very unlikely event of a second thread discovering that
1358
               only_cleanup_pass_is_decoded is false before the first thread changing
1359
               the condition. */
1360
6
            if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
1361
6
                only_cleanup_pass_is_decoded = OPJ_TRUE;
1362
6
                opj_event_msg(p_manager, EVT_WARNING, "Malformed HT codeblock. "
1363
6
                              "When the number of zero planes bitplanes is "
1364
6
                              "equal to the number of bitplanes, only the cleanup "
1365
6
                              "pass makes sense, but we have %d passes in this "
1366
6
                              "codeblock. Therefore, only the cleanup pass will be "
1367
6
                              "decoded. This message will not be displayed again.\n",
1368
6
                              num_passes);
1369
6
            }
1370
6
            if (p_manager_mutex) {
1371
6
                opj_mutex_unlock(p_manager_mutex);
1372
6
            }
1373
6
        }
1374
22
        num_passes = 1;
1375
22
    }
1376
1377
    /* OPJ_UINT32 */
1378
12.5k
    p = cblk->numbps;
1379
1380
    // OPJ_UINT32 zero planes plus 1
1381
12.5k
    zero_bplanes_p1 = zero_bplanes + 1;
1382
1383
12.5k
    if (lengths1 < 2 || (OPJ_UINT32)lengths1 > cblk_len ||
1384
12.1k
            (OPJ_UINT32)(lengths1 + lengths2) > cblk_len) {
1385
407
        if (p_manager_mutex) {
1386
407
            opj_mutex_lock(p_manager_mutex);
1387
407
        }
1388
407
        opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1389
407
                      "Invalid codeblock length values.\n");
1390
1391
407
        if (p_manager_mutex) {
1392
407
            opj_mutex_unlock(p_manager_mutex);
1393
407
        }
1394
407
        return OPJ_FALSE;
1395
407
    }
1396
    // read scup and fix the bytes there
1397
12.1k
    lcup = (int)lengths1;  // length of CUP
1398
    //scup is the length of MEL + VLC
1399
12.1k
    scup = (((int)coded_data[lcup - 1]) << 4) + (coded_data[lcup - 2] & 0xF);
1400
12.1k
    if (scup < 2 || scup > lcup || scup > 4079) { //something is wrong
1401
        /* The standard stipulates 2 <= Scup <= min(Lcup, 4079) */
1402
1.76k
        if (p_manager_mutex) {
1403
1.76k
            opj_mutex_lock(p_manager_mutex);
1404
1.76k
        }
1405
1.76k
        opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1406
1.76k
                      "One of the following condition is not met: "
1407
1.76k
                      "2 <= Scup <= min(Lcup, 4079)\n");
1408
1409
1.76k
        if (p_manager_mutex) {
1410
1.76k
            opj_mutex_unlock(p_manager_mutex);
1411
1.76k
        }
1412
1.76k
        return OPJ_FALSE;
1413
1.76k
    }
1414
1415
    // init structures
1416
10.3k
    if (mel_init(&mel, coded_data, lcup, scup) == OPJ_FALSE) {
1417
276
        if (p_manager_mutex) {
1418
276
            opj_mutex_lock(p_manager_mutex);
1419
276
        }
1420
276
        opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1421
276
                      "Incorrect MEL segment sequence.\n");
1422
276
        if (p_manager_mutex) {
1423
276
            opj_mutex_unlock(p_manager_mutex);
1424
276
        }
1425
276
        return OPJ_FALSE;
1426
276
    }
1427
10.0k
    rev_init(&vlc, coded_data, lcup, scup);
1428
10.0k
    frwd_init(&magsgn, coded_data, lcup - scup, 0xFF);
1429
10.0k
    if (num_passes > 1) { // needs to be tested
1430
7.16k
        frwd_init(&sigprop, coded_data + lengths1, (int)lengths2, 0);
1431
7.16k
    }
1432
10.0k
    if (num_passes > 2) {
1433
3.86k
        rev_init_mrp(&magref, coded_data, (int)lengths1, (int)lengths2);
1434
3.86k
    }
1435
1436
    /** State storage
1437
      *  One byte per quad; for 1024 columns, or 512 quads, we need
1438
      *  512 bytes. We are using 2 extra bytes one on the left and one on
1439
      *  the right for convenience.
1440
      *
1441
      *  The MSB bit in each byte is (\sigma^nw | \sigma^n), and the 7 LSBs
1442
      *  contain max(E^nw | E^n)
1443
      */
1444
1445
    // 514 is enough for a block width of 1024, +2 extra
1446
    // here expanded to 528
1447
10.0k
    line_state = (OPJ_UINT8 *)(mbr2 + 132);
1448
1449
    //initial 2 lines
1450
    /////////////////
1451
10.0k
    lsp = line_state;              // point to line state
1452
10.0k
    lsp[0] = 0;                    // for initial row of quad, we set to 0
1453
10.0k
    run = mel_get_run(&mel);    // decode runs of events from MEL bitstrm
1454
    // data represented as runs of 0 events
1455
    // See mel_decode description
1456
10.0k
    qinf[0] = qinf[1] = 0;      // quad info decoded from VLC bitstream
1457
10.0k
    c_q = 0;                    // context for quad q
1458
10.0k
    sp = decoded_data;          // decoded codeblock samples
1459
    // vlc_val;                 // fetched data from VLC bitstream
1460
1461
64.7k
    for (x = 0; x < width; x += 4) { // one iteration per quad pair
1462
56.1k
        OPJ_UINT32 U_q[2]; // u values for the quad pair
1463
56.1k
        OPJ_UINT32 uvlc_mode;
1464
56.1k
        OPJ_UINT32 consumed_bits;
1465
56.1k
        OPJ_UINT32 m_n, v_n;
1466
56.1k
        OPJ_UINT32 ms_val;
1467
56.1k
        OPJ_UINT32 locs;
1468
1469
        // decode VLC
1470
        /////////////
1471
1472
        //first quad
1473
        // Get the head of the VLC bitstream. One fetch is enough for two
1474
        // quads, since the largest VLC code is 7 bits, and maximum number of
1475
        // bits used for u is 8.  Therefore for two quads we need 30 bits
1476
        // (if we include unstuffing, then 32 bits are enough, since we have
1477
        // a maximum of one stuffing per two bytes)
1478
56.1k
        vlc_val = rev_fetch(&vlc);
1479
1480
        //decode VLC using the context c_q and the head of the VLC bitstream
1481
56.1k
        qinf[0] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F) ];
1482
1483
56.1k
        if (c_q == 0) { // if zero context, we need to use one MEL event
1484
34.6k
            run -= 2; //the number of 0 events is multiplied by 2, so subtract 2
1485
1486
            // Is the run terminated in 1? if so, use decoded VLC code,
1487
            // otherwise, discard decoded data, since we will decoded again
1488
            // using a different context
1489
34.6k
            qinf[0] = (run == -1) ? qinf[0] : 0;
1490
1491
            // is run -1 or -2? this means a run has been consumed
1492
34.6k
            if (run < 0) {
1493
30.1k
                run = mel_get_run(&mel);    // get another run
1494
30.1k
            }
1495
34.6k
        }
1496
1497
        // prepare context for the next quad; eqn. 1 in ITU T.814
1498
56.1k
        c_q = ((qinf[0] & 0x10) >> 4) | ((qinf[0] & 0xE0) >> 5);
1499
1500
        //remove data from vlc stream (0 bits are removed if qinf is not used)
1501
56.1k
        vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
1502
1503
        //update sigma
1504
        // The update depends on the value of x; consider one OPJ_UINT32
1505
        // if x is 0, 8, 16 and so on, then this line update c locations
1506
        //      nibble (4 bits) number   0 1 2 3 4 5 6 7
1507
        //                         LSB   c c 0 0 0 0 0 0
1508
        //                               c c 0 0 0 0 0 0
1509
        //                               0 0 0 0 0 0 0 0
1510
        //                               0 0 0 0 0 0 0 0
1511
        // if x is 4, 12, 20, then this line update locations c
1512
        //      nibble (4 bits) number   0 1 2 3 4 5 6 7
1513
        //                         LSB   0 0 0 0 c c 0 0
1514
        //                               0 0 0 0 c c 0 0
1515
        //                               0 0 0 0 0 0 0 0
1516
        //                               0 0 0 0 0 0 0 0
1517
56.1k
        *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
1518
1519
        //second quad
1520
56.1k
        qinf[1] = 0;
1521
56.1k
        if (x + 2 < width) { // do not run if codeblock is narrower
1522
            //decode VLC using the context c_q and the head of the VLC bitstream
1523
50.1k
            qinf[1] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F)];
1524
1525
            // if context is zero, use one MEL event
1526
50.1k
            if (c_q == 0) { //zero context
1527
20.8k
                run -= 2; //subtract 2, since events number if multiplied by 2
1528
1529
                // if event is 0, discard decoded qinf
1530
20.8k
                qinf[1] = (run == -1) ? qinf[1] : 0;
1531
1532
20.8k
                if (run < 0) { // have we consumed all events in a run
1533
14.9k
                    run = mel_get_run(&mel);    // if yes, then get another run
1534
14.9k
                }
1535
20.8k
            }
1536
1537
            //prepare context for the next quad, eqn. 1 in ITU T.814
1538
50.1k
            c_q = ((qinf[1] & 0x10) >> 4) | ((qinf[1] & 0xE0) >> 5);
1539
1540
            //remove data from vlc stream, if qinf is not used, cwdlen is 0
1541
50.1k
            vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
1542
50.1k
        }
1543
1544
        //update sigma
1545
        // The update depends on the value of x; consider one OPJ_UINT32
1546
        // if x is 0, 8, 16 and so on, then this line update c locations
1547
        //      nibble (4 bits) number   0 1 2 3 4 5 6 7
1548
        //                         LSB   0 0 c c 0 0 0 0
1549
        //                               0 0 c c 0 0 0 0
1550
        //                               0 0 0 0 0 0 0 0
1551
        //                               0 0 0 0 0 0 0 0
1552
        // if x is 4, 12, 20, then this line update locations c
1553
        //      nibble (4 bits) number   0 1 2 3 4 5 6 7
1554
        //                         LSB   0 0 0 0 0 0 c c
1555
        //                               0 0 0 0 0 0 c c
1556
        //                               0 0 0 0 0 0 0 0
1557
        //                               0 0 0 0 0 0 0 0
1558
56.1k
        *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
1559
1560
56.1k
        sip += x & 0x7 ? 1 : 0; // move sigma pointer to next entry
1561
56.1k
        sip_shift ^= 0x10;      // increment/decrement sip_shift by 16
1562
1563
        // retrieve u
1564
        /////////////
1565
1566
        // uvlc_mode is made up of u_offset bits from the quad pair
1567
56.1k
        uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
1568
56.1k
        if (uvlc_mode == 3) { // if both u_offset are set, get an event from
1569
            // the MEL run of events
1570
8.91k
            run -= 2; //subtract 2, since events number if multiplied by 2
1571
8.91k
            uvlc_mode += (run == -1) ? 1 : 0; //increment uvlc_mode if event is 1
1572
8.91k
            if (run < 0) { // if run is consumed (run is -1 or -2), get another run
1573
8.08k
                run = mel_get_run(&mel);
1574
8.08k
            }
1575
8.91k
        }
1576
        //decode uvlc_mode to get u for both quads
1577
56.1k
        consumed_bits = decode_init_uvlc(vlc_val, uvlc_mode, U_q);
1578
56.1k
        if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
1579
1.01k
            if (p_manager_mutex) {
1580
1.01k
                opj_mutex_lock(p_manager_mutex);
1581
1.01k
            }
1582
1.01k
            opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. Decoding "
1583
1.01k
                          "this codeblock is stopped. U_q is larger than zero "
1584
1.01k
                          "bitplanes + 1 \n");
1585
1.01k
            if (p_manager_mutex) {
1586
1.01k
                opj_mutex_unlock(p_manager_mutex);
1587
1.01k
            }
1588
1.01k
            return OPJ_FALSE;
1589
1.01k
        }
1590
1591
        //consume u bits in the VLC code
1592
55.1k
        vlc_val = rev_advance(&vlc, consumed_bits);
1593
1594
        //decode magsgn and update line_state
1595
        /////////////////////////////////////
1596
1597
        //We obtain a mask for the samples locations that needs evaluation
1598
55.1k
        locs = 0xFF;
1599
55.1k
        if (x + 4 > width) {
1600
5.98k
            locs >>= (x + 4 - width) << 1;    // limits width
1601
5.98k
        }
1602
55.1k
        locs = height > 1 ? locs : (locs & 0x55);         // limits height
1603
1604
55.1k
        if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
1605
448
            if (p_manager_mutex) {
1606
448
                opj_mutex_lock(p_manager_mutex);
1607
448
            }
1608
448
            opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1609
448
                          "VLC code produces significant samples outside "
1610
448
                          "the codeblock area.\n");
1611
448
            if (p_manager_mutex) {
1612
448
                opj_mutex_unlock(p_manager_mutex);
1613
448
            }
1614
448
            return OPJ_FALSE;
1615
448
        }
1616
1617
        //first quad, starting at first sample in quad and moving on
1618
54.6k
        if (qinf[0] & 0x10) { //is it significant? (sigma_n)
1619
12.3k
            OPJ_UINT32 val;
1620
1621
12.3k
            ms_val = frwd_fetch(&magsgn);         //get 32 bits of magsgn data
1622
12.3k
            m_n = U_q[0] - ((qinf[0] >> 12) & 1); //evaluate m_n (number of bits
1623
            // to read from bitstream), using EMB e_k
1624
12.3k
            frwd_advance(&magsgn, m_n);         //consume m_n
1625
12.3k
            val = ms_val << 31;                 //get sign bit
1626
12.3k
            v_n = ms_val & ((1U << m_n) - 1);   //keep only m_n bits
1627
12.3k
            v_n |= ((qinf[0] & 0x100) >> 8) << m_n;  //add EMB e_1 as MSB
1628
12.3k
            v_n |= 1;                                //add center of bin
1629
            //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
1630
            //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
1631
12.3k
            sp[0] = val | ((v_n + 2) << (p - 1));
1632
42.2k
        } else if (locs & 0x1) { // if this is inside the codeblock, set the
1633
42.2k
            sp[0] = 0;           // sample to zero
1634
42.2k
        }
1635
1636
54.6k
        if (qinf[0] & 0x20) { //sigma_n
1637
21.3k
            OPJ_UINT32 val, t;
1638
1639
21.3k
            ms_val = frwd_fetch(&magsgn);         //get 32 bits
1640
21.3k
            m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n, uses EMB e_k
1641
21.3k
            frwd_advance(&magsgn, m_n);           //consume m_n
1642
21.3k
            val = ms_val << 31;                   //get sign bit
1643
21.3k
            v_n = ms_val & ((1U << m_n) - 1);     //keep only m_n bits
1644
21.3k
            v_n |= ((qinf[0] & 0x200) >> 9) << m_n; //add EMB e_1
1645
21.3k
            v_n |= 1;                               //bin center
1646
            //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
1647
            //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
1648
21.3k
            sp[stride] = val | ((v_n + 2) << (p - 1));
1649
1650
            //update line_state: bit 7 (\sigma^N), and E^N
1651
21.3k
            t = lsp[0] & 0x7F;       // keep E^NW
1652
21.3k
            v_n = 32 - count_leading_zeros(v_n);
1653
21.3k
            lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
1654
33.3k
        } else if (locs & 0x2) { // if this is inside the codeblock, set the
1655
31.0k
            sp[stride] = 0;      // sample to zero
1656
31.0k
        }
1657
1658
54.6k
        ++lsp; // move to next quad information
1659
54.6k
        ++sp;  // move to next column of samples
1660
1661
        //this is similar to the above two samples
1662
54.6k
        if (qinf[0] & 0x40) {
1663
14.0k
            OPJ_UINT32 val;
1664
1665
14.0k
            ms_val = frwd_fetch(&magsgn);
1666
14.0k
            m_n = U_q[0] - ((qinf[0] >> 14) & 1);
1667
14.0k
            frwd_advance(&magsgn, m_n);
1668
14.0k
            val = ms_val << 31;
1669
14.0k
            v_n = ms_val & ((1U << m_n) - 1);
1670
14.0k
            v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
1671
14.0k
            v_n |= 1;
1672
14.0k
            sp[0] = val | ((v_n + 2) << (p - 1));
1673
40.6k
        } else if (locs & 0x4) {
1674
39.8k
            sp[0] = 0;
1675
39.8k
        }
1676
1677
54.6k
        lsp[0] = 0;
1678
54.6k
        if (qinf[0] & 0x80) {
1679
13.3k
            OPJ_UINT32 val;
1680
13.3k
            ms_val = frwd_fetch(&magsgn);
1681
13.3k
            m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
1682
13.3k
            frwd_advance(&magsgn, m_n);
1683
13.3k
            val = ms_val << 31;
1684
13.3k
            v_n = ms_val & ((1U << m_n) - 1);
1685
13.3k
            v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
1686
13.3k
            v_n |= 1; //center of bin
1687
13.3k
            sp[stride] = val | ((v_n + 2) << (p - 1));
1688
1689
            //line_state: bit 7 (\sigma^NW), and E^NW for next quad
1690
13.3k
            lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1691
41.2k
        } else if (locs & 0x8) { //if outside set to 0
1692
38.1k
            sp[stride] = 0;
1693
38.1k
        }
1694
1695
54.6k
        ++sp; //move to next column
1696
1697
        //second quad
1698
54.6k
        if (qinf[1] & 0x10) {
1699
12.2k
            OPJ_UINT32 val;
1700
1701
12.2k
            ms_val = frwd_fetch(&magsgn);
1702
12.2k
            m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
1703
12.2k
            frwd_advance(&magsgn, m_n);
1704
12.2k
            val = ms_val << 31;
1705
12.2k
            v_n = ms_val & ((1U << m_n) - 1);
1706
12.2k
            v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
1707
12.2k
            v_n |= 1;
1708
12.2k
            sp[0] = val | ((v_n + 2) << (p - 1));
1709
42.4k
        } else if (locs & 0x10) {
1710
36.9k
            sp[0] = 0;
1711
36.9k
        }
1712
1713
54.6k
        if (qinf[1] & 0x20) {
1714
14.9k
            OPJ_UINT32 val, t;
1715
1716
14.9k
            ms_val = frwd_fetch(&magsgn);
1717
14.9k
            m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
1718
14.9k
            frwd_advance(&magsgn, m_n);
1719
14.9k
            val = ms_val << 31;
1720
14.9k
            v_n = ms_val & ((1U << m_n) - 1);
1721
14.9k
            v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
1722
14.9k
            v_n |= 1;
1723
14.9k
            sp[stride] = val | ((v_n + 2) << (p - 1));
1724
1725
            //update line_state: bit 7 (\sigma^N), and E^N
1726
14.9k
            t = lsp[0] & 0x7F;            //E^NW
1727
14.9k
            v_n = 32 - count_leading_zeros(v_n);     //E^N
1728
14.9k
            lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
1729
39.7k
        } else if (locs & 0x20) {
1730
32.8k
            sp[stride] = 0;    //no need to update line_state
1731
32.8k
        }
1732
1733
54.6k
        ++lsp; //move line state to next quad
1734
54.6k
        ++sp;  //move to next sample
1735
1736
54.6k
        if (qinf[1] & 0x40) {
1737
14.7k
            OPJ_UINT32 val;
1738
1739
14.7k
            ms_val = frwd_fetch(&magsgn);
1740
14.7k
            m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
1741
14.7k
            frwd_advance(&magsgn, m_n);
1742
14.7k
            val = ms_val << 31;
1743
14.7k
            v_n = ms_val & ((1U << m_n) - 1);
1744
14.7k
            v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
1745
14.7k
            v_n |= 1;
1746
14.7k
            sp[0] = val | ((v_n + 2) << (p - 1));
1747
39.8k
        } else if (locs & 0x40) {
1748
34.2k
            sp[0] = 0;
1749
34.2k
        }
1750
1751
54.6k
        lsp[0] = 0;
1752
54.6k
        if (qinf[1] & 0x80) {
1753
12.1k
            OPJ_UINT32 val;
1754
1755
12.1k
            ms_val = frwd_fetch(&magsgn);
1756
12.1k
            m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
1757
12.1k
            frwd_advance(&magsgn, m_n);
1758
12.1k
            val = ms_val << 31;
1759
12.1k
            v_n = ms_val & ((1U << m_n) - 1);
1760
12.1k
            v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
1761
12.1k
            v_n |= 1; //center of bin
1762
12.1k
            sp[stride] = val | ((v_n + 2) << (p - 1));
1763
1764
            //line_state: bit 7 (\sigma^NW), and E^NW for next quad
1765
12.1k
            lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1766
42.5k
        } else if (locs & 0x80) {
1767
35.5k
            sp[stride] = 0;
1768
35.5k
        }
1769
1770
54.6k
        ++sp;
1771
54.6k
    }
1772
1773
    //non-initial lines
1774
    //////////////////////////
1775
67.4k
    for (y = 2; y < height; /*done at the end of loop*/) {
1776
61.0k
        OPJ_UINT32 *sip;
1777
61.0k
        OPJ_UINT8 ls0;
1778
61.0k
        OPJ_INT32 x;
1779
1780
61.0k
        sip_shift ^= 0x2;  // shift sigma to the upper half od the nibble
1781
61.0k
        sip_shift &= 0xFFFFFFEFU; //move back to 0 (it might have been at 0x10)
1782
61.0k
        sip = y & 0x4 ? sigma2 : sigma1; //choose sigma array
1783
1784
61.0k
        lsp = line_state;
1785
61.0k
        ls0 = lsp[0];                   // read the line state value
1786
61.0k
        lsp[0] = 0;                     // and set it to zero
1787
61.0k
        sp = decoded_data + y * stride; // generated samples
1788
61.0k
        c_q = 0;                        // context
1789
437k
        for (x = 0; x < width; x += 4) {
1790
378k
            OPJ_UINT32 U_q[2];
1791
378k
            OPJ_UINT32 uvlc_mode, consumed_bits;
1792
378k
            OPJ_UINT32 m_n, v_n;
1793
378k
            OPJ_UINT32 ms_val;
1794
378k
            OPJ_UINT32 locs;
1795
1796
            // decode vlc
1797
            /////////////
1798
1799
            //first quad
1800
            // get context, eqn. 2 ITU T.814
1801
            // c_q has \sigma^W | \sigma^SW
1802
378k
            c_q |= (ls0 >> 7);          //\sigma^NW | \sigma^N
1803
378k
            c_q |= (lsp[1] >> 5) & 0x4; //\sigma^NE | \sigma^NF
1804
1805
            //the following is very similar to previous code, so please refer to
1806
            // that
1807
378k
            vlc_val = rev_fetch(&vlc);
1808
378k
            qinf[0] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
1809
378k
            if (c_q == 0) { //zero context
1810
322k
                run -= 2;
1811
322k
                qinf[0] = (run == -1) ? qinf[0] : 0;
1812
322k
                if (run < 0) {
1813
95.9k
                    run = mel_get_run(&mel);
1814
95.9k
                }
1815
322k
            }
1816
            //prepare context for the next quad, \sigma^W | \sigma^SW
1817
378k
            c_q = ((qinf[0] & 0x40) >> 5) | ((qinf[0] & 0x80) >> 6);
1818
1819
            //remove data from vlc stream
1820
378k
            vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
1821
1822
            //update sigma
1823
            // The update depends on the value of x and y; consider one OPJ_UINT32
1824
            // if x is 0, 8, 16 and so on, and y is 2, 6, etc., then this
1825
            // line update c locations
1826
            //      nibble (4 bits) number   0 1 2 3 4 5 6 7
1827
            //                         LSB   0 0 0 0 0 0 0 0
1828
            //                               0 0 0 0 0 0 0 0
1829
            //                               c c 0 0 0 0 0 0
1830
            //                               c c 0 0 0 0 0 0
1831
378k
            *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
1832
1833
            //second quad
1834
378k
            qinf[1] = 0;
1835
378k
            if (x + 2 < width) {
1836
353k
                c_q |= (lsp[1] >> 7);
1837
353k
                c_q |= (lsp[2] >> 5) & 0x4;
1838
353k
                qinf[1] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
1839
353k
                if (c_q == 0) { //zero context
1840
303k
                    run -= 2;
1841
303k
                    qinf[1] = (run == -1) ? qinf[1] : 0;
1842
303k
                    if (run < 0) {
1843
85.3k
                        run = mel_get_run(&mel);
1844
85.3k
                    }
1845
303k
                }
1846
                //prepare context for the next quad
1847
353k
                c_q = ((qinf[1] & 0x40) >> 5) | ((qinf[1] & 0x80) >> 6);
1848
                //remove data from vlc stream
1849
353k
                vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
1850
353k
            }
1851
1852
            //update sigma
1853
378k
            *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
1854
1855
378k
            sip += x & 0x7 ? 1 : 0;
1856
378k
            sip_shift ^= 0x10;
1857
1858
            //retrieve u
1859
            ////////////
1860
378k
            uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
1861
378k
            consumed_bits = decode_noninit_uvlc(vlc_val, uvlc_mode, U_q);
1862
378k
            vlc_val = rev_advance(&vlc, consumed_bits);
1863
1864
            //calculate E^max and add it to U_q, eqns 5 and 6 in ITU T.814
1865
378k
            if ((qinf[0] & 0xF0) & ((qinf[0] & 0xF0) - 1)) { // is \gamma_q 1?
1866
18.5k
                OPJ_UINT32 E = (ls0 & 0x7Fu);
1867
18.5k
                E = E > (lsp[1] & 0x7Fu) ? E : (lsp[1] & 0x7Fu); //max(E, E^NE, E^NF)
1868
                //since U_q already has u_q + 1, we subtract 2 instead of 1
1869
18.5k
                U_q[0] += E > 2 ? E - 2 : 0;
1870
18.5k
            }
1871
1872
378k
            if ((qinf[1] & 0xF0) & ((qinf[1] & 0xF0) - 1)) { //is \gamma_q 1?
1873
14.8k
                OPJ_UINT32 E = (lsp[1] & 0x7Fu);
1874
14.8k
                E = E > (lsp[2] & 0x7Fu) ? E : (lsp[2] & 0x7Fu); //max(E, E^NE, E^NF)
1875
                //since U_q already has u_q + 1, we subtract 2 instead of 1
1876
14.8k
                U_q[1] += E > 2 ? E - 2 : 0;
1877
14.8k
            }
1878
1879
378k
            if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
1880
1.64k
                if (p_manager_mutex) {
1881
1.64k
                    opj_mutex_lock(p_manager_mutex);
1882
1.64k
                }
1883
1.64k
                opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1884
1.64k
                              "Decoding this codeblock is stopped. U_q is"
1885
1.64k
                              "larger than bitplanes + 1 \n");
1886
1.64k
                if (p_manager_mutex) {
1887
1.64k
                    opj_mutex_unlock(p_manager_mutex);
1888
1.64k
                }
1889
1.64k
                return OPJ_FALSE;
1890
1.64k
            }
1891
1892
376k
            ls0 = lsp[2]; //for next double quad
1893
376k
            lsp[1] = lsp[2] = 0;
1894
1895
            //decode magsgn and update line_state
1896
            /////////////////////////////////////
1897
1898
            //locations where samples need update
1899
376k
            locs = 0xFF;
1900
376k
            if (x + 4 > width) {
1901
26.1k
                locs >>= (x + 4 - width) << 1;
1902
26.1k
            }
1903
376k
            locs = y + 2 <= height ? locs : (locs & 0x55);
1904
1905
376k
            if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
1906
573
                if (p_manager_mutex) {
1907
573
                    opj_mutex_lock(p_manager_mutex);
1908
573
                }
1909
573
                opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1910
573
                              "VLC code produces significant samples outside "
1911
573
                              "the codeblock area.\n");
1912
573
                if (p_manager_mutex) {
1913
573
                    opj_mutex_unlock(p_manager_mutex);
1914
573
                }
1915
573
                return OPJ_FALSE;
1916
573
            }
1917
1918
1919
1920
376k
            if (qinf[0] & 0x10) { //sigma_n
1921
54.2k
                OPJ_UINT32 val;
1922
1923
54.2k
                ms_val = frwd_fetch(&magsgn);
1924
54.2k
                m_n = U_q[0] - ((qinf[0] >> 12) & 1); //m_n
1925
54.2k
                frwd_advance(&magsgn, m_n);
1926
54.2k
                val = ms_val << 31;
1927
54.2k
                v_n = ms_val & ((1U << m_n) - 1);
1928
54.2k
                v_n |= ((qinf[0] & 0x100) >> 8) << m_n;
1929
54.2k
                v_n |= 1; //center of bin
1930
54.2k
                sp[0] = val | ((v_n + 2) << (p - 1));
1931
322k
            } else if (locs & 0x1) {
1932
322k
                sp[0] = 0;
1933
322k
            }
1934
1935
376k
            if (qinf[0] & 0x20) { //sigma_n
1936
16.0k
                OPJ_UINT32 val, t;
1937
1938
16.0k
                ms_val = frwd_fetch(&magsgn);
1939
16.0k
                m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n
1940
16.0k
                frwd_advance(&magsgn, m_n);
1941
16.0k
                val = ms_val << 31;
1942
16.0k
                v_n = ms_val & ((1U << m_n) - 1);
1943
16.0k
                v_n |= ((qinf[0] & 0x200) >> 9) << m_n;
1944
16.0k
                v_n |= 1; //center of bin
1945
16.0k
                sp[stride] = val | ((v_n + 2) << (p - 1));
1946
1947
                //update line_state: bit 7 (\sigma^N), and E^N
1948
16.0k
                t = lsp[0] & 0x7F;          //E^NW
1949
16.0k
                v_n = 32 - count_leading_zeros(v_n);
1950
16.0k
                lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
1951
360k
            } else if (locs & 0x2) {
1952
353k
                sp[stride] = 0;    //no need to update line_state
1953
353k
            }
1954
1955
376k
            ++lsp;
1956
376k
            ++sp;
1957
1958
376k
            if (qinf[0] & 0x40) { //sigma_n
1959
13.8k
                OPJ_UINT32 val;
1960
1961
13.8k
                ms_val = frwd_fetch(&magsgn);
1962
13.8k
                m_n = U_q[0] - ((qinf[0] >> 14) & 1); //m_n
1963
13.8k
                frwd_advance(&magsgn, m_n);
1964
13.8k
                val = ms_val << 31;
1965
13.8k
                v_n = ms_val & ((1U << m_n) - 1);
1966
13.8k
                v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
1967
13.8k
                v_n |= 1;                            //center of bin
1968
13.8k
                sp[0] = val | ((v_n + 2) << (p - 1));
1969
362k
            } else if (locs & 0x4) {
1970
359k
                sp[0] = 0;
1971
359k
            }
1972
1973
376k
            if (qinf[0] & 0x80) { //sigma_n
1974
14.2k
                OPJ_UINT32 val;
1975
1976
14.2k
                ms_val = frwd_fetch(&magsgn);
1977
14.2k
                m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
1978
14.2k
                frwd_advance(&magsgn, m_n);
1979
14.2k
                val = ms_val << 31;
1980
14.2k
                v_n = ms_val & ((1U << m_n) - 1);
1981
14.2k
                v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
1982
14.2k
                v_n |= 1; //center of bin
1983
14.2k
                sp[stride] = val | ((v_n + 2) << (p - 1));
1984
1985
                //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
1986
14.2k
                lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1987
362k
            } else if (locs & 0x8) {
1988
351k
                sp[stride] = 0;
1989
351k
            }
1990
1991
376k
            ++sp;
1992
1993
376k
            if (qinf[1] & 0x10) { //sigma_n
1994
48.4k
                OPJ_UINT32 val;
1995
1996
48.4k
                ms_val = frwd_fetch(&magsgn);
1997
48.4k
                m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
1998
48.4k
                frwd_advance(&magsgn, m_n);
1999
48.4k
                val = ms_val << 31;
2000
48.4k
                v_n = ms_val & ((1U << m_n) - 1);
2001
48.4k
                v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
2002
48.4k
                v_n |= 1;                            //center of bin
2003
48.4k
                sp[0] = val | ((v_n + 2) << (p - 1));
2004
327k
            } else if (locs & 0x10) {
2005
303k
                sp[0] = 0;
2006
303k
            }
2007
2008
376k
            if (qinf[1] & 0x20) { //sigma_n
2009
11.7k
                OPJ_UINT32 val, t;
2010
2011
11.7k
                ms_val = frwd_fetch(&magsgn);
2012
11.7k
                m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
2013
11.7k
                frwd_advance(&magsgn, m_n);
2014
11.7k
                val = ms_val << 31;
2015
11.7k
                v_n = ms_val & ((1U << m_n) - 1);
2016
11.7k
                v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
2017
11.7k
                v_n |= 1; //center of bin
2018
11.7k
                sp[stride] = val | ((v_n + 2) << (p - 1));
2019
2020
                //update line_state: bit 7 (\sigma^N), and E^N
2021
11.7k
                t = lsp[0] & 0x7F;          //E^NW
2022
11.7k
                v_n = 32 - count_leading_zeros(v_n);
2023
11.7k
                lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
2024
364k
            } else if (locs & 0x20) {
2025
334k
                sp[stride] = 0;    //no need to update line_state
2026
334k
            }
2027
2028
376k
            ++lsp;
2029
376k
            ++sp;
2030
2031
376k
            if (qinf[1] & 0x40) { //sigma_n
2032
12.8k
                OPJ_UINT32 val;
2033
2034
12.8k
                ms_val = frwd_fetch(&magsgn);
2035
12.8k
                m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
2036
12.8k
                frwd_advance(&magsgn, m_n);
2037
12.8k
                val = ms_val << 31;
2038
12.8k
                v_n = ms_val & ((1U << m_n) - 1);
2039
12.8k
                v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
2040
12.8k
                v_n |= 1;                            //center of bin
2041
12.8k
                sp[0] = val | ((v_n + 2) << (p - 1));
2042
363k
            } else if (locs & 0x40) {
2043
337k
                sp[0] = 0;
2044
337k
            }
2045
2046
376k
            if (qinf[1] & 0x80) { //sigma_n
2047
10.5k
                OPJ_UINT32 val;
2048
2049
10.5k
                ms_val = frwd_fetch(&magsgn);
2050
10.5k
                m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
2051
10.5k
                frwd_advance(&magsgn, m_n);
2052
10.5k
                val = ms_val << 31;
2053
10.5k
                v_n = ms_val & ((1U << m_n) - 1);
2054
10.5k
                v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
2055
10.5k
                v_n |= 1; //center of bin
2056
10.5k
                sp[stride] = val | ((v_n + 2) << (p - 1));
2057
2058
                //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
2059
10.5k
                lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
2060
365k
            } else if (locs & 0x80) {
2061
334k
                sp[stride] = 0;
2062
334k
            }
2063
2064
376k
            ++sp;
2065
376k
        }
2066
2067
58.8k
        y += 2;
2068
58.8k
        if (num_passes > 1 && (y & 3) == 0) { //executed at multiples of 4
2069
            // This is for SPP and potentially MRP
2070
2071
22.7k
            if (num_passes > 2) { //do MRP
2072
                // select the current stripe
2073
10.0k
                OPJ_UINT32 *cur_sig = y & 0x4 ? sigma1 : sigma2;
2074
                // the address of the data that needs updating
2075
10.0k
                OPJ_UINT32 *dpp = decoded_data + (y - 4) * stride;
2076
10.0k
                OPJ_UINT32 half = 1u << (p - 2); // half the center of the bin
2077
10.0k
                OPJ_INT32 i;
2078
59.7k
                for (i = 0; i < width; i += 8) {
2079
                    //Process one entry from sigma array at a time
2080
                    // Each nibble (4 bits) in the sigma array represents 4 rows,
2081
                    // and the 32 bits contain 8 columns
2082
49.6k
                    OPJ_UINT32 cwd = rev_fetch_mrp(&magref); // get 32 bit data
2083
49.6k
                    OPJ_UINT32 sig = *cur_sig++; // 32 bit that will be processed now
2084
49.6k
                    OPJ_UINT32 col_mask = 0xFu;  // a mask for a column in sig
2085
49.6k
                    OPJ_UINT32 *dp = dpp + i;    // next column in decode samples
2086
49.6k
                    if (sig) { // if any of the 32 bits are set
2087
24.9k
                        int j;
2088
224k
                        for (j = 0; j < 8; ++j, dp++) { //one column at a time
2089
199k
                            if (sig & col_mask) { // lowest nibble
2090
84.8k
                                OPJ_UINT32 sample_mask = 0x11111111u & col_mask; //LSB
2091
2092
84.8k
                                if (sig & sample_mask) { //if LSB is set
2093
53.8k
                                    OPJ_UINT32 sym;
2094
2095
53.8k
                                    assert(dp[0] != 0); // decoded value cannot be zero
2096
53.8k
                                    sym = cwd & 1; // get it value
2097
                                    // remove center of bin if sym is 0
2098
53.8k
                                    dp[0] ^= (1 - sym) << (p - 1);
2099
53.8k
                                    dp[0] |= half;      // put half the center of bin
2100
53.8k
                                    cwd >>= 1;          //consume word
2101
53.8k
                                }
2102
84.8k
                                sample_mask += sample_mask; //next row
2103
2104
84.8k
                                if (sig & sample_mask) {
2105
36.4k
                                    OPJ_UINT32 sym;
2106
2107
36.4k
                                    assert(dp[stride] != 0);
2108
36.4k
                                    sym = cwd & 1;
2109
36.4k
                                    dp[stride] ^= (1 - sym) << (p - 1);
2110
36.4k
                                    dp[stride] |= half;
2111
36.4k
                                    cwd >>= 1;
2112
36.4k
                                }
2113
84.8k
                                sample_mask += sample_mask;
2114
2115
84.8k
                                if (sig & sample_mask) {
2116
34.2k
                                    OPJ_UINT32 sym;
2117
2118
34.2k
                                    assert(dp[2 * stride] != 0);
2119
34.2k
                                    sym = cwd & 1;
2120
34.2k
                                    dp[2 * stride] ^= (1 - sym) << (p - 1);
2121
34.2k
                                    dp[2 * stride] |= half;
2122
34.2k
                                    cwd >>= 1;
2123
34.2k
                                }
2124
84.8k
                                sample_mask += sample_mask;
2125
2126
84.8k
                                if (sig & sample_mask) {
2127
16.1k
                                    OPJ_UINT32 sym;
2128
2129
16.1k
                                    assert(dp[3 * stride] != 0);
2130
16.1k
                                    sym = cwd & 1;
2131
16.1k
                                    dp[3 * stride] ^= (1 - sym) << (p - 1);
2132
16.1k
                                    dp[3 * stride] |= half;
2133
16.1k
                                    cwd >>= 1;
2134
16.1k
                                }
2135
84.8k
                                sample_mask += sample_mask;
2136
84.8k
                            }
2137
199k
                            col_mask <<= 4; //next column
2138
199k
                        }
2139
24.9k
                    }
2140
                    // consume data according to the number of bits set
2141
49.6k
                    rev_advance_mrp(&magref, population_count(sig));
2142
49.6k
                }
2143
10.0k
            }
2144
2145
22.7k
            if (y >= 4) { // update mbr array at the end of each stripe
2146
                //generate mbr corresponding to a stripe
2147
22.7k
                OPJ_UINT32 *sig = y & 0x4 ? sigma1 : sigma2;
2148
22.7k
                OPJ_UINT32 *mbr = y & 0x4 ? mbr1 : mbr2;
2149
2150
                //data is processed in patches of 8 columns, each
2151
                // each 32 bits in sigma1 or mbr1 represent 4 rows
2152
2153
                //integrate horizontally
2154
22.7k
                OPJ_UINT32 prev = 0; // previous columns
2155
22.7k
                OPJ_INT32 i;
2156
105k
                for (i = 0; i < width; i += 8, mbr++, sig++) {
2157
82.7k
                    OPJ_UINT32 t, z;
2158
2159
82.7k
                    mbr[0] = sig[0];         //start with significant samples
2160
82.7k
                    mbr[0] |= prev >> 28;    //for first column, left neighbors
2161
82.7k
                    mbr[0] |= sig[0] << 4;   //left neighbors
2162
82.7k
                    mbr[0] |= sig[0] >> 4;   //right neighbors
2163
82.7k
                    mbr[0] |= sig[1] << 28;  //for last column, right neighbors
2164
82.7k
                    prev = sig[0];           // for next group of columns
2165
2166
                    //integrate vertically
2167
82.7k
                    t = mbr[0], z = mbr[0];
2168
82.7k
                    z |= (t & 0x77777777) << 1; //above neighbors
2169
82.7k
                    z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
2170
82.7k
                    mbr[0] = z & ~sig[0]; //remove already significance samples
2171
82.7k
                }
2172
22.7k
            }
2173
2174
22.7k
            if (y >= 8) { //wait until 8 rows has been processed
2175
17.9k
                OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
2176
17.9k
                OPJ_UINT32 prev;
2177
17.9k
                OPJ_UINT32 val;
2178
17.9k
                OPJ_INT32 i;
2179
2180
                // add membership from the next stripe, obtained above
2181
17.9k
                cur_sig = y & 0x4 ? sigma2 : sigma1;
2182
17.9k
                cur_mbr = y & 0x4 ? mbr2 : mbr1;
2183
17.9k
                nxt_sig = y & 0x4 ? sigma1 : sigma2;  //future samples
2184
17.9k
                prev = 0; // the columns before these group of 8 columns
2185
83.0k
                for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
2186
65.0k
                    OPJ_UINT32 t = nxt_sig[0];
2187
65.0k
                    t |= prev >> 28;        //for first column, left neighbors
2188
65.0k
                    t |= nxt_sig[0] << 4;   //left neighbors
2189
65.0k
                    t |= nxt_sig[0] >> 4;   //right neighbors
2190
65.0k
                    t |= nxt_sig[1] << 28;  //for last column, right neighbors
2191
65.0k
                    prev = nxt_sig[0];      // for next group of columns
2192
2193
65.0k
                    if (!stripe_causal) {
2194
13.6k
                        cur_mbr[0] |= (t & 0x11111111u) << 3; //propagate up to cur_mbr
2195
13.6k
                    }
2196
65.0k
                    cur_mbr[0] &= ~cur_sig[0]; //remove already significance samples
2197
65.0k
                }
2198
2199
                //find new locations and get signs
2200
17.9k
                cur_sig = y & 0x4 ? sigma2 : sigma1;
2201
17.9k
                cur_mbr = y & 0x4 ? mbr2 : mbr1;
2202
17.9k
                nxt_sig = y & 0x4 ? sigma1 : sigma2; //future samples
2203
17.9k
                nxt_mbr = y & 0x4 ? mbr1 : mbr2;     //future samples
2204
17.9k
                val = 3u << (p - 2); // sample values for newly discovered
2205
                // significant samples including the bin center
2206
83.0k
                for (i = 0; i < width;
2207
65.0k
                        i += 8, cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
2208
65.0k
                    OPJ_UINT32 ux, tx;
2209
65.0k
                    OPJ_UINT32 mbr = *cur_mbr;
2210
65.0k
                    OPJ_UINT32 new_sig = 0;
2211
65.0k
                    if (mbr) { //are there any samples that might be significant
2212
36.4k
                        OPJ_INT32 n;
2213
109k
                        for (n = 0; n < 8; n += 4) {
2214
72.9k
                            OPJ_UINT32 col_mask;
2215
72.9k
                            OPJ_UINT32 inv_sig;
2216
72.9k
                            OPJ_INT32 end;
2217
72.9k
                            OPJ_INT32 j;
2218
2219
72.9k
                            OPJ_UINT32 cwd = frwd_fetch(&sigprop); //get 32 bits
2220
72.9k
                            OPJ_UINT32 cnt = 0;
2221
2222
72.9k
                            OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
2223
72.9k
                            dp += i + n; //address for decoded samples
2224
2225
72.9k
                            col_mask = 0xFu << (4 * n); //a mask to select a column
2226
2227
72.9k
                            inv_sig = ~cur_sig[0]; // insignificant samples
2228
2229
                            //find the last sample we operate on
2230
72.9k
                            end = n + 4 + i < width ? n + 4 : width - i;
2231
2232
330k
                            for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2233
257k
                                OPJ_UINT32 sample_mask;
2234
2235
257k
                                if ((col_mask & mbr) == 0) { //no samples need checking
2236
44.9k
                                    continue;
2237
44.9k
                                }
2238
2239
                                //scan mbr to find a new significant sample
2240
212k
                                sample_mask = 0x11111111u & col_mask; // LSB
2241
212k
                                if (mbr & sample_mask) {
2242
132k
                                    assert(dp[0] == 0); // the sample must have been 0
2243
132k
                                    if (cwd & 1) { //if this sample has become significant
2244
                                        // must propagate it to nearby samples
2245
36.3k
                                        OPJ_UINT32 t;
2246
36.3k
                                        new_sig |= sample_mask;  // new significant samples
2247
36.3k
                                        t = 0x32u << (j * 4);// propagation to neighbors
2248
36.3k
                                        mbr |= t & inv_sig; //remove already significant samples
2249
36.3k
                                    }
2250
132k
                                    cwd >>= 1;
2251
132k
                                    ++cnt; //consume bit and increment number of
2252
                                    //consumed bits
2253
132k
                                }
2254
2255
212k
                                sample_mask += sample_mask;  // next row
2256
212k
                                if (mbr & sample_mask) {
2257
160k
                                    assert(dp[stride] == 0);
2258
160k
                                    if (cwd & 1) {
2259
39.9k
                                        OPJ_UINT32 t;
2260
39.9k
                                        new_sig |= sample_mask;
2261
39.9k
                                        t = 0x74u << (j * 4);
2262
39.9k
                                        mbr |= t & inv_sig;
2263
39.9k
                                    }
2264
160k
                                    cwd >>= 1;
2265
160k
                                    ++cnt;
2266
160k
                                }
2267
2268
212k
                                sample_mask += sample_mask;
2269
212k
                                if (mbr & sample_mask) {
2270
133k
                                    assert(dp[2 * stride] == 0);
2271
133k
                                    if (cwd & 1) {
2272
42.7k
                                        OPJ_UINT32 t;
2273
42.7k
                                        new_sig |= sample_mask;
2274
42.7k
                                        t = 0xE8u << (j * 4);
2275
42.7k
                                        mbr |= t & inv_sig;
2276
42.7k
                                    }
2277
133k
                                    cwd >>= 1;
2278
133k
                                    ++cnt;
2279
133k
                                }
2280
2281
212k
                                sample_mask += sample_mask;
2282
212k
                                if (mbr & sample_mask) {
2283
127k
                                    assert(dp[3 * stride] == 0);
2284
127k
                                    if (cwd & 1) {
2285
40.1k
                                        OPJ_UINT32 t;
2286
40.1k
                                        new_sig |= sample_mask;
2287
40.1k
                                        t = 0xC0u << (j * 4);
2288
40.1k
                                        mbr |= t & inv_sig;
2289
40.1k
                                    }
2290
127k
                                    cwd >>= 1;
2291
127k
                                    ++cnt;
2292
127k
                                }
2293
212k
                            }
2294
2295
                            //obtain signs here
2296
72.9k
                            if (new_sig & (0xFFFFu << (4 * n))) { //if any
2297
30.7k
                                OPJ_UINT32 col_mask;
2298
30.7k
                                OPJ_INT32 j;
2299
30.7k
                                OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
2300
30.7k
                                dp += i + n; // decoded samples address
2301
30.7k
                                col_mask = 0xFu << (4 * n); //mask to select a column
2302
2303
146k
                                for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2304
116k
                                    OPJ_UINT32 sample_mask;
2305
2306
116k
                                    if ((col_mask & new_sig) == 0) { //if non is significant
2307
31.1k
                                        continue;
2308
31.1k
                                    }
2309
2310
                                    //scan 4 signs
2311
85.0k
                                    sample_mask = 0x11111111u & col_mask;
2312
85.0k
                                    if (new_sig & sample_mask) {
2313
36.3k
                                        assert(dp[0] == 0);
2314
36.3k
                                        dp[0] |= ((cwd & 1) << 31) | val; //put value and sign
2315
36.3k
                                        cwd >>= 1;
2316
36.3k
                                        ++cnt; //consume bit and increment number
2317
                                        //of consumed bits
2318
36.3k
                                    }
2319
2320
85.0k
                                    sample_mask += sample_mask;
2321
85.0k
                                    if (new_sig & sample_mask) {
2322
39.9k
                                        assert(dp[stride] == 0);
2323
39.9k
                                        dp[stride] |= ((cwd & 1) << 31) | val;
2324
39.9k
                                        cwd >>= 1;
2325
39.9k
                                        ++cnt;
2326
39.9k
                                    }
2327
2328
85.0k
                                    sample_mask += sample_mask;
2329
85.0k
                                    if (new_sig & sample_mask) {
2330
42.7k
                                        assert(dp[2 * stride] == 0);
2331
42.7k
                                        dp[2 * stride] |= ((cwd & 1) << 31) | val;
2332
42.7k
                                        cwd >>= 1;
2333
42.7k
                                        ++cnt;
2334
42.7k
                                    }
2335
2336
85.0k
                                    sample_mask += sample_mask;
2337
85.0k
                                    if (new_sig & sample_mask) {
2338
40.1k
                                        assert(dp[3 * stride] == 0);
2339
40.1k
                                        dp[3 * stride] |= ((cwd & 1) << 31) | val;
2340
40.1k
                                        cwd >>= 1;
2341
40.1k
                                        ++cnt;
2342
40.1k
                                    }
2343
85.0k
                                }
2344
2345
30.7k
                            }
2346
72.9k
                            frwd_advance(&sigprop, cnt); //consume the bits from bitstrm
2347
72.9k
                            cnt = 0;
2348
2349
                            //update the next 8 columns
2350
72.9k
                            if (n == 4) {
2351
                                //horizontally
2352
36.4k
                                OPJ_UINT32 t = new_sig >> 28;
2353
36.4k
                                t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
2354
36.4k
                                cur_mbr[1] |= t & ~cur_sig[1];
2355
36.4k
                            }
2356
72.9k
                        }
2357
36.4k
                    }
2358
                    //update the next stripe (vertically propagation)
2359
65.0k
                    new_sig |= cur_sig[0];
2360
65.0k
                    ux = (new_sig & 0x88888888) >> 3;
2361
65.0k
                    tx = ux | (ux << 4) | (ux >> 4); //left and right neighbors
2362
65.0k
                    if (i > 0) {
2363
47.1k
                        nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
2364
47.1k
                    }
2365
65.0k
                    nxt_mbr[0] |= tx & ~nxt_sig[0];
2366
65.0k
                    nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
2367
65.0k
                }
2368
2369
                //clear current sigma
2370
                //mbr need not be cleared because it is overwritten
2371
17.9k
                cur_sig = y & 0x4 ? sigma2 : sigma1;
2372
17.9k
                memset(cur_sig, 0, ((((OPJ_UINT32)width + 7u) >> 3) + 1u) << 2);
2373
17.9k
            }
2374
22.7k
        }
2375
58.8k
    }
2376
2377
    //terminating
2378
6.38k
    if (num_passes > 1) {
2379
4.71k
        OPJ_INT32 st, y;
2380
2381
4.71k
        if (num_passes > 2 && ((height & 3) == 1 || (height & 3) == 2)) {
2382
            //do magref
2383
1.08k
            OPJ_UINT32 *cur_sig = height & 0x4 ? sigma2 : sigma1; //reversed
2384
1.08k
            OPJ_UINT32 *dpp = decoded_data + (height & 0xFFFFFC) * stride;
2385
1.08k
            OPJ_UINT32 half = 1u << (p - 2);
2386
1.08k
            OPJ_INT32 i;
2387
5.95k
            for (i = 0; i < width; i += 8) {
2388
4.86k
                OPJ_UINT32 cwd = rev_fetch_mrp(&magref);
2389
4.86k
                OPJ_UINT32 sig = *cur_sig++;
2390
4.86k
                OPJ_UINT32 col_mask = 0xF;
2391
4.86k
                OPJ_UINT32 *dp = dpp + i;
2392
4.86k
                if (sig) {
2393
1.53k
                    int j;
2394
13.8k
                    for (j = 0; j < 8; ++j, dp++) {
2395
12.3k
                        if (sig & col_mask) {
2396
3.91k
                            OPJ_UINT32 sample_mask = 0x11111111 & col_mask;
2397
2398
3.91k
                            if (sig & sample_mask) {
2399
3.36k
                                OPJ_UINT32 sym;
2400
3.36k
                                assert(dp[0] != 0);
2401
3.36k
                                sym = cwd & 1;
2402
3.36k
                                dp[0] ^= (1 - sym) << (p - 1);
2403
3.36k
                                dp[0] |= half;
2404
3.36k
                                cwd >>= 1;
2405
3.36k
                            }
2406
3.91k
                            sample_mask += sample_mask;
2407
2408
3.91k
                            if (sig & sample_mask) {
2409
988
                                OPJ_UINT32 sym;
2410
988
                                assert(dp[stride] != 0);
2411
988
                                sym = cwd & 1;
2412
988
                                dp[stride] ^= (1 - sym) << (p - 1);
2413
988
                                dp[stride] |= half;
2414
988
                                cwd >>= 1;
2415
988
                            }
2416
3.91k
                            sample_mask += sample_mask;
2417
2418
3.91k
                            if (sig & sample_mask) {
2419
0
                                OPJ_UINT32 sym;
2420
0
                                assert(dp[2 * stride] != 0);
2421
0
                                sym = cwd & 1;
2422
0
                                dp[2 * stride] ^= (1 - sym) << (p - 1);
2423
0
                                dp[2 * stride] |= half;
2424
0
                                cwd >>= 1;
2425
0
                            }
2426
3.91k
                            sample_mask += sample_mask;
2427
2428
3.91k
                            if (sig & sample_mask) {
2429
0
                                OPJ_UINT32 sym;
2430
0
                                assert(dp[3 * stride] != 0);
2431
0
                                sym = cwd & 1;
2432
0
                                dp[3 * stride] ^= (1 - sym) << (p - 1);
2433
0
                                dp[3 * stride] |= half;
2434
0
                                cwd >>= 1;
2435
0
                            }
2436
3.91k
                            sample_mask += sample_mask;
2437
3.91k
                        }
2438
12.3k
                        col_mask <<= 4;
2439
12.3k
                    }
2440
1.53k
                }
2441
4.86k
                rev_advance_mrp(&magref, population_count(sig));
2442
4.86k
            }
2443
1.08k
        }
2444
2445
        //do the last incomplete stripe
2446
        // for cases of (height & 3) == 0 and 3
2447
        // the should have been processed previously
2448
4.71k
        if ((height & 3) == 1 || (height & 3) == 2) {
2449
            //generate mbr of first stripe
2450
1.68k
            OPJ_UINT32 *sig = height & 0x4 ? sigma2 : sigma1;
2451
1.68k
            OPJ_UINT32 *mbr = height & 0x4 ? mbr2 : mbr1;
2452
            //integrate horizontally
2453
1.68k
            OPJ_UINT32 prev = 0;
2454
1.68k
            OPJ_INT32 i;
2455
7.94k
            for (i = 0; i < width; i += 8, mbr++, sig++) {
2456
6.25k
                OPJ_UINT32 t, z;
2457
2458
6.25k
                mbr[0] = sig[0];
2459
6.25k
                mbr[0] |= prev >> 28;    //for first column, left neighbors
2460
6.25k
                mbr[0] |= sig[0] << 4;   //left neighbors
2461
6.25k
                mbr[0] |= sig[0] >> 4;   //left neighbors
2462
6.25k
                mbr[0] |= sig[1] << 28;  //for last column, right neighbors
2463
6.25k
                prev = sig[0];
2464
2465
                //integrate vertically
2466
6.25k
                t = mbr[0], z = mbr[0];
2467
6.25k
                z |= (t & 0x77777777) << 1; //above neighbors
2468
6.25k
                z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
2469
6.25k
                mbr[0] = z & ~sig[0]; //remove already significance samples
2470
6.25k
            }
2471
1.68k
        }
2472
2473
4.71k
        st = height;
2474
4.71k
        st -= height > 6 ? (((height + 1) & 3) + 3) : height;
2475
10.0k
        for (y = st; y < height; y += 4) {
2476
5.29k
            OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
2477
5.29k
            OPJ_UINT32 val;
2478
5.29k
            OPJ_INT32 i;
2479
2480
5.29k
            OPJ_UINT32 pattern = 0xFFFFFFFFu; // a pattern needed samples
2481
5.29k
            if (height - y == 3) {
2482
605
                pattern = 0x77777777u;
2483
4.69k
            } else if (height - y == 2) {
2484
595
                pattern = 0x33333333u;
2485
4.09k
            } else if (height - y == 1) {
2486
1.09k
                pattern = 0x11111111u;
2487
1.09k
            }
2488
2489
            //add membership from the next stripe, obtained above
2490
5.29k
            if (height - y > 4) {
2491
577
                OPJ_UINT32 prev = 0;
2492
577
                OPJ_INT32 i;
2493
577
                cur_sig = y & 0x4 ? sigma2 : sigma1;
2494
577
                cur_mbr = y & 0x4 ? mbr2 : mbr1;
2495
577
                nxt_sig = y & 0x4 ? sigma1 : sigma2;
2496
5.14k
                for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
2497
4.57k
                    OPJ_UINT32 t = nxt_sig[0];
2498
4.57k
                    t |= prev >> 28;     //for first column, left neighbors
2499
4.57k
                    t |= nxt_sig[0] << 4;   //left neighbors
2500
4.57k
                    t |= nxt_sig[0] >> 4;   //left neighbors
2501
4.57k
                    t |= nxt_sig[1] << 28;  //for last column, right neighbors
2502
4.57k
                    prev = nxt_sig[0];
2503
2504
4.57k
                    if (!stripe_causal) {
2505
1.12k
                        cur_mbr[0] |= (t & 0x11111111u) << 3;
2506
1.12k
                    }
2507
                    //remove already significance samples
2508
4.57k
                    cur_mbr[0] &= ~cur_sig[0];
2509
4.57k
                }
2510
577
            }
2511
2512
            //find new locations and get signs
2513
5.29k
            cur_sig = y & 0x4 ? sigma2 : sigma1;
2514
5.29k
            cur_mbr = y & 0x4 ? mbr2 : mbr1;
2515
5.29k
            nxt_sig = y & 0x4 ? sigma1 : sigma2;
2516
5.29k
            nxt_mbr = y & 0x4 ? mbr1 : mbr2;
2517
5.29k
            val = 3u << (p - 2);
2518
25.9k
            for (i = 0; i < width; i += 8,
2519
20.6k
                    cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
2520
20.6k
                OPJ_UINT32 mbr = *cur_mbr & pattern; //skip unneeded samples
2521
20.6k
                OPJ_UINT32 new_sig = 0;
2522
20.6k
                OPJ_UINT32 ux, tx;
2523
20.6k
                if (mbr) {
2524
10.1k
                    OPJ_INT32 n;
2525
30.4k
                    for (n = 0; n < 8; n += 4) {
2526
20.2k
                        OPJ_UINT32 col_mask;
2527
20.2k
                        OPJ_UINT32 inv_sig;
2528
20.2k
                        OPJ_INT32 end;
2529
20.2k
                        OPJ_INT32 j;
2530
2531
20.2k
                        OPJ_UINT32 cwd = frwd_fetch(&sigprop);
2532
20.2k
                        OPJ_UINT32 cnt = 0;
2533
2534
20.2k
                        OPJ_UINT32 *dp = decoded_data + y * stride;
2535
20.2k
                        dp += i + n;
2536
2537
20.2k
                        col_mask = 0xFu << (4 * n);
2538
2539
20.2k
                        inv_sig = ~cur_sig[0] & pattern;
2540
2541
20.2k
                        end = n + 4 + i < width ? n + 4 : width - i;
2542
95.9k
                        for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2543
75.6k
                            OPJ_UINT32 sample_mask;
2544
2545
75.6k
                            if ((col_mask & mbr) == 0) {
2546
14.9k
                                continue;
2547
14.9k
                            }
2548
2549
                            //scan 4 mbr
2550
60.6k
                            sample_mask = 0x11111111u & col_mask;
2551
60.6k
                            if (mbr & sample_mask) {
2552
46.4k
                                assert(dp[0] == 0);
2553
46.4k
                                if (cwd & 1) {
2554
15.2k
                                    OPJ_UINT32 t;
2555
15.2k
                                    new_sig |= sample_mask;
2556
15.2k
                                    t = 0x32u << (j * 4);
2557
15.2k
                                    mbr |= t & inv_sig;
2558
15.2k
                                }
2559
46.4k
                                cwd >>= 1;
2560
46.4k
                                ++cnt;
2561
46.4k
                            }
2562
2563
60.6k
                            sample_mask += sample_mask;
2564
60.6k
                            if (mbr & sample_mask) {
2565
42.9k
                                assert(dp[stride] == 0);
2566
42.9k
                                if (cwd & 1) {
2567
15.6k
                                    OPJ_UINT32 t;
2568
15.6k
                                    new_sig |= sample_mask;
2569
15.6k
                                    t = 0x74u << (j * 4);
2570
15.6k
                                    mbr |= t & inv_sig;
2571
15.6k
                                }
2572
42.9k
                                cwd >>= 1;
2573
42.9k
                                ++cnt;
2574
42.9k
                            }
2575
2576
60.6k
                            sample_mask += sample_mask;
2577
60.6k
                            if (mbr & sample_mask) {
2578
24.3k
                                assert(dp[2 * stride] == 0);
2579
24.3k
                                if (cwd & 1) {
2580
11.3k
                                    OPJ_UINT32 t;
2581
11.3k
                                    new_sig |= sample_mask;
2582
11.3k
                                    t = 0xE8u << (j * 4);
2583
11.3k
                                    mbr |= t & inv_sig;
2584
11.3k
                                }
2585
24.3k
                                cwd >>= 1;
2586
24.3k
                                ++cnt;
2587
24.3k
                            }
2588
2589
60.6k
                            sample_mask += sample_mask;
2590
60.6k
                            if (mbr & sample_mask) {
2591
19.6k
                                assert(dp[3 * stride] == 0);
2592
19.6k
                                if (cwd & 1) {
2593
9.43k
                                    OPJ_UINT32 t;
2594
9.43k
                                    new_sig |= sample_mask;
2595
9.43k
                                    t = 0xC0u << (j * 4);
2596
9.43k
                                    mbr |= t & inv_sig;
2597
9.43k
                                }
2598
19.6k
                                cwd >>= 1;
2599
19.6k
                                ++cnt;
2600
19.6k
                            }
2601
60.6k
                        }
2602
2603
                        //signs here
2604
20.2k
                        if (new_sig & (0xFFFFu << (4 * n))) {
2605
9.34k
                            OPJ_UINT32 col_mask;
2606
9.34k
                            OPJ_INT32 j;
2607
9.34k
                            OPJ_UINT32 *dp = decoded_data + y * stride;
2608
9.34k
                            dp += i + n;
2609
9.34k
                            col_mask = 0xFu << (4 * n);
2610
2611
44.7k
                            for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2612
35.4k
                                OPJ_UINT32 sample_mask;
2613
35.4k
                                if ((col_mask & new_sig) == 0) {
2614
8.78k
                                    continue;
2615
8.78k
                                }
2616
2617
                                //scan 4 signs
2618
26.6k
                                sample_mask = 0x11111111u & col_mask;
2619
26.6k
                                if (new_sig & sample_mask) {
2620
15.2k
                                    assert(dp[0] == 0);
2621
15.2k
                                    dp[0] |= ((cwd & 1) << 31) | val;
2622
15.2k
                                    cwd >>= 1;
2623
15.2k
                                    ++cnt;
2624
15.2k
                                }
2625
2626
26.6k
                                sample_mask += sample_mask;
2627
26.6k
                                if (new_sig & sample_mask) {
2628
15.6k
                                    assert(dp[stride] == 0);
2629
15.6k
                                    dp[stride] |= ((cwd & 1) << 31) | val;
2630
15.6k
                                    cwd >>= 1;
2631
15.6k
                                    ++cnt;
2632
15.6k
                                }
2633
2634
26.6k
                                sample_mask += sample_mask;
2635
26.6k
                                if (new_sig & sample_mask) {
2636
11.3k
                                    assert(dp[2 * stride] == 0);
2637
11.3k
                                    dp[2 * stride] |= ((cwd & 1) << 31) | val;
2638
11.3k
                                    cwd >>= 1;
2639
11.3k
                                    ++cnt;
2640
11.3k
                                }
2641
2642
26.6k
                                sample_mask += sample_mask;
2643
26.6k
                                if (new_sig & sample_mask) {
2644
9.43k
                                    assert(dp[3 * stride] == 0);
2645
9.43k
                                    dp[3 * stride] |= ((cwd & 1) << 31) | val;
2646
9.43k
                                    cwd >>= 1;
2647
9.43k
                                    ++cnt;
2648
9.43k
                                }
2649
26.6k
                            }
2650
2651
9.34k
                        }
2652
20.2k
                        frwd_advance(&sigprop, cnt);
2653
20.2k
                        cnt = 0;
2654
2655
                        //update next columns
2656
20.2k
                        if (n == 4) {
2657
                            //horizontally
2658
10.1k
                            OPJ_UINT32 t = new_sig >> 28;
2659
10.1k
                            t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
2660
10.1k
                            cur_mbr[1] |= t & ~cur_sig[1];
2661
10.1k
                        }
2662
20.2k
                    }
2663
10.1k
                }
2664
                //propagate down (vertically propagation)
2665
20.6k
                new_sig |= cur_sig[0];
2666
20.6k
                ux = (new_sig & 0x88888888) >> 3;
2667
20.6k
                tx = ux | (ux << 4) | (ux >> 4);
2668
20.6k
                if (i > 0) {
2669
15.3k
                    nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
2670
15.3k
                }
2671
20.6k
                nxt_mbr[0] |= tx & ~nxt_sig[0];
2672
20.6k
                nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
2673
20.6k
            }
2674
5.29k
        }
2675
4.71k
    }
2676
2677
6.38k
    {
2678
6.38k
        OPJ_INT32 x, y;
2679
125k
        for (y = 0; y < height; ++y) {
2680
118k
            OPJ_INT32* sp = (OPJ_INT32*)decoded_data + y * stride;
2681
3.10M
            for (x = 0; x < width; ++x, ++sp) {
2682
2.98M
                OPJ_INT32 val = (*sp & 0x7FFFFFFF);
2683
2.98M
                *sp = ((OPJ_UINT32) * sp & 0x80000000) ? -val : val;
2684
2.98M
            }
2685
118k
        }
2686
6.38k
    }
2687
2688
6.38k
    return OPJ_TRUE;
2689
6.38k
}