4 * Copyright (C) 1991-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains Huffman entropy decoding routines for sequential JPEG.
10 * Much of the complexity here has to do with supporting input suspension.
11 * If the data source module demands suspension, we want to be able to back
12 * up to the start of the current MCU. To do this, we copy state variables
13 * into local working storage, and update them back to the permanent
14 * storage only upon successful completion of an MCU.
17 #define JPEG_INTERNALS
20 #include "jlossy.h" /* Private declarations for lossy codec */
21 #include "jdhuff.h" /* Declarations shared with jd*huff.c */
25 * Private entropy decoder object for Huffman decoding.
27 * The savable_state subrecord contains fields that change within an MCU,
28 * but must not be updated permanently until we complete the MCU.
32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35 /* This macro is to work around compilers with missing or broken
36 * structure assignment. You'll need to fix this code if you have
37 * such a compiler and you change MAX_COMPS_IN_SCAN.
40 #ifndef NO_STRUCT_ASSIGN
41 #define ASSIGN_STATE(dest,src) ((dest) = (src))
43 #if MAX_COMPS_IN_SCAN == 4
44 #define ASSIGN_STATE(dest,src) \
45 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
46 (dest).last_dc_val[1] = (src).last_dc_val[1], \
47 (dest).last_dc_val[2] = (src).last_dc_val[2], \
48 (dest).last_dc_val[3] = (src).last_dc_val[3])
54 huffd_common_fields; /* Fields shared with other entropy decoders */
56 /* These fields are loaded into local variables at start of each MCU.
57 * In case of suspension, we exit WITHOUT updating them.
59 savable_state saved; /* Other state at start of MCU */
61 /* These fields are NOT loaded into local working state. */
62 unsigned int restarts_to_go; /* MCUs left in this restart interval */
64 /* Pointers to derived tables (these workspaces have image lifespan) */
65 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
66 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
68 /* Precalculated info set up by start_pass for use in decode_mcu: */
70 /* Pointers to derived tables to be used for each block within an MCU */
71 d_derived_tbl * dc_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
72 d_derived_tbl * ac_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
73 /* Whether we care about the DC and AC coefficient values for each block */
74 boolean dc_needed[D_MAX_DATA_UNITS_IN_MCU];
75 boolean ac_needed[D_MAX_DATA_UNITS_IN_MCU];
76 } shuff_entropy_decoder;
78 typedef shuff_entropy_decoder * shuff_entropy_ptr;
82 * Initialize for a Huffman-compressed scan.
86 start_pass_huff_decoder (j_decompress_ptr cinfo)
88 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
89 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
90 int ci, blkn, dctbl, actbl;
91 jpeg_component_info * compptr;
93 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
94 * This ought to be an error condition, but we make it a warning because
95 * there are some baseline files out there with all zeroes in these bytes.
97 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
98 cinfo->Ah != 0 || cinfo->Al != 0)
99 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
101 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
102 compptr = cinfo->cur_comp_info[ci];
103 dctbl = compptr->dc_tbl_no;
104 actbl = compptr->ac_tbl_no;
105 /* Compute derived values for Huffman tables */
106 /* We may do this more than once for a table, but it's not expensive */
107 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
108 & entropy->dc_derived_tbls[dctbl]);
109 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
110 & entropy->ac_derived_tbls[actbl]);
111 /* Initialize DC predictions to 0 */
112 entropy->saved.last_dc_val[ci] = 0;
115 /* Precalculate decoding info for each block in an MCU of this scan */
116 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
117 ci = cinfo->MCU_membership[blkn];
118 compptr = cinfo->cur_comp_info[ci];
119 /* Precalculate which table to use for each block */
120 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
121 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
122 /* Decide whether we really care about the coefficient values */
123 if (compptr->component_needed) {
124 entropy->dc_needed[blkn] = TRUE;
125 /* we don't need the ACs if producing a 1/8th-size image */
126 entropy->ac_needed[blkn] = (compptr->codec_data_unit > 1);
128 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
132 /* Initialize bitread state variables */
133 entropy->bitstate.bits_left = 0;
134 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
135 entropy->insufficient_data = FALSE;
137 /* Initialize restart counter */
138 entropy->restarts_to_go = cinfo->restart_interval;
143 * Figure F.12: extend sign bit.
144 * On some machines, a shift and add will be faster than a table lookup.
149 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
153 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
155 static const int extend_test[16] = /* entry n is 2**(n-1) */
156 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
157 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
159 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
160 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
161 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
162 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
163 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
165 #endif /* AVOID_TABLES */
169 * Check for a restart marker & resynchronize decoder.
170 * Returns FALSE if must suspend.
174 process_restart (j_decompress_ptr cinfo)
176 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
177 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
180 /* Throw away any unused bits remaining in bit buffer; */
181 /* include any full bytes in next_marker's count of discarded bytes */
182 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
183 entropy->bitstate.bits_left = 0;
185 /* Advance past the RSTn marker */
186 if (! (*cinfo->marker->read_restart_marker) (cinfo))
189 /* Re-initialize DC predictions to 0 */
190 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
191 entropy->saved.last_dc_val[ci] = 0;
193 /* Reset restart counter */
194 entropy->restarts_to_go = cinfo->restart_interval;
196 /* Reset out-of-data flag, unless read_restart_marker left us smack up
197 * against a marker. In that case we will end up treating the next data
198 * segment as empty, and we can avoid producing bogus output pixels by
199 * leaving the flag set.
201 if (cinfo->unread_marker == 0)
202 entropy->insufficient_data = FALSE;
209 * Decode and return one MCU's worth of Huffman-compressed coefficients.
210 * The coefficients are reordered from zigzag order into natural array order,
211 * but are not dequantized.
213 * The i'th block of the MCU is stored into the block pointed to by
214 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
215 * (Wholesale zeroing is usually a little faster than retail...)
217 * Returns FALSE if data source requested suspension. In that case no
218 * changes have been made to permanent state. (Exception: some output
219 * coefficients may already have been assigned. This is harmless for
220 * this module, since we'll just re-assign them on the next call.)
224 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
226 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
227 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
232 /* Process restart marker if needed; may have to suspend */
233 if (cinfo->restart_interval) {
234 if (entropy->restarts_to_go == 0)
235 if (! process_restart(cinfo))
239 /* If we've run out of data, just leave the MCU set to zeroes.
240 * This way, we return uniform gray for the remainder of the segment.
242 if (! entropy->insufficient_data) {
244 /* Load up working state */
245 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
246 ASSIGN_STATE(state, entropy->saved);
248 /* Outer loop handles each block in the MCU */
250 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
251 JBLOCKROW block = MCU_data[blkn];
252 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
253 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
254 register int s, k, r;
256 /* Decode a single block's worth of coefficients */
258 /* Section F.2.2.1: decode the DC coefficient difference */
259 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
261 CHECK_BIT_BUFFER(br_state, s, return FALSE);
263 s = HUFF_EXTEND(r, s);
266 if (entropy->dc_needed[blkn]) {
267 /* Convert DC difference to actual value, update last_dc_val */
268 int ci = cinfo->MCU_membership[blkn];
269 s += state.last_dc_val[ci];
270 state.last_dc_val[ci] = s;
271 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
272 (*block)[0] = (JCOEF) s;
275 if (entropy->ac_needed[blkn]) {
277 /* Section F.2.2.2: decode the AC coefficients */
278 /* Since zeroes are skipped, output area must be cleared beforehand */
279 for (k = 1; k < DCTSIZE2; k++) {
280 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
287 CHECK_BIT_BUFFER(br_state, s, return FALSE);
289 s = HUFF_EXTEND(r, s);
290 /* Output coefficient in natural (dezigzagged) order.
291 * Note: the extra entries in jpeg_natural_order[] will save us
292 * if k >= DCTSIZE2, which could happen if the data is corrupted.
294 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
304 /* Section F.2.2.2: decode the AC coefficients */
305 /* In this path we just discard the values */
306 for (k = 1; k < DCTSIZE2; k++) {
307 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
314 CHECK_BIT_BUFFER(br_state, s, return FALSE);
326 /* Completed MCU, so update state */
327 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
328 ASSIGN_STATE(entropy->saved, state);
331 /* Account for restart interval (no-op if not using restarts) */
332 entropy->restarts_to_go--;
339 * Module initialization routine for Huffman entropy decoding.
343 jinit_shuff_decoder (j_decompress_ptr cinfo)
345 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
346 shuff_entropy_ptr entropy;
349 entropy = (shuff_entropy_ptr)
350 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
351 SIZEOF(shuff_entropy_decoder));
352 lossyd->entropy_private = (void *) entropy;
353 lossyd->entropy_start_pass = start_pass_huff_decoder;
354 lossyd->entropy_decode_mcu = decode_mcu;
356 /* Mark tables unallocated */
357 for (i = 0; i < NUM_HUFF_TBLS; i++) {
358 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;