4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1995-1997, Thomas G. Lane.
6 * Lossless JPEG Modifications:
7 * Copyright (C) 1999, Ken Murchison.
8 * libjpeg-turbo Modifications:
9 * Copyright (C) 2015-2016, 2018-2022, D. R. Commander.
10 * For conditions of distribution and use, see the accompanying README.ijg
13 * This file contains Huffman entropy decoding routines for progressive JPEG.
15 * Much of the complexity here has to do with supporting input suspension.
16 * If the data source module demands suspension, we want to be able to back
17 * up to the start of the current MCU. To do this, we copy state variables
18 * into local working storage, and update them back to the permanent
19 * storage only upon successful completion of an MCU.
21 * NOTE: All referenced figures are from
22 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
25 #define JPEG_INTERNALS
28 #include "jdhuff.h" /* Declarations shared with jd*huff.c */
32 #ifdef D_PROGRESSIVE_SUPPORTED
35 * Expanded entropy decoder object for progressive Huffman decoding.
37 * The savable_state subrecord contains fields that change within an MCU,
38 * but must not be updated permanently until we complete the MCU.
42 unsigned int EOBRUN
; /* remaining EOBs in EOBRUN */
43 int last_dc_val
[MAX_COMPS_IN_SCAN
]; /* last DC coef for each component */
47 struct jpeg_entropy_decoder pub
; /* public fields */
49 /* These fields are loaded into local variables at start of each MCU.
50 * In case of suspension, we exit WITHOUT updating them.
52 bitread_perm_state bitstate
; /* Bit buffer at start of MCU */
53 savable_state saved
; /* Other state at start of MCU */
55 /* These fields are NOT loaded into local working state. */
56 unsigned int restarts_to_go
; /* MCUs left in this restart interval */
58 /* Pointers to derived tables (these workspaces have image lifespan) */
59 d_derived_tbl
*derived_tbls
[NUM_HUFF_TBLS
];
61 d_derived_tbl
*ac_derived_tbl
; /* active table during an AC scan */
62 } phuff_entropy_decoder
;
64 typedef phuff_entropy_decoder
*phuff_entropy_ptr
;
66 /* Forward declarations */
67 METHODDEF(boolean
) decode_mcu_DC_first(j_decompress_ptr cinfo
,
69 METHODDEF(boolean
) decode_mcu_AC_first(j_decompress_ptr cinfo
,
71 METHODDEF(boolean
) decode_mcu_DC_refine(j_decompress_ptr cinfo
,
73 METHODDEF(boolean
) decode_mcu_AC_refine(j_decompress_ptr cinfo
,
78 * Initialize for a Huffman-compressed scan.
82 start_pass_phuff_decoder(j_decompress_ptr cinfo
)
84 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
85 boolean is_DC_band
, bad
;
87 d_derived_tbl
**pdtbl
;
88 int *coef_bit_ptr
, *prev_coef_bit_ptr
;
89 jpeg_component_info
*compptr
;
91 is_DC_band
= (cinfo
->Ss
== 0);
93 /* Validate scan parameters */
99 /* need not check Ss/Se < 0 since they came from unsigned bytes */
100 if (cinfo
->Ss
> cinfo
->Se
|| cinfo
->Se
>= DCTSIZE2
)
102 /* AC scans may have only one component */
103 if (cinfo
->comps_in_scan
!= 1)
106 if (cinfo
->Ah
!= 0) {
107 /* Successive approximation refinement scan: must have Al = Ah-1. */
108 if (cinfo
->Al
!= cinfo
->Ah
- 1)
111 if (cinfo
->Al
> 13) /* need not check for < 0 */
113 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
114 * but the spec doesn't say so, and we try to be liberal about what we
115 * accept. Note: large Al values could result in out-of-range DC
116 * coefficients during early scans, leading to bizarre displays due to
117 * overflows in the IDCT math. But we won't crash.
120 ERREXIT4(cinfo
, JERR_BAD_PROGRESSION
,
121 cinfo
->Ss
, cinfo
->Se
, cinfo
->Ah
, cinfo
->Al
);
122 /* Update progression status, and verify that scan order is legal.
123 * Note that inter-scan inconsistencies are treated as warnings
124 * not fatal errors ... not clear if this is right way to behave.
126 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
127 int cindex
= cinfo
->cur_comp_info
[ci
]->component_index
;
128 coef_bit_ptr
= &cinfo
->coef_bits
[cindex
][0];
129 prev_coef_bit_ptr
= &cinfo
->coef_bits
[cindex
+ cinfo
->num_components
][0];
130 if (!is_DC_band
&& coef_bit_ptr
[0] < 0) /* AC without prior DC scan */
131 WARNMS2(cinfo
, JWRN_BOGUS_PROGRESSION
, cindex
, 0);
132 for (coefi
= MIN(cinfo
->Ss
, 1); coefi
<= MAX(cinfo
->Se
, 9); coefi
++) {
133 if (cinfo
->input_scan_number
> 1)
134 prev_coef_bit_ptr
[coefi
] = coef_bit_ptr
[coefi
];
136 prev_coef_bit_ptr
[coefi
] = 0;
138 for (coefi
= cinfo
->Ss
; coefi
<= cinfo
->Se
; coefi
++) {
139 int expected
= (coef_bit_ptr
[coefi
] < 0) ? 0 : coef_bit_ptr
[coefi
];
140 if (cinfo
->Ah
!= expected
)
141 WARNMS2(cinfo
, JWRN_BOGUS_PROGRESSION
, cindex
, coefi
);
142 coef_bit_ptr
[coefi
] = cinfo
->Al
;
146 /* Select MCU decoding routine */
147 if (cinfo
->Ah
== 0) {
149 entropy
->pub
.decode_mcu
= decode_mcu_DC_first
;
151 entropy
->pub
.decode_mcu
= decode_mcu_AC_first
;
154 entropy
->pub
.decode_mcu
= decode_mcu_DC_refine
;
156 entropy
->pub
.decode_mcu
= decode_mcu_AC_refine
;
159 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
160 compptr
= cinfo
->cur_comp_info
[ci
];
161 /* Make sure requested tables are present, and compute derived tables.
162 * We may build same derived table more than once, but it's not expensive.
165 if (cinfo
->Ah
== 0) { /* DC refinement needs no table */
166 tbl
= compptr
->dc_tbl_no
;
167 pdtbl
= (d_derived_tbl
**)(entropy
->derived_tbls
) + tbl
;
168 jpeg_make_d_derived_tbl(cinfo
, TRUE
, tbl
, pdtbl
);
171 tbl
= compptr
->ac_tbl_no
;
172 pdtbl
= (d_derived_tbl
**)(entropy
->derived_tbls
) + tbl
;
173 jpeg_make_d_derived_tbl(cinfo
, FALSE
, tbl
, pdtbl
);
174 /* remember the single active table */
175 entropy
->ac_derived_tbl
= entropy
->derived_tbls
[tbl
];
177 /* Initialize DC predictions to 0 */
178 entropy
->saved
.last_dc_val
[ci
] = 0;
181 /* Initialize bitread state variables */
182 entropy
->bitstate
.bits_left
= 0;
183 entropy
->bitstate
.get_buffer
= 0; /* unnecessary, but keeps Purify quiet */
184 entropy
->pub
.insufficient_data
= FALSE
;
186 /* Initialize private state variables */
187 entropy
->saved
.EOBRUN
= 0;
189 /* Initialize restart counter */
190 entropy
->restarts_to_go
= cinfo
->restart_interval
;
195 * Figure F.12: extend sign bit.
196 * On some machines, a shift and add will be faster than a table lookup.
202 #define NEG_1 ((unsigned)-1)
203 #define HUFF_EXTEND(x, s) \
204 ((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x))
208 #define HUFF_EXTEND(x, s) \
209 ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
211 static const int extend_test
[16] = { /* entry n is 2**(n-1) */
212 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
213 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
216 static const int extend_offset
[16] = { /* entry n is (-1 << n) + 1 */
217 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
218 ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
219 ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
220 ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
223 #endif /* AVOID_TABLES */
227 * Check for a restart marker & resynchronize decoder.
228 * Returns FALSE if must suspend.
232 process_restart(j_decompress_ptr cinfo
)
234 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
237 /* Throw away any unused bits remaining in bit buffer; */
238 /* include any full bytes in next_marker's count of discarded bytes */
239 cinfo
->marker
->discarded_bytes
+= entropy
->bitstate
.bits_left
/ 8;
240 entropy
->bitstate
.bits_left
= 0;
242 /* Advance past the RSTn marker */
243 if (!(*cinfo
->marker
->read_restart_marker
) (cinfo
))
246 /* Re-initialize DC predictions to 0 */
247 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++)
248 entropy
->saved
.last_dc_val
[ci
] = 0;
249 /* Re-init EOB run count, too */
250 entropy
->saved
.EOBRUN
= 0;
252 /* Reset restart counter */
253 entropy
->restarts_to_go
= cinfo
->restart_interval
;
255 /* Reset out-of-data flag, unless read_restart_marker left us smack up
256 * against a marker. In that case we will end up treating the next data
257 * segment as empty, and we can avoid producing bogus output pixels by
258 * leaving the flag set.
260 if (cinfo
->unread_marker
== 0)
261 entropy
->pub
.insufficient_data
= FALSE
;
268 * Huffman MCU decoding.
269 * Each of these routines decodes and returns one MCU's worth of
270 * Huffman-compressed coefficients.
271 * The coefficients are reordered from zigzag order into natural array order,
272 * but are not dequantized.
274 * The i'th block of the MCU is stored into the block pointed to by
275 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
277 * We return FALSE if data source requested suspension. In that case no
278 * changes have been made to permanent state. (Exception: some output
279 * coefficients may already have been assigned. This is harmless for
280 * spectral selection, since we'll just re-assign them on the next call.
281 * Successive approximation AC refinement has to be more careful, however.)
285 * MCU decoding for DC initial scan (either spectral selection,
286 * or first pass of successive approximation).
290 decode_mcu_DC_first(j_decompress_ptr cinfo
, JBLOCKROW
*MCU_data
)
292 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
300 jpeg_component_info
*compptr
;
302 /* Process restart marker if needed; may have to suspend */
303 if (cinfo
->restart_interval
) {
304 if (entropy
->restarts_to_go
== 0)
305 if (!process_restart(cinfo
))
309 /* If we've run out of data, just leave the MCU set to zeroes.
310 * This way, we return uniform gray for the remainder of the segment.
312 if (!entropy
->pub
.insufficient_data
) {
314 /* Load up working state */
315 BITREAD_LOAD_STATE(cinfo
, entropy
->bitstate
);
316 state
= entropy
->saved
;
318 /* Outer loop handles each block in the MCU */
320 for (blkn
= 0; blkn
< cinfo
->blocks_in_MCU
; blkn
++) {
321 block
= MCU_data
[blkn
];
322 ci
= cinfo
->MCU_membership
[blkn
];
323 compptr
= cinfo
->cur_comp_info
[ci
];
324 tbl
= entropy
->derived_tbls
[compptr
->dc_tbl_no
];
326 /* Decode a single block's worth of coefficients */
328 /* Section F.2.2.1: decode the DC coefficient difference */
329 HUFF_DECODE(s
, br_state
, tbl
, return FALSE
, label1
);
331 CHECK_BIT_BUFFER(br_state
, s
, return FALSE
);
333 s
= HUFF_EXTEND(r
, s
);
336 /* Convert DC difference to actual value, update last_dc_val */
337 if ((state
.last_dc_val
[ci
] >= 0 &&
338 s
> INT_MAX
- state
.last_dc_val
[ci
]) ||
339 (state
.last_dc_val
[ci
] < 0 && s
< INT_MIN
- state
.last_dc_val
[ci
]))
340 ERREXIT(cinfo
, JERR_BAD_DCT_COEF
);
341 s
+= state
.last_dc_val
[ci
];
342 state
.last_dc_val
[ci
] = s
;
343 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
344 (*block
)[0] = (JCOEF
)LEFT_SHIFT(s
, Al
);
347 /* Completed MCU, so update state */
348 BITREAD_SAVE_STATE(cinfo
, entropy
->bitstate
);
349 entropy
->saved
= state
;
352 /* Account for restart interval (no-op if not using restarts) */
353 if (cinfo
->restart_interval
)
354 entropy
->restarts_to_go
--;
361 * MCU decoding for AC initial scan (either spectral selection,
362 * or first pass of successive approximation).
366 decode_mcu_AC_first(j_decompress_ptr cinfo
, JBLOCKROW
*MCU_data
)
368 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
371 register int s
, k
, r
;
377 /* Process restart marker if needed; may have to suspend */
378 if (cinfo
->restart_interval
) {
379 if (entropy
->restarts_to_go
== 0)
380 if (!process_restart(cinfo
))
384 /* If we've run out of data, just leave the MCU set to zeroes.
385 * This way, we return uniform gray for the remainder of the segment.
387 if (!entropy
->pub
.insufficient_data
) {
389 /* Load up working state.
390 * We can avoid loading/saving bitread state if in an EOB run.
392 EOBRUN
= entropy
->saved
.EOBRUN
; /* only part of saved state we need */
394 /* There is always only one block per MCU */
396 if (EOBRUN
> 0) /* if it's a band of zeroes... */
397 EOBRUN
--; /* ...process it now (we do nothing) */
399 BITREAD_LOAD_STATE(cinfo
, entropy
->bitstate
);
401 tbl
= entropy
->ac_derived_tbl
;
403 for (k
= cinfo
->Ss
; k
<= Se
; k
++) {
404 HUFF_DECODE(s
, br_state
, tbl
, return FALSE
, label2
);
409 CHECK_BIT_BUFFER(br_state
, s
, return FALSE
);
411 s
= HUFF_EXTEND(r
, s
);
412 /* Scale and output coefficient in natural (dezigzagged) order */
413 (*block
)[jpeg_natural_order
[k
]] = (JCOEF
)LEFT_SHIFT(s
, Al
);
415 if (r
== 15) { /* ZRL */
416 k
+= 15; /* skip 15 zeroes in band */
417 } else { /* EOBr, run length is 2^r + appended bits */
419 if (r
) { /* EOBr, r > 0 */
420 CHECK_BIT_BUFFER(br_state
, r
, return FALSE
);
424 EOBRUN
--; /* this band is processed at this moment */
425 break; /* force end-of-band */
430 BITREAD_SAVE_STATE(cinfo
, entropy
->bitstate
);
433 /* Completed MCU, so update state */
434 entropy
->saved
.EOBRUN
= EOBRUN
; /* only part of saved state we need */
437 /* Account for restart interval (no-op if not using restarts) */
438 if (cinfo
->restart_interval
)
439 entropy
->restarts_to_go
--;
446 * MCU decoding for DC successive approximation refinement scan.
447 * Note: we assume such scans can be multi-component, although the spec
448 * is not very clear on the point.
452 decode_mcu_DC_refine(j_decompress_ptr cinfo
, JBLOCKROW
*MCU_data
)
454 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
455 int p1
= 1 << cinfo
->Al
; /* 1 in the bit position being coded */
460 /* Process restart marker if needed; may have to suspend */
461 if (cinfo
->restart_interval
) {
462 if (entropy
->restarts_to_go
== 0)
463 if (!process_restart(cinfo
))
467 /* Not worth the cycles to check insufficient_data here,
468 * since we will not change the data anyway if we read zeroes.
471 /* Load up working state */
472 BITREAD_LOAD_STATE(cinfo
, entropy
->bitstate
);
474 /* Outer loop handles each block in the MCU */
476 for (blkn
= 0; blkn
< cinfo
->blocks_in_MCU
; blkn
++) {
477 block
= MCU_data
[blkn
];
479 /* Encoded data is simply the next bit of the two's-complement DC value */
480 CHECK_BIT_BUFFER(br_state
, 1, return FALSE
);
483 /* Note: since we use |=, repeating the assignment later is safe */
486 /* Completed MCU, so update state */
487 BITREAD_SAVE_STATE(cinfo
, entropy
->bitstate
);
489 /* Account for restart interval (no-op if not using restarts) */
490 if (cinfo
->restart_interval
)
491 entropy
->restarts_to_go
--;
498 * MCU decoding for AC successive approximation refinement scan.
502 decode_mcu_AC_refine(j_decompress_ptr cinfo
, JBLOCKROW
*MCU_data
)
504 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
)cinfo
->entropy
;
506 int p1
= 1 << cinfo
->Al
; /* 1 in the bit position being coded */
507 int m1
= (NEG_1
) << cinfo
->Al
; /* -1 in the bit position being coded */
508 register int s
, k
, r
;
515 int newnz_pos
[DCTSIZE2
];
517 /* Process restart marker if needed; may have to suspend */
518 if (cinfo
->restart_interval
) {
519 if (entropy
->restarts_to_go
== 0)
520 if (!process_restart(cinfo
))
524 /* If we've run out of data, don't modify the MCU.
526 if (!entropy
->pub
.insufficient_data
) {
528 /* Load up working state */
529 BITREAD_LOAD_STATE(cinfo
, entropy
->bitstate
);
530 EOBRUN
= entropy
->saved
.EOBRUN
; /* only part of saved state we need */
532 /* There is always only one block per MCU */
534 tbl
= entropy
->ac_derived_tbl
;
536 /* If we are forced to suspend, we must undo the assignments to any newly
537 * nonzero coefficients in the block, because otherwise we'd get confused
538 * next time about which coefficients were already nonzero.
539 * But we need not undo addition of bits to already-nonzero coefficients;
540 * instead, we can test the current bit to see if we already did it.
544 /* initialize coefficient loop counter to start of band */
548 for (; k
<= Se
; k
++) {
549 HUFF_DECODE(s
, br_state
, tbl
, goto undoit
, label3
);
553 if (s
!= 1) /* size of new coef should always be 1 */
554 WARNMS(cinfo
, JWRN_HUFF_BAD_CODE
);
555 CHECK_BIT_BUFFER(br_state
, 1, goto undoit
);
557 s
= p1
; /* newly nonzero coef is positive */
559 s
= m1
; /* newly nonzero coef is negative */
562 EOBRUN
= 1 << r
; /* EOBr, run length is 2^r + appended bits */
564 CHECK_BIT_BUFFER(br_state
, r
, goto undoit
);
568 break; /* rest of block is handled by EOB logic */
570 /* note s = 0 for processing ZRL */
572 /* Advance over already-nonzero coefs and r still-zero coefs,
573 * appending correction bits to the nonzeroes. A correction bit is 1
574 * if the absolute value of the coefficient must be increased.
577 thiscoef
= *block
+ jpeg_natural_order
[k
];
578 if (*thiscoef
!= 0) {
579 CHECK_BIT_BUFFER(br_state
, 1, goto undoit
);
581 if ((*thiscoef
& p1
) == 0) { /* do nothing if already set it */
583 *thiscoef
+= (JCOEF
)p1
;
585 *thiscoef
+= (JCOEF
)m1
;
590 break; /* reached target zero coefficient */
595 int pos
= jpeg_natural_order
[k
];
596 /* Output newly nonzero coefficient */
597 (*block
)[pos
] = (JCOEF
)s
;
598 /* Remember its position in case we have to suspend */
599 newnz_pos
[num_newnz
++] = pos
;
605 /* Scan any remaining coefficient positions after the end-of-band
606 * (the last newly nonzero coefficient, if any). Append a correction
607 * bit to each already-nonzero coefficient. A correction bit is 1
608 * if the absolute value of the coefficient must be increased.
610 for (; k
<= Se
; k
++) {
611 thiscoef
= *block
+ jpeg_natural_order
[k
];
612 if (*thiscoef
!= 0) {
613 CHECK_BIT_BUFFER(br_state
, 1, goto undoit
);
615 if ((*thiscoef
& p1
) == 0) { /* do nothing if already changed it */
617 *thiscoef
+= (JCOEF
)p1
;
619 *thiscoef
+= (JCOEF
)m1
;
624 /* Count one block completed in EOB run */
628 /* Completed MCU, so update state */
629 BITREAD_SAVE_STATE(cinfo
, entropy
->bitstate
);
630 entropy
->saved
.EOBRUN
= EOBRUN
; /* only part of saved state we need */
633 /* Account for restart interval (no-op if not using restarts) */
634 if (cinfo
->restart_interval
)
635 entropy
->restarts_to_go
--;
640 /* Re-zero any output coefficients that we made newly nonzero */
641 while (num_newnz
> 0)
642 (*block
)[newnz_pos
[--num_newnz
]] = 0;
649 * Module initialization routine for progressive Huffman entropy decoding.
653 jinit_phuff_decoder(j_decompress_ptr cinfo
)
655 phuff_entropy_ptr entropy
;
659 entropy
= (phuff_entropy_ptr
)
660 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
661 sizeof(phuff_entropy_decoder
));
662 cinfo
->entropy
= (struct jpeg_entropy_decoder
*)entropy
;
663 entropy
->pub
.start_pass
= start_pass_phuff_decoder
;
665 /* Mark derived tables unallocated */
666 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
667 entropy
->derived_tbls
[i
] = NULL
;
670 /* Create progression status table */
671 cinfo
->coef_bits
= (int (*)[DCTSIZE2
])
672 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
673 cinfo
->num_components
* 2 * DCTSIZE2
*
675 coef_bit_ptr
= &cinfo
->coef_bits
[0][0];
676 for (ci
= 0; ci
< cinfo
->num_components
; ci
++)
677 for (i
= 0; i
< DCTSIZE2
; i
++)
678 *coef_bit_ptr
++ = -1;
681 #endif /* D_PROGRESSIVE_SUPPORTED */