1/*
2 * jdphuff.c
3 *
4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1995-1997, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015-2016, 2018-2021, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
10 *
11 * This file contains Huffman entropy decoding routines for progressive JPEG.
12 *
13 * Much of the complexity here has to do with supporting input suspension.
14 * If the data source module demands suspension, we want to be able to back
15 * up to the start of the current MCU. To do this, we copy state variables
16 * into local working storage, and update them back to the permanent
17 * storage only upon successful completion of an MCU.
18 *
19 * NOTE: All referenced figures are from
20 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
21 */
22
23#define JPEG_INTERNALS
24#include "jinclude.h"
25#include "jpeglib.h"
26#include "jdhuff.h" /* Declarations shared with jdhuff.c */
27#include <limits.h>
28
29
30#ifdef D_PROGRESSIVE_SUPPORTED
31
32/*
33 * Expanded entropy decoder object for progressive Huffman decoding.
34 *
35 * The savable_state subrecord contains fields that change within an MCU,
36 * but must not be updated permanently until we complete the MCU.
37 */
38
39typedef struct {
40 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
41 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
42} savable_state;
43
44typedef struct {
45 struct jpeg_entropy_decoder pub; /* public fields */
46
47 /* These fields are loaded into local variables at start of each MCU.
48 * In case of suspension, we exit WITHOUT updating them.
49 */
50 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
51 savable_state saved; /* Other state at start of MCU */
52
53 /* These fields are NOT loaded into local working state. */
54 unsigned int restarts_to_go; /* MCUs left in this restart interval */
55
56 /* Pointers to derived tables (these workspaces have image lifespan) */
57 d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
58
59 d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
60} phuff_entropy_decoder;
61
62typedef phuff_entropy_decoder *phuff_entropy_ptr;
63
64/* Forward declarations */
65METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo,
66 JBLOCKROW *MCU_data);
67METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo,
68 JBLOCKROW *MCU_data);
69METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo,
70 JBLOCKROW *MCU_data);
71METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo,
72 JBLOCKROW *MCU_data);
73
74
75/*
76 * Initialize for a Huffman-compressed scan.
77 */
78
79METHODDEF(void)
80start_pass_phuff_decoder(j_decompress_ptr cinfo)
81{
82 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
83 boolean is_DC_band, bad;
84 int ci, coefi, tbl;
85 d_derived_tbl **pdtbl;
86 int *coef_bit_ptr, *prev_coef_bit_ptr;
87 jpeg_component_info *compptr;
88
89 is_DC_band = (cinfo->Ss == 0);
90
91 /* Validate scan parameters */
92 bad = FALSE;
93 if (is_DC_band) {
94 if (cinfo->Se != 0)
95 bad = TRUE;
96 } else {
97 /* need not check Ss/Se < 0 since they came from unsigned bytes */
98 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
99 bad = TRUE;
100 /* AC scans may have only one component */
101 if (cinfo->comps_in_scan != 1)
102 bad = TRUE;
103 }
104 if (cinfo->Ah != 0) {
105 /* Successive approximation refinement scan: must have Al = Ah-1. */
106 if (cinfo->Al != cinfo->Ah - 1)
107 bad = TRUE;
108 }
109 if (cinfo->Al > 13) /* need not check for < 0 */
110 bad = TRUE;
111 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
112 * but the spec doesn't say so, and we try to be liberal about what we
113 * accept. Note: large Al values could result in out-of-range DC
114 * coefficients during early scans, leading to bizarre displays due to
115 * overflows in the IDCT math. But we won't crash.
116 */
117 if (bad)
118 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
119 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
120 /* Update progression status, and verify that scan order is legal.
121 * Note that inter-scan inconsistencies are treated as warnings
122 * not fatal errors ... not clear if this is right way to behave.
123 */
124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125 int cindex = cinfo->cur_comp_info[ci]->component_index;
126 coef_bit_ptr = &cinfo->coef_bits[cindex][0];
127 prev_coef_bit_ptr = &cinfo->coef_bits[cindex + cinfo->num_components][0];
128 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
129 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
130 for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {
131 if (cinfo->input_scan_number > 1)
132 prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];
133 else
134 prev_coef_bit_ptr[coefi] = 0;
135 }
136 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
137 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
138 if (cinfo->Ah != expected)
139 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
140 coef_bit_ptr[coefi] = cinfo->Al;
141 }
142 }
143
144 /* Select MCU decoding routine */
145 if (cinfo->Ah == 0) {
146 if (is_DC_band)
147 entropy->pub.decode_mcu = decode_mcu_DC_first;
148 else
149 entropy->pub.decode_mcu = decode_mcu_AC_first;
150 } else {
151 if (is_DC_band)
152 entropy->pub.decode_mcu = decode_mcu_DC_refine;
153 else
154 entropy->pub.decode_mcu = decode_mcu_AC_refine;
155 }
156
157 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
158 compptr = cinfo->cur_comp_info[ci];
159 /* Make sure requested tables are present, and compute derived tables.
160 * We may build same derived table more than once, but it's not expensive.
161 */
162 if (is_DC_band) {
163 if (cinfo->Ah == 0) { /* DC refinement needs no table */
164 tbl = compptr->dc_tbl_no;
165 pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
166 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
167 }
168 } else {
169 tbl = compptr->ac_tbl_no;
170 pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
171 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
172 /* remember the single active table */
173 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
174 }
175 /* Initialize DC predictions to 0 */
176 entropy->saved.last_dc_val[ci] = 0;
177 }
178
179 /* Initialize bitread state variables */
180 entropy->bitstate.bits_left = 0;
181 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
182 entropy->pub.insufficient_data = FALSE;
183
184 /* Initialize private state variables */
185 entropy->saved.EOBRUN = 0;
186
187 /* Initialize restart counter */
188 entropy->restarts_to_go = cinfo->restart_interval;
189}
190
191
192/*
193 * Figure F.12: extend sign bit.
194 * On some machines, a shift and add will be faster than a table lookup.
195 */
196
197#define AVOID_TABLES
198#ifdef AVOID_TABLES
199
200#define NEG_1 ((unsigned)-1)
201#define HUFF_EXTEND(x, s) \
202 ((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x))
203
204#else
205
206#define HUFF_EXTEND(x, s) \
207 ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
208
209static const int extend_test[16] = { /* entry n is 2**(n-1) */
210 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
211 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
212};
213
214static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */
215 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
216 ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
217 ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
218 ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
219};
220
221#endif /* AVOID_TABLES */
222
223
224/*
225 * Check for a restart marker & resynchronize decoder.
226 * Returns FALSE if must suspend.
227 */
228
229LOCAL(boolean)
230process_restart(j_decompress_ptr cinfo)
231{
232 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
233 int ci;
234
235 /* Throw away any unused bits remaining in bit buffer; */
236 /* include any full bytes in next_marker's count of discarded bytes */
237 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238 entropy->bitstate.bits_left = 0;
239
240 /* Advance past the RSTn marker */
241 if (!(*cinfo->marker->read_restart_marker) (cinfo))
242 return FALSE;
243
244 /* Re-initialize DC predictions to 0 */
245 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246 entropy->saved.last_dc_val[ci] = 0;
247 /* Re-init EOB run count, too */
248 entropy->saved.EOBRUN = 0;
249
250 /* Reset restart counter */
251 entropy->restarts_to_go = cinfo->restart_interval;
252
253 /* Reset out-of-data flag, unless read_restart_marker left us smack up
254 * against a marker. In that case we will end up treating the next data
255 * segment as empty, and we can avoid producing bogus output pixels by
256 * leaving the flag set.
257 */
258 if (cinfo->unread_marker == 0)
259 entropy->pub.insufficient_data = FALSE;
260
261 return TRUE;
262}
263
264
265/*
266 * Huffman MCU decoding.
267 * Each of these routines decodes and returns one MCU's worth of
268 * Huffman-compressed coefficients.
269 * The coefficients are reordered from zigzag order into natural array order,
270 * but are not dequantized.
271 *
272 * The i'th block of the MCU is stored into the block pointed to by
273 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
274 *
275 * We return FALSE if data source requested suspension. In that case no
276 * changes have been made to permanent state. (Exception: some output
277 * coefficients may already have been assigned. This is harmless for
278 * spectral selection, since we'll just re-assign them on the next call.
279 * Successive approximation AC refinement has to be more careful, however.)
280 */
281
282/*
283 * MCU decoding for DC initial scan (either spectral selection,
284 * or first pass of successive approximation).
285 */
286
287METHODDEF(boolean)
288decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
289{
290 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
291 int Al = cinfo->Al;
292 register int s, r;
293 int blkn, ci;
294 JBLOCKROW block;
295 BITREAD_STATE_VARS;
296 savable_state state;
297 d_derived_tbl *tbl;
298 jpeg_component_info *compptr;
299
300 /* Process restart marker if needed; may have to suspend */
301 if (cinfo->restart_interval) {
302 if (entropy->restarts_to_go == 0)
303 if (!process_restart(cinfo))
304 return FALSE;
305 }
306
307 /* If we've run out of data, just leave the MCU set to zeroes.
308 * This way, we return uniform gray for the remainder of the segment.
309 */
310 if (!entropy->pub.insufficient_data) {
311
312 /* Load up working state */
313 BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
314 state = entropy->saved;
315
316 /* Outer loop handles each block in the MCU */
317
318 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
319 block = MCU_data[blkn];
320 ci = cinfo->MCU_membership[blkn];
321 compptr = cinfo->cur_comp_info[ci];
322 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
323
324 /* Decode a single block's worth of coefficients */
325
326 /* Section F.2.2.1: decode the DC coefficient difference */
327 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
328 if (s) {
329 CHECK_BIT_BUFFER(br_state, s, return FALSE);
330 r = GET_BITS(s);
331 s = HUFF_EXTEND(r, s);
332 }
333
334 /* Convert DC difference to actual value, update last_dc_val */
335 if ((state.last_dc_val[ci] >= 0 &&
336 s > INT_MAX - state.last_dc_val[ci]) ||
337 (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
338 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
339 s += state.last_dc_val[ci];
340 state.last_dc_val[ci] = s;
341 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
342 (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
343 }
344
345 /* Completed MCU, so update state */
346 BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
347 entropy->saved = state;
348 }
349
350 /* Account for restart interval (no-op if not using restarts) */
351 if (cinfo->restart_interval)
352 entropy->restarts_to_go--;
353
354 return TRUE;
355}
356
357
358/*
359 * MCU decoding for AC initial scan (either spectral selection,
360 * or first pass of successive approximation).
361 */
362
363METHODDEF(boolean)
364decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
365{
366 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
367 int Se = cinfo->Se;
368 int Al = cinfo->Al;
369 register int s, k, r;
370 unsigned int EOBRUN;
371 JBLOCKROW block;
372 BITREAD_STATE_VARS;
373 d_derived_tbl *tbl;
374
375 /* Process restart marker if needed; may have to suspend */
376 if (cinfo->restart_interval) {
377 if (entropy->restarts_to_go == 0)
378 if (!process_restart(cinfo))
379 return FALSE;
380 }
381
382 /* If we've run out of data, just leave the MCU set to zeroes.
383 * This way, we return uniform gray for the remainder of the segment.
384 */
385 if (!entropy->pub.insufficient_data) {
386
387 /* Load up working state.
388 * We can avoid loading/saving bitread state if in an EOB run.
389 */
390 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
391
392 /* There is always only one block per MCU */
393
394 if (EOBRUN > 0) /* if it's a band of zeroes... */
395 EOBRUN--; /* ...process it now (we do nothing) */
396 else {
397 BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
398 block = MCU_data[0];
399 tbl = entropy->ac_derived_tbl;
400
401 for (k = cinfo->Ss; k <= Se; k++) {
402 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
403 r = s >> 4;
404 s &= 15;
405 if (s) {
406 k += r;
407 CHECK_BIT_BUFFER(br_state, s, return FALSE);
408 r = GET_BITS(s);
409 s = HUFF_EXTEND(r, s);
410 /* Scale and output coefficient in natural (dezigzagged) order */
411 (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
412 } else {
413 if (r == 15) { /* ZRL */
414 k += 15; /* skip 15 zeroes in band */
415 } else { /* EOBr, run length is 2^r + appended bits */
416 EOBRUN = 1 << r;
417 if (r) { /* EOBr, r > 0 */
418 CHECK_BIT_BUFFER(br_state, r, return FALSE);
419 r = GET_BITS(r);
420 EOBRUN += r;
421 }
422 EOBRUN--; /* this band is processed at this moment */
423 break; /* force end-of-band */
424 }
425 }
426 }
427
428 BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
429 }
430
431 /* Completed MCU, so update state */
432 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
433 }
434
435 /* Account for restart interval (no-op if not using restarts) */
436 if (cinfo->restart_interval)
437 entropy->restarts_to_go--;
438
439 return TRUE;
440}
441
442
443/*
444 * MCU decoding for DC successive approximation refinement scan.
445 * Note: we assume such scans can be multi-component, although the spec
446 * is not very clear on the point.
447 */
448
449METHODDEF(boolean)
450decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
451{
452 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
453 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
454 int blkn;
455 JBLOCKROW block;
456 BITREAD_STATE_VARS;
457
458 /* Process restart marker if needed; may have to suspend */
459 if (cinfo->restart_interval) {
460 if (entropy->restarts_to_go == 0)
461 if (!process_restart(cinfo))
462 return FALSE;
463 }
464
465 /* Not worth the cycles to check insufficient_data here,
466 * since we will not change the data anyway if we read zeroes.
467 */
468
469 /* Load up working state */
470 BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
471
472 /* Outer loop handles each block in the MCU */
473
474 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
475 block = MCU_data[blkn];
476
477 /* Encoded data is simply the next bit of the two's-complement DC value */
478 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
479 if (GET_BITS(1))
480 (*block)[0] |= p1;
481 /* Note: since we use |=, repeating the assignment later is safe */
482 }
483
484 /* Completed MCU, so update state */
485 BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
486
487 /* Account for restart interval (no-op if not using restarts) */
488 if (cinfo->restart_interval)
489 entropy->restarts_to_go--;
490
491 return TRUE;
492}
493
494
495/*
496 * MCU decoding for AC successive approximation refinement scan.
497 */
498
499METHODDEF(boolean)
500decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
501{
502 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
503 int Se = cinfo->Se;
504 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
505 int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
506 register int s, k, r;
507 unsigned int EOBRUN;
508 JBLOCKROW block;
509 JCOEFPTR thiscoef;
510 BITREAD_STATE_VARS;
511 d_derived_tbl *tbl;
512 int num_newnz;
513 int newnz_pos[DCTSIZE2];
514
515 /* Process restart marker if needed; may have to suspend */
516 if (cinfo->restart_interval) {
517 if (entropy->restarts_to_go == 0)
518 if (!process_restart(cinfo))
519 return FALSE;
520 }
521
522 /* If we've run out of data, don't modify the MCU.
523 */
524 if (!entropy->pub.insufficient_data) {
525
526 /* Load up working state */
527 BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
528 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
529
530 /* There is always only one block per MCU */
531 block = MCU_data[0];
532 tbl = entropy->ac_derived_tbl;
533
534 /* If we are forced to suspend, we must undo the assignments to any newly
535 * nonzero coefficients in the block, because otherwise we'd get confused
536 * next time about which coefficients were already nonzero.
537 * But we need not undo addition of bits to already-nonzero coefficients;
538 * instead, we can test the current bit to see if we already did it.
539 */
540 num_newnz = 0;
541
542 /* initialize coefficient loop counter to start of band */
543 k = cinfo->Ss;
544
545 if (EOBRUN == 0) {
546 for (; k <= Se; k++) {
547 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
548 r = s >> 4;
549 s &= 15;
550 if (s) {
551 if (s != 1) /* size of new coef should always be 1 */
552 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
553 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
554 if (GET_BITS(1))
555 s = p1; /* newly nonzero coef is positive */
556 else
557 s = m1; /* newly nonzero coef is negative */
558 } else {
559 if (r != 15) {
560 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
561 if (r) {
562 CHECK_BIT_BUFFER(br_state, r, goto undoit);
563 r = GET_BITS(r);
564 EOBRUN += r;
565 }
566 break; /* rest of block is handled by EOB logic */
567 }
568 /* note s = 0 for processing ZRL */
569 }
570 /* Advance over already-nonzero coefs and r still-zero coefs,
571 * appending correction bits to the nonzeroes. A correction bit is 1
572 * if the absolute value of the coefficient must be increased.
573 */
574 do {
575 thiscoef = *block + jpeg_natural_order[k];
576 if (*thiscoef != 0) {
577 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
578 if (GET_BITS(1)) {
579 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
580 if (*thiscoef >= 0)
581 *thiscoef += p1;
582 else
583 *thiscoef += m1;
584 }
585 }
586 } else {
587 if (--r < 0)
588 break; /* reached target zero coefficient */
589 }
590 k++;
591 } while (k <= Se);
592 if (s) {
593 int pos = jpeg_natural_order[k];
594 /* Output newly nonzero coefficient */
595 (*block)[pos] = (JCOEF)s;
596 /* Remember its position in case we have to suspend */
597 newnz_pos[num_newnz++] = pos;
598 }
599 }
600 }
601
602 if (EOBRUN > 0) {
603 /* Scan any remaining coefficient positions after the end-of-band
604 * (the last newly nonzero coefficient, if any). Append a correction
605 * bit to each already-nonzero coefficient. A correction bit is 1
606 * if the absolute value of the coefficient must be increased.
607 */
608 for (; k <= Se; k++) {
609 thiscoef = *block + jpeg_natural_order[k];
610 if (*thiscoef != 0) {
611 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
612 if (GET_BITS(1)) {
613 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
614 if (*thiscoef >= 0)
615 *thiscoef += p1;
616 else
617 *thiscoef += m1;
618 }
619 }
620 }
621 }
622 /* Count one block completed in EOB run */
623 EOBRUN--;
624 }
625
626 /* Completed MCU, so update state */
627 BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
628 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
629 }
630
631 /* Account for restart interval (no-op if not using restarts) */
632 if (cinfo->restart_interval)
633 entropy->restarts_to_go--;
634
635 return TRUE;
636
637undoit:
638 /* Re-zero any output coefficients that we made newly nonzero */
639 while (num_newnz > 0)
640 (*block)[newnz_pos[--num_newnz]] = 0;
641
642 return FALSE;
643}
644
645
646/*
647 * Module initialization routine for progressive Huffman entropy decoding.
648 */
649
650GLOBAL(void)
651jinit_phuff_decoder(j_decompress_ptr cinfo)
652{
653 phuff_entropy_ptr entropy;
654 int *coef_bit_ptr;
655 int ci, i;
656
657 entropy = (phuff_entropy_ptr)
658 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
659 sizeof(phuff_entropy_decoder));
660 cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
661 entropy->pub.start_pass = start_pass_phuff_decoder;
662
663 /* Mark derived tables unallocated */
664 for (i = 0; i < NUM_HUFF_TBLS; i++) {
665 entropy->derived_tbls[i] = NULL;
666 }
667
668 /* Create progression status table */
669 cinfo->coef_bits = (int (*)[DCTSIZE2])
670 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
671 cinfo->num_components * 2 * DCTSIZE2 *
672 sizeof(int));
673 coef_bit_ptr = &cinfo->coef_bits[0][0];
674 for (ci = 0; ci < cinfo->num_components; ci++)
675 for (i = 0; i < DCTSIZE2; i++)
676 *coef_bit_ptr++ = -1;
677}
678
679#endif /* D_PROGRESSIVE_SUPPORTED */
680