4 * This file was part of the Independent JPEG Group's software:
5 * Developed 1997-2009 by Guido Vollbeding.
6 * It was modified by The libjpeg-turbo Project to include only code relevant
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains portable arithmetic entropy decoding routines for JPEG
11 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
13 * Both sequential and progressive modes are supported in this single module.
15 * Suspension is not currently supported in this module.
18 #define JPEG_INTERNALS
23 /* Expanded entropy decoder object for arithmetic decoding. */
26 struct jpeg_entropy_decoder pub; /* public fields */
28 INT32 c; /* C register, base of coding interval + input bit buffer */
29 INT32 a; /* A register, normalized size of coding interval */
30 int ct; /* bit shift counter, # of bits left in bit buffer part of C */
34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
37 unsigned int restarts_to_go; /* MCUs left in this restart interval */
39 /* Pointers to statistics areas (these workspaces have image lifespan) */
40 unsigned char * dc_stats[NUM_ARITH_TBLS];
41 unsigned char * ac_stats[NUM_ARITH_TBLS];
43 /* Statistics bin for coding with fixed probability 0.5 */
44 unsigned char fixed_bin[4];
45 } arith_entropy_decoder;
47 typedef arith_entropy_decoder * arith_entropy_ptr;
49 /* The following two definitions specify the allocation chunk size
50 * for the statistics area.
51 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
52 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
54 * We use a compact representation with 1 byte per statistics bin,
55 * thus the numbers directly represent byte sizes.
56 * This 1 byte per statistics bin contains the meaning of the MPS
57 * (more probable symbol) in the highest bit (mask 0x80), and the
58 * index into the probability estimation state machine table
59 * in the lower bits (mask 0x7F).
62 #define DC_STAT_BINS 64
63 #define AC_STAT_BINS 256
67 get_byte (j_decompress_ptr cinfo)
68 /* Read next input byte; we do not support suspension in this module. */
70 struct jpeg_source_mgr * src = cinfo->src;
72 if (src->bytes_in_buffer == 0)
73 if (! (*src->fill_input_buffer) (cinfo))
74 ERREXIT(cinfo, JERR_CANT_SUSPEND);
75 src->bytes_in_buffer--;
76 return GETJOCTET(*src->next_input_byte++);
81 * The core arithmetic decoding routine (common in JPEG and JBIG).
82 * This needs to go as fast as possible.
83 * Machine-dependent optimization facilities
84 * are not utilized in this portable implementation.
85 * However, this code should be fairly efficient and
86 * may be a good base for further optimizations anyway.
88 * Return value is 0 or 1 (binary decision).
90 * Note: I've changed the handling of the code base & bit
91 * buffer register C compared to other implementations
92 * based on the standards layout & procedures.
93 * While it also contains both the actual base of the
94 * coding interval (16 bits) and the next-bits buffer,
95 * the cut-point between these two parts is floating
96 * (instead of fixed) with the bit shift counter CT.
97 * Thus, we also need only one (variable instead of
98 * fixed size) shift for the LPS/MPS decision, and
99 * we can get away with any renormalization update
100 * of C (except for new data insertion, of course).
102 * I've also introduced a new scheme for accessing
103 * the probability estimation state machine table,
104 * derived from Markus Kuhn's JBIG implementation.
108 arith_decode (j_decompress_ptr cinfo, unsigned char *st)
110 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
111 register unsigned char nl, nm;
112 register INT32 qe, temp;
113 register int sv, data;
115 /* Renormalization & data input per section D.2.6 */
116 while (e->a < 0x8000L) {
118 /* Need to fetch next data byte */
119 if (cinfo->unread_marker)
120 data = 0; /* stuff zero data */
122 data = get_byte(cinfo); /* read next input byte */
123 if (data == 0xFF) { /* zero stuff or marker code */
124 do data = get_byte(cinfo);
125 while (data == 0xFF); /* swallow extra 0xFF bytes */
127 data = 0xFF; /* discard stuffed zero byte */
129 /* Note: Different from the Huffman decoder, hitting
130 * a marker while processing the compressed data
131 * segment is legal in arithmetic coding.
132 * The convention is to supply zero data
133 * then until decoding is complete.
135 cinfo->unread_marker = data;
140 e->c = (e->c << 8) | data; /* insert data into C register */
141 if ((e->ct += 8) < 0) /* update bit shift counter */
142 /* Need more initial bytes */
144 /* Got 2 initial bytes -> re-init A and exit loop */
145 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
150 /* Fetch values from our compact representation of Table D.2:
151 * Qe values and probability estimation state machine
154 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
155 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
156 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
158 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
164 /* Conditional LPS (less probable symbol) exchange */
167 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
170 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
171 sv ^= 0x80; /* Exchange LPS/MPS */
173 } else if (e->a < 0x8000L) {
174 /* Conditional MPS (more probable symbol) exchange */
176 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
177 sv ^= 0x80; /* Exchange LPS/MPS */
179 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
188 * Check for a restart marker & resynchronize decoder.
192 process_restart (j_decompress_ptr cinfo)
194 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
196 jpeg_component_info * compptr;
198 /* Advance past the RSTn marker */
199 if (! (*cinfo->marker->read_restart_marker) (cinfo))
200 ERREXIT(cinfo, JERR_CANT_SUSPEND);
202 /* Re-initialize statistics areas */
203 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
204 compptr = cinfo->cur_comp_info[ci];
205 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
206 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
207 /* Reset DC predictions to 0 */
208 entropy->last_dc_val[ci] = 0;
209 entropy->dc_context[ci] = 0;
211 if (! cinfo->progressive_mode || cinfo->Ss) {
212 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
216 /* Reset arithmetic decoding variables */
219 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
221 /* Reset restart counter */
222 entropy->restarts_to_go = cinfo->restart_interval;
227 * Arithmetic MCU decoding.
228 * Each of these routines decodes and returns one MCU's worth of
229 * arithmetic-compressed coefficients.
230 * The coefficients are reordered from zigzag order into natural array order,
231 * but are not dequantized.
233 * The i'th block of the MCU is stored into the block pointed to by
234 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
238 * MCU decoding for DC initial scan (either spectral selection,
239 * or first pass of successive approximation).
243 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
245 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
248 int blkn, ci, tbl, sign;
251 /* Process restart marker if needed */
252 if (cinfo->restart_interval) {
253 if (entropy->restarts_to_go == 0)
254 process_restart(cinfo);
255 entropy->restarts_to_go--;
258 if (entropy->ct == -1) return TRUE; /* if error do nothing */
260 /* Outer loop handles each block in the MCU */
262 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
263 block = MCU_data[blkn];
264 ci = cinfo->MCU_membership[blkn];
265 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
267 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
269 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
270 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
272 /* Figure F.19: Decode_DC_DIFF */
273 if (arith_decode(cinfo, st) == 0)
274 entropy->dc_context[ci] = 0;
276 /* Figure F.21: Decoding nonzero value v */
277 /* Figure F.22: Decoding the sign of v */
278 sign = arith_decode(cinfo, st + 1);
280 /* Figure F.23: Decoding the magnitude category of v */
281 if ((m = arith_decode(cinfo, st)) != 0) {
282 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
283 while (arith_decode(cinfo, st)) {
284 if ((m <<= 1) == 0x8000) {
285 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
286 entropy->ct = -1; /* magnitude overflow */
292 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
293 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
294 entropy->dc_context[ci] = 0; /* zero diff category */
295 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
296 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
298 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
300 /* Figure F.24: Decoding the magnitude bit pattern of v */
303 if (arith_decode(cinfo, st)) v |= m;
304 v += 1; if (sign) v = -v;
305 entropy->last_dc_val[ci] += v;
308 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
309 (*block)[0] = (JCOEF) LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
317 * MCU decoding for AC initial scan (either spectral selection,
318 * or first pass of successive approximation).
322 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
324 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
330 /* Process restart marker if needed */
331 if (cinfo->restart_interval) {
332 if (entropy->restarts_to_go == 0)
333 process_restart(cinfo);
334 entropy->restarts_to_go--;
337 if (entropy->ct == -1) return TRUE; /* if error do nothing */
339 /* There is always only one block per MCU */
341 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
343 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
345 /* Figure F.20: Decode_AC_coefficients */
346 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
347 st = entropy->ac_stats[tbl] + 3 * (k - 1);
348 if (arith_decode(cinfo, st)) break; /* EOB flag */
349 while (arith_decode(cinfo, st + 1) == 0) {
352 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
353 entropy->ct = -1; /* spectral overflow */
357 /* Figure F.21: Decoding nonzero value v */
358 /* Figure F.22: Decoding the sign of v */
359 sign = arith_decode(cinfo, entropy->fixed_bin);
361 /* Figure F.23: Decoding the magnitude category of v */
362 if ((m = arith_decode(cinfo, st)) != 0) {
363 if (arith_decode(cinfo, st)) {
365 st = entropy->ac_stats[tbl] +
366 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
367 while (arith_decode(cinfo, st)) {
368 if ((m <<= 1) == 0x8000) {
369 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
370 entropy->ct = -1; /* magnitude overflow */
378 /* Figure F.24: Decoding the magnitude bit pattern of v */
381 if (arith_decode(cinfo, st)) v |= m;
382 v += 1; if (sign) v = -v;
383 /* Scale and output coefficient in natural (dezigzagged) order */
384 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
392 * MCU decoding for DC successive approximation refinement scan.
396 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
398 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
402 /* Process restart marker if needed */
403 if (cinfo->restart_interval) {
404 if (entropy->restarts_to_go == 0)
405 process_restart(cinfo);
406 entropy->restarts_to_go--;
409 st = entropy->fixed_bin; /* use fixed probability estimation */
410 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
412 /* Outer loop handles each block in the MCU */
414 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
415 /* Encoded data is simply the next bit of the two's-complement DC value */
416 if (arith_decode(cinfo, st))
417 MCU_data[blkn][0][0] |= p1;
425 * MCU decoding for AC successive approximation refinement scan.
429 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
431 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
438 /* Process restart marker if needed */
439 if (cinfo->restart_interval) {
440 if (entropy->restarts_to_go == 0)
441 process_restart(cinfo);
442 entropy->restarts_to_go--;
445 if (entropy->ct == -1) return TRUE; /* if error do nothing */
447 /* There is always only one block per MCU */
449 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
451 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
452 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
454 /* Establish EOBx (previous stage end-of-block) index */
455 for (kex = cinfo->Se; kex > 0; kex--)
456 if ((*block)[jpeg_natural_order[kex]]) break;
458 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
459 st = entropy->ac_stats[tbl] + 3 * (k - 1);
461 if (arith_decode(cinfo, st)) break; /* EOB flag */
463 thiscoef = *block + jpeg_natural_order[k];
464 if (*thiscoef) { /* previously nonzero coef */
465 if (arith_decode(cinfo, st + 2)) {
473 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
474 if (arith_decode(cinfo, entropy->fixed_bin))
482 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
483 entropy->ct = -1; /* spectral overflow */
494 * Decode one MCU's worth of arithmetic-compressed coefficients.
498 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
500 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
501 jpeg_component_info * compptr;
504 int blkn, ci, tbl, sign, k;
507 /* Process restart marker if needed */
508 if (cinfo->restart_interval) {
509 if (entropy->restarts_to_go == 0)
510 process_restart(cinfo);
511 entropy->restarts_to_go--;
514 if (entropy->ct == -1) return TRUE; /* if error do nothing */
516 /* Outer loop handles each block in the MCU */
518 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
519 block = MCU_data[blkn];
520 ci = cinfo->MCU_membership[blkn];
521 compptr = cinfo->cur_comp_info[ci];
523 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
525 tbl = compptr->dc_tbl_no;
527 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
528 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
530 /* Figure F.19: Decode_DC_DIFF */
531 if (arith_decode(cinfo, st) == 0)
532 entropy->dc_context[ci] = 0;
534 /* Figure F.21: Decoding nonzero value v */
535 /* Figure F.22: Decoding the sign of v */
536 sign = arith_decode(cinfo, st + 1);
538 /* Figure F.23: Decoding the magnitude category of v */
539 if ((m = arith_decode(cinfo, st)) != 0) {
540 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
541 while (arith_decode(cinfo, st)) {
542 if ((m <<= 1) == 0x8000) {
543 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
544 entropy->ct = -1; /* magnitude overflow */
550 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
551 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
552 entropy->dc_context[ci] = 0; /* zero diff category */
553 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
554 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
556 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
558 /* Figure F.24: Decoding the magnitude bit pattern of v */
561 if (arith_decode(cinfo, st)) v |= m;
562 v += 1; if (sign) v = -v;
563 entropy->last_dc_val[ci] += v;
566 (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
568 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
570 tbl = compptr->ac_tbl_no;
572 /* Figure F.20: Decode_AC_coefficients */
573 for (k = 1; k <= DCTSIZE2 - 1; k++) {
574 st = entropy->ac_stats[tbl] + 3 * (k - 1);
575 if (arith_decode(cinfo, st)) break; /* EOB flag */
576 while (arith_decode(cinfo, st + 1) == 0) {
578 if (k > DCTSIZE2 - 1) {
579 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
580 entropy->ct = -1; /* spectral overflow */
584 /* Figure F.21: Decoding nonzero value v */
585 /* Figure F.22: Decoding the sign of v */
586 sign = arith_decode(cinfo, entropy->fixed_bin);
588 /* Figure F.23: Decoding the magnitude category of v */
589 if ((m = arith_decode(cinfo, st)) != 0) {
590 if (arith_decode(cinfo, st)) {
592 st = entropy->ac_stats[tbl] +
593 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
594 while (arith_decode(cinfo, st)) {
595 if ((m <<= 1) == 0x8000) {
596 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
597 entropy->ct = -1; /* magnitude overflow */
605 /* Figure F.24: Decoding the magnitude bit pattern of v */
608 if (arith_decode(cinfo, st)) v |= m;
609 v += 1; if (sign) v = -v;
610 (*block)[jpeg_natural_order[k]] = (JCOEF) v;
619 * Initialize for an arithmetic-compressed scan.
623 start_pass (j_decompress_ptr cinfo)
625 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
627 jpeg_component_info * compptr;
629 if (cinfo->progressive_mode) {
630 /* Validate progressive scan parameters */
631 if (cinfo->Ss == 0) {
635 /* need not check Ss/Se < 0 since they came from unsigned bytes */
636 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
638 /* AC scans may have only one component */
639 if (cinfo->comps_in_scan != 1)
642 if (cinfo->Ah != 0) {
643 /* Successive approximation refinement scan: must have Al = Ah-1. */
644 if (cinfo->Ah-1 != cinfo->Al)
647 if (cinfo->Al > 13) { /* need not check for < 0 */
649 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
650 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
652 /* Update progression status, and verify that scan order is legal.
653 * Note that inter-scan inconsistencies are treated as warnings
654 * not fatal errors ... not clear if this is right way to behave.
656 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
657 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
658 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
659 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
660 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
661 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
662 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
663 if (cinfo->Ah != expected)
664 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
665 coef_bit_ptr[coefi] = cinfo->Al;
668 /* Select MCU decoding routine */
669 if (cinfo->Ah == 0) {
671 entropy->pub.decode_mcu = decode_mcu_DC_first;
673 entropy->pub.decode_mcu = decode_mcu_AC_first;
676 entropy->pub.decode_mcu = decode_mcu_DC_refine;
678 entropy->pub.decode_mcu = decode_mcu_AC_refine;
681 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
682 * This ought to be an error condition, but we make it a warning.
684 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
685 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
686 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
687 /* Select MCU decoding routine */
688 entropy->pub.decode_mcu = decode_mcu;
691 /* Allocate & initialize requested statistics areas */
692 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
693 compptr = cinfo->cur_comp_info[ci];
694 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
695 tbl = compptr->dc_tbl_no;
696 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
697 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
698 if (entropy->dc_stats[tbl] == NULL)
699 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
700 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
701 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
702 /* Initialize DC predictions to 0 */
703 entropy->last_dc_val[ci] = 0;
704 entropy->dc_context[ci] = 0;
706 if (! cinfo->progressive_mode || cinfo->Ss) {
707 tbl = compptr->ac_tbl_no;
708 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
709 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
710 if (entropy->ac_stats[tbl] == NULL)
711 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
712 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
713 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
717 /* Initialize arithmetic decoding variables */
720 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
722 /* Initialize restart counter */
723 entropy->restarts_to_go = cinfo->restart_interval;
728 * Module initialization routine for arithmetic entropy decoding.
732 jinit_arith_decoder (j_decompress_ptr cinfo)
734 arith_entropy_ptr entropy;
737 entropy = (arith_entropy_ptr)
738 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
739 sizeof(arith_entropy_decoder));
740 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
741 entropy->pub.start_pass = start_pass;
743 /* Mark tables unallocated */
744 for (i = 0; i < NUM_ARITH_TBLS; i++) {
745 entropy->dc_stats[i] = NULL;
746 entropy->ac_stats[i] = NULL;
749 /* Initialize index for fixed probability estimation */
750 entropy->fixed_bin[0] = 113;
752 if (cinfo->progressive_mode) {
753 /* Create progression status table */
754 int *coef_bit_ptr, ci;
755 cinfo->coef_bits = (int (*)[DCTSIZE2])
756 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
757 cinfo->num_components*DCTSIZE2*sizeof(int));
758 coef_bit_ptr = & cinfo->coef_bits[0][0];
759 for (ci = 0; ci < cinfo->num_components; ci++)
760 for (i = 0; i < DCTSIZE2; i++)
761 *coef_bit_ptr++ = -1;