1 //***************************************************************************/
2 // This software is released under the 2-Clause BSD license, included
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
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
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.
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.
35 // Date: 01 September 2021
36 //***************************************************************************/
38 //***************************************************************************/
40 * @brief implements HTJ2K block decoder
45 #include "opj_includes.h"
47 #include "t1_ht_luts.h"
49 /////////////////////////////////////////////////////////////////////////////
51 /////////////////////////////////////////////////////////////////////////////
53 #define OPJ_COMPILER_MSVC
54 #elif (defined __GNUC__)
55 #define OPJ_COMPILER_GNUC
58 //************************************************************************/
59 /** @brief Displays the error message for disabling the decoding of SPP and
62 static OPJ_BOOL only_cleanup_pass_is_decoded = OPJ_FALSE;
64 //************************************************************************/
65 /** @brief Generates population count (i.e., the number of set bits)
67 * @param [in] val is the value for which population count is sought
70 OPJ_UINT32 population_count(OPJ_UINT32 val)
72 #if defined(OPJ_COMPILER_MSVC) && (defined(_M_IX86) || defined(_M_AMD64))
73 return (OPJ_UINT32)__popcnt(val);
74 #elif (defined OPJ_COMPILER_GNUC)
75 return (OPJ_UINT32)__builtin_popcount(val);
77 val -= ((val >> 1) & 0x55555555);
78 val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
79 val = (((val >> 4) + val) & 0x0f0f0f0f);
82 return (OPJ_UINT32)(val & 0x0000003f);
86 //************************************************************************/
87 /** @brief Counts the number of leading zeros
89 * @param [in] val is the value for which leading zero count is sought
91 #ifdef OPJ_COMPILER_MSVC
92 #pragma intrinsic(_BitScanReverse)
95 OPJ_UINT32 count_leading_zeros(OPJ_UINT32 val)
97 #ifdef OPJ_COMPILER_MSVC
98 unsigned long result = 0;
99 _BitScanReverse(&result, val);
100 return 31U ^ (OPJ_UINT32)result;
101 #elif (defined OPJ_COMPILER_GNUC)
102 return (OPJ_UINT32)__builtin_clz(val);
109 return 32U - population_count(val);
113 //************************************************************************/
114 /** @brief Read a little-endian serialized UINT32.
116 * @param [in] dataIn pointer to byte stream to read from
118 static INLINE OPJ_UINT32 read_le_uint32(const void* dataIn)
120 #if defined(OPJ_BIG_ENDIAN)
121 const OPJ_UINT8* data = (const OPJ_UINT8*)dataIn;
122 return ((OPJ_UINT32)data[0]) | (OPJ_UINT32)(data[1] << 8) | (OPJ_UINT32)(
124 OPJ_UINT32)data[3]) <<
127 return *(OPJ_UINT32*)dataIn;
131 //************************************************************************/
132 /** @brief MEL state structure for reading and decoding the MEL bitstream
134 * A number of events is decoded from the MEL bitstream ahead of time
135 * and stored in run/num_runs.
136 * Each run represents the number of zero events before a one event.
138 typedef struct dec_mel {
139 // data decoding machinery
140 OPJ_UINT8* data; //!<the address of data (or bitstream)
141 OPJ_UINT64 tmp; //!<temporary buffer for read data
142 int bits; //!<number of bits stored in tmp
143 int size; //!<number of bytes in MEL code
144 OPJ_BOOL unstuff; //!<true if the next bit needs to be unstuffed
145 int k; //!<state of MEL decoder
147 // queue of decoded runs
148 int num_runs; //!<number of decoded runs left in runs (maximum 8)
149 OPJ_UINT64 runs; //!<runs of decoded MEL codewords (7 bits/run)
152 //************************************************************************/
153 /** @brief Reads and unstuffs the MEL bitstream
155 * This design needs more bytes in the codeblock buffer than the length
156 * of the cleanup pass by up to 2 bytes.
158 * Unstuffing removes the MSB of the byte following a byte whose
159 * value is 0xFF; this prevents sequences larger than 0xFF7F in value
160 * from appearing the bitstream.
162 * @param [in] melp is a pointer to dec_mel_t structure
165 void mel_read(dec_mel_t *melp)
172 if (melp->bits > 32) { //there are enough bits in the tmp variable
173 return; // return without reading new data
176 val = 0xFFFFFFFF; // feed in 0xFF if buffer is exhausted
177 if (melp->size > 4) { // if there is more than 4 bytes the MEL segment
178 val = read_le_uint32(melp->data); // read 32 bits from MEL data
179 melp->data += 4; // advance pointer
180 melp->size -= 4; // reduce counter
181 } else if (melp->size > 0) { // 4 or less
184 while (melp->size > 1) {
185 OPJ_UINT32 v = *melp->data++; // read one byte at a time
186 OPJ_UINT32 m = ~(0xFFu << i); // mask of location
187 val = (val & m) | (v << i); // put byte in its correct location
192 v = *melp->data++; // the one before the last is different
193 v |= 0xF; // MEL and VLC segments can overlap
195 val = (val & m) | (v << i);
199 // next we unstuff them before adding them to the buffer
200 bits = 32 - melp->unstuff; // number of bits in val, subtract 1 if
201 // the previously read byte requires
204 // data is unstuffed and accumulated in t
205 // bits has the number of bits in t
207 unstuff = ((val & 0xFF) == 0xFF); // true if the byte needs unstuffing
208 bits -= unstuff; // there is one less bit in t if unstuffing is needed
209 t = t << (8 - unstuff); // move up to make room for the next byte
211 //this is a repeat of the above
212 t |= (val >> 8) & 0xFF;
213 unstuff = (((val >> 8) & 0xFF) == 0xFF);
215 t = t << (8 - unstuff);
217 t |= (val >> 16) & 0xFF;
218 unstuff = (((val >> 16) & 0xFF) == 0xFF);
220 t = t << (8 - unstuff);
222 t |= (val >> 24) & 0xFF;
223 melp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
225 // move t to tmp, and push the result all the way up, so we read from
227 melp->tmp |= ((OPJ_UINT64)t) << (64 - bits - melp->bits);
228 melp->bits += bits; //increment the number of bits in tmp
231 //************************************************************************/
232 /** @brief Decodes unstuffed MEL segment bits stored in tmp to runs
234 * Runs are stored in "runs" and the number of runs in "num_runs".
235 * Each run represents a number of zero events that may or may not
236 * terminate in a 1 event.
237 * Each run is stored in 7 bits. The LSB is 1 if the run terminates in
238 * a 1 event, 0 otherwise. The next 6 bits, for the case terminating
239 * with 1, contain the number of consecutive 0 zero events * 2; for the
240 * case terminating with 0, they store (number of consecutive 0 zero
242 * A total of 6 bits (made up of 1 + 5) should have been enough.
244 * @param [in] melp is a pointer to dec_mel_t structure
247 void mel_decode(dec_mel_t *melp)
249 static const int mel_exp[13] = { //MEL exponents
250 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5
253 if (melp->bits < 6) { // if there are less than 6 bits in tmp
254 mel_read(melp); // then read from the MEL bitstream
256 // 6 bits is the largest decodable MEL cwd
258 //repeat so long that there is enough decodable bits in tmp,
259 // and the runs store is not full (num_runs < 8)
260 while (melp->bits >= 6 && melp->num_runs < 8) {
261 int eval = mel_exp[melp->k]; // number of bits associated with state
263 if (melp->tmp & (1ull << 63)) { //The next bit to decode (stored in MSB)
266 run--; // consecutive runs of 0 events - 1
267 melp->k = melp->k + 1 < 12 ? melp->k + 1 : 12;//increment, max is 12
268 melp->tmp <<= 1; // consume one bit from tmp
270 run = run << 1; // a stretch of zeros not terminating in one
273 run = (int)(melp->tmp >> (63 - eval)) & ((1 << eval) - 1);
274 melp->k = melp->k - 1 > 0 ? melp->k - 1 : 0; //decrement, min is 0
275 melp->tmp <<= eval + 1; //consume eval + 1 bits (max is 6)
276 melp->bits -= eval + 1;
277 run = (run << 1) + 1; // a stretch of zeros terminating with one
279 eval = melp->num_runs * 7; // 7 bits per run
280 melp->runs &= ~((OPJ_UINT64)0x3F << eval); // 6 bits are sufficient
281 melp->runs |= ((OPJ_UINT64)run) << eval; // store the value in runs
282 melp->num_runs++; // increment count
286 //************************************************************************/
287 /** @brief Initiates a dec_mel_t structure for MEL decoding and reads
288 * some bytes in order to get the read address to a multiple
291 * @param [in] melp is a pointer to dec_mel_t structure
292 * @param [in] bbuf is a pointer to byte buffer
293 * @param [in] lcup is the length of MagSgn+MEL+VLC segments
294 * @param [in] scup is the length of MEL+VLC segments
297 OPJ_BOOL mel_init(dec_mel_t *melp, OPJ_UINT8* bbuf, int lcup, int scup)
302 melp->data = bbuf + lcup - scup; // move the pointer to the start of MEL
303 melp->bits = 0; // 0 bits in tmp
305 melp->unstuff = OPJ_FALSE; // no unstuffing
306 melp->size = scup - 1; // size is the length of MEL+VLC-1
307 melp->k = 0; // 0 for state
308 melp->num_runs = 0; // num_runs is 0
311 //This code is borrowed; original is for a different architecture
312 //These few lines take care of the case where data is not at a multiple
313 // of 4 boundary. It reads 1,2,3 up to 4 bytes from the MEL segment
314 num = 4 - (int)((intptr_t)(melp->data) & 0x3);
315 for (i = 0; i < num; ++i) { // this code is similar to mel_read
319 if (melp->unstuff == OPJ_TRUE && melp->data[0] > 0x8F) {
322 d = (melp->size > 0) ? *melp->data : 0xFF; // if buffer is consumed
324 if (melp->size == 1) {
325 d |= 0xF; //if this is MEL+VLC-1, set LSBs to 0xF
328 melp->data += melp->size-- > 0; //increment if the end is not reached
329 d_bits = 8 - melp->unstuff; //if unstuffing is needed, reduce by 1
330 melp->tmp = (melp->tmp << d_bits) | d; //store bits in tmp
331 melp->bits += d_bits; //increment tmp by number of bits
332 melp->unstuff = ((d & 0xFF) == 0xFF); //true of next byte needs
335 melp->tmp <<= (64 - melp->bits); //push all the way up so the first bit
340 //************************************************************************/
341 /** @brief Retrieves one run from dec_mel_t; if there are no runs stored
342 * MEL segment is decoded
344 * @param [in] melp is a pointer to dec_mel_t structure
347 int mel_get_run(dec_mel_t *melp)
350 if (melp->num_runs == 0) { //if no runs, decode more bit from MEL segment
354 t = melp->runs & 0x7F; //retrieve one run
355 melp->runs >>= 7; // remove the retrieved run
357 return t; // return run
360 //************************************************************************/
361 /** @brief A structure for reading and unstuffing a segment that grows
362 * backward, such as VLC and MRP
364 typedef struct rev_struct {
366 OPJ_UINT8* data; //!<pointer to where to read data
367 OPJ_UINT64 tmp; //!<temporary buffer of read data
368 OPJ_UINT32 bits; //!<number of bits stored in tmp
369 int size; //!<number of bytes left
370 OPJ_BOOL unstuff; //!<true if the last byte is more than 0x8F
371 //!<then the current byte is unstuffed if it is 0x7F
374 //************************************************************************/
375 /** @brief Read and unstuff data from a backwardly-growing segment
377 * This reader can read up to 8 bytes from before the VLC segment.
378 * Care must be taken not read from unreadable memory, causing a
379 * segmentation fault.
381 * Note that there is another subroutine rev_read_mrp that is slightly
382 * different. The other one fills zeros when the buffer is exhausted.
383 * This one basically does not care if the bytes are consumed, because
384 * any extra data should not be used in the actual decoding.
386 * Unstuffing is needed to prevent sequences more than 0xFF8F from
387 * appearing in the bits stream; since we are reading backward, we keep
388 * watch when a value larger than 0x8F appears in the bitstream.
389 * If the byte following this is 0x7F, we unstuff this byte (ignore the
390 * MSB of that byte, which should be 0).
392 * @param [in] vlcp is a pointer to rev_struct_t structure
395 void rev_read(rev_struct_t *vlcp)
402 //process 4 bytes at a time
403 if (vlcp->bits > 32) { // if there are more than 32 bits in tmp, then
404 return; // reading 32 bits can overflow vlcp->tmp
407 //the next line (the if statement) needs to be tested first
408 if (vlcp->size > 3) { // if there are more than 3 bytes left in VLC
409 // (vlcp->data - 3) move pointer back to read 32 bits at once
410 val = read_le_uint32(vlcp->data - 3); // then read 32 bits
411 vlcp->data -= 4; // move data pointer back by 4
412 vlcp->size -= 4; // reduce available byte by 4
413 } else if (vlcp->size > 0) { // 4 or less
415 while (vlcp->size > 0) {
416 OPJ_UINT32 v = *vlcp->data--; // read one byte at a time
417 val |= (v << i); // put byte in its correct location
423 //accumulate in tmp, number of bits in tmp are stored in bits
424 tmp = val >> 24; //start with the MSB byte
426 // test unstuff (previous byte is >0x8F), and this byte is 0x7F
427 bits = 8u - ((vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
428 unstuff = (val >> 24) > 0x8F; //this is for the next byte
430 tmp |= ((val >> 16) & 0xFF) << bits; //process the next byte
431 bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
432 unstuff = ((val >> 16) & 0xFF) > 0x8F;
434 tmp |= ((val >> 8) & 0xFF) << bits;
435 bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
436 unstuff = ((val >> 8) & 0xFF) > 0x8F;
438 tmp |= (val & 0xFF) << bits;
439 bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
440 unstuff = (val & 0xFF) > 0x8F;
442 // now move the read and unstuffed bits into vlcp->tmp
443 vlcp->tmp |= (OPJ_UINT64)tmp << vlcp->bits;
445 vlcp->unstuff = unstuff; // this for the next read
448 //************************************************************************/
449 /** @brief Initiates the rev_struct_t structure and reads a few bytes to
450 * move the read address to multiple of 4
452 * There is another similar rev_init_mrp subroutine. The difference is
453 * that this one, rev_init, discards the first 12 bits (they have the
454 * sum of the lengths of VLC and MEL segments), and first unstuff depends
457 * @param [in] vlcp is a pointer to rev_struct_t structure
458 * @param [in] data is a pointer to byte at the start of the cleanup pass
459 * @param [in] lcup is the length of MagSgn+MEL+VLC segments
460 * @param [in] scup is the length of MEL+VLC segments
463 void rev_init(rev_struct_t *vlcp, OPJ_UINT8* data, int lcup, int scup)
468 //first byte has only the upper 4 bits
469 vlcp->data = data + lcup - 2;
471 //size can not be larger than this, in fact it should be smaller
472 vlcp->size = scup - 2;
474 d = *vlcp->data--; // read one byte (this is a half byte)
475 vlcp->tmp = d >> 4; // both initialize and set
476 vlcp->bits = 4 - ((vlcp->tmp & 7) == 7); //check standard
477 vlcp->unstuff = (d | 0xF) > 0x8F; //this is useful for the next byte
479 //This code is designed for an architecture that read address should
480 // align to the read size (address multiple of 4 if read size is 4)
481 //These few lines take care of the case where data is not at a multiple
482 // of 4 boundary. It reads 1,2,3 up to 4 bytes from the VLC bitstream.
483 // To read 32 bits, read from (vlcp->data - 3)
484 num = 1 + (int)((intptr_t)(vlcp->data) & 0x3);
485 tnum = num < vlcp->size ? num : vlcp->size;
486 for (i = 0; i < tnum; ++i) {
489 d = *vlcp->data--; // read one byte and move read pointer
490 //check if the last byte was >0x8F (unstuff == true) and this is 0x7F
491 d_bits = 8u - ((vlcp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
492 vlcp->tmp |= d << vlcp->bits; // move data to vlcp->tmp
493 vlcp->bits += d_bits;
494 vlcp->unstuff = d > 0x8F; // for next byte
497 rev_read(vlcp); // read another 32 buts
500 //************************************************************************/
501 /** @brief Retrieves 32 bits from the head of a rev_struct structure
503 * By the end of this call, vlcp->tmp must have no less than 33 bits
505 * @param [in] vlcp is a pointer to rev_struct structure
508 OPJ_UINT32 rev_fetch(rev_struct_t *vlcp)
510 if (vlcp->bits < 32) { // if there are less then 32 bits, read more
511 rev_read(vlcp); // read 32 bits, but unstuffing might reduce this
512 if (vlcp->bits < 32) { // if there is still space in vlcp->tmp for 32 bits
513 rev_read(vlcp); // read another 32
516 return (OPJ_UINT32)vlcp->tmp; // return the head (bottom-most) of vlcp->tmp
519 //************************************************************************/
520 /** @brief Consumes num_bits from a rev_struct structure
522 * @param [in] vlcp is a pointer to rev_struct structure
523 * @param [in] num_bits is the number of bits to be removed
526 OPJ_UINT32 rev_advance(rev_struct_t *vlcp, OPJ_UINT32 num_bits)
528 assert(num_bits <= vlcp->bits); // vlcp->tmp must have more than num_bits
529 vlcp->tmp >>= num_bits; // remove bits
530 vlcp->bits -= num_bits; // decrement the number of bits
531 return (OPJ_UINT32)vlcp->tmp;
534 //************************************************************************/
535 /** @brief Reads and unstuffs from rev_struct
537 * This is different than rev_read in that this fills in zeros when the
538 * the available data is consumed. The other does not care about the
539 * values when all data is consumed.
541 * See rev_read for more information about unstuffing
543 * @param [in] mrp is a pointer to rev_struct structure
546 void rev_read_mrp(rev_struct_t *mrp)
553 //process 4 bytes at a time
554 if (mrp->bits > 32) {
558 if (mrp->size > 3) { // If there are 3 byte or more
559 // (mrp->data - 3) move pointer back to read 32 bits at once
560 val = read_le_uint32(mrp->data - 3); // read 32 bits
561 mrp->data -= 4; // move back pointer
562 mrp->size -= 4; // reduce count
563 } else if (mrp->size > 0) {
565 while (mrp->size > 0) {
566 OPJ_UINT32 v = *mrp->data--; // read one byte at a time
567 val |= (v << i); // put byte in its correct location
574 //accumulate in tmp, and keep count in bits
577 //test if the last byte > 0x8F (unstuff must be true) and this is 0x7F
578 bits = 8u - ((mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
579 unstuff = (val >> 24) > 0x8F;
581 //process the next byte
582 tmp |= ((val >> 16) & 0xFF) << bits;
583 bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
584 unstuff = ((val >> 16) & 0xFF) > 0x8F;
586 tmp |= ((val >> 8) & 0xFF) << bits;
587 bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
588 unstuff = ((val >> 8) & 0xFF) > 0x8F;
590 tmp |= (val & 0xFF) << bits;
591 bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
592 unstuff = (val & 0xFF) > 0x8F;
594 mrp->tmp |= (OPJ_UINT64)tmp << mrp->bits; // move data to mrp pointer
596 mrp->unstuff = unstuff; // next byte
599 //************************************************************************/
600 /** @brief Initialized rev_struct structure for MRP segment, and reads
601 * a number of bytes such that the next 32 bits read are from
602 * an address that is a multiple of 4. Note this is designed for
603 * an architecture that read size must be compatible with the
604 * alignment of the read address
606 * There is another similar subroutine rev_init. This subroutine does
607 * NOT skip the first 12 bits, and starts with unstuff set to true.
609 * @param [in] mrp is a pointer to rev_struct structure
610 * @param [in] data is a pointer to byte at the start of the cleanup pass
611 * @param [in] lcup is the length of MagSgn+MEL+VLC segments
612 * @param [in] len2 is the length of SPP+MRP segments
615 void rev_init_mrp(rev_struct_t *mrp, OPJ_UINT8* data, int lcup, int len2)
619 mrp->data = data + lcup + len2 - 1;
621 mrp->unstuff = OPJ_TRUE;
625 //This code is designed for an architecture that read address should
626 // align to the read size (address multiple of 4 if read size is 4)
627 //These few lines take care of the case where data is not at a multiple
628 // of 4 boundary. It reads 1,2,3 up to 4 bytes from the MRP stream
629 num = 1 + (int)((intptr_t)(mrp->data) & 0x3);
630 for (i = 0; i < num; ++i) {
634 //read a byte, 0 if no more data
635 d = (mrp->size-- > 0) ? *mrp->data-- : 0;
636 //check if unstuffing is needed
637 d_bits = 8u - ((mrp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
638 mrp->tmp |= d << mrp->bits; // move data to vlcp->tmp
640 mrp->unstuff = d > 0x8F; // for next byte
645 //************************************************************************/
646 /** @brief Retrieves 32 bits from the head of a rev_struct structure
648 * By the end of this call, mrp->tmp must have no less than 33 bits
650 * @param [in] mrp is a pointer to rev_struct structure
653 OPJ_UINT32 rev_fetch_mrp(rev_struct_t *mrp)
655 if (mrp->bits < 32) { // if there are less than 32 bits in mrp->tmp
656 rev_read_mrp(mrp); // read 30-32 bits from mrp
657 if (mrp->bits < 32) { // if there is a space of 32 bits
658 rev_read_mrp(mrp); // read more
661 return (OPJ_UINT32)mrp->tmp; // return the head of mrp->tmp
664 //************************************************************************/
665 /** @brief Consumes num_bits from a rev_struct structure
667 * @param [in] mrp is a pointer to rev_struct structure
668 * @param [in] num_bits is the number of bits to be removed
671 OPJ_UINT32 rev_advance_mrp(rev_struct_t *mrp, OPJ_UINT32 num_bits)
673 assert(num_bits <= mrp->bits); // we must not consume more than mrp->bits
674 mrp->tmp >>= num_bits; // discard the lowest num_bits bits
675 mrp->bits -= num_bits;
676 return (OPJ_UINT32)mrp->tmp; // return data after consumption
679 //************************************************************************/
680 /** @brief Decode initial UVLC to get the u value (or u_q)
682 * @param [in] vlc is the head of the VLC bitstream
683 * @param [in] mode is 0, 1, 2, 3, or 4. Values in 0 to 3 are composed of
684 * u_off of 1st quad and 2nd quad of a quad pair. The value
685 * 4 occurs when both bits are 1, and the event decoded
686 * from MEL bitstream is also 1.
687 * @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
688 * this value is a partial calculation of u + kappa.
691 OPJ_UINT32 decode_init_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
693 //table stores possible decoding three bits from vlc
694 // there are 8 entries for xx1, x10, 100, 000, where x means do not care
695 // table value is made up of
696 // 2 bits in the LSB for prefix length
697 // 3 bits for suffix length
698 // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
699 static const OPJ_UINT8 dec[8] = { // the index is the prefix codeword
700 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
701 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
702 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
703 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
704 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
705 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
706 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
707 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
710 OPJ_UINT32 consumed_bits = 0;
711 if (mode == 0) { // both u_off are 0
712 u[0] = u[1] = 1; //Kappa is 1 for initial line
713 } else if (mode <= 2) { // u_off are either 01 or 10
715 OPJ_UINT32 suffix_len;
717 d = dec[vlc & 0x7]; //look at the least significant 3 bits
718 vlc >>= d & 0x3; //prefix length
719 consumed_bits += d & 0x3;
721 suffix_len = ((d >> 2) & 0x7);
722 consumed_bits += suffix_len;
724 d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
725 u[0] = (mode == 1) ? d + 1 : 1; // kappa is 1 for initial line
726 u[1] = (mode == 1) ? 1 : d + 1; // kappa is 1 for initial line
727 } else if (mode == 3) { // both u_off are 1, and MEL event is 0
728 OPJ_UINT32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
729 vlc >>= d1 & 0x3; // Consume bits
730 consumed_bits += d1 & 0x3;
732 if ((d1 & 0x3) > 2) {
733 OPJ_UINT32 suffix_len;
736 u[1] = (vlc & 1) + 1 + 1; //Kappa is 1 for initial line
740 suffix_len = ((d1 >> 2) & 0x7);
741 consumed_bits += suffix_len;
742 d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
743 u[0] = d1 + 1; //Kappa is 1 for initial line
746 OPJ_UINT32 suffix_len;
748 d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
749 vlc >>= d2 & 0x3; // Consume bits
750 consumed_bits += d2 & 0x3;
752 suffix_len = ((d1 >> 2) & 0x7);
753 consumed_bits += suffix_len;
755 d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
756 u[0] = d1 + 1; //Kappa is 1 for initial line
759 suffix_len = ((d2 >> 2) & 0x7);
760 consumed_bits += suffix_len;
762 d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
763 u[1] = d2 + 1; //Kappa is 1 for initial line
765 } else if (mode == 4) { // both u_off are 1, and MEL event is 1
768 OPJ_UINT32 suffix_len;
770 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
771 vlc >>= d1 & 0x3; // Consume bits
772 consumed_bits += d1 & 0x3;
774 d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
775 vlc >>= d2 & 0x3; // Consume bits
776 consumed_bits += d2 & 0x3;
778 suffix_len = ((d1 >> 2) & 0x7);
779 consumed_bits += suffix_len;
781 d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
782 u[0] = d1 + 3; // add 2+kappa
785 suffix_len = ((d2 >> 2) & 0x7);
786 consumed_bits += suffix_len;
788 d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
789 u[1] = d2 + 3; // add 2+kappa
791 return consumed_bits;
794 //************************************************************************/
795 /** @brief Decode non-initial UVLC to get the u value (or u_q)
797 * @param [in] vlc is the head of the VLC bitstream
798 * @param [in] mode is 0, 1, 2, or 3. The 1st bit is u_off of 1st quad
799 * and 2nd for 2nd quad of a quad pair
800 * @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
801 * this value is a partial calculation of u + kappa.
804 OPJ_UINT32 decode_noninit_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
806 //table stores possible decoding three bits from vlc
807 // there are 8 entries for xx1, x10, 100, 000, where x means do not care
808 // table value is made up of
809 // 2 bits in the LSB for prefix length
810 // 3 bits for suffix length
811 // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
812 static const OPJ_UINT8 dec[8] = {
813 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
814 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
815 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
816 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
817 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
818 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
819 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
820 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
823 OPJ_UINT32 consumed_bits = 0;
825 u[0] = u[1] = 1; //for kappa
826 } else if (mode <= 2) { //u_off are either 01 or 10
828 OPJ_UINT32 suffix_len;
830 d = dec[vlc & 0x7]; //look at the least significant 3 bits
831 vlc >>= d & 0x3; //prefix length
832 consumed_bits += d & 0x3;
834 suffix_len = ((d >> 2) & 0x7);
835 consumed_bits += suffix_len;
837 d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
838 u[0] = (mode == 1) ? d + 1 : 1; //for kappa
839 u[1] = (mode == 1) ? 1 : d + 1; //for kappa
840 } else if (mode == 3) { // both u_off are 1
843 OPJ_UINT32 suffix_len;
845 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
846 vlc >>= d1 & 0x3; // Consume bits
847 consumed_bits += d1 & 0x3;
849 d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
850 vlc >>= d2 & 0x3; // Consume bits
851 consumed_bits += d2 & 0x3;
853 suffix_len = ((d1 >> 2) & 0x7);
854 consumed_bits += suffix_len;
856 d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
857 u[0] = d1 + 1; //1 for kappa
860 suffix_len = ((d2 >> 2) & 0x7);
861 consumed_bits += suffix_len;
863 d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
864 u[1] = d2 + 1; //1 for kappa
866 return consumed_bits;
869 //************************************************************************/
870 /** @brief State structure for reading and unstuffing of forward-growing
871 * bitstreams; these are: MagSgn and SPP bitstreams
873 typedef struct frwd_struct {
874 const OPJ_UINT8* data; //!<pointer to bitstream
875 OPJ_UINT64 tmp; //!<temporary buffer of read data
876 OPJ_UINT32 bits; //!<number of bits stored in tmp
877 OPJ_BOOL unstuff; //!<true if a bit needs to be unstuffed from next byte
878 int size; //!<size of data
879 OPJ_UINT32 X; //!<0 or 0xFF, X's are inserted at end of bitstream
882 //************************************************************************/
883 /** @brief Read and unstuffs 32 bits from forward-growing bitstream
885 * A subroutine to read from both the MagSgn or SPP bitstreams;
886 * in particular, when MagSgn bitstream is consumed, 0xFF's are fed,
887 * while when SPP is exhausted 0's are fed in.
888 * X controls this value.
890 * Unstuffing prevent sequences that are more than 0xFF7F from appearing
891 * in the conpressed sequence. So whenever a value of 0xFF is coded, the
892 * MSB of the next byte is set 0 and must be ignored during decoding.
894 * Reading can go beyond the end of buffer by up to 3 bytes.
896 * @param [in] msp is a pointer to frwd_struct_t structure
900 void frwd_read(frwd_struct_t *msp)
907 assert(msp->bits <= 32); // assert that there is a space for 32 bits
911 val = read_le_uint32(msp->data); // read 32 bits
912 msp->data += 4; // increment pointer
913 msp->size -= 4; // reduce size
914 } else if (msp->size > 0) {
916 val = msp->X != 0 ? 0xFFFFFFFFu : 0;
917 while (msp->size > 0) {
918 OPJ_UINT32 v = *msp->data++; // read one byte at a time
919 OPJ_UINT32 m = ~(0xFFu << i); // mask of location
920 val = (val & m) | (v << i); // put one byte in its correct location
925 val = msp->X != 0 ? 0xFFFFFFFFu : 0;
928 // we accumulate in t and keep a count of the number of bits in bits
929 bits = 8u - (msp->unstuff ? 1u : 0u);
931 unstuff = ((val & 0xFF) == 0xFF); // Do we need unstuffing next?
933 t |= ((val >> 8) & 0xFF) << bits;
934 bits += 8u - (unstuff ? 1u : 0u);
935 unstuff = (((val >> 8) & 0xFF) == 0xFF);
937 t |= ((val >> 16) & 0xFF) << bits;
938 bits += 8u - (unstuff ? 1u : 0u);
939 unstuff = (((val >> 16) & 0xFF) == 0xFF);
941 t |= ((val >> 24) & 0xFF) << bits;
942 bits += 8u - (unstuff ? 1u : 0u);
943 msp->unstuff = (((val >> 24) & 0xFF) == 0xFF); // for next byte
945 msp->tmp |= ((OPJ_UINT64)t) << msp->bits; // move data to msp->tmp
949 //************************************************************************/
950 /** @brief Initialize frwd_struct_t struct and reads some bytes
952 * @param [in] msp is a pointer to frwd_struct_t
953 * @param [in] data is a pointer to the start of data
954 * @param [in] size is the number of byte in the bitstream
955 * @param [in] X is the value fed in when the bitstream is exhausted.
959 void frwd_init(frwd_struct_t *msp, const OPJ_UINT8* data, int size,
967 msp->unstuff = OPJ_FALSE;
970 assert(msp->X == 0 || msp->X == 0xFF);
972 //This code is designed for an architecture that read address should
973 // align to the read size (address multiple of 4 if read size is 4)
974 //These few lines take care of the case where data is not at a multiple
975 // of 4 boundary. It reads 1,2,3 up to 4 bytes from the bitstream
976 num = 4 - (int)((intptr_t)(msp->data) & 0x3);
977 for (i = 0; i < num; ++i) {
979 //read a byte if the buffer is not exhausted, otherwise set it to X
980 d = msp->size-- > 0 ? *msp->data++ : msp->X;
981 msp->tmp |= (d << msp->bits); // store data in msp->tmp
982 msp->bits += 8u - (msp->unstuff ? 1u : 0u); // number of bits added to msp->tmp
983 msp->unstuff = ((d & 0xFF) == 0xFF); // unstuffing for next byte
985 frwd_read(msp); // read 32 bits more
988 //************************************************************************/
989 /** @brief Consume num_bits bits from the bitstream of frwd_struct_t
991 * @param [in] msp is a pointer to frwd_struct_t
992 * @param [in] num_bits is the number of bit to consume
995 void frwd_advance(frwd_struct_t *msp, OPJ_UINT32 num_bits)
997 assert(num_bits <= msp->bits);
998 msp->tmp >>= num_bits; // consume num_bits
999 msp->bits -= num_bits;
1002 //************************************************************************/
1003 /** @brief Fetches 32 bits from the frwd_struct_t bitstream
1005 * @param [in] msp is a pointer to frwd_struct_t
1008 OPJ_UINT32 frwd_fetch(frwd_struct_t *msp)
1010 if (msp->bits < 32) {
1012 if (msp->bits < 32) { //need to test
1016 return (OPJ_UINT32)msp->tmp;
1019 //************************************************************************/
1020 /** @brief Allocates T1 buffers
1022 * @param [in, out] t1 is codeblock cofficients storage
1023 * @param [in] w is codeblock width
1024 * @param [in] h is codeblock height
1026 static OPJ_BOOL opj_t1_allocate_buffers(
1031 OPJ_UINT32 flagssize;
1033 /* No risk of overflow. Prior checks ensure those assert are met */
1034 /* They are per the specification */
1037 assert(w * h <= 4096);
1039 /* encoder uses tile buffer, so no need to allocate */
1041 OPJ_UINT32 datasize = w * h;
1043 if (datasize > t1->datasize) {
1044 opj_aligned_free(t1->data);
1045 t1->data = (OPJ_INT32*)
1046 opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1048 /* FIXME event manager error callback */
1051 t1->datasize = datasize;
1053 /* memset first arg is declared to never be null by gcc */
1054 if (t1->data != NULL) {
1055 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1059 // We expand these buffers to multiples of 16 bytes.
1060 // We need 4 buffers of 129 integers each, expanded to 132 integers each
1061 // We also need 514 bytes of buffer, expanded to 528 bytes
1062 flagssize = 132U * sizeof(OPJ_UINT32) * 4U; // expanded to multiple of 16
1063 flagssize += 528U; // 514 expanded to multiples of 16
1066 if (flagssize > t1->flagssize) {
1068 opj_aligned_free(t1->flags);
1069 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1071 /* FIXME event manager error callback */
1075 t1->flagssize = flagssize;
1077 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1086 //************************************************************************/
1087 /** @brief Decodes one codeblock, processing the cleanup, siginificance
1088 * propagation, and magnitude refinement pass
1090 * @param [in, out] t1 is codeblock cofficients storage
1091 * @param [in] cblk is codeblock properties
1092 * @param [in] orient is the subband to which the codeblock belongs (not needed)
1093 * @param [in] roishift is region of interest shift
1094 * @param [in] cblksty is codeblock style
1095 * @param [in] p_manager is events print manager
1096 * @param [in] p_manager_mutex a mutex to control access to p_manager
1097 * @param [in] check_pterm: check termination (not used)
1099 OPJ_BOOL opj_t1_ht_decode_cblk(opj_t1_t *t1,
1100 opj_tcd_cblk_dec_t* cblk,
1102 OPJ_UINT32 roishift,
1104 opj_event_mgr_t *p_manager,
1105 opj_mutex_t* p_manager_mutex,
1106 OPJ_BOOL check_pterm)
1108 OPJ_BYTE* cblkdata = NULL;
1109 OPJ_UINT8* coded_data;
1110 OPJ_UINT32* decoded_data;
1111 OPJ_UINT32 zero_bplanes;
1112 OPJ_UINT32 num_passes;
1113 OPJ_UINT32 lengths1;
1114 OPJ_UINT32 lengths2;
1118 OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
1120 OPJ_UINT32 zero_bplanes_p1;
1124 frwd_struct_t magsgn;
1125 frwd_struct_t sigprop;
1126 rev_struct_t magref;
1127 OPJ_UINT8 *lsp, *line_state;
1129 OPJ_UINT32 vlc_val; // fetched data from VLC bitstream
1133 OPJ_INT32 x, y; // loop indices
1134 OPJ_BOOL stripe_causal = (cblksty & J2K_CCP_CBLKSTY_VSC) != 0;
1135 OPJ_UINT32 cblk_len = 0;
1137 (void)(orient); // stops unused parameter message
1138 (void)(check_pterm); // stops unused parameter message
1140 // We ignor orient, because the same decoder is used for all subbands
1141 // We also ignore check_pterm, because I am not sure how it applies
1142 if (roishift != 0) {
1143 if (p_manager_mutex) {
1144 opj_mutex_lock(p_manager_mutex);
1146 opj_event_msg(p_manager, EVT_ERROR, "We do not support ROI in decoding "
1148 if (p_manager_mutex) {
1149 opj_mutex_unlock(p_manager_mutex);
1154 if (!opj_t1_allocate_buffers(
1156 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1157 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1161 if (cblk->Mb == 0) {
1165 /* numbps = Mb + 1 - zero_bplanes, Mb = Kmax, zero_bplanes = missing_msbs */
1166 zero_bplanes = (cblk->Mb + 1) - cblk->numbps;
1168 /* Compute whole codeblock length from chunk lengths */
1172 for (i = 0; i < cblk->numchunks; i++) {
1173 cblk_len += cblk->chunks[i].len;
1177 if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
1180 /* Allocate temporary memory if needed */
1181 if (cblk_len > t1->cblkdatabuffersize) {
1182 cblkdata = (OPJ_BYTE*)opj_realloc(
1183 t1->cblkdatabuffer, cblk_len);
1184 if (cblkdata == NULL) {
1187 t1->cblkdatabuffer = cblkdata;
1188 t1->cblkdatabuffersize = cblk_len;
1191 /* Concatenate all chunks */
1192 cblkdata = t1->cblkdatabuffer;
1194 for (i = 0; i < cblk->numchunks; i++) {
1195 memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
1196 cblk_len += cblk->chunks[i].len;
1198 } else if (cblk->numchunks == 1) {
1199 cblkdata = cblk->chunks[0].data;
1201 /* Not sure if that can happen in practice, but avoid Coverity to */
1202 /* think we will dereference a null cblkdta pointer */
1206 // OPJ_BYTE* coded_data is a pointer to bitstream
1207 coded_data = cblkdata;
1208 // OPJ_UINT32* decoded_data is a pointer to decoded codeblock data buf.
1209 decoded_data = (OPJ_UINT32*)t1->data;
1210 // OPJ_UINT32 num_passes is the number of passes: 1 if CUP only, 2 for
1211 // CUP+SPP, and 3 for CUP+SPP+MRP
1212 num_passes = cblk->numsegs > 0 ? cblk->segs[0].real_num_passes : 0;
1213 num_passes += cblk->numsegs > 1 ? cblk->segs[1].real_num_passes : 0;
1214 // OPJ_UINT32 lengths1 is the length of cleanup pass
1215 lengths1 = num_passes > 0 ? cblk->segs[0].len : 0;
1216 // OPJ_UINT32 lengths2 is the length of refinement passes (either SPP only or SPP+MRP)
1217 lengths2 = num_passes > 1 ? cblk->segs[1].len : 0;
1218 // OPJ_INT32 width is the decoded codeblock width
1219 width = cblk->x1 - cblk->x0;
1220 // OPJ_INT32 height is the decoded codeblock height
1221 height = cblk->y1 - cblk->y0;
1222 // OPJ_INT32 stride is the decoded codeblock buffer stride
1225 /* sigma1 and sigma2 contains significant (i.e., non-zero) pixel
1226 * locations. The buffers are used interchangeably, because we need
1227 * more than 4 rows of significance information at a given time.
1228 * Each 32 bits contain significance information for 4 rows of 8
1229 * columns each. If we denote 32 bits by 0xaaaaaaaa, the each "a" is
1230 * called a nibble and has significance information for 4 rows.
1231 * The least significant nibble has information for the first column,
1232 * and so on. The nibble's LSB is for the first row, and so on.
1233 * Since, at most, we can have 1024 columns in a quad, we need 128
1234 * entries; we added 1 for convenience when propagation of signifcance
1235 * goes outside the structure
1236 * To work in OpenJPEG these buffers has been expanded to 132.
1238 // OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
1239 pflags = (OPJ_UINT32 *)t1->flags;
1241 sigma2 = sigma1 + 132;
1242 // mbr arrangement is similar to sigma; mbr contains locations
1243 // that become significant during significance propagation pass
1244 mbr1 = sigma2 + 132;
1246 //a pointer to sigma
1247 sip = sigma1; //pointers to arrays to be used interchangeably
1248 sip_shift = 0; //the amount of shift needed for sigma
1250 if (num_passes > 1 && lengths2 == 0) {
1251 if (p_manager_mutex) {
1252 opj_mutex_lock(p_manager_mutex);
1254 opj_event_msg(p_manager, EVT_WARNING, "A malformed codeblock that has "
1255 "more than one coding pass, but zero length for "
1256 "2nd and potentially the 3rd pass in an HT codeblock.\n");
1257 if (p_manager_mutex) {
1258 opj_mutex_unlock(p_manager_mutex);
1262 if (num_passes > 3) {
1263 if (p_manager_mutex) {
1264 opj_mutex_lock(p_manager_mutex);
1266 opj_event_msg(p_manager, EVT_ERROR, "We do not support more than 3 "
1267 "coding passes in an HT codeblock; This codeblocks has "
1268 "%d passes.\n", num_passes);
1269 if (p_manager_mutex) {
1270 opj_mutex_unlock(p_manager_mutex);
1275 if (cblk->Mb > 30) {
1276 /* This check is better moved to opj_t2_read_packet_header() in t2.c
1277 We do not have enough precision to decode any passes
1278 The design of openjpeg assumes that the bits of a 32-bit integer are
1279 assigned as follows:
1281 bits 30-1 are for magnitude
1282 bit 0 is for the center of the quantization bin
1283 Therefore we can only do values of cblk->Mb <= 30
1285 if (p_manager_mutex) {
1286 opj_mutex_lock(p_manager_mutex);
1288 opj_event_msg(p_manager, EVT_ERROR, "32 bits are not enough to "
1289 "decode this codeblock, since the number of "
1290 "bitplane, %d, is larger than 30.\n", cblk->Mb);
1291 if (p_manager_mutex) {
1292 opj_mutex_unlock(p_manager_mutex);
1296 if (zero_bplanes > cblk->Mb) {
1297 /* This check is better moved to opj_t2_read_packet_header() in t2.c,
1298 in the line "l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;"
1299 where i is the zero bitplanes, and should be no larger than cblk->Mb
1300 We cannot have more zero bitplanes than there are planes. */
1301 if (p_manager_mutex) {
1302 opj_mutex_lock(p_manager_mutex);
1304 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1305 "Decoding this codeblock is stopped. There are "
1306 "%d zero bitplanes in %d bitplanes.\n",
1307 zero_bplanes, cblk->Mb);
1309 if (p_manager_mutex) {
1310 opj_mutex_unlock(p_manager_mutex);
1313 } else if (zero_bplanes == cblk->Mb && num_passes > 1) {
1314 /* When the number of zero bitplanes is equal to the number of bitplanes,
1315 only the cleanup pass makes sense*/
1316 if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
1317 if (p_manager_mutex) {
1318 opj_mutex_lock(p_manager_mutex);
1320 /* We have a second check to prevent the possibility of an overrun condition,
1321 in the very unlikely event of a second thread discovering that
1322 only_cleanup_pass_is_decoded is false before the first thread changing
1324 if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
1325 only_cleanup_pass_is_decoded = OPJ_TRUE;
1326 opj_event_msg(p_manager, EVT_WARNING, "Malformed HT codeblock. "
1327 "When the number of zero planes bitplanes is "
1328 "equal to the number of bitplanes, only the cleanup "
1329 "pass makes sense, but we have %d passes in this "
1330 "codeblock. Therefore, only the cleanup pass will be "
1331 "decoded. This message will not be displayed again.\n",
1334 if (p_manager_mutex) {
1335 opj_mutex_unlock(p_manager_mutex);
1344 // OPJ_UINT32 zero planes plus 1
1345 zero_bplanes_p1 = zero_bplanes + 1;
1347 if (lengths1 < 2 || (OPJ_UINT32)lengths1 > cblk_len ||
1348 (OPJ_UINT32)(lengths1 + lengths2) > cblk_len) {
1349 if (p_manager_mutex) {
1350 opj_mutex_lock(p_manager_mutex);
1352 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1353 "Invalid codeblock length values.\n");
1355 if (p_manager_mutex) {
1356 opj_mutex_unlock(p_manager_mutex);
1360 // read scup and fix the bytes there
1361 lcup = (int)lengths1; // length of CUP
1362 //scup is the length of MEL + VLC
1363 scup = (((int)coded_data[lcup - 1]) << 4) + (coded_data[lcup - 2] & 0xF);
1364 if (scup < 2 || scup > lcup || scup > 4079) { //something is wrong
1365 /* The standard stipulates 2 <= Scup <= min(Lcup, 4079) */
1366 if (p_manager_mutex) {
1367 opj_mutex_lock(p_manager_mutex);
1369 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1370 "One of the following condition is not met: "
1371 "2 <= Scup <= min(Lcup, 4079)\n");
1373 if (p_manager_mutex) {
1374 opj_mutex_unlock(p_manager_mutex);
1380 if (mel_init(&mel, coded_data, lcup, scup) == OPJ_FALSE) {
1381 if (p_manager_mutex) {
1382 opj_mutex_lock(p_manager_mutex);
1384 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1385 "Incorrect MEL segment sequence.\n");
1386 if (p_manager_mutex) {
1387 opj_mutex_unlock(p_manager_mutex);
1391 rev_init(&vlc, coded_data, lcup, scup);
1392 frwd_init(&magsgn, coded_data, lcup - scup, 0xFF);
1393 if (num_passes > 1) { // needs to be tested
1394 frwd_init(&sigprop, coded_data + lengths1, (int)lengths2, 0);
1396 if (num_passes > 2) {
1397 rev_init_mrp(&magref, coded_data, (int)lengths1, (int)lengths2);
1401 * One byte per quad; for 1024 columns, or 512 quads, we need
1402 * 512 bytes. We are using 2 extra bytes one on the left and one on
1403 * the right for convenience.
1405 * The MSB bit in each byte is (\sigma^nw | \sigma^n), and the 7 LSBs
1406 * contain max(E^nw | E^n)
1409 // 514 is enough for a block width of 1024, +2 extra
1410 // here expanded to 528
1411 line_state = (OPJ_UINT8 *)(mbr2 + 132);
1415 lsp = line_state; // point to line state
1416 lsp[0] = 0; // for initial row of quad, we set to 0
1417 run = mel_get_run(&mel); // decode runs of events from MEL bitstrm
1418 // data represented as runs of 0 events
1419 // See mel_decode description
1420 qinf[0] = qinf[1] = 0; // quad info decoded from VLC bitstream
1421 c_q = 0; // context for quad q
1422 sp = decoded_data; // decoded codeblock samples
1423 // vlc_val; // fetched data from VLC bitstream
1425 for (x = 0; x < width; x += 4) { // one iteration per quad pair
1426 OPJ_UINT32 U_q[2]; // u values for the quad pair
1427 OPJ_UINT32 uvlc_mode;
1428 OPJ_UINT32 consumed_bits;
1429 OPJ_UINT32 m_n, v_n;
1437 // Get the head of the VLC bitstream. One fetch is enough for two
1438 // quads, since the largest VLC code is 7 bits, and maximum number of
1439 // bits used for u is 8. Therefore for two quads we need 30 bits
1440 // (if we include unstuffing, then 32 bits are enough, since we have
1441 // a maximum of one stuffing per two bytes)
1442 vlc_val = rev_fetch(&vlc);
1444 //decode VLC using the context c_q and the head of the VLC bitstream
1445 qinf[0] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F) ];
1447 if (c_q == 0) { // if zero context, we need to use one MEL event
1448 run -= 2; //the number of 0 events is multiplied by 2, so subtract 2
1450 // Is the run terminated in 1? if so, use decoded VLC code,
1451 // otherwise, discard decoded data, since we will decoded again
1452 // using a different context
1453 qinf[0] = (run == -1) ? qinf[0] : 0;
1455 // is run -1 or -2? this means a run has been consumed
1457 run = mel_get_run(&mel); // get another run
1461 // prepare context for the next quad; eqn. 1 in ITU T.814
1462 c_q = ((qinf[0] & 0x10) >> 4) | ((qinf[0] & 0xE0) >> 5);
1464 //remove data from vlc stream (0 bits are removed if qinf is not used)
1465 vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
1468 // The update depends on the value of x; consider one OPJ_UINT32
1469 // if x is 0, 8, 16 and so on, then this line update c locations
1470 // nibble (4 bits) number 0 1 2 3 4 5 6 7
1471 // LSB c c 0 0 0 0 0 0
1475 // if x is 4, 12, 20, then this line update locations c
1476 // nibble (4 bits) number 0 1 2 3 4 5 6 7
1477 // LSB 0 0 0 0 c c 0 0
1481 *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
1485 if (x + 2 < width) { // do not run if codeblock is narrower
1486 //decode VLC using the context c_q and the head of the VLC bitstream
1487 qinf[1] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F)];
1489 // if context is zero, use one MEL event
1490 if (c_q == 0) { //zero context
1491 run -= 2; //subtract 2, since events number if multiplied by 2
1493 // if event is 0, discard decoded qinf
1494 qinf[1] = (run == -1) ? qinf[1] : 0;
1496 if (run < 0) { // have we consumed all events in a run
1497 run = mel_get_run(&mel); // if yes, then get another run
1501 //prepare context for the next quad, eqn. 1 in ITU T.814
1502 c_q = ((qinf[1] & 0x10) >> 4) | ((qinf[1] & 0xE0) >> 5);
1504 //remove data from vlc stream, if qinf is not used, cwdlen is 0
1505 vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
1509 // The update depends on the value of x; consider one OPJ_UINT32
1510 // if x is 0, 8, 16 and so on, then this line update c locations
1511 // nibble (4 bits) number 0 1 2 3 4 5 6 7
1512 // LSB 0 0 c c 0 0 0 0
1516 // if x is 4, 12, 20, then this line update locations c
1517 // nibble (4 bits) number 0 1 2 3 4 5 6 7
1518 // LSB 0 0 0 0 0 0 c c
1522 *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
1524 sip += x & 0x7 ? 1 : 0; // move sigma pointer to next entry
1525 sip_shift ^= 0x10; // increment/decrement sip_shift by 16
1530 // uvlc_mode is made up of u_offset bits from the quad pair
1531 uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
1532 if (uvlc_mode == 3) { // if both u_offset are set, get an event from
1533 // the MEL run of events
1534 run -= 2; //subtract 2, since events number if multiplied by 2
1535 uvlc_mode += (run == -1) ? 1 : 0; //increment uvlc_mode if event is 1
1536 if (run < 0) { // if run is consumed (run is -1 or -2), get another run
1537 run = mel_get_run(&mel);
1540 //decode uvlc_mode to get u for both quads
1541 consumed_bits = decode_init_uvlc(vlc_val, uvlc_mode, U_q);
1542 if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
1543 if (p_manager_mutex) {
1544 opj_mutex_lock(p_manager_mutex);
1546 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. Decoding "
1547 "this codeblock is stopped. U_q is larger than zero "
1548 "bitplanes + 1 \n");
1549 if (p_manager_mutex) {
1550 opj_mutex_unlock(p_manager_mutex);
1555 //consume u bits in the VLC code
1556 vlc_val = rev_advance(&vlc, consumed_bits);
1558 //decode magsgn and update line_state
1559 /////////////////////////////////////
1561 //We obtain a mask for the samples locations that needs evaluation
1563 if (x + 4 > width) {
1564 locs >>= (x + 4 - width) << 1; // limits width
1566 locs = height > 1 ? locs : (locs & 0x55); // limits height
1568 if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
1569 if (p_manager_mutex) {
1570 opj_mutex_lock(p_manager_mutex);
1572 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1573 "VLC code produces significant samples outside "
1574 "the codeblock area.\n");
1575 if (p_manager_mutex) {
1576 opj_mutex_unlock(p_manager_mutex);
1581 //first quad, starting at first sample in quad and moving on
1582 if (qinf[0] & 0x10) { //is it significant? (sigma_n)
1585 ms_val = frwd_fetch(&magsgn); //get 32 bits of magsgn data
1586 m_n = U_q[0] - ((qinf[0] >> 12) & 1); //evaluate m_n (number of bits
1587 // to read from bitstream), using EMB e_k
1588 frwd_advance(&magsgn, m_n); //consume m_n
1589 val = ms_val << 31; //get sign bit
1590 v_n = ms_val & ((1U << m_n) - 1); //keep only m_n bits
1591 v_n |= ((qinf[0] & 0x100) >> 8) << m_n; //add EMB e_1 as MSB
1592 v_n |= 1; //add center of bin
1593 //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
1594 //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
1595 sp[0] = val | ((v_n + 2) << (p - 1));
1596 } else if (locs & 0x1) { // if this is inside the codeblock, set the
1597 sp[0] = 0; // sample to zero
1600 if (qinf[0] & 0x20) { //sigma_n
1603 ms_val = frwd_fetch(&magsgn); //get 32 bits
1604 m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n, uses EMB e_k
1605 frwd_advance(&magsgn, m_n); //consume m_n
1606 val = ms_val << 31; //get sign bit
1607 v_n = ms_val & ((1U << m_n) - 1); //keep only m_n bits
1608 v_n |= ((qinf[0] & 0x200) >> 9) << m_n; //add EMB e_1
1609 v_n |= 1; //bin center
1610 //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
1611 //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
1612 sp[stride] = val | ((v_n + 2) << (p - 1));
1614 //update line_state: bit 7 (\sigma^N), and E^N
1615 t = lsp[0] & 0x7F; // keep E^NW
1616 v_n = 32 - count_leading_zeros(v_n);
1617 lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
1618 } else if (locs & 0x2) { // if this is inside the codeblock, set the
1619 sp[stride] = 0; // sample to zero
1622 ++lsp; // move to next quad information
1623 ++sp; // move to next column of samples
1625 //this is similar to the above two samples
1626 if (qinf[0] & 0x40) {
1629 ms_val = frwd_fetch(&magsgn);
1630 m_n = U_q[0] - ((qinf[0] >> 14) & 1);
1631 frwd_advance(&magsgn, m_n);
1633 v_n = ms_val & ((1U << m_n) - 1);
1634 v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
1636 sp[0] = val | ((v_n + 2) << (p - 1));
1637 } else if (locs & 0x4) {
1642 if (qinf[0] & 0x80) {
1644 ms_val = frwd_fetch(&magsgn);
1645 m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
1646 frwd_advance(&magsgn, m_n);
1648 v_n = ms_val & ((1U << m_n) - 1);
1649 v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
1650 v_n |= 1; //center of bin
1651 sp[stride] = val | ((v_n + 2) << (p - 1));
1653 //line_state: bit 7 (\sigma^NW), and E^NW for next quad
1654 lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1655 } else if (locs & 0x8) { //if outside set to 0
1659 ++sp; //move to next column
1662 if (qinf[1] & 0x10) {
1665 ms_val = frwd_fetch(&magsgn);
1666 m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
1667 frwd_advance(&magsgn, m_n);
1669 v_n = ms_val & ((1U << m_n) - 1);
1670 v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
1672 sp[0] = val | ((v_n + 2) << (p - 1));
1673 } else if (locs & 0x10) {
1677 if (qinf[1] & 0x20) {
1680 ms_val = frwd_fetch(&magsgn);
1681 m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
1682 frwd_advance(&magsgn, m_n);
1684 v_n = ms_val & ((1U << m_n) - 1);
1685 v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
1687 sp[stride] = val | ((v_n + 2) << (p - 1));
1689 //update line_state: bit 7 (\sigma^N), and E^N
1690 t = lsp[0] & 0x7F; //E^NW
1691 v_n = 32 - count_leading_zeros(v_n); //E^N
1692 lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
1693 } else if (locs & 0x20) {
1694 sp[stride] = 0; //no need to update line_state
1697 ++lsp; //move line state to next quad
1698 ++sp; //move to next sample
1700 if (qinf[1] & 0x40) {
1703 ms_val = frwd_fetch(&magsgn);
1704 m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
1705 frwd_advance(&magsgn, m_n);
1707 v_n = ms_val & ((1U << m_n) - 1);
1708 v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
1710 sp[0] = val | ((v_n + 2) << (p - 1));
1711 } else if (locs & 0x40) {
1716 if (qinf[1] & 0x80) {
1719 ms_val = frwd_fetch(&magsgn);
1720 m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
1721 frwd_advance(&magsgn, m_n);
1723 v_n = ms_val & ((1U << m_n) - 1);
1724 v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
1725 v_n |= 1; //center of bin
1726 sp[stride] = val | ((v_n + 2) << (p - 1));
1728 //line_state: bit 7 (\sigma^NW), and E^NW for next quad
1729 lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1730 } else if (locs & 0x80) {
1738 //////////////////////////
1739 for (y = 2; y < height; /*done at the end of loop*/) {
1744 sip_shift ^= 0x2; // shift sigma to the upper half od the nibble
1745 sip_shift &= 0xFFFFFFEFU; //move back to 0 (it might have been at 0x10)
1746 sip = y & 0x4 ? sigma2 : sigma1; //choose sigma array
1749 ls0 = lsp[0]; // read the line state value
1750 lsp[0] = 0; // and set it to zero
1751 sp = decoded_data + y * stride; // generated samples
1753 for (x = 0; x < width; x += 4) {
1755 OPJ_UINT32 uvlc_mode, consumed_bits;
1756 OPJ_UINT32 m_n, v_n;
1764 // get context, eqn. 2 ITU T.814
1765 // c_q has \sigma^W | \sigma^SW
1766 c_q |= (ls0 >> 7); //\sigma^NW | \sigma^N
1767 c_q |= (lsp[1] >> 5) & 0x4; //\sigma^NE | \sigma^NF
1769 //the following is very similar to previous code, so please refer to
1771 vlc_val = rev_fetch(&vlc);
1772 qinf[0] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
1773 if (c_q == 0) { //zero context
1775 qinf[0] = (run == -1) ? qinf[0] : 0;
1777 run = mel_get_run(&mel);
1780 //prepare context for the next quad, \sigma^W | \sigma^SW
1781 c_q = ((qinf[0] & 0x40) >> 5) | ((qinf[0] & 0x80) >> 6);
1783 //remove data from vlc stream
1784 vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
1787 // The update depends on the value of x and y; consider one OPJ_UINT32
1788 // if x is 0, 8, 16 and so on, and y is 2, 6, etc., then this
1789 // line update c locations
1790 // nibble (4 bits) number 0 1 2 3 4 5 6 7
1791 // LSB 0 0 0 0 0 0 0 0
1795 *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
1799 if (x + 2 < width) {
1800 c_q |= (lsp[1] >> 7);
1801 c_q |= (lsp[2] >> 5) & 0x4;
1802 qinf[1] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
1803 if (c_q == 0) { //zero context
1805 qinf[1] = (run == -1) ? qinf[1] : 0;
1807 run = mel_get_run(&mel);
1810 //prepare context for the next quad
1811 c_q = ((qinf[1] & 0x40) >> 5) | ((qinf[1] & 0x80) >> 6);
1812 //remove data from vlc stream
1813 vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
1817 *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
1819 sip += x & 0x7 ? 1 : 0;
1824 uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
1825 consumed_bits = decode_noninit_uvlc(vlc_val, uvlc_mode, U_q);
1826 vlc_val = rev_advance(&vlc, consumed_bits);
1828 //calculate E^max and add it to U_q, eqns 5 and 6 in ITU T.814
1829 if ((qinf[0] & 0xF0) & ((qinf[0] & 0xF0) - 1)) { // is \gamma_q 1?
1830 OPJ_UINT32 E = (ls0 & 0x7Fu);
1831 E = E > (lsp[1] & 0x7Fu) ? E : (lsp[1] & 0x7Fu); //max(E, E^NE, E^NF)
1832 //since U_q already has u_q + 1, we subtract 2 instead of 1
1833 U_q[0] += E > 2 ? E - 2 : 0;
1836 if ((qinf[1] & 0xF0) & ((qinf[1] & 0xF0) - 1)) { //is \gamma_q 1?
1837 OPJ_UINT32 E = (lsp[1] & 0x7Fu);
1838 E = E > (lsp[2] & 0x7Fu) ? E : (lsp[2] & 0x7Fu); //max(E, E^NE, E^NF)
1839 //since U_q already has u_q + 1, we subtract 2 instead of 1
1840 U_q[1] += E > 2 ? E - 2 : 0;
1843 if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
1844 if (p_manager_mutex) {
1845 opj_mutex_lock(p_manager_mutex);
1847 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1848 "Decoding this codeblock is stopped. U_q is"
1849 "larger than bitplanes + 1 \n");
1850 if (p_manager_mutex) {
1851 opj_mutex_unlock(p_manager_mutex);
1856 ls0 = lsp[2]; //for next double quad
1857 lsp[1] = lsp[2] = 0;
1859 //decode magsgn and update line_state
1860 /////////////////////////////////////
1862 //locations where samples need update
1864 if (x + 4 > width) {
1865 locs >>= (x + 4 - width) << 1;
1867 locs = y + 2 <= height ? locs : (locs & 0x55);
1869 if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
1870 if (p_manager_mutex) {
1871 opj_mutex_lock(p_manager_mutex);
1873 opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
1874 "VLC code produces significant samples outside "
1875 "the codeblock area.\n");
1876 if (p_manager_mutex) {
1877 opj_mutex_unlock(p_manager_mutex);
1884 if (qinf[0] & 0x10) { //sigma_n
1887 ms_val = frwd_fetch(&magsgn);
1888 m_n = U_q[0] - ((qinf[0] >> 12) & 1); //m_n
1889 frwd_advance(&magsgn, m_n);
1891 v_n = ms_val & ((1U << m_n) - 1);
1892 v_n |= ((qinf[0] & 0x100) >> 8) << m_n;
1893 v_n |= 1; //center of bin
1894 sp[0] = val | ((v_n + 2) << (p - 1));
1895 } else if (locs & 0x1) {
1899 if (qinf[0] & 0x20) { //sigma_n
1902 ms_val = frwd_fetch(&magsgn);
1903 m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n
1904 frwd_advance(&magsgn, m_n);
1906 v_n = ms_val & ((1U << m_n) - 1);
1907 v_n |= ((qinf[0] & 0x200) >> 9) << m_n;
1908 v_n |= 1; //center of bin
1909 sp[stride] = val | ((v_n + 2) << (p - 1));
1911 //update line_state: bit 7 (\sigma^N), and E^N
1912 t = lsp[0] & 0x7F; //E^NW
1913 v_n = 32 - count_leading_zeros(v_n);
1914 lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
1915 } else if (locs & 0x2) {
1916 sp[stride] = 0; //no need to update line_state
1922 if (qinf[0] & 0x40) { //sigma_n
1925 ms_val = frwd_fetch(&magsgn);
1926 m_n = U_q[0] - ((qinf[0] >> 14) & 1); //m_n
1927 frwd_advance(&magsgn, m_n);
1929 v_n = ms_val & ((1U << m_n) - 1);
1930 v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
1931 v_n |= 1; //center of bin
1932 sp[0] = val | ((v_n + 2) << (p - 1));
1933 } else if (locs & 0x4) {
1937 if (qinf[0] & 0x80) { //sigma_n
1940 ms_val = frwd_fetch(&magsgn);
1941 m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
1942 frwd_advance(&magsgn, m_n);
1944 v_n = ms_val & ((1U << m_n) - 1);
1945 v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
1946 v_n |= 1; //center of bin
1947 sp[stride] = val | ((v_n + 2) << (p - 1));
1949 //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
1950 lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
1951 } else if (locs & 0x8) {
1957 if (qinf[1] & 0x10) { //sigma_n
1960 ms_val = frwd_fetch(&magsgn);
1961 m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
1962 frwd_advance(&magsgn, m_n);
1964 v_n = ms_val & ((1U << m_n) - 1);
1965 v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
1966 v_n |= 1; //center of bin
1967 sp[0] = val | ((v_n + 2) << (p - 1));
1968 } else if (locs & 0x10) {
1972 if (qinf[1] & 0x20) { //sigma_n
1975 ms_val = frwd_fetch(&magsgn);
1976 m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
1977 frwd_advance(&magsgn, m_n);
1979 v_n = ms_val & ((1U << m_n) - 1);
1980 v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
1981 v_n |= 1; //center of bin
1982 sp[stride] = val | ((v_n + 2) << (p - 1));
1984 //update line_state: bit 7 (\sigma^N), and E^N
1985 t = lsp[0] & 0x7F; //E^NW
1986 v_n = 32 - count_leading_zeros(v_n);
1987 lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
1988 } else if (locs & 0x20) {
1989 sp[stride] = 0; //no need to update line_state
1995 if (qinf[1] & 0x40) { //sigma_n
1998 ms_val = frwd_fetch(&magsgn);
1999 m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
2000 frwd_advance(&magsgn, m_n);
2002 v_n = ms_val & ((1U << m_n) - 1);
2003 v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
2004 v_n |= 1; //center of bin
2005 sp[0] = val | ((v_n + 2) << (p - 1));
2006 } else if (locs & 0x40) {
2010 if (qinf[1] & 0x80) { //sigma_n
2013 ms_val = frwd_fetch(&magsgn);
2014 m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
2015 frwd_advance(&magsgn, m_n);
2017 v_n = ms_val & ((1U << m_n) - 1);
2018 v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
2019 v_n |= 1; //center of bin
2020 sp[stride] = val | ((v_n + 2) << (p - 1));
2022 //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
2023 lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
2024 } else if (locs & 0x80) {
2032 if (num_passes > 1 && (y & 3) == 0) { //executed at multiples of 4
2033 // This is for SPP and potentially MRP
2035 if (num_passes > 2) { //do MRP
2036 // select the current stripe
2037 OPJ_UINT32 *cur_sig = y & 0x4 ? sigma1 : sigma2;
2038 // the address of the data that needs updating
2039 OPJ_UINT32 *dpp = decoded_data + (y - 4) * stride;
2040 OPJ_UINT32 half = 1u << (p - 2); // half the center of the bin
2042 for (i = 0; i < width; i += 8) {
2043 //Process one entry from sigma array at a time
2044 // Each nibble (4 bits) in the sigma array represents 4 rows,
2045 // and the 32 bits contain 8 columns
2046 OPJ_UINT32 cwd = rev_fetch_mrp(&magref); // get 32 bit data
2047 OPJ_UINT32 sig = *cur_sig++; // 32 bit that will be processed now
2048 OPJ_UINT32 col_mask = 0xFu; // a mask for a column in sig
2049 OPJ_UINT32 *dp = dpp + i; // next column in decode samples
2050 if (sig) { // if any of the 32 bits are set
2052 for (j = 0; j < 8; ++j, dp++) { //one column at a time
2053 if (sig & col_mask) { // lowest nibble
2054 OPJ_UINT32 sample_mask = 0x11111111u & col_mask; //LSB
2056 if (sig & sample_mask) { //if LSB is set
2059 assert(dp[0] != 0); // decoded value cannot be zero
2060 sym = cwd & 1; // get it value
2061 // remove center of bin if sym is 0
2062 dp[0] ^= (1 - sym) << (p - 1);
2063 dp[0] |= half; // put half the center of bin
2064 cwd >>= 1; //consume word
2066 sample_mask += sample_mask; //next row
2068 if (sig & sample_mask) {
2071 assert(dp[stride] != 0);
2073 dp[stride] ^= (1 - sym) << (p - 1);
2077 sample_mask += sample_mask;
2079 if (sig & sample_mask) {
2082 assert(dp[2 * stride] != 0);
2084 dp[2 * stride] ^= (1 - sym) << (p - 1);
2085 dp[2 * stride] |= half;
2088 sample_mask += sample_mask;
2090 if (sig & sample_mask) {
2093 assert(dp[3 * stride] != 0);
2095 dp[3 * stride] ^= (1 - sym) << (p - 1);
2096 dp[3 * stride] |= half;
2099 sample_mask += sample_mask;
2101 col_mask <<= 4; //next column
2104 // consume data according to the number of bits set
2105 rev_advance_mrp(&magref, population_count(sig));
2109 if (y >= 4) { // update mbr array at the end of each stripe
2110 //generate mbr corresponding to a stripe
2111 OPJ_UINT32 *sig = y & 0x4 ? sigma1 : sigma2;
2112 OPJ_UINT32 *mbr = y & 0x4 ? mbr1 : mbr2;
2114 //data is processed in patches of 8 columns, each
2115 // each 32 bits in sigma1 or mbr1 represent 4 rows
2117 //integrate horizontally
2118 OPJ_UINT32 prev = 0; // previous columns
2120 for (i = 0; i < width; i += 8, mbr++, sig++) {
2123 mbr[0] = sig[0]; //start with significant samples
2124 mbr[0] |= prev >> 28; //for first column, left neighbors
2125 mbr[0] |= sig[0] << 4; //left neighbors
2126 mbr[0] |= sig[0] >> 4; //right neighbors
2127 mbr[0] |= sig[1] << 28; //for last column, right neighbors
2128 prev = sig[0]; // for next group of columns
2130 //integrate vertically
2131 t = mbr[0], z = mbr[0];
2132 z |= (t & 0x77777777) << 1; //above neighbors
2133 z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
2134 mbr[0] = z & ~sig[0]; //remove already significance samples
2138 if (y >= 8) { //wait until 8 rows has been processed
2139 OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
2144 // add membership from the next stripe, obtained above
2145 cur_sig = y & 0x4 ? sigma2 : sigma1;
2146 cur_mbr = y & 0x4 ? mbr2 : mbr1;
2147 nxt_sig = y & 0x4 ? sigma1 : sigma2; //future samples
2148 prev = 0; // the columns before these group of 8 columns
2149 for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
2150 OPJ_UINT32 t = nxt_sig[0];
2151 t |= prev >> 28; //for first column, left neighbors
2152 t |= nxt_sig[0] << 4; //left neighbors
2153 t |= nxt_sig[0] >> 4; //right neighbors
2154 t |= nxt_sig[1] << 28; //for last column, right neighbors
2155 prev = nxt_sig[0]; // for next group of columns
2157 if (!stripe_causal) {
2158 cur_mbr[0] |= (t & 0x11111111u) << 3; //propagate up to cur_mbr
2160 cur_mbr[0] &= ~cur_sig[0]; //remove already significance samples
2163 //find new locations and get signs
2164 cur_sig = y & 0x4 ? sigma2 : sigma1;
2165 cur_mbr = y & 0x4 ? mbr2 : mbr1;
2166 nxt_sig = y & 0x4 ? sigma1 : sigma2; //future samples
2167 nxt_mbr = y & 0x4 ? mbr1 : mbr2; //future samples
2168 val = 3u << (p - 2); // sample values for newly discovered
2169 // significant samples including the bin center
2170 for (i = 0; i < width;
2171 i += 8, cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
2173 OPJ_UINT32 mbr = *cur_mbr;
2174 OPJ_UINT32 new_sig = 0;
2175 if (mbr) { //are there any samples that might be significant
2177 for (n = 0; n < 8; n += 4) {
2178 OPJ_UINT32 col_mask;
2183 OPJ_UINT32 cwd = frwd_fetch(&sigprop); //get 32 bits
2186 OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
2187 dp += i + n; //address for decoded samples
2189 col_mask = 0xFu << (4 * n); //a mask to select a column
2191 inv_sig = ~cur_sig[0]; // insignificant samples
2193 //find the last sample we operate on
2194 end = n + 4 + i < width ? n + 4 : width - i;
2196 for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2197 OPJ_UINT32 sample_mask;
2199 if ((col_mask & mbr) == 0) { //no samples need checking
2203 //scan mbr to find a new significant sample
2204 sample_mask = 0x11111111u & col_mask; // LSB
2205 if (mbr & sample_mask) {
2206 assert(dp[0] == 0); // the sample must have been 0
2207 if (cwd & 1) { //if this sample has become significant
2208 // must propagate it to nearby samples
2210 new_sig |= sample_mask; // new significant samples
2211 t = 0x32u << (j * 4);// propagation to neighbors
2212 mbr |= t & inv_sig; //remove already significant samples
2215 ++cnt; //consume bit and increment number of
2219 sample_mask += sample_mask; // next row
2220 if (mbr & sample_mask) {
2221 assert(dp[stride] == 0);
2224 new_sig |= sample_mask;
2225 t = 0x74u << (j * 4);
2232 sample_mask += sample_mask;
2233 if (mbr & sample_mask) {
2234 assert(dp[2 * stride] == 0);
2237 new_sig |= sample_mask;
2238 t = 0xE8u << (j * 4);
2245 sample_mask += sample_mask;
2246 if (mbr & sample_mask) {
2247 assert(dp[3 * stride] == 0);
2250 new_sig |= sample_mask;
2251 t = 0xC0u << (j * 4);
2260 if (new_sig & (0xFFFFu << (4 * n))) { //if any
2261 OPJ_UINT32 col_mask;
2263 OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
2264 dp += i + n; // decoded samples address
2265 col_mask = 0xFu << (4 * n); //mask to select a column
2267 for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2268 OPJ_UINT32 sample_mask;
2270 if ((col_mask & new_sig) == 0) { //if non is significant
2275 sample_mask = 0x11111111u & col_mask;
2276 if (new_sig & sample_mask) {
2278 dp[0] |= ((cwd & 1) << 31) | val; //put value and sign
2280 ++cnt; //consume bit and increment number
2284 sample_mask += sample_mask;
2285 if (new_sig & sample_mask) {
2286 assert(dp[stride] == 0);
2287 dp[stride] |= ((cwd & 1) << 31) | val;
2292 sample_mask += sample_mask;
2293 if (new_sig & sample_mask) {
2294 assert(dp[2 * stride] == 0);
2295 dp[2 * stride] |= ((cwd & 1) << 31) | val;
2300 sample_mask += sample_mask;
2301 if (new_sig & sample_mask) {
2302 assert(dp[3 * stride] == 0);
2303 dp[3 * stride] |= ((cwd & 1) << 31) | val;
2310 frwd_advance(&sigprop, cnt); //consume the bits from bitstrm
2313 //update the next 8 columns
2316 OPJ_UINT32 t = new_sig >> 28;
2317 t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
2318 cur_mbr[1] |= t & ~cur_sig[1];
2322 //update the next stripe (vertically propagation)
2323 new_sig |= cur_sig[0];
2324 ux = (new_sig & 0x88888888) >> 3;
2325 tx = ux | (ux << 4) | (ux >> 4); //left and right neighbors
2327 nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
2329 nxt_mbr[0] |= tx & ~nxt_sig[0];
2330 nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
2333 //clear current sigma
2334 //mbr need not be cleared because it is overwritten
2335 cur_sig = y & 0x4 ? sigma2 : sigma1;
2336 memset(cur_sig, 0, ((((OPJ_UINT32)width + 7u) >> 3) + 1u) << 2);
2342 if (num_passes > 1) {
2345 if (num_passes > 2 && ((height & 3) == 1 || (height & 3) == 2)) {
2347 OPJ_UINT32 *cur_sig = height & 0x4 ? sigma2 : sigma1; //reversed
2348 OPJ_UINT32 *dpp = decoded_data + (height & 0xFFFFFC) * stride;
2349 OPJ_UINT32 half = 1u << (p - 2);
2351 for (i = 0; i < width; i += 8) {
2352 OPJ_UINT32 cwd = rev_fetch_mrp(&magref);
2353 OPJ_UINT32 sig = *cur_sig++;
2354 OPJ_UINT32 col_mask = 0xF;
2355 OPJ_UINT32 *dp = dpp + i;
2358 for (j = 0; j < 8; ++j, dp++) {
2359 if (sig & col_mask) {
2360 OPJ_UINT32 sample_mask = 0x11111111 & col_mask;
2362 if (sig & sample_mask) {
2366 dp[0] ^= (1 - sym) << (p - 1);
2370 sample_mask += sample_mask;
2372 if (sig & sample_mask) {
2374 assert(dp[stride] != 0);
2376 dp[stride] ^= (1 - sym) << (p - 1);
2380 sample_mask += sample_mask;
2382 if (sig & sample_mask) {
2384 assert(dp[2 * stride] != 0);
2386 dp[2 * stride] ^= (1 - sym) << (p - 1);
2387 dp[2 * stride] |= half;
2390 sample_mask += sample_mask;
2392 if (sig & sample_mask) {
2394 assert(dp[3 * stride] != 0);
2396 dp[3 * stride] ^= (1 - sym) << (p - 1);
2397 dp[3 * stride] |= half;
2400 sample_mask += sample_mask;
2405 rev_advance_mrp(&magref, population_count(sig));
2409 //do the last incomplete stripe
2410 // for cases of (height & 3) == 0 and 3
2411 // the should have been processed previously
2412 if ((height & 3) == 1 || (height & 3) == 2) {
2413 //generate mbr of first stripe
2414 OPJ_UINT32 *sig = height & 0x4 ? sigma2 : sigma1;
2415 OPJ_UINT32 *mbr = height & 0x4 ? mbr2 : mbr1;
2416 //integrate horizontally
2417 OPJ_UINT32 prev = 0;
2419 for (i = 0; i < width; i += 8, mbr++, sig++) {
2423 mbr[0] |= prev >> 28; //for first column, left neighbors
2424 mbr[0] |= sig[0] << 4; //left neighbors
2425 mbr[0] |= sig[0] >> 4; //left neighbors
2426 mbr[0] |= sig[1] << 28; //for last column, right neighbors
2429 //integrate vertically
2430 t = mbr[0], z = mbr[0];
2431 z |= (t & 0x77777777) << 1; //above neighbors
2432 z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
2433 mbr[0] = z & ~sig[0]; //remove already significance samples
2438 st -= height > 6 ? (((height + 1) & 3) + 3) : height;
2439 for (y = st; y < height; y += 4) {
2440 OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
2444 OPJ_UINT32 pattern = 0xFFFFFFFFu; // a pattern needed samples
2445 if (height - y == 3) {
2446 pattern = 0x77777777u;
2447 } else if (height - y == 2) {
2448 pattern = 0x33333333u;
2449 } else if (height - y == 1) {
2450 pattern = 0x11111111u;
2453 //add membership from the next stripe, obtained above
2454 if (height - y > 4) {
2455 OPJ_UINT32 prev = 0;
2457 cur_sig = y & 0x4 ? sigma2 : sigma1;
2458 cur_mbr = y & 0x4 ? mbr2 : mbr1;
2459 nxt_sig = y & 0x4 ? sigma1 : sigma2;
2460 for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
2461 OPJ_UINT32 t = nxt_sig[0];
2462 t |= prev >> 28; //for first column, left neighbors
2463 t |= nxt_sig[0] << 4; //left neighbors
2464 t |= nxt_sig[0] >> 4; //left neighbors
2465 t |= nxt_sig[1] << 28; //for last column, right neighbors
2468 if (!stripe_causal) {
2469 cur_mbr[0] |= (t & 0x11111111u) << 3;
2471 //remove already significance samples
2472 cur_mbr[0] &= ~cur_sig[0];
2476 //find new locations and get signs
2477 cur_sig = y & 0x4 ? sigma2 : sigma1;
2478 cur_mbr = y & 0x4 ? mbr2 : mbr1;
2479 nxt_sig = y & 0x4 ? sigma1 : sigma2;
2480 nxt_mbr = y & 0x4 ? mbr1 : mbr2;
2481 val = 3u << (p - 2);
2482 for (i = 0; i < width; i += 8,
2483 cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
2484 OPJ_UINT32 mbr = *cur_mbr & pattern; //skip unneeded samples
2485 OPJ_UINT32 new_sig = 0;
2489 for (n = 0; n < 8; n += 4) {
2490 OPJ_UINT32 col_mask;
2495 OPJ_UINT32 cwd = frwd_fetch(&sigprop);
2498 OPJ_UINT32 *dp = decoded_data + y * stride;
2501 col_mask = 0xFu << (4 * n);
2503 inv_sig = ~cur_sig[0] & pattern;
2505 end = n + 4 + i < width ? n + 4 : width - i;
2506 for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2507 OPJ_UINT32 sample_mask;
2509 if ((col_mask & mbr) == 0) {
2514 sample_mask = 0x11111111u & col_mask;
2515 if (mbr & sample_mask) {
2519 new_sig |= sample_mask;
2520 t = 0x32u << (j * 4);
2527 sample_mask += sample_mask;
2528 if (mbr & sample_mask) {
2529 assert(dp[stride] == 0);
2532 new_sig |= sample_mask;
2533 t = 0x74u << (j * 4);
2540 sample_mask += sample_mask;
2541 if (mbr & sample_mask) {
2542 assert(dp[2 * stride] == 0);
2545 new_sig |= sample_mask;
2546 t = 0xE8u << (j * 4);
2553 sample_mask += sample_mask;
2554 if (mbr & sample_mask) {
2555 assert(dp[3 * stride] == 0);
2558 new_sig |= sample_mask;
2559 t = 0xC0u << (j * 4);
2568 if (new_sig & (0xFFFFu << (4 * n))) {
2569 OPJ_UINT32 col_mask;
2571 OPJ_UINT32 *dp = decoded_data + y * stride;
2573 col_mask = 0xFu << (4 * n);
2575 for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
2576 OPJ_UINT32 sample_mask;
2577 if ((col_mask & new_sig) == 0) {
2582 sample_mask = 0x11111111u & col_mask;
2583 if (new_sig & sample_mask) {
2585 dp[0] |= ((cwd & 1) << 31) | val;
2590 sample_mask += sample_mask;
2591 if (new_sig & sample_mask) {
2592 assert(dp[stride] == 0);
2593 dp[stride] |= ((cwd & 1) << 31) | val;
2598 sample_mask += sample_mask;
2599 if (new_sig & sample_mask) {
2600 assert(dp[2 * stride] == 0);
2601 dp[2 * stride] |= ((cwd & 1) << 31) | val;
2606 sample_mask += sample_mask;
2607 if (new_sig & sample_mask) {
2608 assert(dp[3 * stride] == 0);
2609 dp[3 * stride] |= ((cwd & 1) << 31) | val;
2616 frwd_advance(&sigprop, cnt);
2619 //update next columns
2622 OPJ_UINT32 t = new_sig >> 28;
2623 t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
2624 cur_mbr[1] |= t & ~cur_sig[1];
2628 //propagate down (vertically propagation)
2629 new_sig |= cur_sig[0];
2630 ux = (new_sig & 0x88888888) >> 3;
2631 tx = ux | (ux << 4) | (ux >> 4);
2633 nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
2635 nxt_mbr[0] |= tx & ~nxt_sig[0];
2636 nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
2643 for (y = 0; y < height; ++y) {
2644 OPJ_INT32* sp = (OPJ_INT32*)decoded_data + y * stride;
2645 for (x = 0; x < width; ++x, ++sp) {
2646 OPJ_INT32 val = (*sp & 0x7FFFFFFF);
2647 *sp = ((OPJ_UINT32) * sp & 0x80000000) ? -val : val;