blob: 04b38d5d462cd3ce751b97e8b32c6450fe10eeb7 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdhuff.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains Huffman entropy decoding routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 *
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
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 * into local working storage, and update them back to the permanent
14 * storage only upon successful completion of an MCU.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015 */
16
DRCe2816642009-09-28 00:33:02 +000017/* Modifications:
18 * Copyright (C)2007 Sun Microsystems, Inc.
DRC0fbb28e2010-07-30 17:15:52 +000019 * Copyright (C)2009-2010 D. R. Commander
DRCe2816642009-09-28 00:33:02 +000020 *
21 * This library is free software and may be redistributed and/or modified under
22 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
23 * any later version. The full license is in the LICENSE.txt file included
24 * with this distribution.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * wxWindows Library License for more details.
30 */
31
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000033#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034#include "jpeglib.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000035#include "jdhuff.h" /* Declarations shared with jdphuff.c */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000036
37
Thomas G. Lanebc79e061995-08-02 00:00:00 +000038/*
39 * Expanded entropy decoder object for Huffman decoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040 *
41 * The savable_state subrecord contains fields that change within an MCU,
42 * but must not be updated permanently until we complete the MCU.
43 */
44
45typedef struct {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
47} savable_state;
48
49/* This macro is to work around compilers with missing or broken
50 * structure assignment. You'll need to fix this code if you have
51 * such a compiler and you change MAX_COMPS_IN_SCAN.
52 */
53
54#ifndef NO_STRUCT_ASSIGN
55#define ASSIGN_STATE(dest,src) ((dest) = (src))
56#else
57#if MAX_COMPS_IN_SCAN == 4
58#define ASSIGN_STATE(dest,src) \
Thomas G. Lanebc79e061995-08-02 00:00:00 +000059 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060 (dest).last_dc_val[1] = (src).last_dc_val[1], \
61 (dest).last_dc_val[2] = (src).last_dc_val[2], \
62 (dest).last_dc_val[3] = (src).last_dc_val[3])
63#endif
64#endif
65
66
67typedef struct {
68 struct jpeg_entropy_decoder pub; /* public fields */
69
Thomas G. Lanebc79e061995-08-02 00:00:00 +000070 /* These fields are loaded into local variables at start of each MCU.
71 * In case of suspension, we exit WITHOUT updating them.
72 */
73 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
74 savable_state saved; /* Other state at start of MCU */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075
76 /* These fields are NOT loaded into local working state. */
77 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078
79 /* Pointers to derived tables (these workspaces have image lifespan) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000080 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
81 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000082
83 /* Precalculated info set up by start_pass for use in decode_mcu: */
84
85 /* Pointers to derived tables to be used for each block within an MCU */
86 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
87 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
88 /* Whether we care about the DC and AC coefficient values for each block */
89 boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
90 boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091} huff_entropy_decoder;
92
93typedef huff_entropy_decoder * huff_entropy_ptr;
94
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095
96/*
97 * Initialize for a Huffman-compressed scan.
98 */
99
Thomas G. Lane489583f1996-02-07 00:00:00 +0000100METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101start_pass_huff_decoder (j_decompress_ptr cinfo)
102{
103 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000104 int ci, blkn, dctbl, actbl;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105 jpeg_component_info * compptr;
106
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000107 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
108 * This ought to be an error condition, but we make it a warning because
109 * there are some baseline files out there with all zeroes in these bytes.
110 */
111 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
112 cinfo->Ah != 0 || cinfo->Al != 0)
113 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
114
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
116 compptr = cinfo->cur_comp_info[ci];
117 dctbl = compptr->dc_tbl_no;
118 actbl = compptr->ac_tbl_no;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000119 /* Compute derived values for Huffman tables */
120 /* We may do this more than once for a table, but it's not expensive */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000121 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000122 & entropy->dc_derived_tbls[dctbl]);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000123 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 & entropy->ac_derived_tbls[actbl]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125 /* Initialize DC predictions to 0 */
126 entropy->saved.last_dc_val[ci] = 0;
127 }
128
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000129 /* Precalculate decoding info for each block in an MCU of this scan */
130 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
131 ci = cinfo->MCU_membership[blkn];
132 compptr = cinfo->cur_comp_info[ci];
133 /* Precalculate which table to use for each block */
134 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
135 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
136 /* Decide whether we really care about the coefficient values */
137 if (compptr->component_needed) {
138 entropy->dc_needed[blkn] = TRUE;
139 /* we don't need the ACs if producing a 1/8th-size image */
140 entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
141 } else {
142 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
143 }
144 }
145
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000146 /* Initialize bitread state variables */
147 entropy->bitstate.bits_left = 0;
148 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000149 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150
151 /* Initialize restart counter */
152 entropy->restarts_to_go = cinfo->restart_interval;
153}
154
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000155
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000156/*
157 * Compute the derived values for a Huffman table.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000158 * This routine also performs some validation checks on the table.
159 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160 * Note this is also used by jdphuff.c.
161 */
162
Thomas G. Lane489583f1996-02-07 00:00:00 +0000163GLOBAL(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000164jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000165 d_derived_tbl ** pdtbl)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000166{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000167 JHUFF_TBL *htbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000168 d_derived_tbl *dtbl;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000169 int p, i, l, si, numsymbols;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000170 int lookbits, ctr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000171 char huffsize[257];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172 unsigned int huffcode[257];
173 unsigned int code;
174
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000175 /* Note that huffsize[] and huffcode[] are filled in code-length order,
176 * paralleling the order of the symbols themselves in htbl->huffval[].
177 */
178
179 /* Find the input Huffman table */
180 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
182 htbl =
183 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
184 if (htbl == NULL)
185 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
186
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 /* Allocate a workspace if we haven't already done so. */
188 if (*pdtbl == NULL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000189 *pdtbl = (d_derived_tbl *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000191 SIZEOF(d_derived_tbl));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 dtbl = *pdtbl;
193 dtbl->pub = htbl; /* fill in back link */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000194
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000195 /* Figure C.1: make table of Huffman code length for each symbol */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000196
197 p = 0;
198 for (l = 1; l <= 16; l++) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000199 i = (int) htbl->bits[l];
200 if (i < 0 || p + i > 256) /* protect against table overrun */
201 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
202 while (i--)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000203 huffsize[p++] = (char) l;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000204 }
205 huffsize[p] = 0;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000206 numsymbols = p;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000207
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000208 /* Figure C.2: generate the codes themselves */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000209 /* We also validate that the counts represent a legal Huffman code tree. */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000210
211 code = 0;
212 si = huffsize[0];
213 p = 0;
214 while (huffsize[p]) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000215 while (((int) huffsize[p]) == si) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000216 huffcode[p++] = code;
217 code++;
218 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000219 /* code is now 1 more than the last code used for codelength si; but
220 * it must still fit in si bits, since no code is allowed to be all ones.
221 */
222 if (((INT32) code) >= (((INT32) 1) << si))
223 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000224 code <<= 1;
225 si++;
226 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000227
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000228 /* Figure F.15: generate decoding tables for bit-sequential decoding */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000229
230 p = 0;
231 for (l = 1; l <= 16; l++) {
232 if (htbl->bits[l]) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000233 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
234 * minus the minimum code of length l
235 */
236 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000237 p += htbl->bits[l];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000238 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000239 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000241 }
242 }
DRC0fbb28e2010-07-30 17:15:52 +0000243 dtbl->valoffset[17] = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000244 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000245
246 /* Compute lookahead tables to speed up decoding.
247 * First we set all the table entries to 0, indicating "too long";
248 * then we iterate through the Huffman codes that are short enough and
249 * fill in all the entries that correspond to bit sequences starting
250 * with that code.
251 */
252
DRCe2816642009-09-28 00:33:02 +0000253 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++)
254 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000255
256 p = 0;
257 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
258 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
259 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
260 /* Generate left-justified code followed by all possible bit sequences */
261 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
262 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
DRCe2816642009-09-28 00:33:02 +0000263 dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p];
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000264 lookbits++;
265 }
266 }
267 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000268
269 /* Validate symbols as being reasonable.
270 * For AC tables, we make no check, but accept all byte values 0..255.
271 * For DC tables, we require the symbols to be in range 0..15.
272 * (Tighter bounds could be applied depending on the data depth and mode,
273 * but this is sufficient to ensure safe decoding.)
274 */
275 if (isDC) {
276 for (i = 0; i < numsymbols; i++) {
277 int sym = htbl->huffval[i];
278 if (sym < 0 || sym > 15)
279 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
280 }
281 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000282}
283
284
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000285/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000286 * Out-of-line code for bit fetching (shared with jdphuff.c).
287 * See jdhuff.h for info about usage.
288 * Note: current values of get_buffer and bits_left are passed as parameters,
289 * but are returned in the corresponding fields of the state struct.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000290 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000291 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
292 * of get_buffer to be used. (On machines with wider words, an even larger
293 * buffer could be used.) However, on some machines 32-bit shifts are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000294 * quite slow and take time proportional to the number of places shifted.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000295 * (This is true with most PC compilers, for instance.) In this case it may
296 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000297 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000298 */
299
300#ifdef SLOW_SHIFT_32
301#define MIN_GET_BITS 15 /* minimum allowable value */
302#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000303#define MIN_GET_BITS (BIT_BUF_SIZE-7)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000304#endif
305
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000306
Thomas G. Lane489583f1996-02-07 00:00:00 +0000307GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000308jpeg_fill_bit_buffer (bitread_working_state * state,
309 register bit_buf_type get_buffer, register int bits_left,
310 int nbits)
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000311/* Load up the bit buffer to a depth of at least nbits */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000312{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313 /* Copy heavily used state fields into locals (hopefully registers) */
314 register const JOCTET * next_input_byte = state->next_input_byte;
315 register size_t bytes_in_buffer = state->bytes_in_buffer;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000316 j_decompress_ptr cinfo = state->cinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000318 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000319 /* (It is assumed that no request will be for more than that many bits.) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000320 /* We fail to do so only if we hit a marker or are forced to suspend. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000321
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000322 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
323 while (bits_left < MIN_GET_BITS) {
324 register int c;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000326 /* Attempt to read a byte */
327 if (bytes_in_buffer == 0) {
328 if (! (*cinfo->src->fill_input_buffer) (cinfo))
329 return FALSE;
330 next_input_byte = cinfo->src->next_input_byte;
331 bytes_in_buffer = cinfo->src->bytes_in_buffer;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000332 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000333 bytes_in_buffer--;
334 c = GETJOCTET(*next_input_byte++);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000335
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000336 /* If it's 0xFF, check and discard stuffed zero byte */
337 if (c == 0xFF) {
338 /* Loop here to discard any padding FF's on terminating marker,
339 * so that we can save a valid unread_marker value. NOTE: we will
340 * accept multiple FF's followed by a 0 as meaning a single FF data
341 * byte. This data pattern is not valid according to the standard.
342 */
343 do {
344 if (bytes_in_buffer == 0) {
345 if (! (*cinfo->src->fill_input_buffer) (cinfo))
346 return FALSE;
347 next_input_byte = cinfo->src->next_input_byte;
348 bytes_in_buffer = cinfo->src->bytes_in_buffer;
349 }
350 bytes_in_buffer--;
351 c = GETJOCTET(*next_input_byte++);
352 } while (c == 0xFF);
353
354 if (c == 0) {
355 /* Found FF/00, which represents an FF data byte */
356 c = 0xFF;
357 } else {
358 /* Oops, it's actually a marker indicating end of compressed data.
359 * Save the marker code for later use.
360 * Fine point: it might appear that we should save the marker into
361 * bitread working state, not straight into permanent state. But
362 * once we have hit a marker, we cannot need to suspend within the
363 * current MCU, because we will read no more bytes from the data
364 * source. So it is OK to update permanent state right away.
365 */
366 cinfo->unread_marker = c;
367 /* See if we need to insert some fake zero bits. */
368 goto no_more_bytes;
369 }
370 }
371
372 /* OK, load c into get_buffer */
373 get_buffer = (get_buffer << 8) | c;
374 bits_left += 8;
375 } /* end while */
376 } else {
377 no_more_bytes:
378 /* We get here if we've read the marker that terminates the compressed
379 * data segment. There should be enough bits in the buffer register
380 * to satisfy the request; if so, no problem.
381 */
382 if (nbits > bits_left) {
383 /* Uh-oh. Report corrupted data to user and stuff zeroes into
384 * the data stream, so that we can produce some kind of image.
385 * We use a nonvolatile flag to ensure that only one warning message
386 * appears per data segment.
387 */
388 if (! cinfo->entropy->insufficient_data) {
389 WARNMS(cinfo, JWRN_HIT_MARKER);
390 cinfo->entropy->insufficient_data = TRUE;
391 }
392 /* Fill the buffer with zero bits */
393 get_buffer <<= MIN_GET_BITS - bits_left;
394 bits_left = MIN_GET_BITS;
395 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000396 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397
398 /* Unload the local registers */
399 state->next_input_byte = next_input_byte;
400 state->bytes_in_buffer = bytes_in_buffer;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000401 state->get_buffer = get_buffer;
402 state->bits_left = bits_left;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403
404 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000405}
406
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000407
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000408/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000409 * Out-of-line code for Huffman code decoding.
410 * See jdhuff.h for info about usage.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000411 */
412
Thomas G. Lane489583f1996-02-07 00:00:00 +0000413GLOBAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000414jpeg_huff_decode (bitread_working_state * state,
415 register bit_buf_type get_buffer, register int bits_left,
416 d_derived_tbl * htbl, int min_bits)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000417{
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000418 register int l = min_bits;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000419 register INT32 code;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000420
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000421 /* HUFF_DECODE has determined that the code is at least min_bits */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000422 /* bits long, so fetch that many bits in one swoop. */
423
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000424 CHECK_BIT_BUFFER(*state, l, return -1);
425 code = GET_BITS(l);
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000426
427 /* Collect the rest of the Huffman code one bit at a time. */
428 /* This is per Figure F.16 in the JPEG spec. */
429
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430 while (code > htbl->maxcode[l]) {
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000431 code <<= 1;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000432 CHECK_BIT_BUFFER(*state, 1, return -1);
433 code |= GET_BITS(1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000434 l++;
435 }
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000436
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000437 /* Unload the local registers */
438 state->get_buffer = get_buffer;
439 state->bits_left = bits_left;
440
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000441 /* With garbage input we may reach the sentinel value l = 17. */
442
443 if (l > 16) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000444 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000445 return 0; /* fake a zero as the safest result */
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000446 }
447
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000448 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000449}
450
451
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000452/*
453 * Figure F.12: extend sign bit.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000454 * On some machines, a shift and add will be faster than a table lookup.
455 */
456
DRCe2816642009-09-28 00:33:02 +0000457#define AVOID_TABLES
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000458#ifdef AVOID_TABLES
459
DRCe2816642009-09-28 00:33:02 +0000460#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1)))
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000461
462#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000463
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000464#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000465
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000466static const int extend_test[16] = /* entry n is 2**(n-1) */
467 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
468 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000469
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000470static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
471 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
472 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
473 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
474 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000475
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000476#endif /* AVOID_TABLES */
477
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000478
479/*
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000480 * Check for a restart marker & resynchronize decoder.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000481 * Returns FALSE if must suspend.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000482 */
483
Thomas G. Lane489583f1996-02-07 00:00:00 +0000484LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000485process_restart (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000486{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000487 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
488 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000489
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000490 /* Throw away any unused bits remaining in bit buffer; */
491 /* include any full bytes in next_marker's count of discarded bytes */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
493 entropy->bitstate.bits_left = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000494
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000495 /* Advance past the RSTn marker */
496 if (! (*cinfo->marker->read_restart_marker) (cinfo))
497 return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000498
499 /* Re-initialize DC predictions to 0 */
500 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000501 entropy->saved.last_dc_val[ci] = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000502
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000503 /* Reset restart counter */
504 entropy->restarts_to_go = cinfo->restart_interval;
505
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000506 /* Reset out-of-data flag, unless read_restart_marker left us smack up
507 * against a marker. In that case we will end up treating the next data
508 * segment as empty, and we can avoid producing bogus output pixels by
509 * leaving the flag set.
510 */
511 if (cinfo->unread_marker == 0)
512 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000513
514 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000515}
516
517
DRCe2816642009-09-28 00:33:02 +0000518LOCAL(boolean)
519decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
520{
521 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
522 BITREAD_STATE_VARS;
523 int blkn;
524 savable_state state;
525 /* Outer loop handles each block in the MCU */
526
527 /* Load up working state */
528 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
529 ASSIGN_STATE(state, entropy->saved);
530
531 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
532 JBLOCKROW block = MCU_data[blkn];
533 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
534 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
535 register int s, k, r;
536
537 /* Decode a single block's worth of coefficients */
538
539 /* Section F.2.2.1: decode the DC coefficient difference */
540 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
541 if (s) {
542 CHECK_BIT_BUFFER(br_state, s, return FALSE);
543 r = GET_BITS(s);
544 s = HUFF_EXTEND(r, s);
545 }
546
547 if (entropy->dc_needed[blkn]) {
548 /* Convert DC difference to actual value, update last_dc_val */
549 int ci = cinfo->MCU_membership[blkn];
550 s += state.last_dc_val[ci];
551 state.last_dc_val[ci] = s;
552 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
553 (*block)[0] = (JCOEF) s;
554 }
555
556 if (entropy->ac_needed[blkn]) {
557
558 /* Section F.2.2.2: decode the AC coefficients */
559 /* Since zeroes are skipped, output area must be cleared beforehand */
560 for (k = 1; k < DCTSIZE2; k++) {
561 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
562
563 r = s >> 4;
564 s &= 15;
565
566 if (s) {
567 k += r;
568 CHECK_BIT_BUFFER(br_state, s, return FALSE);
569 r = GET_BITS(s);
570 s = HUFF_EXTEND(r, s);
571 /* Output coefficient in natural (dezigzagged) order.
572 * Note: the extra entries in jpeg_natural_order[] will save us
573 * if k >= DCTSIZE2, which could happen if the data is corrupted.
574 */
575 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
576 } else {
577 if (r != 15)
578 break;
579 k += 15;
580 }
581 }
582
583 } else {
584
585 /* Section F.2.2.2: decode the AC coefficients */
586 /* In this path we just discard the values */
587 for (k = 1; k < DCTSIZE2; k++) {
588 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
589
590 r = s >> 4;
591 s &= 15;
592
593 if (s) {
594 k += r;
595 CHECK_BIT_BUFFER(br_state, s, return FALSE);
596 DROP_BITS(s);
597 } else {
598 if (r != 15)
599 break;
600 k += 15;
601 }
602 }
603 }
604 }
605
606 /* Completed MCU, so update state */
607 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
608 ASSIGN_STATE(entropy->saved, state);
609 return TRUE;
610}
611
612
613/***************************************************************/
614
615#define ADD_BYTE { \
616 int val0 = *(buffer++); \
617 int val1 = *(buffer); \
618 \
619 bits_left += 8; \
620 get_buffer = (get_buffer << 8) | (val0); \
621 if (val0 == 0xFF) { \
622 buffer++; \
623 if (val1 != 0) { \
624 buffer -= 2; \
625 get_buffer &= ~0xFF; \
626 } \
627 } \
628}
629
630/***************************************************************/
631
DRC830d5fc2010-04-20 21:13:26 +0000632#if __WORDSIZE == 64 || defined(_WIN64)
DRCe2816642009-09-28 00:33:02 +0000633
634#define ENSURE_SHORT \
635 if (bits_left < 16) { \
636 ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE \
637 }
638
639#else
640
641#define ENSURE_SHORT if (bits_left < 16) { ADD_BYTE ADD_BYTE }
642
643#endif
644
645/***************************************************************/
646
647#define HUFF_DECODE_FAST(symbol, size, htbl) { \
648 ENSURE_SHORT \
649 symbol = PEEK_BITS(HUFF_LOOKAHEAD); \
650 symbol = htbl->lookup[symbol]; \
651 size = symbol >> 8; \
652 bits_left -= size; \
653 symbol = symbol & ((1 << HUFF_LOOKAHEAD) - 1); \
654 if (size == HUFF_LOOKAHEAD + 1) { \
655 symbol = (get_buffer >> bits_left) & ((1 << (size)) - 1); \
656 while (symbol > htbl->maxcode[size]) { \
657 symbol <<= 1; \
658 symbol |= GET_BITS(1); \
659 size++; \
660 } \
661 symbol = htbl->pub->huffval[ (int) (symbol + htbl->valoffset[size]) ]; \
662 } \
663}
664
665/***************************************************************/
666
667LOCAL(boolean)
668decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
669{
670 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
671 BITREAD_STATE_VARS;
672 JOCTET *buffer;
673 int blkn;
674 savable_state state;
675 /* Outer loop handles each block in the MCU */
676
677 /* Load up working state */
678 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
679 buffer = (JOCTET *) br_state.next_input_byte;
680 ASSIGN_STATE(state, entropy->saved);
681
682 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
683 JBLOCKROW block = MCU_data[blkn];
684 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
685 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
686 register int s, k, r, l;
687
688 HUFF_DECODE_FAST(s, l, dctbl);
689 if (s) {
690 ENSURE_SHORT
691 r = GET_BITS(s);
692 s = HUFF_EXTEND(r, s);
693 }
694
695 if (entropy->dc_needed[blkn]) {
696 int ci = cinfo->MCU_membership[blkn];
697 s += state.last_dc_val[ci];
698 state.last_dc_val[ci] = s;
699 (*block)[0] = (JCOEF) s;
700 }
701
702 if (entropy->ac_needed[blkn]) {
703
704 for (k = 1; k < DCTSIZE2; k++) {
705 HUFF_DECODE_FAST(s, l, actbl);
706 r = s >> 4;
707 s &= 15;
708
709 if (s) {
710 k += r;
711 ENSURE_SHORT
712 r = GET_BITS(s);
713 s = HUFF_EXTEND(r, s);
714 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
715 } else {
716 if (r != 15) break;
717 k += 15;
718 }
719 }
720
721 } else {
722
723 for (k = 1; k < DCTSIZE2; k++) {
724 HUFF_DECODE_FAST(s, l, actbl);
725 r = s >> 4;
726 s &= 15;
727
728 if (s) {
729 k += r;
730 ENSURE_SHORT
731 DROP_BITS(s);
732 } else {
733 if (r != 15) break;
734 k += 15;
735 }
736 }
737 }
738 }
739
740 br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte);
741 br_state.next_input_byte = buffer;
742 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
743 ASSIGN_STATE(entropy->saved, state);
744 return TRUE;
745}
746
747
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000748/*
749 * Decode and return one MCU's worth of Huffman-compressed coefficients.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000750 * The coefficients are reordered from zigzag order into natural array order,
751 * but are not dequantized.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000752 *
753 * The i'th block of the MCU is stored into the block pointed to by
754 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
755 * (Wholesale zeroing is usually a little faster than retail...)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000756 *
757 * Returns FALSE if data source requested suspension. In that case no
758 * changes have been made to permanent state. (Exception: some output
759 * coefficients may already have been assigned. This is harmless for
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000760 * this module, since we'll just re-assign them on the next call.)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000761 */
762
DRCe2816642009-09-28 00:33:02 +0000763#define BUFSIZE (DCTSIZE2 * 2)
764
Thomas G. Lane489583f1996-02-07 00:00:00 +0000765METHODDEF(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000766decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000767{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000768 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
DRC97f8ec42010-03-20 22:38:53 +0000769 int usefast = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000770
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000771 /* Process restart marker if needed; may have to suspend */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000772 if (cinfo->restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000773 if (entropy->restarts_to_go == 0)
774 if (! process_restart(cinfo))
775 return FALSE;
DRC97f8ec42010-03-20 22:38:53 +0000776 usefast = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000777 }
778
DRC97f8ec42010-03-20 22:38:53 +0000779 if (cinfo->src->bytes_in_buffer < BUFSIZE * cinfo->blocks_in_MCU)
780 usefast = 0;
781
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000782 /* If we've run out of data, just leave the MCU set to zeroes.
783 * This way, we return uniform gray for the remainder of the segment.
784 */
785 if (! entropy->pub.insufficient_data) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000786
DRC97f8ec42010-03-20 22:38:53 +0000787 if (usefast) {
DRCe2816642009-09-28 00:33:02 +0000788 if (!decode_mcu_fast(cinfo, MCU_data)) return FALSE;
789 }
790 else {
791 if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000792 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000793
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000794 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000795
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000796 /* Account for restart interval (no-op if not using restarts) */
797 entropy->restarts_to_go--;
798
799 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000800}
801
802
803/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000804 * Module initialization routine for Huffman entropy decoding.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000805 */
806
Thomas G. Lane489583f1996-02-07 00:00:00 +0000807GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000808jinit_huff_decoder (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000809{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000810 huff_entropy_ptr entropy;
811 int i;
812
813 entropy = (huff_entropy_ptr)
814 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
815 SIZEOF(huff_entropy_decoder));
816 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
817 entropy->pub.start_pass = start_pass_huff_decoder;
818 entropy->pub.decode_mcu = decode_mcu;
819
820 /* Mark tables unallocated */
821 for (i = 0; i < NUM_HUFF_TBLS; i++) {
822 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000823 }
824}