rev |
line source |
nuclear@1
|
1 /*
|
nuclear@1
|
2 * jdhuff.c
|
nuclear@1
|
3 *
|
nuclear@1
|
4 * Copyright (C) 1991-1997, Thomas G. Lane.
|
nuclear@1
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@1
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@1
|
7 *
|
nuclear@1
|
8 * This file contains Huffman entropy decoding routines.
|
nuclear@1
|
9 *
|
nuclear@1
|
10 * Much of the complexity here has to do with supporting input suspension.
|
nuclear@1
|
11 * If the data source module demands suspension, we want to be able to back
|
nuclear@1
|
12 * up to the start of the current MCU. To do this, we copy state variables
|
nuclear@1
|
13 * into local working storage, and update them back to the permanent
|
nuclear@1
|
14 * storage only upon successful completion of an MCU.
|
nuclear@1
|
15 */
|
nuclear@1
|
16
|
nuclear@1
|
17 #define JPEG_INTERNALS
|
nuclear@1
|
18 #include "jinclude.h"
|
nuclear@1
|
19 #include "jpeglib.h"
|
nuclear@1
|
20 #include "jdhuff.h" /* Declarations shared with jdphuff.c */
|
nuclear@1
|
21
|
nuclear@1
|
22
|
nuclear@1
|
23 /*
|
nuclear@1
|
24 * Expanded entropy decoder object for Huffman decoding.
|
nuclear@1
|
25 *
|
nuclear@1
|
26 * The savable_state subrecord contains fields that change within an MCU,
|
nuclear@1
|
27 * but must not be updated permanently until we complete the MCU.
|
nuclear@1
|
28 */
|
nuclear@1
|
29
|
nuclear@1
|
30 typedef struct {
|
nuclear@1
|
31 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
nuclear@1
|
32 } savable_state;
|
nuclear@1
|
33
|
nuclear@1
|
34 /* This macro is to work around compilers with missing or broken
|
nuclear@1
|
35 * structure assignment. You'll need to fix this code if you have
|
nuclear@1
|
36 * such a compiler and you change MAX_COMPS_IN_SCAN.
|
nuclear@1
|
37 */
|
nuclear@1
|
38
|
nuclear@1
|
39 #ifndef NO_STRUCT_ASSIGN
|
nuclear@1
|
40 #define ASSIGN_STATE(dest,src) ((dest) = (src))
|
nuclear@1
|
41 #else
|
nuclear@1
|
42 #if MAX_COMPS_IN_SCAN == 4
|
nuclear@1
|
43 #define ASSIGN_STATE(dest,src) \
|
nuclear@1
|
44 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
|
nuclear@1
|
45 (dest).last_dc_val[1] = (src).last_dc_val[1], \
|
nuclear@1
|
46 (dest).last_dc_val[2] = (src).last_dc_val[2], \
|
nuclear@1
|
47 (dest).last_dc_val[3] = (src).last_dc_val[3])
|
nuclear@1
|
48 #endif
|
nuclear@1
|
49 #endif
|
nuclear@1
|
50
|
nuclear@1
|
51
|
nuclear@1
|
52 typedef struct {
|
nuclear@1
|
53 struct jpeg_entropy_decoder pub; /* public fields */
|
nuclear@1
|
54
|
nuclear@1
|
55 /* These fields are loaded into local variables at start of each MCU.
|
nuclear@1
|
56 * In case of suspension, we exit WITHOUT updating them.
|
nuclear@1
|
57 */
|
nuclear@1
|
58 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
|
nuclear@1
|
59 savable_state saved; /* Other state at start of MCU */
|
nuclear@1
|
60
|
nuclear@1
|
61 /* These fields are NOT loaded into local working state. */
|
nuclear@1
|
62 unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
nuclear@1
|
63
|
nuclear@1
|
64 /* Pointers to derived tables (these workspaces have image lifespan) */
|
nuclear@1
|
65 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
|
nuclear@1
|
66 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
|
nuclear@1
|
67
|
nuclear@1
|
68 /* Precalculated info set up by start_pass for use in decode_mcu: */
|
nuclear@1
|
69
|
nuclear@1
|
70 /* Pointers to derived tables to be used for each block within an MCU */
|
nuclear@1
|
71 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
|
nuclear@1
|
72 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
|
nuclear@1
|
73 /* Whether we care about the DC and AC coefficient values for each block */
|
nuclear@1
|
74 boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
|
nuclear@1
|
75 boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
|
nuclear@1
|
76 } huff_entropy_decoder;
|
nuclear@1
|
77
|
nuclear@1
|
78 typedef huff_entropy_decoder * huff_entropy_ptr;
|
nuclear@1
|
79
|
nuclear@1
|
80
|
nuclear@1
|
81 /*
|
nuclear@1
|
82 * Initialize for a Huffman-compressed scan.
|
nuclear@1
|
83 */
|
nuclear@1
|
84
|
nuclear@1
|
85 METHODDEF(void)
|
nuclear@1
|
86 start_pass_huff_decoder (j_decompress_ptr cinfo)
|
nuclear@1
|
87 {
|
nuclear@1
|
88 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
89 int ci, blkn, dctbl, actbl;
|
nuclear@1
|
90 jpeg_component_info * compptr;
|
nuclear@1
|
91
|
nuclear@1
|
92 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
|
nuclear@1
|
93 * This ought to be an error condition, but we make it a warning because
|
nuclear@1
|
94 * there are some baseline files out there with all zeroes in these bytes.
|
nuclear@1
|
95 */
|
nuclear@1
|
96 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
|
nuclear@1
|
97 cinfo->Ah != 0 || cinfo->Al != 0)
|
nuclear@1
|
98 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
|
nuclear@1
|
99
|
nuclear@1
|
100 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
nuclear@1
|
101 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
102 dctbl = compptr->dc_tbl_no;
|
nuclear@1
|
103 actbl = compptr->ac_tbl_no;
|
nuclear@1
|
104 /* Compute derived values for Huffman tables */
|
nuclear@1
|
105 /* We may do this more than once for a table, but it's not expensive */
|
nuclear@1
|
106 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
|
nuclear@1
|
107 & entropy->dc_derived_tbls[dctbl]);
|
nuclear@1
|
108 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
|
nuclear@1
|
109 & entropy->ac_derived_tbls[actbl]);
|
nuclear@1
|
110 /* Initialize DC predictions to 0 */
|
nuclear@1
|
111 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@1
|
112 }
|
nuclear@1
|
113
|
nuclear@1
|
114 /* Precalculate decoding info for each block in an MCU of this scan */
|
nuclear@1
|
115 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@1
|
116 ci = cinfo->MCU_membership[blkn];
|
nuclear@1
|
117 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
118 /* Precalculate which table to use for each block */
|
nuclear@1
|
119 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
|
nuclear@1
|
120 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
|
nuclear@1
|
121 /* Decide whether we really care about the coefficient values */
|
nuclear@1
|
122 if (compptr->component_needed) {
|
nuclear@1
|
123 entropy->dc_needed[blkn] = TRUE;
|
nuclear@1
|
124 /* we don't need the ACs if producing a 1/8th-size image */
|
nuclear@1
|
125 entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
|
nuclear@1
|
126 } else {
|
nuclear@1
|
127 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
|
nuclear@1
|
128 }
|
nuclear@1
|
129 }
|
nuclear@1
|
130
|
nuclear@1
|
131 /* Initialize bitread state variables */
|
nuclear@1
|
132 entropy->bitstate.bits_left = 0;
|
nuclear@1
|
133 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
nuclear@1
|
134 entropy->pub.insufficient_data = FALSE;
|
nuclear@1
|
135
|
nuclear@1
|
136 /* Initialize restart counter */
|
nuclear@1
|
137 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@1
|
138 }
|
nuclear@1
|
139
|
nuclear@1
|
140
|
nuclear@1
|
141 /*
|
nuclear@1
|
142 * Compute the derived values for a Huffman table.
|
nuclear@1
|
143 * This routine also performs some validation checks on the table.
|
nuclear@1
|
144 *
|
nuclear@1
|
145 * Note this is also used by jdphuff.c.
|
nuclear@1
|
146 */
|
nuclear@1
|
147
|
nuclear@1
|
148 GLOBAL(void)
|
nuclear@1
|
149 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
|
nuclear@1
|
150 d_derived_tbl ** pdtbl)
|
nuclear@1
|
151 {
|
nuclear@1
|
152 JHUFF_TBL *htbl;
|
nuclear@1
|
153 d_derived_tbl *dtbl;
|
nuclear@1
|
154 int p, i, l, si, numsymbols;
|
nuclear@1
|
155 int lookbits, ctr;
|
nuclear@1
|
156 char huffsize[257];
|
nuclear@1
|
157 unsigned int huffcode[257];
|
nuclear@1
|
158 unsigned int code;
|
nuclear@1
|
159
|
nuclear@1
|
160 /* Note that huffsize[] and huffcode[] are filled in code-length order,
|
nuclear@1
|
161 * paralleling the order of the symbols themselves in htbl->huffval[].
|
nuclear@1
|
162 */
|
nuclear@1
|
163
|
nuclear@1
|
164 /* Find the input Huffman table */
|
nuclear@1
|
165 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
|
nuclear@1
|
166 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
nuclear@1
|
167 htbl =
|
nuclear@1
|
168 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
|
nuclear@1
|
169 if (htbl == NULL)
|
nuclear@1
|
170 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
nuclear@1
|
171
|
nuclear@1
|
172 /* Allocate a workspace if we haven't already done so. */
|
nuclear@1
|
173 if (*pdtbl == NULL)
|
nuclear@1
|
174 *pdtbl = (d_derived_tbl *)
|
nuclear@1
|
175 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
176 SIZEOF(d_derived_tbl));
|
nuclear@1
|
177 dtbl = *pdtbl;
|
nuclear@1
|
178 dtbl->pub = htbl; /* fill in back link */
|
nuclear@1
|
179
|
nuclear@1
|
180 /* Figure C.1: make table of Huffman code length for each symbol */
|
nuclear@1
|
181
|
nuclear@1
|
182 p = 0;
|
nuclear@1
|
183 for (l = 1; l <= 16; l++) {
|
nuclear@1
|
184 i = (int) htbl->bits[l];
|
nuclear@1
|
185 if (i < 0 || p + i > 256) /* protect against table overrun */
|
nuclear@1
|
186 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
187 while (i--)
|
nuclear@1
|
188 huffsize[p++] = (char) l;
|
nuclear@1
|
189 }
|
nuclear@1
|
190 huffsize[p] = 0;
|
nuclear@1
|
191 numsymbols = p;
|
nuclear@1
|
192
|
nuclear@1
|
193 /* Figure C.2: generate the codes themselves */
|
nuclear@1
|
194 /* We also validate that the counts represent a legal Huffman code tree. */
|
nuclear@1
|
195
|
nuclear@1
|
196 code = 0;
|
nuclear@1
|
197 si = huffsize[0];
|
nuclear@1
|
198 p = 0;
|
nuclear@1
|
199 while (huffsize[p]) {
|
nuclear@1
|
200 while (((int) huffsize[p]) == si) {
|
nuclear@1
|
201 huffcode[p++] = code;
|
nuclear@1
|
202 code++;
|
nuclear@1
|
203 }
|
nuclear@1
|
204 /* code is now 1 more than the last code used for codelength si; but
|
nuclear@1
|
205 * it must still fit in si bits, since no code is allowed to be all ones.
|
nuclear@1
|
206 */
|
nuclear@1
|
207 if (((INT32) code) >= (((INT32) 1) << si))
|
nuclear@1
|
208 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
209 code <<= 1;
|
nuclear@1
|
210 si++;
|
nuclear@1
|
211 }
|
nuclear@1
|
212
|
nuclear@1
|
213 /* Figure F.15: generate decoding tables for bit-sequential decoding */
|
nuclear@1
|
214
|
nuclear@1
|
215 p = 0;
|
nuclear@1
|
216 for (l = 1; l <= 16; l++) {
|
nuclear@1
|
217 if (htbl->bits[l]) {
|
nuclear@1
|
218 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
|
nuclear@1
|
219 * minus the minimum code of length l
|
nuclear@1
|
220 */
|
nuclear@1
|
221 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
|
nuclear@1
|
222 p += htbl->bits[l];
|
nuclear@1
|
223 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
|
nuclear@1
|
224 } else {
|
nuclear@1
|
225 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
|
nuclear@1
|
226 }
|
nuclear@1
|
227 }
|
nuclear@1
|
228 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
|
nuclear@1
|
229
|
nuclear@1
|
230 /* Compute lookahead tables to speed up decoding.
|
nuclear@1
|
231 * First we set all the table entries to 0, indicating "too long";
|
nuclear@1
|
232 * then we iterate through the Huffman codes that are short enough and
|
nuclear@1
|
233 * fill in all the entries that correspond to bit sequences starting
|
nuclear@1
|
234 * with that code.
|
nuclear@1
|
235 */
|
nuclear@1
|
236
|
nuclear@1
|
237 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
|
nuclear@1
|
238
|
nuclear@1
|
239 p = 0;
|
nuclear@1
|
240 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
|
nuclear@1
|
241 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
|
nuclear@1
|
242 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
|
nuclear@1
|
243 /* Generate left-justified code followed by all possible bit sequences */
|
nuclear@1
|
244 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
|
nuclear@1
|
245 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
|
nuclear@1
|
246 dtbl->look_nbits[lookbits] = l;
|
nuclear@1
|
247 dtbl->look_sym[lookbits] = htbl->huffval[p];
|
nuclear@1
|
248 lookbits++;
|
nuclear@1
|
249 }
|
nuclear@1
|
250 }
|
nuclear@1
|
251 }
|
nuclear@1
|
252
|
nuclear@1
|
253 /* Validate symbols as being reasonable.
|
nuclear@1
|
254 * For AC tables, we make no check, but accept all byte values 0..255.
|
nuclear@1
|
255 * For DC tables, we require the symbols to be in range 0..15.
|
nuclear@1
|
256 * (Tighter bounds could be applied depending on the data depth and mode,
|
nuclear@1
|
257 * but this is sufficient to ensure safe decoding.)
|
nuclear@1
|
258 */
|
nuclear@1
|
259 if (isDC) {
|
nuclear@1
|
260 for (i = 0; i < numsymbols; i++) {
|
nuclear@1
|
261 int sym = htbl->huffval[i];
|
nuclear@1
|
262 if (sym < 0 || sym > 15)
|
nuclear@1
|
263 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
264 }
|
nuclear@1
|
265 }
|
nuclear@1
|
266 }
|
nuclear@1
|
267
|
nuclear@1
|
268
|
nuclear@1
|
269 /*
|
nuclear@1
|
270 * Out-of-line code for bit fetching (shared with jdphuff.c).
|
nuclear@1
|
271 * See jdhuff.h for info about usage.
|
nuclear@1
|
272 * Note: current values of get_buffer and bits_left are passed as parameters,
|
nuclear@1
|
273 * but are returned in the corresponding fields of the state struct.
|
nuclear@1
|
274 *
|
nuclear@1
|
275 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
|
nuclear@1
|
276 * of get_buffer to be used. (On machines with wider words, an even larger
|
nuclear@1
|
277 * buffer could be used.) However, on some machines 32-bit shifts are
|
nuclear@1
|
278 * quite slow and take time proportional to the number of places shifted.
|
nuclear@1
|
279 * (This is true with most PC compilers, for instance.) In this case it may
|
nuclear@1
|
280 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
|
nuclear@1
|
281 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
|
nuclear@1
|
282 */
|
nuclear@1
|
283
|
nuclear@1
|
284 #ifdef SLOW_SHIFT_32
|
nuclear@1
|
285 #define MIN_GET_BITS 15 /* minimum allowable value */
|
nuclear@1
|
286 #else
|
nuclear@1
|
287 #define MIN_GET_BITS (BIT_BUF_SIZE-7)
|
nuclear@1
|
288 #endif
|
nuclear@1
|
289
|
nuclear@1
|
290
|
nuclear@1
|
291 GLOBAL(boolean)
|
nuclear@1
|
292 jpeg_fill_bit_buffer (bitread_working_state * state,
|
nuclear@1
|
293 register bit_buf_type get_buffer, register int bits_left,
|
nuclear@1
|
294 int nbits)
|
nuclear@1
|
295 /* Load up the bit buffer to a depth of at least nbits */
|
nuclear@1
|
296 {
|
nuclear@1
|
297 /* Copy heavily used state fields into locals (hopefully registers) */
|
nuclear@1
|
298 register const JOCTET * next_input_byte = state->next_input_byte;
|
nuclear@1
|
299 register size_t bytes_in_buffer = state->bytes_in_buffer;
|
nuclear@1
|
300 j_decompress_ptr cinfo = state->cinfo;
|
nuclear@1
|
301
|
nuclear@1
|
302 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
|
nuclear@1
|
303 /* (It is assumed that no request will be for more than that many bits.) */
|
nuclear@1
|
304 /* We fail to do so only if we hit a marker or are forced to suspend. */
|
nuclear@1
|
305
|
nuclear@1
|
306 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
|
nuclear@1
|
307 while (bits_left < MIN_GET_BITS) {
|
nuclear@1
|
308 register int c;
|
nuclear@1
|
309
|
nuclear@1
|
310 /* Attempt to read a byte */
|
nuclear@1
|
311 if (bytes_in_buffer == 0) {
|
nuclear@1
|
312 if (! (*cinfo->src->fill_input_buffer) (cinfo))
|
nuclear@1
|
313 return FALSE;
|
nuclear@1
|
314 next_input_byte = cinfo->src->next_input_byte;
|
nuclear@1
|
315 bytes_in_buffer = cinfo->src->bytes_in_buffer;
|
nuclear@1
|
316 }
|
nuclear@1
|
317 bytes_in_buffer--;
|
nuclear@1
|
318 c = GETJOCTET(*next_input_byte++);
|
nuclear@1
|
319
|
nuclear@1
|
320 /* If it's 0xFF, check and discard stuffed zero byte */
|
nuclear@1
|
321 if (c == 0xFF) {
|
nuclear@1
|
322 /* Loop here to discard any padding FF's on terminating marker,
|
nuclear@1
|
323 * so that we can save a valid unread_marker value. NOTE: we will
|
nuclear@1
|
324 * accept multiple FF's followed by a 0 as meaning a single FF data
|
nuclear@1
|
325 * byte. This data pattern is not valid according to the standard.
|
nuclear@1
|
326 */
|
nuclear@1
|
327 do {
|
nuclear@1
|
328 if (bytes_in_buffer == 0) {
|
nuclear@1
|
329 if (! (*cinfo->src->fill_input_buffer) (cinfo))
|
nuclear@1
|
330 return FALSE;
|
nuclear@1
|
331 next_input_byte = cinfo->src->next_input_byte;
|
nuclear@1
|
332 bytes_in_buffer = cinfo->src->bytes_in_buffer;
|
nuclear@1
|
333 }
|
nuclear@1
|
334 bytes_in_buffer--;
|
nuclear@1
|
335 c = GETJOCTET(*next_input_byte++);
|
nuclear@1
|
336 } while (c == 0xFF);
|
nuclear@1
|
337
|
nuclear@1
|
338 if (c == 0) {
|
nuclear@1
|
339 /* Found FF/00, which represents an FF data byte */
|
nuclear@1
|
340 c = 0xFF;
|
nuclear@1
|
341 } else {
|
nuclear@1
|
342 /* Oops, it's actually a marker indicating end of compressed data.
|
nuclear@1
|
343 * Save the marker code for later use.
|
nuclear@1
|
344 * Fine point: it might appear that we should save the marker into
|
nuclear@1
|
345 * bitread working state, not straight into permanent state. But
|
nuclear@1
|
346 * once we have hit a marker, we cannot need to suspend within the
|
nuclear@1
|
347 * current MCU, because we will read no more bytes from the data
|
nuclear@1
|
348 * source. So it is OK to update permanent state right away.
|
nuclear@1
|
349 */
|
nuclear@1
|
350 cinfo->unread_marker = c;
|
nuclear@1
|
351 /* See if we need to insert some fake zero bits. */
|
nuclear@1
|
352 goto no_more_bytes;
|
nuclear@1
|
353 }
|
nuclear@1
|
354 }
|
nuclear@1
|
355
|
nuclear@1
|
356 /* OK, load c into get_buffer */
|
nuclear@1
|
357 get_buffer = (get_buffer << 8) | c;
|
nuclear@1
|
358 bits_left += 8;
|
nuclear@1
|
359 } /* end while */
|
nuclear@1
|
360 } else {
|
nuclear@1
|
361 no_more_bytes:
|
nuclear@1
|
362 /* We get here if we've read the marker that terminates the compressed
|
nuclear@1
|
363 * data segment. There should be enough bits in the buffer register
|
nuclear@1
|
364 * to satisfy the request; if so, no problem.
|
nuclear@1
|
365 */
|
nuclear@1
|
366 if (nbits > bits_left) {
|
nuclear@1
|
367 /* Uh-oh. Report corrupted data to user and stuff zeroes into
|
nuclear@1
|
368 * the data stream, so that we can produce some kind of image.
|
nuclear@1
|
369 * We use a nonvolatile flag to ensure that only one warning message
|
nuclear@1
|
370 * appears per data segment.
|
nuclear@1
|
371 */
|
nuclear@1
|
372 if (! cinfo->entropy->insufficient_data) {
|
nuclear@1
|
373 WARNMS(cinfo, JWRN_HIT_MARKER);
|
nuclear@1
|
374 cinfo->entropy->insufficient_data = TRUE;
|
nuclear@1
|
375 }
|
nuclear@1
|
376 /* Fill the buffer with zero bits */
|
nuclear@1
|
377 get_buffer <<= MIN_GET_BITS - bits_left;
|
nuclear@1
|
378 bits_left = MIN_GET_BITS;
|
nuclear@1
|
379 }
|
nuclear@1
|
380 }
|
nuclear@1
|
381
|
nuclear@1
|
382 /* Unload the local registers */
|
nuclear@1
|
383 state->next_input_byte = next_input_byte;
|
nuclear@1
|
384 state->bytes_in_buffer = bytes_in_buffer;
|
nuclear@1
|
385 state->get_buffer = get_buffer;
|
nuclear@1
|
386 state->bits_left = bits_left;
|
nuclear@1
|
387
|
nuclear@1
|
388 return TRUE;
|
nuclear@1
|
389 }
|
nuclear@1
|
390
|
nuclear@1
|
391
|
nuclear@1
|
392 /*
|
nuclear@1
|
393 * Out-of-line code for Huffman code decoding.
|
nuclear@1
|
394 * See jdhuff.h for info about usage.
|
nuclear@1
|
395 */
|
nuclear@1
|
396
|
nuclear@1
|
397 GLOBAL(int)
|
nuclear@1
|
398 jpeg_huff_decode (bitread_working_state * state,
|
nuclear@1
|
399 register bit_buf_type get_buffer, register int bits_left,
|
nuclear@1
|
400 d_derived_tbl * htbl, int min_bits)
|
nuclear@1
|
401 {
|
nuclear@1
|
402 register int l = min_bits;
|
nuclear@1
|
403 register INT32 code;
|
nuclear@1
|
404
|
nuclear@1
|
405 /* HUFF_DECODE has determined that the code is at least min_bits */
|
nuclear@1
|
406 /* bits long, so fetch that many bits in one swoop. */
|
nuclear@1
|
407
|
nuclear@1
|
408 CHECK_BIT_BUFFER(*state, l, return -1);
|
nuclear@1
|
409 code = GET_BITS(l);
|
nuclear@1
|
410
|
nuclear@1
|
411 /* Collect the rest of the Huffman code one bit at a time. */
|
nuclear@1
|
412 /* This is per Figure F.16 in the JPEG spec. */
|
nuclear@1
|
413
|
nuclear@1
|
414 while (code > htbl->maxcode[l]) {
|
nuclear@1
|
415 code <<= 1;
|
nuclear@1
|
416 CHECK_BIT_BUFFER(*state, 1, return -1);
|
nuclear@1
|
417 code |= GET_BITS(1);
|
nuclear@1
|
418 l++;
|
nuclear@1
|
419 }
|
nuclear@1
|
420
|
nuclear@1
|
421 /* Unload the local registers */
|
nuclear@1
|
422 state->get_buffer = get_buffer;
|
nuclear@1
|
423 state->bits_left = bits_left;
|
nuclear@1
|
424
|
nuclear@1
|
425 /* With garbage input we may reach the sentinel value l = 17. */
|
nuclear@1
|
426
|
nuclear@1
|
427 if (l > 16) {
|
nuclear@1
|
428 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
|
nuclear@1
|
429 return 0; /* fake a zero as the safest result */
|
nuclear@1
|
430 }
|
nuclear@1
|
431
|
nuclear@1
|
432 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
|
nuclear@1
|
433 }
|
nuclear@1
|
434
|
nuclear@1
|
435
|
nuclear@1
|
436 /*
|
nuclear@1
|
437 * Figure F.12: extend sign bit.
|
nuclear@1
|
438 * On some machines, a shift and add will be faster than a table lookup.
|
nuclear@1
|
439 */
|
nuclear@1
|
440
|
nuclear@1
|
441 #ifdef AVOID_TABLES
|
nuclear@1
|
442
|
nuclear@1
|
443 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
|
nuclear@1
|
444
|
nuclear@1
|
445 #else
|
nuclear@1
|
446
|
nuclear@1
|
447 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
|
nuclear@1
|
448
|
nuclear@1
|
449 static const int extend_test[16] = /* entry n is 2**(n-1) */
|
nuclear@1
|
450 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
|
nuclear@1
|
451 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
|
nuclear@1
|
452
|
nuclear@1
|
453 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
|
nuclear@1
|
454 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
|
nuclear@1
|
455 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
|
nuclear@1
|
456 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
|
nuclear@1
|
457 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
|
nuclear@1
|
458
|
nuclear@1
|
459 #endif /* AVOID_TABLES */
|
nuclear@1
|
460
|
nuclear@1
|
461
|
nuclear@1
|
462 /*
|
nuclear@1
|
463 * Check for a restart marker & resynchronize decoder.
|
nuclear@1
|
464 * Returns FALSE if must suspend.
|
nuclear@1
|
465 */
|
nuclear@1
|
466
|
nuclear@1
|
467 LOCAL(boolean)
|
nuclear@1
|
468 process_restart (j_decompress_ptr cinfo)
|
nuclear@1
|
469 {
|
nuclear@1
|
470 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
471 int ci;
|
nuclear@1
|
472
|
nuclear@1
|
473 /* Throw away any unused bits remaining in bit buffer; */
|
nuclear@1
|
474 /* include any full bytes in next_marker's count of discarded bytes */
|
nuclear@1
|
475 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
|
nuclear@1
|
476 entropy->bitstate.bits_left = 0;
|
nuclear@1
|
477
|
nuclear@1
|
478 /* Advance past the RSTn marker */
|
nuclear@1
|
479 if (! (*cinfo->marker->read_restart_marker) (cinfo))
|
nuclear@1
|
480 return FALSE;
|
nuclear@1
|
481
|
nuclear@1
|
482 /* Re-initialize DC predictions to 0 */
|
nuclear@1
|
483 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
nuclear@1
|
484 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@1
|
485
|
nuclear@1
|
486 /* Reset restart counter */
|
nuclear@1
|
487 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@1
|
488
|
nuclear@1
|
489 /* Reset out-of-data flag, unless read_restart_marker left us smack up
|
nuclear@1
|
490 * against a marker. In that case we will end up treating the next data
|
nuclear@1
|
491 * segment as empty, and we can avoid producing bogus output pixels by
|
nuclear@1
|
492 * leaving the flag set.
|
nuclear@1
|
493 */
|
nuclear@1
|
494 if (cinfo->unread_marker == 0)
|
nuclear@1
|
495 entropy->pub.insufficient_data = FALSE;
|
nuclear@1
|
496
|
nuclear@1
|
497 return TRUE;
|
nuclear@1
|
498 }
|
nuclear@1
|
499
|
nuclear@1
|
500
|
nuclear@1
|
501 /*
|
nuclear@1
|
502 * Decode and return one MCU's worth of Huffman-compressed coefficients.
|
nuclear@1
|
503 * The coefficients are reordered from zigzag order into natural array order,
|
nuclear@1
|
504 * but are not dequantized.
|
nuclear@1
|
505 *
|
nuclear@1
|
506 * The i'th block of the MCU is stored into the block pointed to by
|
nuclear@1
|
507 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
|
nuclear@1
|
508 * (Wholesale zeroing is usually a little faster than retail...)
|
nuclear@1
|
509 *
|
nuclear@1
|
510 * Returns FALSE if data source requested suspension. In that case no
|
nuclear@1
|
511 * changes have been made to permanent state. (Exception: some output
|
nuclear@1
|
512 * coefficients may already have been assigned. This is harmless for
|
nuclear@1
|
513 * this module, since we'll just re-assign them on the next call.)
|
nuclear@1
|
514 */
|
nuclear@1
|
515
|
nuclear@1
|
516 METHODDEF(boolean)
|
nuclear@1
|
517 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@1
|
518 {
|
nuclear@1
|
519 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
520 int blkn;
|
nuclear@1
|
521 BITREAD_STATE_VARS;
|
nuclear@1
|
522 savable_state state;
|
nuclear@1
|
523
|
nuclear@1
|
524 /* Process restart marker if needed; may have to suspend */
|
nuclear@1
|
525 if (cinfo->restart_interval) {
|
nuclear@1
|
526 if (entropy->restarts_to_go == 0)
|
nuclear@1
|
527 if (! process_restart(cinfo))
|
nuclear@1
|
528 return FALSE;
|
nuclear@1
|
529 }
|
nuclear@1
|
530
|
nuclear@1
|
531 /* If we've run out of data, just leave the MCU set to zeroes.
|
nuclear@1
|
532 * This way, we return uniform gray for the remainder of the segment.
|
nuclear@1
|
533 */
|
nuclear@1
|
534 if (! entropy->pub.insufficient_data) {
|
nuclear@1
|
535
|
nuclear@1
|
536 /* Load up working state */
|
nuclear@1
|
537 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
|
nuclear@1
|
538 ASSIGN_STATE(state, entropy->saved);
|
nuclear@1
|
539
|
nuclear@1
|
540 /* Outer loop handles each block in the MCU */
|
nuclear@1
|
541
|
nuclear@1
|
542 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@1
|
543 JBLOCKROW block = MCU_data[blkn];
|
nuclear@1
|
544 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
|
nuclear@1
|
545 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
|
nuclear@1
|
546 register int s, k, r;
|
nuclear@1
|
547
|
nuclear@1
|
548 /* Decode a single block's worth of coefficients */
|
nuclear@1
|
549
|
nuclear@1
|
550 /* Section F.2.2.1: decode the DC coefficient difference */
|
nuclear@1
|
551 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
|
nuclear@1
|
552 if (s) {
|
nuclear@1
|
553 CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
nuclear@1
|
554 r = GET_BITS(s);
|
nuclear@1
|
555 s = HUFF_EXTEND(r, s);
|
nuclear@1
|
556 }
|
nuclear@1
|
557
|
nuclear@1
|
558 if (entropy->dc_needed[blkn]) {
|
nuclear@1
|
559 /* Convert DC difference to actual value, update last_dc_val */
|
nuclear@1
|
560 int ci = cinfo->MCU_membership[blkn];
|
nuclear@1
|
561 s += state.last_dc_val[ci];
|
nuclear@1
|
562 state.last_dc_val[ci] = s;
|
nuclear@1
|
563 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
|
nuclear@1
|
564 (*block)[0] = (JCOEF) s;
|
nuclear@1
|
565 }
|
nuclear@1
|
566
|
nuclear@1
|
567 if (entropy->ac_needed[blkn]) {
|
nuclear@1
|
568
|
nuclear@1
|
569 /* Section F.2.2.2: decode the AC coefficients */
|
nuclear@1
|
570 /* Since zeroes are skipped, output area must be cleared beforehand */
|
nuclear@1
|
571 for (k = 1; k < DCTSIZE2; k++) {
|
nuclear@1
|
572 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
|
nuclear@1
|
573
|
nuclear@1
|
574 r = s >> 4;
|
nuclear@1
|
575 s &= 15;
|
nuclear@1
|
576
|
nuclear@1
|
577 if (s) {
|
nuclear@1
|
578 k += r;
|
nuclear@1
|
579 CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
nuclear@1
|
580 r = GET_BITS(s);
|
nuclear@1
|
581 s = HUFF_EXTEND(r, s);
|
nuclear@1
|
582 /* Output coefficient in natural (dezigzagged) order.
|
nuclear@1
|
583 * Note: the extra entries in jpeg_natural_order[] will save us
|
nuclear@1
|
584 * if k >= DCTSIZE2, which could happen if the data is corrupted.
|
nuclear@1
|
585 */
|
nuclear@1
|
586 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
|
nuclear@1
|
587 } else {
|
nuclear@1
|
588 if (r != 15)
|
nuclear@1
|
589 break;
|
nuclear@1
|
590 k += 15;
|
nuclear@1
|
591 }
|
nuclear@1
|
592 }
|
nuclear@1
|
593
|
nuclear@1
|
594 } else {
|
nuclear@1
|
595
|
nuclear@1
|
596 /* Section F.2.2.2: decode the AC coefficients */
|
nuclear@1
|
597 /* In this path we just discard the values */
|
nuclear@1
|
598 for (k = 1; k < DCTSIZE2; k++) {
|
nuclear@1
|
599 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
|
nuclear@1
|
600
|
nuclear@1
|
601 r = s >> 4;
|
nuclear@1
|
602 s &= 15;
|
nuclear@1
|
603
|
nuclear@1
|
604 if (s) {
|
nuclear@1
|
605 k += r;
|
nuclear@1
|
606 CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
nuclear@1
|
607 DROP_BITS(s);
|
nuclear@1
|
608 } else {
|
nuclear@1
|
609 if (r != 15)
|
nuclear@1
|
610 break;
|
nuclear@1
|
611 k += 15;
|
nuclear@1
|
612 }
|
nuclear@1
|
613 }
|
nuclear@1
|
614
|
nuclear@1
|
615 }
|
nuclear@1
|
616 }
|
nuclear@1
|
617
|
nuclear@1
|
618 /* Completed MCU, so update state */
|
nuclear@1
|
619 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
|
nuclear@1
|
620 ASSIGN_STATE(entropy->saved, state);
|
nuclear@1
|
621 }
|
nuclear@1
|
622
|
nuclear@1
|
623 /* Account for restart interval (no-op if not using restarts) */
|
nuclear@1
|
624 entropy->restarts_to_go--;
|
nuclear@1
|
625
|
nuclear@1
|
626 return TRUE;
|
nuclear@1
|
627 }
|
nuclear@1
|
628
|
nuclear@1
|
629
|
nuclear@1
|
630 /*
|
nuclear@1
|
631 * Module initialization routine for Huffman entropy decoding.
|
nuclear@1
|
632 */
|
nuclear@1
|
633
|
nuclear@1
|
634 GLOBAL(void)
|
nuclear@1
|
635 jinit_huff_decoder (j_decompress_ptr cinfo)
|
nuclear@1
|
636 {
|
nuclear@1
|
637 huff_entropy_ptr entropy;
|
nuclear@1
|
638 int i;
|
nuclear@1
|
639
|
nuclear@1
|
640 entropy = (huff_entropy_ptr)
|
nuclear@1
|
641 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
642 SIZEOF(huff_entropy_decoder));
|
nuclear@1
|
643 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
|
nuclear@1
|
644 entropy->pub.start_pass = start_pass_huff_decoder;
|
nuclear@1
|
645 entropy->pub.decode_mcu = decode_mcu;
|
nuclear@1
|
646
|
nuclear@1
|
647 /* Mark tables unallocated */
|
nuclear@1
|
648 for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
nuclear@1
|
649 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
|
nuclear@1
|
650 }
|
nuclear@1
|
651 }
|