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 which are shared
9 * by the sequential, progressive and lossless decoders.
12 #define JPEG_INTERNALS
15 #include "jlossy.h" /* Private declarations for lossy codec */
16 #include "jlossls.h" /* Private declarations for lossless codec */
17 #include "jdhuff.h" /* Declarations shared with jd*huff.c */
21 * Compute the derived values for a Huffman table.
22 * This routine also performs some validation checks on the table.
26 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
27 d_derived_tbl ** pdtbl)
31 int p, i, l, si, numsymbols;
34 unsigned int huffcode[257];
37 /* Note that huffsize[] and huffcode[] are filled in code-length order,
38 * paralleling the order of the symbols themselves in htbl->huffval[].
41 /* Find the input Huffman table */
42 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
43 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
45 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
47 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
49 /* Allocate a workspace if we haven't already done so. */
51 *pdtbl = (d_derived_tbl *)
52 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
53 SIZEOF(d_derived_tbl));
55 dtbl->pub = htbl; /* fill in back link */
57 /* Figure C.1: make table of Huffman code length for each symbol */
60 for (l = 1; l <= 16; l++) {
61 i = (int) htbl->bits[l];
62 if (i < 0 || p + i > 256) /* protect against table overrun */
63 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
65 huffsize[p++] = (char) l;
70 /* Figure C.2: generate the codes themselves */
71 /* We also validate that the counts represent a legal Huffman code tree. */
77 while (((int) huffsize[p]) == si) {
81 /* code is now 1 more than the last code used for codelength si; but
82 * it must still fit in si bits, since no code is allowed to be all ones.
83 * BUG FIX 2001-09-03: Comparison must be >, not >=
85 if (((INT32) code) > (((INT32) 1) << si))
86 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
91 /* Figure F.15: generate decoding tables for bit-sequential decoding */
94 for (l = 1; l <= 16; l++) {
96 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
97 * minus the minimum code of length l
99 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
101 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
103 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
106 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
108 /* Compute lookahead tables to speed up decoding.
109 * First we set all the table entries to 0, indicating "too long";
110 * then we iterate through the Huffman codes that are short enough and
111 * fill in all the entries that correspond to bit sequences starting
115 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
118 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
119 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
120 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
121 /* Generate left-justified code followed by all possible bit sequences */
122 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
123 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
124 dtbl->look_nbits[lookbits] = l;
125 dtbl->look_sym[lookbits] = htbl->huffval[p];
131 /* Validate symbols as being reasonable.
132 * For AC tables, we make no check, but accept all byte values 0..255.
133 * For DC tables, we require the symbols to be in range 0..16.
134 * (Tighter bounds could be applied depending on the data depth and mode,
135 * but this is sufficient to ensure safe decoding.)
138 for (i = 0; i < numsymbols; i++) {
139 int sym = htbl->huffval[i];
140 if (sym < 0 || sym > 16)
141 /* The following is needed to be able to read certain Philips DICOM MRI images */
142 #if BITS_IN_JSAMPLE == 12
145 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
146 #endif /* BITS_IN_JSAMPLE == 12 */
153 * Out-of-line code for bit fetching.
154 * See jdhuff.h for info about usage.
155 * Note: current values of get_buffer and bits_left are passed as parameters,
156 * but are returned in the corresponding fields of the state struct.
158 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
159 * of get_buffer to be used. (On machines with wider words, an even larger
160 * buffer could be used.) However, on some machines 32-bit shifts are
161 * quite slow and take time proportional to the number of places shifted.
162 * (This is true with most PC compilers, for instance.) In this case it may
163 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
164 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
168 #define MIN_GET_BITS 15 /* minimum allowable value */
170 #define MIN_GET_BITS (BIT_BUF_SIZE-7)
175 jpeg_fill_bit_buffer (bitread_working_state * state,
176 register bit_buf_type get_buffer, register int bits_left,
178 /* Load up the bit buffer to a depth of at least nbits */
180 /* Copy heavily used state fields into locals (hopefully registers) */
181 register const JOCTET * next_input_byte = state->next_input_byte;
182 register size_t bytes_in_buffer = state->bytes_in_buffer;
183 j_decompress_ptr cinfo = state->cinfo;
185 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
186 /* (It is assumed that no request will be for more than that many bits.) */
187 /* We fail to do so only if we hit a marker or are forced to suspend. */
189 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
190 while (bits_left < MIN_GET_BITS) {
193 /* Attempt to read a byte */
194 if (bytes_in_buffer == 0) {
195 if (! (*cinfo->src->fill_input_buffer) (cinfo))
197 next_input_byte = cinfo->src->next_input_byte;
198 bytes_in_buffer = cinfo->src->bytes_in_buffer;
201 c = GETJOCTET(*next_input_byte++);
203 /* If it's 0xFF, check and discard stuffed zero byte */
205 /* Loop here to discard any padding FF's on terminating marker,
206 * so that we can save a valid unread_marker value. NOTE: we will
207 * accept multiple FF's followed by a 0 as meaning a single FF data
208 * byte. This data pattern is not valid according to the standard.
211 if (bytes_in_buffer == 0) {
212 if (! (*cinfo->src->fill_input_buffer) (cinfo))
214 next_input_byte = cinfo->src->next_input_byte;
215 bytes_in_buffer = cinfo->src->bytes_in_buffer;
218 c = GETJOCTET(*next_input_byte++);
222 /* Found FF/00, which represents an FF data byte */
225 /* Oops, it's actually a marker indicating end of compressed data.
226 * Save the marker code for later use.
227 * Fine point: it might appear that we should save the marker into
228 * bitread working state, not straight into permanent state. But
229 * once we have hit a marker, we cannot need to suspend within the
230 * current MCU, because we will read no more bytes from the data
231 * source. So it is OK to update permanent state right away.
233 cinfo->unread_marker = c;
234 /* See if we need to insert some fake zero bits. */
239 /* OK, load c into get_buffer */
240 get_buffer = (get_buffer << 8) | c;
245 /* We get here if we've read the marker that terminates the compressed
246 * data segment. There should be enough bits in the buffer register
247 * to satisfy the request; if so, no problem.
249 if (nbits > bits_left) {
250 /* Uh-oh. Report corrupted data to user and stuff zeroes into
251 * the data stream, so that we can produce some kind of image.
252 * We use a nonvolatile flag to ensure that only one warning message
253 * appears per data segment.
255 huffd_common_ptr huffd;
256 if (cinfo->process == JPROC_LOSSLESS)
257 huffd = (huffd_common_ptr) ((j_lossless_d_ptr) cinfo->codec)->entropy_private;
259 huffd = (huffd_common_ptr) ((j_lossy_d_ptr) cinfo->codec)->entropy_private;
260 if (! huffd->insufficient_data) {
261 WARNMS(cinfo, JWRN_HIT_MARKER);
262 huffd->insufficient_data = TRUE;
264 /* Fill the buffer with zero bits */
265 get_buffer <<= MIN_GET_BITS - bits_left;
266 bits_left = MIN_GET_BITS;
270 /* Unload the local registers */
271 state->next_input_byte = next_input_byte;
272 state->bytes_in_buffer = bytes_in_buffer;
273 state->get_buffer = get_buffer;
274 state->bits_left = bits_left;
281 * Out-of-line code for Huffman code decoding.
282 * See jdhuff.h for info about usage.
286 jpeg_huff_decode (bitread_working_state * state,
287 register bit_buf_type get_buffer, register int bits_left,
288 d_derived_tbl * htbl, int min_bits)
290 register int l = min_bits;
293 /* HUFF_DECODE has determined that the code is at least min_bits */
294 /* bits long, so fetch that many bits in one swoop. */
296 CHECK_BIT_BUFFER(*state, l, return -1);
299 /* Collect the rest of the Huffman code one bit at a time. */
300 /* This is per Figure F.16 in the JPEG spec. */
302 while (code > htbl->maxcode[l]) {
304 CHECK_BIT_BUFFER(*state, 1, return -1);
309 /* Unload the local registers */
310 state->get_buffer = get_buffer;
311 state->bits_left = bits_left;
313 /* With garbage input we may reach the sentinel value l = 17. */
316 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
317 return 0; /* fake a zero as the safest result */
320 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];