4 * Developed 1997-2009 by Guido Vollbeding.
\r
5 * This file is part of the Independent JPEG Group's software.
\r
6 * For conditions of distribution and use, see the accompanying README file.
\r
8 * This file contains portable arithmetic entropy decoding routines for JPEG
\r
9 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
\r
11 * Both sequential and progressive modes are supported in this single module.
\r
13 * Suspension is not currently supported in this module.
\r
16 #define JPEG_INTERNALS
\r
17 #include "jinclude.h"
\r
18 #include "jpeglib.h"
\r
21 /* Expanded entropy decoder object for arithmetic decoding. */
\r
24 struct jpeg_entropy_decoder pub; /* public fields */
\r
26 INT32 c; /* C register, base of coding interval + input bit buffer */
\r
27 INT32 a; /* A register, normalized size of coding interval */
\r
28 int ct; /* bit shift counter, # of bits left in bit buffer part of C */
\r
29 /* init: ct = -16 */
\r
30 /* run: ct = 0..7 */
\r
31 /* error: ct = -1 */
\r
32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
\r
33 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
\r
35 unsigned int restarts_to_go; /* MCUs left in this restart interval */
\r
37 /* Pointers to statistics areas (these workspaces have image lifespan) */
\r
38 unsigned char * dc_stats[NUM_ARITH_TBLS];
\r
39 unsigned char * ac_stats[NUM_ARITH_TBLS];
\r
41 /* Statistics bin for coding with fixed probability 0.5 */
\r
42 unsigned char fixed_bin[4];
\r
43 } arith_entropy_decoder;
\r
45 typedef arith_entropy_decoder * arith_entropy_ptr;
\r
47 /* The following two definitions specify the allocation chunk size
\r
48 * for the statistics area.
\r
49 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
\r
50 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
\r
52 * We use a compact representation with 1 byte per statistics bin,
\r
53 * thus the numbers directly represent byte sizes.
\r
54 * This 1 byte per statistics bin contains the meaning of the MPS
\r
55 * (more probable symbol) in the highest bit (mask 0x80), and the
\r
56 * index into the probability estimation state machine table
\r
57 * in the lower bits (mask 0x7F).
\r
60 #define DC_STAT_BINS 64
\r
61 #define AC_STAT_BINS 256
\r
65 get_byte (j_decompress_ptr cinfo)
\r
66 /* Read next input byte; we do not support suspension in this module. */
\r
68 struct jpeg_source_mgr * src = cinfo->src;
\r
70 if (src->bytes_in_buffer == 0)
\r
71 if (! (*src->fill_input_buffer) (cinfo))
\r
72 ERREXIT(cinfo, JERR_CANT_SUSPEND);
\r
73 src->bytes_in_buffer--;
\r
74 return GETJOCTET(*src->next_input_byte++);
\r
79 * The core arithmetic decoding routine (common in JPEG and JBIG).
\r
80 * This needs to go as fast as possible.
\r
81 * Machine-dependent optimization facilities
\r
82 * are not utilized in this portable implementation.
\r
83 * However, this code should be fairly efficient and
\r
84 * may be a good base for further optimizations anyway.
\r
86 * Return value is 0 or 1 (binary decision).
\r
88 * Note: I've changed the handling of the code base & bit
\r
89 * buffer register C compared to other implementations
\r
90 * based on the standards layout & procedures.
\r
91 * While it also contains both the actual base of the
\r
92 * coding interval (16 bits) and the next-bits buffer,
\r
93 * the cut-point between these two parts is floating
\r
94 * (instead of fixed) with the bit shift counter CT.
\r
95 * Thus, we also need only one (variable instead of
\r
96 * fixed size) shift for the LPS/MPS decision, and
\r
97 * we can get away with any renormalization update
\r
98 * of C (except for new data insertion, of course).
\r
100 * I've also introduced a new scheme for accessing
\r
101 * the probability estimation state machine table,
\r
102 * derived from Markus Kuhn's JBIG implementation.
\r
106 arith_decode (j_decompress_ptr cinfo, unsigned char *st)
\r
108 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
\r
109 register unsigned char nl, nm;
\r
110 register INT32 qe, temp;
\r
111 register int sv, data;
\r
113 /* Renormalization & data input per section D.2.6 */
\r
114 while (e->a < 0x8000L) {
\r
116 /* Need to fetch next data byte */
\r
117 if (cinfo->unread_marker)
\r
118 data = 0; /* stuff zero data */
\r
120 data = get_byte(cinfo); /* read next input byte */
\r
121 if (data == 0xFF) { /* zero stuff or marker code */
\r
122 do data = get_byte(cinfo);
\r
123 while (data == 0xFF); /* swallow extra 0xFF bytes */
\r
125 data = 0xFF; /* discard stuffed zero byte */
\r
127 /* Note: Different from the Huffman decoder, hitting
\r
128 * a marker while processing the compressed data
\r
129 * segment is legal in arithmetic coding.
\r
130 * The convention is to supply zero data
\r
131 * then until decoding is complete.
\r
133 cinfo->unread_marker = data;
\r
138 e->c = (e->c << 8) | data; /* insert data into C register */
\r
139 if ((e->ct += 8) < 0) /* update bit shift counter */
\r
140 /* Need more initial bytes */
\r
142 /* Got 2 initial bytes -> re-init A and exit loop */
\r
143 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
\r
148 /* Fetch values from our compact representation of Table D.2:
\r
149 * Qe values and probability estimation state machine
\r
152 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
\r
153 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
\r
154 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
\r
156 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
\r
160 if (e->c >= temp) {
\r
162 /* Conditional LPS (less probable symbol) exchange */
\r
165 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
\r
168 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
\r
169 sv ^= 0x80; /* Exchange LPS/MPS */
\r
171 } else if (e->a < 0x8000L) {
\r
172 /* Conditional MPS (more probable symbol) exchange */
\r
174 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
\r
175 sv ^= 0x80; /* Exchange LPS/MPS */
\r
177 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
\r
186 * Check for a restart marker & resynchronize decoder.
\r
190 process_restart (j_decompress_ptr cinfo)
\r
192 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
194 jpeg_component_info * compptr;
\r
196 /* Advance past the RSTn marker */
\r
197 if (! (*cinfo->marker->read_restart_marker) (cinfo))
\r
198 ERREXIT(cinfo, JERR_CANT_SUSPEND);
\r
200 /* Re-initialize statistics areas */
\r
201 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
\r
202 compptr = cinfo->cur_comp_info[ci];
\r
203 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
\r
204 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
\r
205 /* Reset DC predictions to 0 */
\r
206 entropy->last_dc_val[ci] = 0;
\r
207 entropy->dc_context[ci] = 0;
\r
209 if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
\r
210 (cinfo->progressive_mode && cinfo->Ss)) {
\r
211 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
\r
215 /* Reset arithmetic decoding variables */
\r
218 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
\r
220 /* Reset restart counter */
\r
221 entropy->restarts_to_go = cinfo->restart_interval;
\r
226 * Arithmetic MCU decoding.
\r
227 * Each of these routines decodes and returns one MCU's worth of
\r
228 * arithmetic-compressed coefficients.
\r
229 * The coefficients are reordered from zigzag order into natural array order,
\r
230 * but are not dequantized.
\r
232 * The i'th block of the MCU is stored into the block pointed to by
\r
233 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
\r
237 * MCU decoding for DC initial scan (either spectral selection,
\r
238 * or first pass of successive approximation).
\r
242 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
\r
244 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
247 int blkn, ci, tbl, sign;
\r
250 /* Process restart marker if needed */
\r
251 if (cinfo->restart_interval) {
\r
252 if (entropy->restarts_to_go == 0)
\r
253 process_restart(cinfo);
\r
254 entropy->restarts_to_go--;
\r
257 if (entropy->ct == -1) return TRUE; /* if error do nothing */
\r
259 /* Outer loop handles each block in the MCU */
\r
261 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
\r
262 block = MCU_data[blkn];
\r
263 ci = cinfo->MCU_membership[blkn];
\r
264 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
\r
266 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
\r
268 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
\r
269 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
\r
271 /* Figure F.19: Decode_DC_DIFF */
\r
272 if (arith_decode(cinfo, st) == 0)
\r
273 entropy->dc_context[ci] = 0;
\r
275 /* Figure F.21: Decoding nonzero value v */
\r
276 /* Figure F.22: Decoding the sign of v */
\r
277 sign = arith_decode(cinfo, st + 1);
\r
278 st += 2; st += sign;
\r
279 /* Figure F.23: Decoding the magnitude category of v */
\r
280 if ((m = arith_decode(cinfo, st)) != 0) {
\r
281 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
\r
282 while (arith_decode(cinfo, st)) {
\r
283 if ((m <<= 1) == 0x8000) {
\r
284 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
285 entropy->ct = -1; /* magnitude overflow */
\r
291 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
\r
292 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
\r
293 entropy->dc_context[ci] = 0; /* zero diff category */
\r
294 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
\r
295 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
\r
297 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
\r
299 /* Figure F.24: Decoding the magnitude bit pattern of v */
\r
302 if (arith_decode(cinfo, st)) v |= m;
\r
303 v += 1; if (sign) v = -v;
\r
304 entropy->last_dc_val[ci] += v;
\r
307 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
\r
308 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
\r
316 * MCU decoding for AC initial scan (either spectral selection,
\r
317 * or first pass of successive approximation).
\r
321 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
\r
323 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
328 const int * natural_order;
\r
330 /* Process restart marker if needed */
\r
331 if (cinfo->restart_interval) {
\r
332 if (entropy->restarts_to_go == 0)
\r
333 process_restart(cinfo);
\r
334 entropy->restarts_to_go--;
\r
337 if (entropy->ct == -1) return TRUE; /* if error do nothing */
\r
339 natural_order = cinfo->natural_order;
\r
341 /* There is always only one block per MCU */
\r
342 block = MCU_data[0];
\r
343 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
\r
345 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
\r
347 /* Figure F.20: Decode_AC_coefficients */
\r
348 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
\r
349 st = entropy->ac_stats[tbl] + 3 * (k - 1);
\r
350 if (arith_decode(cinfo, st)) break; /* EOB flag */
\r
351 while (arith_decode(cinfo, st + 1) == 0) {
\r
353 if (k > cinfo->Se) {
\r
354 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
355 entropy->ct = -1; /* spectral overflow */
\r
359 /* Figure F.21: Decoding nonzero value v */
\r
360 /* Figure F.22: Decoding the sign of v */
\r
361 sign = arith_decode(cinfo, entropy->fixed_bin);
\r
363 /* Figure F.23: Decoding the magnitude category of v */
\r
364 if ((m = arith_decode(cinfo, st)) != 0) {
\r
365 if (arith_decode(cinfo, st)) {
\r
367 st = entropy->ac_stats[tbl] +
\r
368 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
\r
369 while (arith_decode(cinfo, st)) {
\r
370 if ((m <<= 1) == 0x8000) {
\r
371 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
372 entropy->ct = -1; /* magnitude overflow */
\r
380 /* Figure F.24: Decoding the magnitude bit pattern of v */
\r
383 if (arith_decode(cinfo, st)) v |= m;
\r
384 v += 1; if (sign) v = -v;
\r
385 /* Scale and output coefficient in natural (dezigzagged) order */
\r
386 (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
\r
394 * MCU decoding for DC successive approximation refinement scan.
\r
398 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
\r
400 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
404 /* Process restart marker if needed */
\r
405 if (cinfo->restart_interval) {
\r
406 if (entropy->restarts_to_go == 0)
\r
407 process_restart(cinfo);
\r
408 entropy->restarts_to_go--;
\r
411 st = entropy->fixed_bin; /* use fixed probability estimation */
\r
412 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
\r
414 /* Outer loop handles each block in the MCU */
\r
416 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
\r
417 /* Encoded data is simply the next bit of the two's-complement DC value */
\r
418 if (arith_decode(cinfo, st))
\r
419 MCU_data[blkn][0][0] |= p1;
\r
427 * MCU decoding for AC successive approximation refinement scan.
\r
431 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
\r
433 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
439 const int * natural_order;
\r
441 /* Process restart marker if needed */
\r
442 if (cinfo->restart_interval) {
\r
443 if (entropy->restarts_to_go == 0)
\r
444 process_restart(cinfo);
\r
445 entropy->restarts_to_go--;
\r
448 if (entropy->ct == -1) return TRUE; /* if error do nothing */
\r
450 natural_order = cinfo->natural_order;
\r
452 /* There is always only one block per MCU */
\r
453 block = MCU_data[0];
\r
454 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
\r
456 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
\r
457 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
\r
459 /* Establish EOBx (previous stage end-of-block) index */
\r
460 for (kex = cinfo->Se; kex > 0; kex--)
\r
461 if ((*block)[natural_order[kex]]) break;
\r
463 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
\r
464 st = entropy->ac_stats[tbl] + 3 * (k - 1);
\r
466 if (arith_decode(cinfo, st)) break; /* EOB flag */
\r
468 thiscoef = *block + natural_order[k];
\r
469 if (*thiscoef) { /* previously nonzero coef */
\r
470 if (arith_decode(cinfo, st + 2)) {
\r
478 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
\r
479 if (arith_decode(cinfo, entropy->fixed_bin))
\r
486 if (k > cinfo->Se) {
\r
487 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
488 entropy->ct = -1; /* spectral overflow */
\r
499 * Decode one MCU's worth of arithmetic-compressed coefficients.
\r
503 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
\r
505 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
506 jpeg_component_info * compptr;
\r
509 int blkn, ci, tbl, sign, k;
\r
511 const int * natural_order;
\r
513 /* Process restart marker if needed */
\r
514 if (cinfo->restart_interval) {
\r
515 if (entropy->restarts_to_go == 0)
\r
516 process_restart(cinfo);
\r
517 entropy->restarts_to_go--;
\r
520 if (entropy->ct == -1) return TRUE; /* if error do nothing */
\r
522 natural_order = cinfo->natural_order;
\r
524 /* Outer loop handles each block in the MCU */
\r
526 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
\r
527 block = MCU_data[blkn];
\r
528 ci = cinfo->MCU_membership[blkn];
\r
529 compptr = cinfo->cur_comp_info[ci];
\r
531 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
\r
533 tbl = compptr->dc_tbl_no;
\r
535 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
\r
536 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
\r
538 /* Figure F.19: Decode_DC_DIFF */
\r
539 if (arith_decode(cinfo, st) == 0)
\r
540 entropy->dc_context[ci] = 0;
\r
542 /* Figure F.21: Decoding nonzero value v */
\r
543 /* Figure F.22: Decoding the sign of v */
\r
544 sign = arith_decode(cinfo, st + 1);
\r
545 st += 2; st += sign;
\r
546 /* Figure F.23: Decoding the magnitude category of v */
\r
547 if ((m = arith_decode(cinfo, st)) != 0) {
\r
548 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
\r
549 while (arith_decode(cinfo, st)) {
\r
550 if ((m <<= 1) == 0x8000) {
\r
551 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
552 entropy->ct = -1; /* magnitude overflow */
\r
558 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
\r
559 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
\r
560 entropy->dc_context[ci] = 0; /* zero diff category */
\r
561 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
\r
562 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
\r
564 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
\r
566 /* Figure F.24: Decoding the magnitude bit pattern of v */
\r
569 if (arith_decode(cinfo, st)) v |= m;
\r
570 v += 1; if (sign) v = -v;
\r
571 entropy->last_dc_val[ci] += v;
\r
574 (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
\r
576 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
\r
578 tbl = compptr->ac_tbl_no;
\r
580 /* Figure F.20: Decode_AC_coefficients */
\r
581 for (k = 1; k <= cinfo->lim_Se; k++) {
\r
582 st = entropy->ac_stats[tbl] + 3 * (k - 1);
\r
583 if (arith_decode(cinfo, st)) break; /* EOB flag */
\r
584 while (arith_decode(cinfo, st + 1) == 0) {
\r
586 if (k > cinfo->lim_Se) {
\r
587 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
588 entropy->ct = -1; /* spectral overflow */
\r
592 /* Figure F.21: Decoding nonzero value v */
\r
593 /* Figure F.22: Decoding the sign of v */
\r
594 sign = arith_decode(cinfo, entropy->fixed_bin);
\r
596 /* Figure F.23: Decoding the magnitude category of v */
\r
597 if ((m = arith_decode(cinfo, st)) != 0) {
\r
598 if (arith_decode(cinfo, st)) {
\r
600 st = entropy->ac_stats[tbl] +
\r
601 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
\r
602 while (arith_decode(cinfo, st)) {
\r
603 if ((m <<= 1) == 0x8000) {
\r
604 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
\r
605 entropy->ct = -1; /* magnitude overflow */
\r
613 /* Figure F.24: Decoding the magnitude bit pattern of v */
\r
616 if (arith_decode(cinfo, st)) v |= m;
\r
617 v += 1; if (sign) v = -v;
\r
618 (*block)[natural_order[k]] = (JCOEF) v;
\r
627 * Initialize for an arithmetic-compressed scan.
\r
631 start_pass (j_decompress_ptr cinfo)
\r
633 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
\r
635 jpeg_component_info * compptr;
\r
637 if (cinfo->progressive_mode) {
\r
638 /* Validate progressive scan parameters */
\r
639 if (cinfo->Ss == 0) {
\r
640 if (cinfo->Se != 0)
\r
643 /* need not check Ss/Se < 0 since they came from unsigned bytes */
\r
644 if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
\r
646 /* AC scans may have only one component */
\r
647 if (cinfo->comps_in_scan != 1)
\r
650 if (cinfo->Ah != 0) {
\r
651 /* Successive approximation refinement scan: must have Al = Ah-1. */
\r
652 if (cinfo->Ah-1 != cinfo->Al)
\r
655 if (cinfo->Al > 13) { /* need not check for < 0 */
\r
657 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
\r
658 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
\r
660 /* Update progression status, and verify that scan order is legal.
\r
661 * Note that inter-scan inconsistencies are treated as warnings
\r
662 * not fatal errors ... not clear if this is right way to behave.
\r
664 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
\r
665 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
\r
666 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
\r
667 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
\r
668 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
\r
669 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
\r
670 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
\r
671 if (cinfo->Ah != expected)
\r
672 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
\r
673 coef_bit_ptr[coefi] = cinfo->Al;
\r
676 /* Select MCU decoding routine */
\r
677 if (cinfo->Ah == 0) {
\r
678 if (cinfo->Ss == 0)
\r
679 entropy->pub.decode_mcu = decode_mcu_DC_first;
\r
681 entropy->pub.decode_mcu = decode_mcu_AC_first;
\r
683 if (cinfo->Ss == 0)
\r
684 entropy->pub.decode_mcu = decode_mcu_DC_refine;
\r
686 entropy->pub.decode_mcu = decode_mcu_AC_refine;
\r
689 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
\r
690 * This ought to be an error condition, but we make it a warning.
\r
692 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
\r
693 (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
\r
694 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
\r
695 /* Select MCU decoding routine */
\r
696 entropy->pub.decode_mcu = decode_mcu;
\r
699 /* Allocate & initialize requested statistics areas */
\r
700 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
\r
701 compptr = cinfo->cur_comp_info[ci];
\r
702 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
\r
703 tbl = compptr->dc_tbl_no;
\r
704 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
\r
705 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
\r
706 if (entropy->dc_stats[tbl] == NULL)
\r
707 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
\r
708 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
\r
709 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
\r
710 /* Initialize DC predictions to 0 */
\r
711 entropy->last_dc_val[ci] = 0;
\r
712 entropy->dc_context[ci] = 0;
\r
714 if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
\r
715 (cinfo->progressive_mode && cinfo->Ss)) {
\r
716 tbl = compptr->ac_tbl_no;
\r
717 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
\r
718 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
\r
719 if (entropy->ac_stats[tbl] == NULL)
\r
720 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
\r
721 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
\r
722 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
\r
726 /* Initialize arithmetic decoding variables */
\r
729 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
\r
731 /* Initialize restart counter */
\r
732 entropy->restarts_to_go = cinfo->restart_interval;
\r
737 * Module initialization routine for arithmetic entropy decoding.
\r
741 jinit_arith_decoder (j_decompress_ptr cinfo)
\r
743 arith_entropy_ptr entropy;
\r
746 entropy = (arith_entropy_ptr)
\r
747 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
\r
748 SIZEOF(arith_entropy_decoder));
\r
749 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
\r
750 entropy->pub.start_pass = start_pass;
\r
752 /* Mark tables unallocated */
\r
753 for (i = 0; i < NUM_ARITH_TBLS; i++) {
\r
754 entropy->dc_stats[i] = NULL;
\r
755 entropy->ac_stats[i] = NULL;
\r
758 /* Initialize index for fixed probability estimation */
\r
759 entropy->fixed_bin[0] = 113;
\r
761 if (cinfo->progressive_mode) {
\r
762 /* Create progression status table */
\r
763 int *coef_bit_ptr, ci;
\r
764 cinfo->coef_bits = (int (*)[DCTSIZE2])
\r
765 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
\r
766 cinfo->num_components*DCTSIZE2*SIZEOF(int));
\r
767 coef_bit_ptr = & cinfo->coef_bits[0][0];
\r
768 for (ci = 0; ci < cinfo->num_components; ci++)
\r
769 for (i = 0; i < DCTSIZE2; i++)
\r
770 *coef_bit_ptr++ = -1;
\r