98 ui32 val = 0xFFFFFFFF;
104 else if (
melp->size > 0)
107 while (
melp->size > 1) {
110 val = (val &
m) | (
v <<
i);
118 val = (val &
m) | (
v <<
i);
123 int bits = 32 -
melp->unstuff;
130 bool unstuff = ((val & 0xFF) == 0xFF);
132 t =
t << (8 - unstuff);
135 t |= (val>>8) & 0xFF;
136 unstuff = (((val >> 8) & 0xFF) == 0xFF);
138 t =
t << (8 - unstuff);
140 t |= (val>>16) & 0xFF;
141 unstuff = (((val >> 16) & 0xFF) == 0xFF);
143 t =
t << (8 - unstuff);
145 t |= (val>>24) & 0xFF;
146 melp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
172 static const int mel_exp[13] = {
173 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5
182 while (
melp->bits >= 6 &&
melp->num_runs < 8)
186 if (
melp->tmp & (1ull<<63))
201 run = (run << 1) + 1;
226 melp->unstuff =
false;
236 for (
int i = 0;
i <
num; ++
i) {
240 if (
melp->size == 1)
d |= 0xF;
246 melp->unstuff = ((
d & 0xFF) == 0xFF);
262 if (
melp->num_runs == 0)
265 int t =
melp->runs & 0x7F;
322 else if (
vlcp->size > 0)
325 while (
vlcp->size > 0) {
334 ui32 tmp = val >> 24;
338 bits = 8 - ((
vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1 : 0);
339 bool unstuff = (val >> 24) > 0x8F;
341 tmp |= ((val >> 16) & 0xFF) << bits;
342 bits += 8 - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1 : 0);
343 unstuff = ((val >> 16) & 0xFF) > 0x8F;
345 tmp |= ((val >> 8) & 0xFF) << bits;
346 bits += 8 - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1 : 0);
347 unstuff = ((val >> 8) & 0xFF) > 0x8F;
349 tmp |= (val & 0xFF) << bits;
350 bits += 8 - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1 : 0);
351 unstuff = (val & 0xFF) > 0x8F;
356 vlcp->unstuff = unstuff;
384 vlcp->bits = 4 - ((
vlcp->tmp & 7) == 7);
385 vlcp->unstuff = (
d | 0xF) > 0x8F;
394 for (
int i = 0;
i <
tnum; ++
i) {
401 vlcp->unstuff =
d > 0x8F;
436 vlcp->tmp >>= num_bits;
437 vlcp->bits -= num_bits;
465 else if (
mrp->size > 0)
468 while (
mrp->size > 0) {
477 ui32 bits, tmp = val >> 24;
480 bits = 8 - ((
mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1 : 0);
481 bool unstuff = (val >> 24) > 0x8F;
484 tmp |= ((val >> 16) & 0xFF) << bits;
485 bits += 8 - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1 : 0);
486 unstuff = ((val >> 16) & 0xFF) > 0x8F;
488 tmp |= ((val >> 8) & 0xFF) << bits;
489 bits += 8 - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1 : 0);
490 unstuff = ((val >> 8) & 0xFF) > 0x8F;
492 tmp |= (val & 0xFF) << bits;
493 bits += 8 - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1 : 0);
494 unstuff = (val & 0xFF) > 0x8F;
498 mrp->unstuff = unstuff;
530 for (
int i = 0;
i <
num; ++
i) {
533 d = (
mrp->size-- > 0) ? *
mrp->data-- : 0;
535 ui32 d_bits = 8 - ((
mrp->unstuff && ((
d & 0x7F) == 0x7F)) ? 1 : 0);
538 mrp->unstuff =
d > 0x8F;
572 mrp->tmp >>= num_bits;
573 mrp->bits -= num_bits;
619 else if (
msp->size > 0)
622 val =
X != 0 ? 0xFFFFFFFFu : 0;
623 while (
msp->size > 0) {
626 val = (val &
m) | (
v <<
i);
632 val =
X != 0 ? 0xFFFFFFFFu : 0;
637 bool unstuff = ((val & 0xFF) == 0xFF);
639 t |= ((val >> 8) & 0xFF) << bits;
641 unstuff = (((val >> 8) & 0xFF) == 0xFF);
643 t |= ((val >> 16) & 0xFF) << bits;
645 unstuff = (((val >> 16) & 0xFF) == 0xFF);
647 t |= ((val >> 24) & 0xFF) << bits;
649 msp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
679 for (
int i = 0;
i <
num; ++
i)
683 d =
msp->size-- > 0 ? *
msp->data++ :
X;
685 msp->bits += 8 -
msp->unstuff;
686 msp->unstuff = ((
d & 0xFF) == 0xFF);
701 msp->tmp >>= num_bits;
702 msp->bits -= num_bits;
752 if (num_passes > 1 &&
lengths2 == 0)
754 OJPH_WARN(0x00010001,
"A malformed codeblock that has more than "
755 "one coding pass, but zero length for "
756 "2nd and potential 3rd pass.\n");
762 OJPH_WARN(0x00010002,
"We do not support more than 3 coding passes; "
763 "This codeblocks has %d passes.\n",
768 if (missing_msbs > 30)
773 OJPH_WARN(0x00010003,
"32 bits are not enough to decode this "
774 "codeblock. This message will not be "
775 "displayed again.\n");
779 else if (missing_msbs == 30)
783 OJPH_WARN(0x00010004,
"Not enough precision to decode the cleanup "
784 "pass. The code can be modified to support "
785 "this case. This message will not be "
786 "displayed again.\n");
790 else if (missing_msbs == 29)
792 if (num_passes > 1) {
796 OJPH_WARN(0x00010005,
"Not enough precision to decode the SgnProp "
797 "nor MagRef passes; both will be skipped. "
798 "This message will not be displayed "
803 ui32 p = 30 - missing_msbs;
809 OJPH_WARN(0x00010006,
"Wrong codeblock length.\n");
836 ui16 scratch[8 * 513] = {0};
844 ui32 sstr = ((width + 2u) + 7u) & ~7u;
870 for (
ui32 x = 0; x < width;
sp += 4)
889 t0 = (run == -1) ?
t0 : 0;
903 c_q = ((
t0 & 0x10U) << 3) | ((
t0 & 0xE0U) << 2);
915 if (
c_q == 0 && x < width)
920 t1 = (run == -1) ?
t1 : 0;
925 t1 = x < width ?
t1 : 0;
934 c_q = ((
t1 & 0x10U) << 3) | ((
t1 & 0xE0U) << 2);
979 for (
ui32 y = 2; y < height; y += 2)
984 for (
ui32 x = 0; x < width;
sp += 4)
1007 t0 = (run == -1) ?
t0 : 0;
1022 c_q = ((
t0 & 0x40U) << 2) | ((
t0 & 0x80U) << 1);
1039 if (
c_q == 0 && x < width)
1044 t1 = (run == -1) ?
t1 : 0;
1049 t1 = x < width ?
t1 : 0;
1059 c_q = ((
t1 & 0x40U) << 2) | ((
t1 & 0x80U) << 1);
1110 for (
ui32 x = 0; x < width;
sp += 2, ++
vp)
1120 if (
inf & (1 << (4 +
bit)))
1133 val |= (
v_n + 2) << (
p - 1);
1140 if (
inf & (1 << (4 +
bit)))
1153 val |= (
v_n + 2) << (
p - 1);
1164 if (
inf & (1 << (4 +
bit)))
1177 val |= (
v_n + 2) << (
p - 1);
1184 if (
inf & (1 << (4 +
bit)))
1197 val |= (
v_n + 2) << (
p - 1);
1206 for (
ui32 y = 2; y < height; y += 2)
1213 for (
ui32 x = 0; x < width;
sp += 2, ++
vp)
1230 if (
inf & (1 << (4 +
bit)))
1243 val |= (
v_n + 2) << (
p - 1);
1250 if (
inf & (1 << (4 +
bit)))
1263 val |= (
v_n + 2) << (
p - 1);
1274 if (
inf & (1 << (4 +
bit)))
1287 val |= (
v_n + 2) << (
p - 1);
1294 if (
inf & (1 << (4 +
bit)))
1307 val |= (
v_n + 2) << (
p - 1);
1335 for (y = 0; y < height; y += 4)
1339 for (
ui32 x = 0; x < width; x += 4,
sp += 4, ++
dp) {
1341 t0 = ((
sp[0 ] & 0x30u) >> 4) | ((
sp[0 ] & 0xC0u) >> 2);
1342 t0 |= ((
sp[2 ] & 0x30u) << 4) | ((
sp[2 ] & 0xC0u) << 6);
1352 for (
ui32 x = 0; x < width; x += 4, ++
dp)
1374 for (
ui32 y = 0; y < height; y += 4)
1377 if (height - y < 4) {
1379 if (height - y < 3) {
1413 ui32 u = (
ps & 0x88888888) >> 3;
1415 u |= (
ns & 0x11111111) << 3;
1420 mbr |= (
cs & 0x77777777) << 1;
1421 mbr |= (
cs & 0xEEEEEEEE) >> 1;
1502 ui32 val = 3u << (
p - 2);
1514 dp[0] = (
cwd << 31) | val;
1522 dp[stride] = (
cwd << 31) | val;
1530 dp[2 * stride] = (
cwd << 31) | val;
1538 dp[3 * stride] = (
cwd << 31) | val;
1567 for (
ui32 y = 0; y < height; y += 4)
1572 for (
ui32 i = 0;
i < width;
i += 8)
1582 for (
int j = 0;
j < 8; ++
j)
1589 for (
int k = 0; k < 4; ++k) {
ui16 uvlc_tbl0[256+64]
uvlc_tbl0 contains decoding information for initial row of quads
ui16 uvlc_tbl1[256]
uvlc_tbl1 contains decoding information for non-initial row of quads
ui16 vlc_tbl0[1024]
vlc_tbl0 contains decoding information for initial row of quads
ui16 vlc_tbl1[1024]
vlc_tbl1 contains decoding information for non-initial row of quads
static ui32 rev_fetch(rev_struct *vlcp)
Retrieves 32 bits from the head of a rev_struct structure.
static void rev_init_mrp(rev_struct *mrp, ui8 *data, int lcup, int len2)
Initialized rev_struct structure for MRP segment, and reads a number of bytes such that the next 32 b...
static void mel_read(dec_mel_st *melp)
Reads and unstuffs the MEL bitstream.
static void frwd_advance(frwd_struct *msp, ui32 num_bits)
Consume num_bits bits from the bitstream of frwd_struct.
static void rev_read_mrp(rev_struct *mrp)
Reads and unstuffs from rev_struct.
static ui32 rev_fetch_mrp(rev_struct *mrp)
Retrieves 32 bits from the head of a rev_struct structure.
static ui32 rev_advance_mrp(rev_struct *mrp, ui32 num_bits)
Consumes num_bits from a rev_struct structure.
static void rev_read(rev_struct *vlcp)
Read and unstuff data from a backwardly-growing segment.
static int mel_get_run(dec_mel_st *melp)
Retrieves one run from dec_mel_st; if there are no runs stored MEL segment is decoded.
static void rev_init(rev_struct *vlcp, ui8 *data, int lcup, int scup)
Initiates the rev_struct structure and reads a few bytes to move the read address to multiple of 4.
static void mel_init(dec_mel_st *melp, ui8 *bbuf, int lcup, int scup)
Initiates a dec_mel_st structure for MEL decoding and reads some bytes in order to get the read addre...
static ui32 rev_advance(rev_struct *vlcp, ui32 num_bits)
Consumes num_bits from a rev_struct structure.
static void frwd_read(frwd_struct *msp)
Read and unstuffs 32 bits from forward-growing bitstream.
static ui32 frwd_fetch(frwd_struct *msp)
Fetches 32 bits from the frwd_struct bitstream.
static void frwd_init(frwd_struct *msp, const ui8 *data, int size)
Initialize frwd_struct struct and reads some bytes.
bool ojph_decode_codeblock(ui8 *coded_data, ui32 *decoded_data, ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, ui32 width, ui32 height, ui32 stride, bool stripe_causal)
Decodes one codeblock, processing the cleanup, siginificance propagation, and magnitude refinement pa...
static void mel_decode(dec_mel_st *melp)
Decodes unstuffed MEL segment bits stored in tmp to runs.
static ui32 population_count(ui32 val)
static ui32 count_leading_zeros(ui32 val)
MEL state structure for reading and decoding the MEL bitstream.
bool unstuff
true if the next bit needs to be unstuffed
int num_runs
number of decoded runs left in runs (maximum 8)
int size
number of bytes in MEL code
ui8 * data
the address of data (or bitstream)
int k
state of MEL decoder
int bits
number of bits stored in tmp
ui64 tmp
temporary buffer for read data
ui64 runs
runs of decoded MEL codewords (7 bits/run)
State structure for reading and unstuffing of forward-growing bitstreams; these are: MagSgn and SPP b...
const ui8 * data
pointer to bitstream
ui32 bits
number of bits stored in tmp
ui64 tmp
temporary buffer of read data
ui32 unstuff
1 if a bit needs to be unstuffed from next byte
A structure for reading and unstuffing a segment that grows backward, such as VLC and MRP.
ui32 bits
number of bits stored in tmp
int size
number of bytes left
ui8 * data
pointer to where to read data
ui64 tmp
temporary buffer of read data