blob: aacc0366d7ee40ea3c29e99f85e21c8a2959d024 [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"
DRCe5eaf372014-05-09 18:00:32 +000022#include "jdhuff.h" /* Declarations shared with jdphuff.c */
DRC36a6eec2010-10-08 08:05:44 +000023#include "jpegcomp.h"
DRCa1135062014-01-31 17:22:15 +000024#include "jstdhuff.c"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000025
26
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027/*
28 * Expanded entropy decoder object for Huffman decoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029 *
30 * The savable_state subrecord contains fields that change within an MCU,
31 * but must not be updated permanently until we complete the MCU.
32 */
33
34typedef struct {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
36} savable_state;
37
38/* This macro is to work around compilers with missing or broken
39 * structure assignment. You'll need to fix this code if you have
40 * such a compiler and you change MAX_COMPS_IN_SCAN.
41 */
42
43#ifndef NO_STRUCT_ASSIGN
44#define ASSIGN_STATE(dest,src) ((dest) = (src))
45#else
46#if MAX_COMPS_IN_SCAN == 4
47#define ASSIGN_STATE(dest,src) \
DRCe5eaf372014-05-09 18:00:32 +000048 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
49 (dest).last_dc_val[1] = (src).last_dc_val[1], \
50 (dest).last_dc_val[2] = (src).last_dc_val[2], \
51 (dest).last_dc_val[3] = (src).last_dc_val[3])
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052#endif
53#endif
54
55
56typedef struct {
57 struct jpeg_entropy_decoder pub; /* public fields */
58
Thomas G. Lanebc79e061995-08-02 00:00:00 +000059 /* These fields are loaded into local variables at start of each MCU.
60 * In case of suspension, we exit WITHOUT updating them.
61 */
DRCe5eaf372014-05-09 18:00:32 +000062 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
63 savable_state saved; /* Other state at start of MCU */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000064
65 /* These fields are NOT loaded into local working state. */
DRCe5eaf372014-05-09 18:00:32 +000066 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067
68 /* Pointers to derived tables (these workspaces have image lifespan) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000069 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
70 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000071
72 /* Precalculated info set up by start_pass for use in decode_mcu: */
73
74 /* Pointers to derived tables to be used for each block within an MCU */
75 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
76 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
77 /* Whether we care about the DC and AC coefficient values for each block */
78 boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
79 boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080} huff_entropy_decoder;
81
82typedef huff_entropy_decoder * huff_entropy_ptr;
83
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084
85/*
86 * Initialize for a Huffman-compressed scan.
87 */
88
Thomas G. Lane489583f1996-02-07 00:00:00 +000089METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090start_pass_huff_decoder (j_decompress_ptr cinfo)
91{
92 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000093 int ci, blkn, dctbl, actbl;
DRC8e9cef22015-09-21 12:57:41 -050094 d_derived_tbl **pdtbl;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095 jpeg_component_info * compptr;
96
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
98 * This ought to be an error condition, but we make it a warning because
99 * there are some baseline files out there with all zeroes in these bytes.
100 */
101 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
102 cinfo->Ah != 0 || cinfo->Al != 0)
103 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
104
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
106 compptr = cinfo->cur_comp_info[ci];
107 dctbl = compptr->dc_tbl_no;
108 actbl = compptr->ac_tbl_no;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 /* Compute derived values for Huffman tables */
110 /* We may do this more than once for a table, but it's not expensive */
DRC8e9cef22015-09-21 12:57:41 -0500111 pdtbl = entropy->dc_derived_tbls + dctbl;
112 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, pdtbl);
113 pdtbl = entropy->ac_derived_tbls + actbl;
114 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, pdtbl);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115 /* Initialize DC predictions to 0 */
116 entropy->saved.last_dc_val[ci] = 0;
117 }
118
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000119 /* Precalculate decoding info for each block in an MCU of this scan */
120 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
121 ci = cinfo->MCU_membership[blkn];
122 compptr = cinfo->cur_comp_info[ci];
123 /* Precalculate which table to use for each block */
124 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
125 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
126 /* Decide whether we really care about the coefficient values */
127 if (compptr->component_needed) {
128 entropy->dc_needed[blkn] = TRUE;
129 /* we don't need the ACs if producing a 1/8th-size image */
DRC49967cd2010-10-09 19:57:51 +0000130 entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000131 } else {
132 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
133 }
134 }
135
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000136 /* Initialize bitread state variables */
137 entropy->bitstate.bits_left = 0;
138 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000139 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140
141 /* Initialize restart counter */
142 entropy->restarts_to_go = cinfo->restart_interval;
143}
144
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000145
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000146/*
147 * Compute the derived values for a Huffman table.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000148 * This routine also performs some validation checks on the table.
149 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000150 * Note this is also used by jdphuff.c.
151 */
152
Thomas G. Lane489583f1996-02-07 00:00:00 +0000153GLOBAL(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000154jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
DRCe5eaf372014-05-09 18:00:32 +0000155 d_derived_tbl ** pdtbl)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000156{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000157 JHUFF_TBL *htbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000158 d_derived_tbl *dtbl;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000159 int p, i, l, si, numsymbols;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000160 int lookbits, ctr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000161 char huffsize[257];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000162 unsigned int huffcode[257];
163 unsigned int code;
164
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000165 /* Note that huffsize[] and huffcode[] are filled in code-length order,
166 * paralleling the order of the symbols themselves in htbl->huffval[].
167 */
168
169 /* Find the input Huffman table */
170 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
171 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
172 htbl =
173 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
174 if (htbl == NULL)
175 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
176
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177 /* Allocate a workspace if we haven't already done so. */
178 if (*pdtbl == NULL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000179 *pdtbl = (d_derived_tbl *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000181 sizeof(d_derived_tbl));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000182 dtbl = *pdtbl;
DRCe5eaf372014-05-09 18:00:32 +0000183 dtbl->pub = htbl; /* fill in back link */
184
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000185 /* Figure C.1: make table of Huffman code length for each symbol */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000186
187 p = 0;
188 for (l = 1; l <= 16; l++) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000189 i = (int) htbl->bits[l];
DRCe5eaf372014-05-09 18:00:32 +0000190 if (i < 0 || p + i > 256) /* protect against table overrun */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000191 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
192 while (i--)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000193 huffsize[p++] = (char) l;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000194 }
195 huffsize[p] = 0;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000196 numsymbols = p;
DRCe5eaf372014-05-09 18:00:32 +0000197
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000198 /* Figure C.2: generate the codes themselves */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000199 /* We also validate that the counts represent a legal Huffman code tree. */
DRCe5eaf372014-05-09 18:00:32 +0000200
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000201 code = 0;
202 si = huffsize[0];
203 p = 0;
204 while (huffsize[p]) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000205 while (((int) huffsize[p]) == si) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000206 huffcode[p++] = code;
207 code++;
208 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000209 /* code is now 1 more than the last code used for codelength si; but
210 * it must still fit in si bits, since no code is allowed to be all ones.
211 */
212 if (((INT32) code) >= (((INT32) 1) << si))
213 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000214 code <<= 1;
215 si++;
216 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000217
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000218 /* Figure F.15: generate decoding tables for bit-sequential decoding */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000219
220 p = 0;
221 for (l = 1; l <= 16; l++) {
222 if (htbl->bits[l]) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000223 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
224 * minus the minimum code of length l
225 */
226 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000227 p += htbl->bits[l];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000229 } else {
DRCe5eaf372014-05-09 18:00:32 +0000230 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000231 }
232 }
DRC0fbb28e2010-07-30 17:15:52 +0000233 dtbl->valoffset[17] = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000235
236 /* Compute lookahead tables to speed up decoding.
237 * First we set all the table entries to 0, indicating "too long";
238 * then we iterate through the Huffman codes that are short enough and
239 * fill in all the entries that correspond to bit sequences starting
240 * with that code.
241 */
242
DRCe2816642009-09-28 00:33:02 +0000243 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++)
244 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000245
246 p = 0;
247 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
248 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
249 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
250 /* Generate left-justified code followed by all possible bit sequences */
251 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
252 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000253 dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p];
254 lookbits++;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000255 }
256 }
257 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000258
259 /* Validate symbols as being reasonable.
260 * For AC tables, we make no check, but accept all byte values 0..255.
261 * For DC tables, we require the symbols to be in range 0..15.
262 * (Tighter bounds could be applied depending on the data depth and mode,
263 * but this is sufficient to ensure safe decoding.)
264 */
265 if (isDC) {
266 for (i = 0; i < numsymbols; i++) {
267 int sym = htbl->huffval[i];
268 if (sym < 0 || sym > 15)
DRCe5eaf372014-05-09 18:00:32 +0000269 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000270 }
271 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000272}
273
274
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000275/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 * Out-of-line code for bit fetching (shared with jdphuff.c).
277 * See jdhuff.h for info about usage.
278 * Note: current values of get_buffer and bits_left are passed as parameters,
279 * but are returned in the corresponding fields of the state struct.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000280 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000281 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
282 * of get_buffer to be used. (On machines with wider words, an even larger
283 * buffer could be used.) However, on some machines 32-bit shifts are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284 * quite slow and take time proportional to the number of places shifted.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000285 * (This is true with most PC compilers, for instance.) In this case it may
286 * 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 +0000287 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000288 */
289
290#ifdef SLOW_SHIFT_32
DRCe5eaf372014-05-09 18:00:32 +0000291#define MIN_GET_BITS 15 /* minimum allowable value */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000292#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293#define MIN_GET_BITS (BIT_BUF_SIZE-7)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000294#endif
295
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000296
Thomas G. Lane489583f1996-02-07 00:00:00 +0000297GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000298jpeg_fill_bit_buffer (bitread_working_state * state,
DRCe5eaf372014-05-09 18:00:32 +0000299 register bit_buf_type get_buffer, register int bits_left,
300 int nbits)
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000301/* Load up the bit buffer to a depth of at least nbits */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000302{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 /* Copy heavily used state fields into locals (hopefully registers) */
304 register const JOCTET * next_input_byte = state->next_input_byte;
305 register size_t bytes_in_buffer = state->bytes_in_buffer;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000306 j_decompress_ptr cinfo = state->cinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000308 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000309 /* (It is assumed that no request will be for more than that many bits.) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000310 /* We fail to do so only if we hit a marker or are forced to suspend. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311
DRCe5eaf372014-05-09 18:00:32 +0000312 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000313 while (bits_left < MIN_GET_BITS) {
314 register int c;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000316 /* Attempt to read a byte */
317 if (bytes_in_buffer == 0) {
DRCe5eaf372014-05-09 18:00:32 +0000318 if (! (*cinfo->src->fill_input_buffer) (cinfo))
319 return FALSE;
320 next_input_byte = cinfo->src->next_input_byte;
321 bytes_in_buffer = cinfo->src->bytes_in_buffer;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000322 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000323 bytes_in_buffer--;
324 c = GETJOCTET(*next_input_byte++);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000325
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000326 /* If it's 0xFF, check and discard stuffed zero byte */
327 if (c == 0xFF) {
DRCe5eaf372014-05-09 18:00:32 +0000328 /* Loop here to discard any padding FF's on terminating marker,
329 * so that we can save a valid unread_marker value. NOTE: we will
330 * accept multiple FF's followed by a 0 as meaning a single FF data
331 * byte. This data pattern is not valid according to the standard.
332 */
333 do {
334 if (bytes_in_buffer == 0) {
335 if (! (*cinfo->src->fill_input_buffer) (cinfo))
336 return FALSE;
337 next_input_byte = cinfo->src->next_input_byte;
338 bytes_in_buffer = cinfo->src->bytes_in_buffer;
339 }
340 bytes_in_buffer--;
341 c = GETJOCTET(*next_input_byte++);
342 } while (c == 0xFF);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000343
DRCe5eaf372014-05-09 18:00:32 +0000344 if (c == 0) {
345 /* Found FF/00, which represents an FF data byte */
346 c = 0xFF;
347 } else {
348 /* Oops, it's actually a marker indicating end of compressed data.
349 * Save the marker code for later use.
350 * Fine point: it might appear that we should save the marker into
351 * bitread working state, not straight into permanent state. But
352 * once we have hit a marker, we cannot need to suspend within the
353 * current MCU, because we will read no more bytes from the data
354 * source. So it is OK to update permanent state right away.
355 */
356 cinfo->unread_marker = c;
357 /* See if we need to insert some fake zero bits. */
358 goto no_more_bytes;
359 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000360 }
361
362 /* OK, load c into get_buffer */
363 get_buffer = (get_buffer << 8) | c;
364 bits_left += 8;
365 } /* end while */
366 } else {
367 no_more_bytes:
368 /* We get here if we've read the marker that terminates the compressed
369 * data segment. There should be enough bits in the buffer register
370 * to satisfy the request; if so, no problem.
371 */
372 if (nbits > bits_left) {
373 /* Uh-oh. Report corrupted data to user and stuff zeroes into
374 * the data stream, so that we can produce some kind of image.
375 * We use a nonvolatile flag to ensure that only one warning message
376 * appears per data segment.
377 */
378 if (! cinfo->entropy->insufficient_data) {
DRCe5eaf372014-05-09 18:00:32 +0000379 WARNMS(cinfo, JWRN_HIT_MARKER);
380 cinfo->entropy->insufficient_data = TRUE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000381 }
382 /* Fill the buffer with zero bits */
383 get_buffer <<= MIN_GET_BITS - bits_left;
384 bits_left = MIN_GET_BITS;
385 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000386 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387
388 /* Unload the local registers */
389 state->next_input_byte = next_input_byte;
390 state->bytes_in_buffer = bytes_in_buffer;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000391 state->get_buffer = get_buffer;
392 state->bits_left = bits_left;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393
394 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000395}
396
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000397
DRCbc515802011-04-18 06:52:07 +0000398/* Macro version of the above, which performs much better but does not
399 handle markers. We have to hand off any blocks with markers to the
400 slower routines. */
401
402#define GET_BYTE \
403{ \
404 register int c0, c1; \
405 c0 = GETJOCTET(*buffer++); \
406 c1 = GETJOCTET(*buffer); \
407 /* Pre-execute most common case */ \
408 get_buffer = (get_buffer << 8) | c0; \
409 bits_left += 8; \
410 if (c0 == 0xFF) { \
411 /* Pre-execute case of FF/00, which represents an FF data byte */ \
412 buffer++; \
413 if (c1 != 0) { \
414 /* Oops, it's actually a marker indicating end of compressed data. */ \
415 cinfo->unread_marker = c1; \
416 /* Back out pre-execution and fill the buffer with zero bits */ \
417 buffer -= 2; \
418 get_buffer &= ~0xFF; \
419 } \
420 } \
421}
422
DRC3ebcc322015-05-15 19:09:44 +0000423#if SIZEOF_SIZE_T==8 || defined(_WIN64)
DRCbc515802011-04-18 06:52:07 +0000424
425/* Pre-fetch 48 bytes, because the holding register is 64-bit */
426#define FILL_BIT_BUFFER_FAST \
DRC944aa8e2015-07-21 16:43:39 -0500427 if (bits_left <= 16) { \
DRCbc515802011-04-18 06:52:07 +0000428 GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \
429 }
430
431#else
432
433/* Pre-fetch 16 bytes, because the holding register is 32-bit */
434#define FILL_BIT_BUFFER_FAST \
DRC944aa8e2015-07-21 16:43:39 -0500435 if (bits_left <= 16) { \
DRCbc515802011-04-18 06:52:07 +0000436 GET_BYTE GET_BYTE \
437 }
438
439#endif
440
441
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000442/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000443 * Out-of-line code for Huffman code decoding.
444 * See jdhuff.h for info about usage.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000445 */
446
Thomas G. Lane489583f1996-02-07 00:00:00 +0000447GLOBAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000448jpeg_huff_decode (bitread_working_state * state,
DRCe5eaf372014-05-09 18:00:32 +0000449 register bit_buf_type get_buffer, register int bits_left,
450 d_derived_tbl * htbl, int min_bits)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000451{
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000452 register int l = min_bits;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000453 register INT32 code;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000454
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000455 /* HUFF_DECODE has determined that the code is at least min_bits */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000456 /* bits long, so fetch that many bits in one swoop. */
457
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000458 CHECK_BIT_BUFFER(*state, l, return -1);
459 code = GET_BITS(l);
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000460
461 /* Collect the rest of the Huffman code one bit at a time. */
462 /* This is per Figure F.16 in the JPEG spec. */
463
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000464 while (code > htbl->maxcode[l]) {
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000465 code <<= 1;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000466 CHECK_BIT_BUFFER(*state, 1, return -1);
467 code |= GET_BITS(1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000468 l++;
469 }
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000470
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000471 /* Unload the local registers */
472 state->get_buffer = get_buffer;
473 state->bits_left = bits_left;
474
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000475 /* With garbage input we may reach the sentinel value l = 17. */
476
477 if (l > 16) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
DRCe5eaf372014-05-09 18:00:32 +0000479 return 0; /* fake a zero as the safest result */
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000480 }
481
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000482 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000483}
484
485
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000486/*
487 * Figure F.12: extend sign bit.
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000488 * On some machines, a shift and add will be faster than a table lookup.
489 */
490
DRCe2816642009-09-28 00:33:02 +0000491#define AVOID_TABLES
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000492#ifdef AVOID_TABLES
493
DRC8e9cef22015-09-21 12:57:41 -0500494#define NEG_1 ((unsigned int)-1)
495#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((NEG_1)<<(s)) + 1)))
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000496
497#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000498
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000499#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000500
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000501static const int extend_test[16] = /* entry n is 2**(n-1) */
502 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
503 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000504
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000505static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
506 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
507 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
508 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
509 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000510
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000511#endif /* AVOID_TABLES */
512
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000513
514/*
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000515 * Check for a restart marker & resynchronize decoder.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000516 * Returns FALSE if must suspend.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000517 */
518
Thomas G. Lane489583f1996-02-07 00:00:00 +0000519LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000520process_restart (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000521{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000522 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
523 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000524
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000525 /* Throw away any unused bits remaining in bit buffer; */
526 /* include any full bytes in next_marker's count of discarded bytes */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000527 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
528 entropy->bitstate.bits_left = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000529
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000530 /* Advance past the RSTn marker */
531 if (! (*cinfo->marker->read_restart_marker) (cinfo))
532 return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000533
534 /* Re-initialize DC predictions to 0 */
535 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000536 entropy->saved.last_dc_val[ci] = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000537
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000538 /* Reset restart counter */
539 entropy->restarts_to_go = cinfo->restart_interval;
540
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000541 /* Reset out-of-data flag, unless read_restart_marker left us smack up
542 * against a marker. In that case we will end up treating the next data
543 * segment as empty, and we can avoid producing bogus output pixels by
544 * leaving the flag set.
545 */
546 if (cinfo->unread_marker == 0)
547 entropy->pub.insufficient_data = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000548
549 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000550}
551
552
DRCe2816642009-09-28 00:33:02 +0000553LOCAL(boolean)
554decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
555{
556 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
557 BITREAD_STATE_VARS;
558 int blkn;
559 savable_state state;
560 /* Outer loop handles each block in the MCU */
561
562 /* Load up working state */
563 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
564 ASSIGN_STATE(state, entropy->saved);
565
566 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
567 JBLOCKROW block = MCU_data[blkn];
568 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
569 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
570 register int s, k, r;
571
572 /* Decode a single block's worth of coefficients */
573
574 /* Section F.2.2.1: decode the DC coefficient difference */
575 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
576 if (s) {
577 CHECK_BIT_BUFFER(br_state, s, return FALSE);
578 r = GET_BITS(s);
579 s = HUFF_EXTEND(r, s);
580 }
581
582 if (entropy->dc_needed[blkn]) {
583 /* Convert DC difference to actual value, update last_dc_val */
584 int ci = cinfo->MCU_membership[blkn];
585 s += state.last_dc_val[ci];
586 state.last_dc_val[ci] = s;
587 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
588 (*block)[0] = (JCOEF) s;
589 }
590
591 if (entropy->ac_needed[blkn]) {
592
593 /* Section F.2.2.2: decode the AC coefficients */
594 /* Since zeroes are skipped, output area must be cleared beforehand */
595 for (k = 1; k < DCTSIZE2; k++) {
596 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
597
598 r = s >> 4;
599 s &= 15;
DRCe5eaf372014-05-09 18:00:32 +0000600
DRCe2816642009-09-28 00:33:02 +0000601 if (s) {
602 k += r;
603 CHECK_BIT_BUFFER(br_state, s, return FALSE);
604 r = GET_BITS(s);
605 s = HUFF_EXTEND(r, s);
606 /* Output coefficient in natural (dezigzagged) order.
607 * Note: the extra entries in jpeg_natural_order[] will save us
608 * if k >= DCTSIZE2, which could happen if the data is corrupted.
609 */
610 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
611 } else {
612 if (r != 15)
613 break;
614 k += 15;
615 }
616 }
617
618 } else {
619
620 /* Section F.2.2.2: decode the AC coefficients */
621 /* In this path we just discard the values */
622 for (k = 1; k < DCTSIZE2; k++) {
623 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
624
625 r = s >> 4;
626 s &= 15;
627
628 if (s) {
629 k += r;
630 CHECK_BIT_BUFFER(br_state, s, return FALSE);
631 DROP_BITS(s);
632 } else {
633 if (r != 15)
634 break;
635 k += 15;
636 }
637 }
638 }
639 }
640
641 /* Completed MCU, so update state */
642 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
643 ASSIGN_STATE(entropy->saved, state);
644 return TRUE;
645}
646
647
DRCe2816642009-09-28 00:33:02 +0000648LOCAL(boolean)
649decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
650{
651 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
652 BITREAD_STATE_VARS;
653 JOCTET *buffer;
654 int blkn;
655 savable_state state;
656 /* Outer loop handles each block in the MCU */
657
658 /* Load up working state */
659 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
660 buffer = (JOCTET *) br_state.next_input_byte;
661 ASSIGN_STATE(state, entropy->saved);
662
663 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
664 JBLOCKROW block = MCU_data[blkn];
665 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
666 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
667 register int s, k, r, l;
668
669 HUFF_DECODE_FAST(s, l, dctbl);
670 if (s) {
DRCbc515802011-04-18 06:52:07 +0000671 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000672 r = GET_BITS(s);
673 s = HUFF_EXTEND(r, s);
674 }
675
676 if (entropy->dc_needed[blkn]) {
677 int ci = cinfo->MCU_membership[blkn];
678 s += state.last_dc_val[ci];
679 state.last_dc_val[ci] = s;
680 (*block)[0] = (JCOEF) s;
681 }
682
683 if (entropy->ac_needed[blkn]) {
684
685 for (k = 1; k < DCTSIZE2; k++) {
686 HUFF_DECODE_FAST(s, l, actbl);
687 r = s >> 4;
688 s &= 15;
DRCe5eaf372014-05-09 18:00:32 +0000689
DRCe2816642009-09-28 00:33:02 +0000690 if (s) {
691 k += r;
DRCbc515802011-04-18 06:52:07 +0000692 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000693 r = GET_BITS(s);
694 s = HUFF_EXTEND(r, s);
695 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
696 } else {
697 if (r != 15) break;
698 k += 15;
699 }
700 }
701
702 } else {
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;
DRCbc515802011-04-18 06:52:07 +0000711 FILL_BIT_BUFFER_FAST
DRCe2816642009-09-28 00:33:02 +0000712 DROP_BITS(s);
713 } else {
714 if (r != 15) break;
715 k += 15;
716 }
717 }
718 }
719 }
720
DRC051d9622011-04-16 18:53:26 +0000721 if (cinfo->unread_marker != 0) {
722 cinfo->unread_marker = 0;
723 return FALSE;
DRCfe6a2ee2011-03-18 05:49:26 +0000724 }
725
DRCe2816642009-09-28 00:33:02 +0000726 br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte);
727 br_state.next_input_byte = buffer;
728 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
729 ASSIGN_STATE(entropy->saved, state);
730 return TRUE;
731}
732
733
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000734/*
735 * Decode and return one MCU's worth of Huffman-compressed coefficients.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000736 * The coefficients are reordered from zigzag order into natural array order,
737 * but are not dequantized.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000738 *
739 * The i'th block of the MCU is stored into the block pointed to by
740 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
741 * (Wholesale zeroing is usually a little faster than retail...)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000742 *
743 * Returns FALSE if data source requested suspension. In that case no
744 * changes have been made to permanent state. (Exception: some output
745 * coefficients may already have been assigned. This is harmless for
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000746 * this module, since we'll just re-assign them on the next call.)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000747 */
748
DRCe2816642009-09-28 00:33:02 +0000749#define BUFSIZE (DCTSIZE2 * 2)
750
Thomas G. Lane489583f1996-02-07 00:00:00 +0000751METHODDEF(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000752decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000753{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000754 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
DRC97f8ec42010-03-20 22:38:53 +0000755 int usefast = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000756
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000757 /* Process restart marker if needed; may have to suspend */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000758 if (cinfo->restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000759 if (entropy->restarts_to_go == 0)
760 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000761 return FALSE;
DRC97f8ec42010-03-20 22:38:53 +0000762 usefast = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000763 }
764
DRCefe28ce2012-01-17 11:48:38 +0000765 if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU
DRC051d9622011-04-16 18:53:26 +0000766 || cinfo->unread_marker != 0)
DRC97f8ec42010-03-20 22:38:53 +0000767 usefast = 0;
768
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000769 /* If we've run out of data, just leave the MCU set to zeroes.
770 * This way, we return uniform gray for the remainder of the segment.
771 */
772 if (! entropy->pub.insufficient_data) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000773
DRC97f8ec42010-03-20 22:38:53 +0000774 if (usefast) {
DRC051d9622011-04-16 18:53:26 +0000775 if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow;
DRCe2816642009-09-28 00:33:02 +0000776 }
777 else {
DRC051d9622011-04-16 18:53:26 +0000778 use_slow:
DRCe2816642009-09-28 00:33:02 +0000779 if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000780 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000781
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000782 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000783
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000784 /* Account for restart interval (no-op if not using restarts) */
785 entropy->restarts_to_go--;
786
787 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000788}
789
790
791/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000792 * Module initialization routine for Huffman entropy decoding.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000793 */
794
Thomas G. Lane489583f1996-02-07 00:00:00 +0000795GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000796jinit_huff_decoder (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000797{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000798 huff_entropy_ptr entropy;
799 int i;
800
DRCa1135062014-01-31 17:22:15 +0000801 /* Motion JPEG frames typically do not include the Huffman tables if they
802 are the default tables. Thus, if the tables are not set by the time
803 the Huffman decoder is initialized (usually within the body of
804 jpeg_start_decompress()), we set them to default values. */
805 std_huff_tables((j_common_ptr) cinfo);
806
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000807 entropy = (huff_entropy_ptr)
808 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000809 sizeof(huff_entropy_decoder));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000810 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
811 entropy->pub.start_pass = start_pass_huff_decoder;
812 entropy->pub.decode_mcu = decode_mcu;
813
814 /* Mark tables unallocated */
815 for (i = 0; i < NUM_HUFF_TBLS; i++) {
816 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000817 }
818}