blob: dba5f18a6c46ab2a3ac5ee3913fdc2c38c4366b1 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdhuff.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRCbc515802011-04-18 06:52:07 +00007 * Copyright (C) 2009-2011, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains Huffman entropy decoding routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * Much of the complexity here has to do with supporting input suspension.
13 * If the data source module demands suspension, we want to be able to back
14 * up to the start of the current MCU. To do this, we copy state variables
Thomas G. Lanebc79e061995-08-02 00:00:00 +000015 * into local working storage, and update them back to the permanent
16 * storage only upon successful completion of an MCU.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000017 */
18
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000020#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000021#include "jpeglib.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000022#include "jdhuff.h" /* Declarations shared with jdphuff.c */
DRC36a6eec2010-10-08 08:05:44 +000023#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000024
25
Thomas G. Lanebc79e061995-08-02 00:00:00 +000026/*
27 * Expanded entropy decoder object for Huffman decoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028 *
29 * The savable_state subrecord contains fields that change within an MCU,
30 * but must not be updated permanently until we complete the MCU.
31 */
32
33typedef struct {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35} savable_state;
36
37/* This macro is to work around compilers with missing or broken
38 * structure assignment. You'll need to fix this code if you have
39 * such a compiler and you change MAX_COMPS_IN_SCAN.
40 */
41
42#ifndef NO_STRUCT_ASSIGN
43#define ASSIGN_STATE(dest,src) ((dest) = (src))
44#else
45#if MAX_COMPS_IN_SCAN == 4
46#define ASSIGN_STATE(dest,src) \
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048 (dest).last_dc_val[1] = (src).last_dc_val[1], \
49 (dest).last_dc_val[2] = (src).last_dc_val[2], \
50 (dest).last_dc_val[3] = (src).last_dc_val[3])
51#endif
52#endif
53
54
55typedef struct {
56 struct jpeg_entropy_decoder pub; /* public fields */
57
Thomas G. Lanebc79e061995-08-02 00:00:00 +000058 /* These fields are loaded into local variables at start of each MCU.
59 * In case of suspension, we exit WITHOUT updating them.
60 */
61 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
62 savable_state saved; /* Other state at start of MCU */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063
64 /* These fields are NOT loaded into local working state. */
65 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066
67 /* Pointers to derived tables (these workspaces have image lifespan) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000068 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
69 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000070
71 /* Precalculated info set up by start_pass for use in decode_mcu: */
72
73 /* Pointers to derived tables to be used for each block within an MCU */
74 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
75 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
76 /* Whether we care about the DC and AC coefficient values for each block */
77 boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
78 boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079} huff_entropy_decoder;
80
81typedef huff_entropy_decoder * huff_entropy_ptr;
82
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083
84/*
85 * Initialize for a Huffman-compressed scan.
86 */
87
Thomas G. Lane489583f1996-02-07 00:00:00 +000088METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089start_pass_huff_decoder (j_decompress_ptr cinfo)
90{
91 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000092 int ci, blkn, dctbl, actbl;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000093 jpeg_component_info * compptr;
94
Thomas G. Lanebc79e061995-08-02 00:00:00 +000095 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
96 * This ought to be an error condition, but we make it a warning because
97 * there are some baseline files out there with all zeroes in these bytes.
98 */
99 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
100 cinfo->Ah != 0 || cinfo->Al != 0)
101 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
102
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
104 compptr = cinfo->cur_comp_info[ci];
105 dctbl = compptr->dc_tbl_no;
106 actbl = compptr->ac_tbl_no;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107 /* Compute derived values for Huffman tables */
108 /* We may do this more than once for a table, but it's not expensive */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000109 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000110 & entropy->dc_derived_tbls[dctbl]);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000111 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000112 & entropy->ac_derived_tbls[actbl]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113 /* Initialize DC predictions to 0 */
114 entropy->saved.last_dc_val[ci] = 0;
115 }
116
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000117 /* Precalculate decoding info for each block in an MCU of this scan */
118 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
119 ci = cinfo->MCU_membership[blkn];
120 compptr = cinfo->cur_comp_info[ci];
121 /* Precalculate which table to use for each block */
122 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
123 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
124 /* Decide whether we really care about the coefficient values */
125 if (compptr->component_needed) {
126 entropy->dc_needed[blkn] = TRUE;
127 /* we don't need the ACs if producing a 1/8th-size image */
DRC49967cd2010-10-09 19:57:51 +0000128 entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000129 } else {
130 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
131 }
132 }
133
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000134 /* Initialize bitread state variables */
135 entropy->bitstate.bits_left = 0;
136 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000137 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138
139 /* Initialize restart counter */
140 entropy->restarts_to_go = cinfo->restart_interval;
141}
142
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000143
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000144/*
145 * Compute the derived values for a Huffman table.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000146 * This routine also performs some validation checks on the table.
147 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000148 * Note this is also used by jdphuff.c.
149 */
150
Thomas G. Lane489583f1996-02-07 00:00:00 +0000151GLOBAL(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000152jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000153 d_derived_tbl ** pdtbl)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000154{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000155 JHUFF_TBL *htbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000156 d_derived_tbl *dtbl;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000157 int p, i, l, si, numsymbols;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000158 int lookbits, ctr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000159 char huffsize[257];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 unsigned int huffcode[257];
161 unsigned int code;
162
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000163 /* Note that huffsize[] and huffcode[] are filled in code-length order,
164 * paralleling the order of the symbols themselves in htbl->huffval[].
165 */
166
167 /* Find the input Huffman table */
168 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
169 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
170 htbl =
171 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
172 if (htbl == NULL)
173 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
174
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175 /* Allocate a workspace if we haven't already done so. */
176 if (*pdtbl == NULL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000177 *pdtbl = (d_derived_tbl *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000179 SIZEOF(d_derived_tbl));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180 dtbl = *pdtbl;
181 dtbl->pub = htbl; /* fill in back link */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000182
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000183 /* Figure C.1: make table of Huffman code length for each symbol */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000184
185 p = 0;
186 for (l = 1; l <= 16; l++) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000187 i = (int) htbl->bits[l];
188 if (i < 0 || p + i > 256) /* protect against table overrun */
189 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
190 while (i--)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000191 huffsize[p++] = (char) l;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000192 }
193 huffsize[p] = 0;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000194 numsymbols = p;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000195
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000196 /* Figure C.2: generate the codes themselves */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000197 /* We also validate that the counts represent a legal Huffman code tree. */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000198
199 code = 0;
200 si = huffsize[0];
201 p = 0;
202 while (huffsize[p]) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000203 while (((int) huffsize[p]) == si) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000204 huffcode[p++] = code;
205 code++;
206 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000207 /* code is now 1 more than the last code used for codelength si; but
208 * it must still fit in si bits, since no code is allowed to be all ones.
209 */
210 if (((INT32) code) >= (((INT32) 1) << si))
211 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000212 code <<= 1;
213 si++;
214 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000215
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000216 /* Figure F.15: generate decoding tables for bit-sequential decoding */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000217
218 p = 0;
219 for (l = 1; l <= 16; l++) {
220 if (htbl->bits[l]) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000221 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
222 * minus the minimum code of length l
223 */
224 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000225 p += htbl->bits[l];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000227 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000229 }
230 }
DRC0fbb28e2010-07-30 17:15:52 +0000231 dtbl->valoffset[17] = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000232 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000233
234 /* Compute lookahead tables to speed up decoding.
235 * First we set all the table entries to 0, indicating "too long";
236 * then we iterate through the Huffman codes that are short enough and
237 * fill in all the entries that correspond to bit sequences starting
238 * with that code.
239 */
240
DRCe2816642009-09-28 00:33:02 +0000241 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++)
242 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000243
244 p = 0;
245 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
246 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
247 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
248 /* Generate left-justified code followed by all possible bit sequences */
249 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
250 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
DRCe2816642009-09-28 00:33:02 +0000251 dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p];
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000252 lookbits++;
253 }
254 }
255 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000256
257 /* Validate symbols as being reasonable.
258 * For AC tables, we make no check, but accept all byte values 0..255.
259 * For DC tables, we require the symbols to be in range 0..15.
260 * (Tighter bounds could be applied depending on the data depth and mode,
261 * but this is sufficient to ensure safe decoding.)
262 */
263 if (isDC) {
264 for (i = 0; i < numsymbols; i++) {
265 int sym = htbl->huffval[i];
266 if (sym < 0 || sym > 15)
267 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
268 }
269 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000270}
271
272
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000273/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000274 * Out-of-line code for bit fetching (shared with jdphuff.c).
275 * See jdhuff.h for info about usage.
276 * Note: current values of get_buffer and bits_left are passed as parameters,
277 * but are returned in the corresponding fields of the state struct.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000278 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000279 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
280 * of get_buffer to be used. (On machines with wider words, an even larger
281 * buffer could be used.) However, on some machines 32-bit shifts are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282 * quite slow and take time proportional to the number of places shifted.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000283 * (This is true with most PC compilers, for instance.) In this case it may
284 * 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 +0000285 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000286 */
287
288#ifdef SLOW_SHIFT_32
289#define MIN_GET_BITS 15 /* minimum allowable value */
290#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000291#define MIN_GET_BITS (BIT_BUF_SIZE-7)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000292#endif
293
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000294
Thomas G. Lane489583f1996-02-07 00:00:00 +0000295GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000296jpeg_fill_bit_buffer (bitread_working_state * state,
297 register bit_buf_type get_buffer, register int bits_left,
298 int nbits)
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000299/* Load up the bit buffer to a depth of at least nbits */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000300{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 /* Copy heavily used state fields into locals (hopefully registers) */
302 register const JOCTET * next_input_byte = state->next_input_byte;
303 register size_t bytes_in_buffer = state->bytes_in_buffer;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000304 j_decompress_ptr cinfo = state->cinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000306 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000307 /* (It is assumed that no request will be for more than that many bits.) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000308 /* We fail to do so only if we hit a marker or are forced to suspend. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000310 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
311 while (bits_left < MIN_GET_BITS) {
312 register int c;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000314 /* Attempt to read a byte */
315 if (bytes_in_buffer == 0) {
316 if (! (*cinfo->src->fill_input_buffer) (cinfo))
317 return FALSE;
318 next_input_byte = cinfo->src->next_input_byte;
319 bytes_in_buffer = cinfo->src->bytes_in_buffer;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000320 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000321 bytes_in_buffer--;
322 c = GETJOCTET(*next_input_byte++);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000323
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000324 /* If it's 0xFF, check and discard stuffed zero byte */
325 if (c == 0xFF) {
326 /* Loop here to discard any padding FF's on terminating marker,
327 * so that we can save a valid unread_marker value. NOTE: we will
328 * accept multiple FF's followed by a 0 as meaning a single FF data
329 * byte. This data pattern is not valid according to the standard.
330 */
331 do {
332 if (bytes_in_buffer == 0) {
333 if (! (*cinfo->src->fill_input_buffer) (cinfo))
334 return FALSE;
335 next_input_byte = cinfo->src->next_input_byte;
336 bytes_in_buffer = cinfo->src->bytes_in_buffer;
337 }
338 bytes_in_buffer--;
339 c = GETJOCTET(*next_input_byte++);
340 } while (c == 0xFF);
341
342 if (c == 0) {
343 /* Found FF/00, which represents an FF data byte */
344 c = 0xFF;
345 } else {
346 /* Oops, it's actually a marker indicating end of compressed data.
347 * Save the marker code for later use.
348 * Fine point: it might appear that we should save the marker into
349 * bitread working state, not straight into permanent state. But
350 * once we have hit a marker, we cannot need to suspend within the
351 * current MCU, because we will read no more bytes from the data
352 * source. So it is OK to update permanent state right away.
353 */
354 cinfo->unread_marker = c;
355 /* See if we need to insert some fake zero bits. */
356 goto no_more_bytes;
357 }
358 }
359
360 /* OK, load c into get_buffer */
361 get_buffer = (get_buffer << 8) | c;
362 bits_left += 8;
363 } /* end while */
364 } else {
365 no_more_bytes:
366 /* We get here if we've read the marker that terminates the compressed
367 * data segment. There should be enough bits in the buffer register
368 * to satisfy the request; if so, no problem.
369 */
370 if (nbits > bits_left) {
371 /* Uh-oh. Report corrupted data to user and stuff zeroes into
372 * the data stream, so that we can produce some kind of image.
373 * We use a nonvolatile flag to ensure that only one warning message
374 * appears per data segment.
375 */
376 if (! cinfo->entropy->insufficient_data) {
377 WARNMS(cinfo, JWRN_HIT_MARKER);
378 cinfo->entropy->insufficient_data = TRUE;
379 }
380 /* Fill the buffer with zero bits */
381 get_buffer <<= MIN_GET_BITS - bits_left;
382 bits_left = MIN_GET_BITS;
383 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000384 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000385
386 /* Unload the local registers */
387 state->next_input_byte = next_input_byte;
388 state->bytes_in_buffer = bytes_in_buffer;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000389 state->get_buffer = get_buffer;
390 state->bits_left = bits_left;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391
392 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000393}
394
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000395
DRCbc515802011-04-18 06:52:07 +0000396/* Macro version of the above, which performs much better but does not
397 handle markers. We have to hand off any blocks with markers to the
398 slower routines. */
399
400#define GET_BYTE \
401{ \
402 register int c0, c1; \
403 c0 = GETJOCTET(*buffer++); \
404 c1 = GETJOCTET(*buffer); \
405 /* Pre-execute most common case */ \
406 get_buffer = (get_buffer << 8) | c0; \
407 bits_left += 8; \
408 if (c0 == 0xFF) { \
409 /* Pre-execute case of FF/00, which represents an FF data byte */ \
410 buffer++; \
411 if (c1 != 0) { \
412 /* Oops, it's actually a marker indicating end of compressed data. */ \
413 cinfo->unread_marker = c1; \
414 /* Back out pre-execution and fill the buffer with zero bits */ \
415 buffer -= 2; \
416 get_buffer &= ~0xFF; \
417 } \
418 } \
419}
420
421#if __WORDSIZE == 64 || defined(_WIN64)
422
423/* Pre-fetch 48 bytes, because the holding register is 64-bit */
424#define FILL_BIT_BUFFER_FAST \
425 if (bits_left < 16) { \
426 GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \
427 }
428
429#else
430
431/* Pre-fetch 16 bytes, because the holding register is 32-bit */
432#define FILL_BIT_BUFFER_FAST \
433 if (bits_left < 16) { \
434 GET_BYTE GET_BYTE \
435 }
436
437#endif
438
439
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000440/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000441 * Out-of-line code for Huffman code decoding.
442 * See jdhuff.h for info about usage.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000443 */
444
Thomas G. Lane489583f1996-02-07 00:00:00 +0000445GLOBAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000446jpeg_huff_decode (bitread_working_state * state,
447 register bit_buf_type get_buffer, register int bits_left,
448 d_derived_tbl * htbl, int min_bits)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000449{
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000450 register int l = min_bits;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000451 register INT32 code;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000452
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000453 /* HUFF_DECODE has determined that the code is at least min_bits */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000454 /* bits long, so fetch that many bits in one swoop. */
455
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000456 CHECK_BIT_BUFFER(*state, l, return -1);
457 code = GET_BITS(l);
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000458
459 /* Collect the rest of the Huffman code one bit at a time. */
460 /* This is per Figure F.16 in the JPEG spec. */
461
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000462 while (code > htbl->maxcode[l]) {
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000463 code <<= 1;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000464 CHECK_BIT_BUFFER(*state, 1, return -1);
465 code |= GET_BITS(1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000466 l++;
467 }
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000468
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000469 /* Unload the local registers */
470 state->get_buffer = get_buffer;
471 state->bits_left = bits_left;
472
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000473 /* With garbage input we may reach the sentinel value l = 17. */
474
475 if (l > 16) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000476 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000477 return 0; /* fake a zero as the safest result */
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000478 }
479
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000480 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000481}
482
483
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000484/*
485 * Figure F.12: extend sign bit.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000486 * On some machines, a shift and add will be faster than a table lookup.
487 */
488
DRCe2816642009-09-28 00:33:02 +0000489#define AVOID_TABLES
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000490#ifdef AVOID_TABLES
491
DRCe2816642009-09-28 00:33:02 +0000492#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1)))
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000493
494#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000495
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000496#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000497
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000498static const int extend_test[16] = /* entry n is 2**(n-1) */
499 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
500 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000501
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000502static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
503 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
504 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
505 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
506 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000507
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000508#endif /* AVOID_TABLES */
509
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000510
511/*
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000512 * Check for a restart marker & resynchronize decoder.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000513 * Returns FALSE if must suspend.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000514 */
515
Thomas G. Lane489583f1996-02-07 00:00:00 +0000516LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000517process_restart (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000518{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000519 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
520 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000521
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000522 /* Throw away any unused bits remaining in bit buffer; */
523 /* include any full bytes in next_marker's count of discarded bytes */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000524 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
525 entropy->bitstate.bits_left = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000526
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 /* Advance past the RSTn marker */
528 if (! (*cinfo->marker->read_restart_marker) (cinfo))
529 return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000530
531 /* Re-initialize DC predictions to 0 */
532 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000533 entropy->saved.last_dc_val[ci] = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000534
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000535 /* Reset restart counter */
536 entropy->restarts_to_go = cinfo->restart_interval;
537
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000538 /* Reset out-of-data flag, unless read_restart_marker left us smack up
539 * against a marker. In that case we will end up treating the next data
540 * segment as empty, and we can avoid producing bogus output pixels by
541 * leaving the flag set.
542 */
543 if (cinfo->unread_marker == 0)
544 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000545
546 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000547}
548
549
DRCe2816642009-09-28 00:33:02 +0000550LOCAL(boolean)
551decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
552{
553 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
554 BITREAD_STATE_VARS;
555 int blkn;
556 savable_state state;
557 /* Outer loop handles each block in the MCU */
558
559 /* Load up working state */
560 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
561 ASSIGN_STATE(state, entropy->saved);
562
563 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
564 JBLOCKROW block = MCU_data[blkn];
565 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
566 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
567 register int s, k, r;
568
569 /* Decode a single block's worth of coefficients */
570
571 /* Section F.2.2.1: decode the DC coefficient difference */
572 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
573 if (s) {
574 CHECK_BIT_BUFFER(br_state, s, return FALSE);
575 r = GET_BITS(s);
576 s = HUFF_EXTEND(r, s);
577 }
578
579 if (entropy->dc_needed[blkn]) {
580 /* Convert DC difference to actual value, update last_dc_val */
581 int ci = cinfo->MCU_membership[blkn];
582 s += state.last_dc_val[ci];
583 state.last_dc_val[ci] = s;
584 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
585 (*block)[0] = (JCOEF) s;
586 }
587
588 if (entropy->ac_needed[blkn]) {
589
590 /* Section F.2.2.2: decode the AC coefficients */
591 /* Since zeroes are skipped, output area must be cleared beforehand */
592 for (k = 1; k < DCTSIZE2; k++) {
593 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
594
595 r = s >> 4;
596 s &= 15;
597
598 if (s) {
599 k += r;
600 CHECK_BIT_BUFFER(br_state, s, return FALSE);
601 r = GET_BITS(s);
602 s = HUFF_EXTEND(r, s);
603 /* Output coefficient in natural (dezigzagged) order.
604 * Note: the extra entries in jpeg_natural_order[] will save us
605 * if k >= DCTSIZE2, which could happen if the data is corrupted.
606 */
607 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
608 } else {
609 if (r != 15)
610 break;
611 k += 15;
612 }
613 }
614
615 } else {
616
617 /* Section F.2.2.2: decode the AC coefficients */
618 /* In this path we just discard the values */
619 for (k = 1; k < DCTSIZE2; k++) {
620 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
621
622 r = s >> 4;
623 s &= 15;
624
625 if (s) {
626 k += r;
627 CHECK_BIT_BUFFER(br_state, s, return FALSE);
628 DROP_BITS(s);
629 } else {
630 if (r != 15)
631 break;
632 k += 15;
633 }
634 }
635 }
636 }
637
638 /* Completed MCU, so update state */
639 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
640 ASSIGN_STATE(entropy->saved, state);
641 return TRUE;
642}
643
644
DRCe2816642009-09-28 00:33:02 +0000645LOCAL(boolean)
646decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
647{
648 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
649 BITREAD_STATE_VARS;
650 JOCTET *buffer;
651 int blkn;
652 savable_state state;
653 /* Outer loop handles each block in the MCU */
654
655 /* Load up working state */
656 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
657 buffer = (JOCTET *) br_state.next_input_byte;
658 ASSIGN_STATE(state, entropy->saved);
659
660 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
661 JBLOCKROW block = MCU_data[blkn];
662 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
663 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
664 register int s, k, r, l;
665
666 HUFF_DECODE_FAST(s, l, dctbl);
667 if (s) {
DRCbc515802011-04-18 06:52:07 +0000668 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000669 r = GET_BITS(s);
670 s = HUFF_EXTEND(r, s);
671 }
672
673 if (entropy->dc_needed[blkn]) {
674 int ci = cinfo->MCU_membership[blkn];
675 s += state.last_dc_val[ci];
676 state.last_dc_val[ci] = s;
677 (*block)[0] = (JCOEF) s;
678 }
679
680 if (entropy->ac_needed[blkn]) {
681
682 for (k = 1; k < DCTSIZE2; k++) {
683 HUFF_DECODE_FAST(s, l, actbl);
684 r = s >> 4;
685 s &= 15;
686
687 if (s) {
688 k += r;
DRCbc515802011-04-18 06:52:07 +0000689 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000690 r = GET_BITS(s);
691 s = HUFF_EXTEND(r, s);
692 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
693 } else {
694 if (r != 15) break;
695 k += 15;
696 }
697 }
698
699 } else {
700
701 for (k = 1; k < DCTSIZE2; k++) {
702 HUFF_DECODE_FAST(s, l, actbl);
703 r = s >> 4;
704 s &= 15;
705
706 if (s) {
707 k += r;
DRCbc515802011-04-18 06:52:07 +0000708 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000709 DROP_BITS(s);
710 } else {
711 if (r != 15) break;
712 k += 15;
713 }
714 }
715 }
716 }
717
DRC051d9622011-04-16 18:53:26 +0000718 if (cinfo->unread_marker != 0) {
719 cinfo->unread_marker = 0;
720 return FALSE;
DRCfe6a2ee2011-03-18 05:49:26 +0000721 }
722
DRCe2816642009-09-28 00:33:02 +0000723 br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte);
724 br_state.next_input_byte = buffer;
725 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
726 ASSIGN_STATE(entropy->saved, state);
727 return TRUE;
728}
729
730
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000731/*
732 * Decode and return one MCU's worth of Huffman-compressed coefficients.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000733 * The coefficients are reordered from zigzag order into natural array order,
734 * but are not dequantized.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000735 *
736 * The i'th block of the MCU is stored into the block pointed to by
737 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
738 * (Wholesale zeroing is usually a little faster than retail...)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000739 *
740 * Returns FALSE if data source requested suspension. In that case no
741 * changes have been made to permanent state. (Exception: some output
742 * coefficients may already have been assigned. This is harmless for
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000743 * this module, since we'll just re-assign them on the next call.)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000744 */
745
DRCe2816642009-09-28 00:33:02 +0000746#define BUFSIZE (DCTSIZE2 * 2)
747
Thomas G. Lane489583f1996-02-07 00:00:00 +0000748METHODDEF(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000749decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000750{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000751 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
DRC97f8ec42010-03-20 22:38:53 +0000752 int usefast = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000753
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000754 /* Process restart marker if needed; may have to suspend */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000755 if (cinfo->restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000756 if (entropy->restarts_to_go == 0)
757 if (! process_restart(cinfo))
758 return FALSE;
DRC97f8ec42010-03-20 22:38:53 +0000759 usefast = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000760 }
761
DRCefe28ce2012-01-17 11:48:38 +0000762 if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU
DRC051d9622011-04-16 18:53:26 +0000763 || cinfo->unread_marker != 0)
DRC97f8ec42010-03-20 22:38:53 +0000764 usefast = 0;
765
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000766 /* If we've run out of data, just leave the MCU set to zeroes.
767 * This way, we return uniform gray for the remainder of the segment.
768 */
769 if (! entropy->pub.insufficient_data) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000770
DRC97f8ec42010-03-20 22:38:53 +0000771 if (usefast) {
DRC051d9622011-04-16 18:53:26 +0000772 if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow;
DRCe2816642009-09-28 00:33:02 +0000773 }
774 else {
DRC051d9622011-04-16 18:53:26 +0000775 use_slow:
DRCe2816642009-09-28 00:33:02 +0000776 if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000777 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000778
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000779 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000780
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000781 /* Account for restart interval (no-op if not using restarts) */
782 entropy->restarts_to_go--;
783
784 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000785}
786
787
788/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000789 * Module initialization routine for Huffman entropy decoding.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000790 */
791
Thomas G. Lane489583f1996-02-07 00:00:00 +0000792GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000793jinit_huff_decoder (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000794{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000795 huff_entropy_ptr entropy;
796 int i;
797
798 entropy = (huff_entropy_ptr)
799 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
800 SIZEOF(huff_entropy_decoder));
801 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
802 entropy->pub.start_pass = start_pass_huff_decoder;
803 entropy->pub.decode_mcu = decode_mcu;
804
805 /* Mark tables unallocated */
806 for (i = 0; i < NUM_HUFF_TBLS; i++) {
807 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000808 }
809}