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