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