2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdhuff.c /main/2 1996/05/09 03:47:48 drk $ */
27 * Copyright (C) 1991-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains Huffman entropy decoding routines.
33 * Much of the complexity here has to do with supporting input suspension.
34 * If the data source module demands suspension, we want to be able to back
35 * up to the start of the current MCU. To do this, we copy state variables
36 * into local working storage, and update them back to the permanent
37 * storage only upon successful completion of an MCU.
40 #define JPEG_INTERNALS
43 #include "jdhuff.h" /* Declarations shared with jdphuff.c */
47 * Expanded entropy decoder object for Huffman decoding.
49 * The savable_state subrecord contains fields that change within an MCU,
50 * but must not be updated permanently until we complete the MCU.
54 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
57 /* This macro is to work around compilers with missing or broken
58 * structure assignment. You'll need to fix this code if you have
59 * such a compiler and you change MAX_COMPS_IN_SCAN.
62 #ifndef NO_STRUCT_ASSIGN
63 #define ASSIGN_STATE(dest,src) ((dest) = (src))
65 #if MAX_COMPS_IN_SCAN == 4
66 #define ASSIGN_STATE(dest,src) \
67 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
68 (dest).last_dc_val[1] = (src).last_dc_val[1], \
69 (dest).last_dc_val[2] = (src).last_dc_val[2], \
70 (dest).last_dc_val[3] = (src).last_dc_val[3])
76 struct jpeg_entropy_decoder pub; /* public fields */
78 /* These fields are loaded into local variables at start of each MCU.
79 * In case of suspension, we exit WITHOUT updating them.
81 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
82 savable_state saved; /* Other state at start of MCU */
84 /* These fields are NOT loaded into local working state. */
85 unsigned int restarts_to_go; /* MCUs left in this restart interval */
87 /* Pointers to derived tables (these workspaces have image lifespan) */
88 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
89 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
90 } huff_entropy_decoder;
92 typedef huff_entropy_decoder * huff_entropy_ptr;
96 * Initialize for a Huffman-compressed scan.
100 start_pass_huff_decoder (j_decompress_ptr cinfo)
102 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
103 int ci, dctbl, actbl;
104 jpeg_component_info * compptr;
106 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
107 * This ought to be an error condition, but we make it a warning because
108 * there are some baseline files out there with all zeroes in these bytes.
110 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
111 cinfo->Ah != 0 || cinfo->Al != 0)
112 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
114 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
115 compptr = cinfo->cur_comp_info[ci];
116 dctbl = compptr->dc_tbl_no;
117 actbl = compptr->ac_tbl_no;
118 /* Make sure requested tables are present */
119 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
120 cinfo->dc_huff_tbl_ptrs[dctbl] == NULL)
121 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
122 if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
123 cinfo->ac_huff_tbl_ptrs[actbl] == NULL)
124 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
125 /* Compute derived values for Huffman tables */
126 /* We may do this more than once for a table, but it's not expensive */
127 jpeg_make_d_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
128 & entropy->dc_derived_tbls[dctbl]);
129 jpeg_make_d_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
130 & entropy->ac_derived_tbls[actbl]);
131 /* Initialize DC predictions to 0 */
132 entropy->saved.last_dc_val[ci] = 0;
135 /* Initialize bitread state variables */
136 entropy->bitstate.bits_left = 0;
137 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
138 entropy->bitstate.printed_eod = FALSE;
140 /* Initialize restart counter */
141 entropy->restarts_to_go = cinfo->restart_interval;
146 * Compute the derived values for a Huffman table.
147 * Note this is also used by jdphuff.c.
151 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, JHUFF_TBL * htbl,
152 d_derived_tbl ** pdtbl)
158 unsigned int huffcode[257];
161 /* Allocate a workspace if we haven't already done so. */
163 *pdtbl = (d_derived_tbl *)
164 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
165 SIZEOF(d_derived_tbl));
167 dtbl->pub = htbl; /* fill in back link */
169 /* Figure C.1: make table of Huffman code length for each symbol */
170 /* Note that this is in code-length order. */
173 for (l = 1; l <= 16; l++) {
174 for (i = 1; i <= (int) htbl->bits[l]; i++)
175 huffsize[p++] = (char) l;
179 /* Figure C.2: generate the codes themselves */
180 /* Note that this is in code-length order. */
185 while (huffsize[p]) {
186 while (((int) huffsize[p]) == si) {
187 huffcode[p++] = code;
194 /* Figure F.15: generate decoding tables for bit-sequential decoding */
197 for (l = 1; l <= 16; l++) {
199 dtbl->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
200 dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
202 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
204 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
207 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
209 /* Compute lookahead tables to speed up decoding.
210 * First we set all the table entries to 0, indicating "too long";
211 * then we iterate through the Huffman codes that are short enough and
212 * fill in all the entries that correspond to bit sequences starting
216 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
219 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
220 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
221 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
222 /* Generate left-justified code followed by all possible bit sequences */
223 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
224 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
225 dtbl->look_nbits[lookbits] = l;
226 dtbl->look_sym[lookbits] = htbl->huffval[p];
235 * Out-of-line code for bit fetching (shared with jdphuff.c).
236 * See jdhuff.h for info about usage.
237 * Note: current values of get_buffer and bits_left are passed as parameters,
238 * but are returned in the corresponding fields of the state struct.
240 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
241 * of get_buffer to be used. (On machines with wider words, an even larger
242 * buffer could be used.) However, on some machines 32-bit shifts are
243 * quite slow and take time proportional to the number of places shifted.
244 * (This is true with most PC compilers, for instance.) In this case it may
245 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
246 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
250 #define MIN_GET_BITS 15 /* minimum allowable value */
252 #define MIN_GET_BITS (BIT_BUF_SIZE-7)
257 jpeg_fill_bit_buffer (bitread_working_state * state,
258 bit_buf_type get_buffer, int bits_left,
260 /* Load up the bit buffer to a depth of at least nbits */
262 /* Copy heavily used state fields into locals (hopefully registers) */
263 const JOCTET * next_input_byte = state->next_input_byte;
264 size_t bytes_in_buffer = state->bytes_in_buffer;
267 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
268 /* (It is assumed that no request will be for more than that many bits.) */
270 while (bits_left < MIN_GET_BITS) {
271 /* Attempt to read a byte */
272 if (state->unread_marker != 0)
273 goto no_more_data; /* can't advance past a marker */
275 if (bytes_in_buffer == 0) {
276 if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
278 next_input_byte = state->cinfo->src->next_input_byte;
279 bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
282 c = GETJOCTET(*next_input_byte++);
284 /* If it's 0xFF, check and discard stuffed zero byte */
287 if (bytes_in_buffer == 0) {
288 if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
290 next_input_byte = state->cinfo->src->next_input_byte;
291 bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
294 c = GETJOCTET(*next_input_byte++);
298 /* Found FF/00, which represents an FF data byte */
301 /* Oops, it's actually a marker indicating end of compressed data. */
302 /* Better put it back for use later */
303 state->unread_marker = c;
306 /* There should be enough bits still left in the data segment; */
307 /* if so, just break out of the outer while loop. */
308 if (bits_left >= nbits)
310 /* Uh-oh. Report corrupted data to user and stuff zeroes into
311 * the data stream, so that we can produce some kind of image.
312 * Note that this code will be repeated for each byte demanded
313 * for the rest of the segment. We use a nonvolatile flag to ensure
314 * that only one warning message appears.
316 if (! *(state->printed_eod_ptr)) {
317 WARNMS(state->cinfo, JWRN_HIT_MARKER);
318 *(state->printed_eod_ptr) = TRUE;
320 c = 0; /* insert a zero byte into bit buffer */
324 /* OK, load c into get_buffer */
325 get_buffer = (get_buffer << 8) | c;
329 /* Unload the local registers */
330 state->next_input_byte = next_input_byte;
331 state->bytes_in_buffer = bytes_in_buffer;
332 state->get_buffer = get_buffer;
333 state->bits_left = bits_left;
340 * Out-of-line code for Huffman code decoding.
341 * See jdhuff.h for info about usage.
345 jpeg_huff_decode (bitread_working_state * state,
346 bit_buf_type get_buffer, int bits_left,
347 d_derived_tbl * htbl, int min_bits)
352 /* HUFF_DECODE has determined that the code is at least min_bits */
353 /* bits long, so fetch that many bits in one swoop. */
355 CHECK_BIT_BUFFER(*state, l, return -1);
358 /* Collect the rest of the Huffman code one bit at a time. */
359 /* This is per Figure F.16 in the JPEG spec. */
361 while (code > htbl->maxcode[l]) {
363 CHECK_BIT_BUFFER(*state, 1, return -1);
368 /* Unload the local registers */
369 state->get_buffer = get_buffer;
370 state->bits_left = bits_left;
372 /* With garbage input we may reach the sentinel value l = 17. */
375 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
376 return 0; /* fake a zero as the safest result */
379 return htbl->pub->huffval[ htbl->valptr[l] +
380 ((int) (code - htbl->mincode[l])) ];
385 * Figure F.12: extend sign bit.
386 * On some machines, a shift and add will be faster than a table lookup.
391 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
395 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
397 static const int extend_test[16] = /* entry n is 2**(n-1) */
398 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
399 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
401 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
402 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
403 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
404 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
405 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
407 #endif /* AVOID_TABLES */
411 * Check for a restart marker & resynchronize decoder.
412 * Returns FALSE if must suspend.
416 process_restart (j_decompress_ptr cinfo)
418 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
421 /* Throw away any unused bits remaining in bit buffer; */
422 /* include any full bytes in next_marker's count of discarded bytes */
423 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
424 entropy->bitstate.bits_left = 0;
426 /* Advance past the RSTn marker */
427 if (! (*cinfo->marker->read_restart_marker) (cinfo))
430 /* Re-initialize DC predictions to 0 */
431 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
432 entropy->saved.last_dc_val[ci] = 0;
434 /* Reset restart counter */
435 entropy->restarts_to_go = cinfo->restart_interval;
437 /* Next segment can get another out-of-data warning */
438 entropy->bitstate.printed_eod = FALSE;
445 * Decode and return one MCU's worth of Huffman-compressed coefficients.
446 * The coefficients are reordered from zigzag order into natural array order,
447 * but are not dequantized.
449 * The i'th block of the MCU is stored into the block pointed to by
450 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
451 * (Wholesale zeroing is usually a little faster than retail...)
453 * Returns FALSE if data source requested suspension. In that case no
454 * changes have been made to permanent state. (Exception: some output
455 * coefficients may already have been assigned. This is harmless for
456 * this module, since we'll just re-assign them on the next call.)
460 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
462 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
468 d_derived_tbl * dctbl;
469 d_derived_tbl * actbl;
470 jpeg_component_info * compptr;
472 /* Process restart marker if needed; may have to suspend */
473 if (cinfo->restart_interval) {
474 if (entropy->restarts_to_go == 0)
475 if (! process_restart(cinfo))
479 /* Load up working state */
480 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
481 ASSIGN_STATE(state, entropy->saved);
483 /* Outer loop handles each block in the MCU */
485 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
486 block = MCU_data[blkn];
487 ci = cinfo->MCU_membership[blkn];
488 compptr = cinfo->cur_comp_info[ci];
489 dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
490 actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
492 /* Decode a single block's worth of coefficients */
494 /* Section F.2.2.1: decode the DC coefficient difference */
495 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
497 CHECK_BIT_BUFFER(br_state, s, return FALSE);
499 s = HUFF_EXTEND(r, s);
502 /* Shortcut if component's values are not interesting */
503 if (! compptr->component_needed)
506 /* Convert DC difference to actual value, update last_dc_val */
507 s += state.last_dc_val[ci];
508 state.last_dc_val[ci] = s;
509 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
510 (*block)[0] = (JCOEF) s;
512 /* Do we need to decode the AC coefficients for this component? */
513 if (compptr->DCT_scaled_size > 1) {
515 /* Section F.2.2.2: decode the AC coefficients */
516 /* Since zeroes are skipped, output area must be cleared beforehand */
517 for (k = 1; k < DCTSIZE2; k++) {
518 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
525 CHECK_BIT_BUFFER(br_state, s, return FALSE);
527 s = HUFF_EXTEND(r, s);
528 /* Output coefficient in natural (dezigzagged) order.
529 * Note: the extra entries in jpeg_natural_order[] will save us
530 * if k >= DCTSIZE2, which could happen if the data is corrupted.
532 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
543 /* Section F.2.2.2: decode the AC coefficients */
544 /* In this path we just discard the values */
545 for (k = 1; k < DCTSIZE2; k++) {
546 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
553 CHECK_BIT_BUFFER(br_state, s, return FALSE);
565 /* Completed MCU, so update state */
566 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
567 ASSIGN_STATE(entropy->saved, state);
569 /* Account for restart interval (no-op if not using restarts) */
570 entropy->restarts_to_go--;
577 * Module initialization routine for Huffman entropy decoding.
581 jinit_huff_decoder (j_decompress_ptr cinfo)
583 huff_entropy_ptr entropy;
586 entropy = (huff_entropy_ptr)
587 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
588 SIZEOF(huff_entropy_decoder));
589 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
590 entropy->pub.start_pass = start_pass_huff_decoder;
591 entropy->pub.decode_mcu = decode_mcu;
593 /* Mark tables unallocated */
594 for (i = 0; i < NUM_HUFF_TBLS; i++) {
595 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;