1 /* 2 * jdarith.c 3 * 4 * Developed 1997-2009 by Guido Vollbeding. 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 portable arithmetic entropy decoding routines for JPEG 9 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). 10 * 11 * Both sequential and progressive modes are supported in this single module. 12 * 13 * Suspension is not currently supported in this module. 14 */ 15 16 #define JPEG_INTERNALS 17 #include "jinclude.h" 18 #include "jpeglib.h" 19 20 21 /* Expanded entropy decoder object for arithmetic decoding. */ 22 23 typedef struct { 24 struct jpeg_entropy_decoder pub; /* public fields */ 25 26 INT32 c; /* C register, base of coding interval + input bit buffer */ 27 INT32 a; /* A register, normalized size of coding interval */ 28 int ct; /* bit shift counter, # of bits left in bit buffer part of C */ 29 /* init: ct = -16 */ 30 /* run: ct = 0..7 */ 31 /* error: ct = -1 */ 32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 33 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ 34 35 unsigned int restarts_to_go; /* MCUs left in this restart interval */ 36 37 /* Pointers to statistics areas (these workspaces have image lifespan) */ 38 unsigned char * dc_stats[NUM_ARITH_TBLS]; 39 unsigned char * ac_stats[NUM_ARITH_TBLS]; 40 41 /* Statistics bin for coding with fixed probability 0.5 */ 42 unsigned char fixed_bin[4]; 43 } arith_entropy_decoder; 44 45 typedef arith_entropy_decoder * arith_entropy_ptr; 46 47 /* The following two definitions specify the allocation chunk size 48 * for the statistics area. 49 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least 50 * 49 statistics bins for DC, and 245 statistics bins for AC coding. 51 * 52 * We use a compact representation with 1 byte per statistics bin, 53 * thus the numbers directly represent byte sizes. 54 * This 1 byte per statistics bin contains the meaning of the MPS 55 * (more probable symbol) in the highest bit (mask 0x80), and the 56 * index into the probability estimation state machine table 57 * in the lower bits (mask 0x7F). 58 */ 59 60 #define DC_STAT_BINS 64 61 #define AC_STAT_BINS 256 62 63 64 LOCAL(int) 65 get_byte (j_decompress_ptr cinfo) 66 /* Read next input byte; we do not support suspension in this module. */ 67 { 68 struct jpeg_source_mgr * src = cinfo->src; 69 70 if (src->bytes_in_buffer == 0) 71 if (! (*src->fill_input_buffer) (cinfo)) 72 ERREXIT(cinfo, JERR_CANT_SUSPEND); 73 src->bytes_in_buffer--; 74 return GETJOCTET(*src->next_input_byte++); 75 } 76 77 78 /* 79 * The core arithmetic decoding routine (common in JPEG and JBIG). 80 * This needs to go as fast as possible. 81 * Machine-dependent optimization facilities 82 * are not utilized in this portable implementation. 83 * However, this code should be fairly efficient and 84 * may be a good base for further optimizations anyway. 85 * 86 * Return value is 0 or 1 (binary decision). 87 * 88 * Note: I've changed the handling of the code base & bit 89 * buffer register C compared to other implementations 90 * based on the standards layout & procedures. 91 * While it also contains both the actual base of the 92 * coding interval (16 bits) and the next-bits buffer, 93 * the cut-point between these two parts is floating 94 * (instead of fixed) with the bit shift counter CT. 95 * Thus, we also need only one (variable instead of 96 * fixed size) shift for the LPS/MPS decision, and 97 * we can get away with any renormalization update 98 * of C (except for new data insertion, of course). 99 * 100 * I've also introduced a new scheme for accessing 101 * the probability estimation state machine table, 102 * derived from Markus Kuhn's JBIG implementation. 103 */ 104 105 LOCAL(int) 106 arith_decode (j_decompress_ptr cinfo, unsigned char *st) 107 { 108 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; 109 register unsigned char nl, nm; 110 register INT32 qe, temp; 111 register int sv, data; 112 113 /* Renormalization & data input per section D.2.6 */ 114 while (e->a < 0x8000L) { 115 if (--e->ct < 0) { 116 /* Need to fetch next data byte */ 117 if (cinfo->unread_marker) 118 data = 0; /* stuff zero data */ 119 else { 120 data = get_byte(cinfo); /* read next input byte */ 121 if (data == 0xFF) { /* zero stuff or marker code */ 122 do data = get_byte(cinfo); 123 while (data == 0xFF); /* swallow extra 0xFF bytes */ 124 if (data == 0) 125 data = 0xFF; /* discard stuffed zero byte */ 126 else { 127 /* Note: Different from the Huffman decoder, hitting 128 * a marker while processing the compressed data 129 * segment is legal in arithmetic coding. 130 * The convention is to supply zero data 131 * then until decoding is complete. 132 */ 133 cinfo->unread_marker = data; 134 data = 0; 135 } 136 } 137 } 138 e->c = (e->c << 8) | data; /* insert data into C register */ 139 if ((e->ct += 8) < 0) /* update bit shift counter */ 140 /* Need more initial bytes */ 141 if (++e->ct == 0) 142 /* Got 2 initial bytes -> re-init A and exit loop */ 143 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ 144 } 145 e->a <<= 1; 146 } 147 148 /* Fetch values from our compact representation of Table D.2: 149 * Qe values and probability estimation state machine 150 */ 151 sv = *st; 152 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ 153 nl = (unsigned char) qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ 154 nm = (unsigned char) qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ 155 156 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ 157 temp = e->a - qe; 158 e->a = temp; 159 temp <<= e->ct; 160 if (e->c >= temp) { 161 e->c -= temp; 162 /* Conditional LPS (less probable symbol) exchange */ 163 if (e->a < qe) { 164 e->a = qe; 165 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ 166 } else { 167 e->a = qe; 168 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ 169 sv ^= 0x80; /* Exchange LPS/MPS */ 170 } 171 } else if (e->a < 0x8000L) { 172 /* Conditional MPS (more probable symbol) exchange */ 173 if (e->a < qe) { 174 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ 175 sv ^= 0x80; /* Exchange LPS/MPS */ 176 } else { 177 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ 178 } 179 } 180 181 return sv >> 7; 182 } 183 184 185 /* 186 * Check for a restart marker & resynchronize decoder. 187 */ 188 189 LOCAL(void) 190 process_restart (j_decompress_ptr cinfo) 191 { 192 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 193 int ci; 194 jpeg_component_info * compptr; 195 196 /* Advance past the RSTn marker */ 197 if (! (*cinfo->marker->read_restart_marker) (cinfo)) 198 ERREXIT(cinfo, JERR_CANT_SUSPEND); 199 200 /* Re-initialize statistics areas */ 201 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 202 compptr = cinfo->cur_comp_info[ci]; 203 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { 204 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); 205 /* Reset DC predictions to 0 */ 206 entropy->last_dc_val[ci] = 0; 207 entropy->dc_context[ci] = 0; 208 } 209 if (! cinfo->progressive_mode || cinfo->Ss) { 210 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); 211 } 212 } 213 214 /* Reset arithmetic decoding variables */ 215 entropy->c = 0; 216 entropy->a = 0; 217 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ 218 219 /* Reset restart counter */ 220 entropy->restarts_to_go = cinfo->restart_interval; 221 } 222 223 224 /* 225 * Arithmetic MCU decoding. 226 * Each of these routines decodes and returns one MCU's worth of 227 * arithmetic-compressed coefficients. 228 * The coefficients are reordered from zigzag order into natural array order, 229 * but are not dequantized. 230 * 231 * The i'th block of the MCU is stored into the block pointed to by 232 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. 233 */ 234 235 /* 236 * MCU decoding for DC initial scan (either spectral selection, 237 * or first pass of successive approximation). 238 */ 239 240 METHODDEF(boolean) 241 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 242 { 243 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 244 JBLOCKROW block; 245 unsigned char *st; 246 int blkn, ci, tbl, sign; 247 int v, m; 248 249 /* Process restart marker if needed */ 250 if (cinfo->restart_interval) { 251 if (entropy->restarts_to_go == 0) 252 process_restart(cinfo); 253 entropy->restarts_to_go--; 254 } 255 256 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 257 258 /* Outer loop handles each block in the MCU */ 259 260 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 261 block = MCU_data[blkn]; 262 ci = cinfo->MCU_membership[blkn]; 263 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; 264 265 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ 266 267 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ 268 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; 269 270 /* Figure F.19: Decode_DC_DIFF */ 271 if (arith_decode(cinfo, st) == 0) 272 entropy->dc_context[ci] = 0; 273 else { 274 /* Figure F.21: Decoding nonzero value v */ 275 /* Figure F.22: Decoding the sign of v */ 276 sign = arith_decode(cinfo, st + 1); 277 st += 2; st += sign; 278 /* Figure F.23: Decoding the magnitude category of v */ 279 if ((m = arith_decode(cinfo, st)) != 0) { 280 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ 281 while (arith_decode(cinfo, st)) { 282 if ((m <<= 1) == 0x8000) { 283 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 284 entropy->ct = -1; /* magnitude overflow */ 285 return TRUE; 286 } 287 st += 1; 288 } 289 } 290 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ 291 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) 292 entropy->dc_context[ci] = 0; /* zero diff category */ 293 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) 294 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ 295 else 296 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ 297 v = m; 298 /* Figure F.24: Decoding the magnitude bit pattern of v */ 299 st += 14; 300 while (m >>= 1) 301 if (arith_decode(cinfo, st)) v |= m; 302 v += 1; if (sign) v = -v; 303 entropy->last_dc_val[ci] += v; 304 } 305 306 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ 307 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); 308 } 309 310 return TRUE; 311 } 312 313 314 /* 315 * MCU decoding for AC initial scan (either spectral selection, 316 * or first pass of successive approximation). 317 */ 318 319 METHODDEF(boolean) 320 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 321 { 322 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 323 JBLOCKROW block; 324 unsigned char *st; 325 int tbl, sign, k; 326 int v, m; 327 328 /* Process restart marker if needed */ 329 if (cinfo->restart_interval) { 330 if (entropy->restarts_to_go == 0) 331 process_restart(cinfo); 332 entropy->restarts_to_go--; 333 } 334 335 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 336 337 /* There is always only one block per MCU */ 338 block = MCU_data[0]; 339 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; 340 341 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ 342 343 /* Figure F.20: Decode_AC_coefficients */ 344 for (k = cinfo->Ss; k <= cinfo->Se; k++) { 345 st = entropy->ac_stats[tbl] + 3 * (k - 1); 346 if (arith_decode(cinfo, st)) break; /* EOB flag */ 347 while (arith_decode(cinfo, st + 1) == 0) { 348 st += 3; k++; 349 if (k > cinfo->Se) { 350 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 351 entropy->ct = -1; /* spectral overflow */ 352 return TRUE; 353 } 354 } 355 /* Figure F.21: Decoding nonzero value v */ 356 /* Figure F.22: Decoding the sign of v */ 357 sign = arith_decode(cinfo, entropy->fixed_bin); 358 st += 2; 359 /* Figure F.23: Decoding the magnitude category of v */ 360 if ((m = arith_decode(cinfo, st)) != 0) { 361 if (arith_decode(cinfo, st)) { 362 m <<= 1; 363 st = entropy->ac_stats[tbl] + 364 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); 365 while (arith_decode(cinfo, st)) { 366 if ((m <<= 1) == 0x8000) { 367 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 368 entropy->ct = -1; /* magnitude overflow */ 369 return TRUE; 370 } 371 st += 1; 372 } 373 } 374 } 375 v = m; 376 /* Figure F.24: Decoding the magnitude bit pattern of v */ 377 st += 14; 378 while (m >>= 1) 379 if (arith_decode(cinfo, st)) v |= m; 380 v += 1; if (sign) v = -v; 381 /* Scale and output coefficient in natural (dezigzagged) order */ 382 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); 383 } 384 385 return TRUE; 386 } 387 388 389 /* 390 * MCU decoding for DC successive approximation refinement scan. 391 */ 392 393 METHODDEF(boolean) 394 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 395 { 396 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 397 unsigned char *st; 398 int p1, blkn; 399 400 /* Process restart marker if needed */ 401 if (cinfo->restart_interval) { 402 if (entropy->restarts_to_go == 0) 403 process_restart(cinfo); 404 entropy->restarts_to_go--; 405 } 406 407 st = entropy->fixed_bin; /* use fixed probability estimation */ 408 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 409 410 /* Outer loop handles each block in the MCU */ 411 412 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 413 /* Encoded data is simply the next bit of the two's-complement DC value */ 414 if (arith_decode(cinfo, st)) 415 MCU_data[blkn][0][0] |= p1; 416 } 417 418 return TRUE; 419 } 420 421 422 /* 423 * MCU decoding for AC successive approximation refinement scan. 424 */ 425 426 METHODDEF(boolean) 427 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 428 { 429 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 430 JBLOCKROW block; 431 JCOEFPTR thiscoef; 432 unsigned char *st; 433 int tbl, k, kex; 434 int p1, m1; 435 436 /* Process restart marker if needed */ 437 if (cinfo->restart_interval) { 438 if (entropy->restarts_to_go == 0) 439 process_restart(cinfo); 440 entropy->restarts_to_go--; 441 } 442 443 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 444 445 /* There is always only one block per MCU */ 446 block = MCU_data[0]; 447 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; 448 449 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 450 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ 451 452 /* Establish EOBx (previous stage end-of-block) index */ 453 for (kex = cinfo->Se; kex > 0; kex--) 454 if ((*block)[jpeg_natural_order[kex]]) break; 455 456 for (k = cinfo->Ss; k <= cinfo->Se; k++) { 457 st = entropy->ac_stats[tbl] + 3 * (k - 1); 458 if (k > kex) 459 if (arith_decode(cinfo, st)) break; /* EOB flag */ 460 for (;;) { 461 thiscoef = *block + jpeg_natural_order[k]; 462 if (*thiscoef) { /* previously nonzero coef */ 463 if (arith_decode(cinfo, st + 2)) { 464 if (*thiscoef < 0) 465 *thiscoef += m1; 466 else 467 *thiscoef += p1; 468 } 469 break; 470 } 471 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ 472 if (arith_decode(cinfo, entropy->fixed_bin)) 473 *thiscoef = m1; 474 else 475 *thiscoef = p1; 476 break; 477 } 478 st += 3; k++; 479 if (k > cinfo->Se) { 480 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 481 entropy->ct = -1; /* spectral overflow */ 482 return TRUE; 483 } 484 } 485 } 486 487 return TRUE; 488 } 489 490 491 /* 492 * Decode one MCU's worth of arithmetic-compressed coefficients. 493 */ 494 495 METHODDEF(boolean) 496 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 497 { 498 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 499 jpeg_component_info * compptr; 500 JBLOCKROW block; 501 unsigned char *st; 502 int blkn, ci, tbl, sign, k; 503 int v, m; 504 505 /* Process restart marker if needed */ 506 if (cinfo->restart_interval) { 507 if (entropy->restarts_to_go == 0) 508 process_restart(cinfo); 509 entropy->restarts_to_go--; 510 } 511 512 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 513 514 /* Outer loop handles each block in the MCU */ 515 516 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 517 block = MCU_data[blkn]; 518 ci = cinfo->MCU_membership[blkn]; 519 compptr = cinfo->cur_comp_info[ci]; 520 521 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ 522 523 tbl = compptr->dc_tbl_no; 524 525 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ 526 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; 527 528 /* Figure F.19: Decode_DC_DIFF */ 529 if (arith_decode(cinfo, st) == 0) 530 entropy->dc_context[ci] = 0; 531 else { 532 /* Figure F.21: Decoding nonzero value v */ 533 /* Figure F.22: Decoding the sign of v */ 534 sign = arith_decode(cinfo, st + 1); 535 st += 2; st += sign; 536 /* Figure F.23: Decoding the magnitude category of v */ 537 if ((m = arith_decode(cinfo, st)) != 0) { 538 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ 539 while (arith_decode(cinfo, st)) { 540 if ((m <<= 1) == 0x8000) { 541 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 542 entropy->ct = -1; /* magnitude overflow */ 543 return TRUE; 544 } 545 st += 1; 546 } 547 } 548 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ 549 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) 550 entropy->dc_context[ci] = 0; /* zero diff category */ 551 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) 552 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ 553 else 554 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ 555 v = m; 556 /* Figure F.24: Decoding the magnitude bit pattern of v */ 557 st += 14; 558 while (m >>= 1) 559 if (arith_decode(cinfo, st)) v |= m; 560 v += 1; if (sign) v = -v; 561 entropy->last_dc_val[ci] += v; 562 } 563 564 (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; 565 566 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ 567 568 tbl = compptr->ac_tbl_no; 569 570 /* Figure F.20: Decode_AC_coefficients */ 571 for (k = 1; k <= DCTSIZE2 - 1; k++) { 572 st = entropy->ac_stats[tbl] + 3 * (k - 1); 573 if (arith_decode(cinfo, st)) break; /* EOB flag */ 574 while (arith_decode(cinfo, st + 1) == 0) { 575 st += 3; k++; 576 if (k > DCTSIZE2 - 1) { 577 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 578 entropy->ct = -1; /* spectral overflow */ 579 return TRUE; 580 } 581 } 582 /* Figure F.21: Decoding nonzero value v */ 583 /* Figure F.22: Decoding the sign of v */ 584 sign = arith_decode(cinfo, entropy->fixed_bin); 585 st += 2; 586 /* Figure F.23: Decoding the magnitude category of v */ 587 if ((m = arith_decode(cinfo, st)) != 0) { 588 if (arith_decode(cinfo, st)) { 589 m <<= 1; 590 st = entropy->ac_stats[tbl] + 591 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); 592 while (arith_decode(cinfo, st)) { 593 if ((m <<= 1) == 0x8000) { 594 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 595 entropy->ct = -1; /* magnitude overflow */ 596 return TRUE; 597 } 598 st += 1; 599 } 600 } 601 } 602 v = m; 603 /* Figure F.24: Decoding the magnitude bit pattern of v */ 604 st += 14; 605 while (m >>= 1) 606 if (arith_decode(cinfo, st)) v |= m; 607 v += 1; if (sign) v = -v; 608 (*block)[jpeg_natural_order[k]] = (JCOEF) v; 609 } 610 } 611 612 return TRUE; 613 } 614 615 616 /* 617 * Initialize for an arithmetic-compressed scan. 618 */ 619 620 METHODDEF(void) 621 start_pass (j_decompress_ptr cinfo) 622 { 623 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 624 int ci, tbl; 625 jpeg_component_info * compptr; 626 627 if (cinfo->progressive_mode) { 628 /* Validate progressive scan parameters */ 629 if (cinfo->Ss == 0) { 630 if (cinfo->Se != 0) 631 goto bad; 632 } else { 633 /* need not check Ss/Se < 0 since they came from unsigned bytes */ 634 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) 635 goto bad; 636 /* AC scans may have only one component */ 637 if (cinfo->comps_in_scan != 1) 638 goto bad; 639 } 640 if (cinfo->Ah != 0) { 641 /* Successive approximation refinement scan: must have Al = Ah-1. */ 642 if (cinfo->Ah-1 != cinfo->Al) 643 goto bad; 644 } 645 if (cinfo->Al > 13) { /* need not check for < 0 */ 646 bad: 647 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 648 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); 649 } 650 /* Update progression status, and verify that scan order is legal. 651 * Note that inter-scan inconsistencies are treated as warnings 652 * not fatal errors ... not clear if this is right way to behave. 653 */ 654 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 655 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; 656 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; 657 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ 658 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 659 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { 660 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; 661 if (cinfo->Ah != expected) 662 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); 663 coef_bit_ptr[coefi] = cinfo->Al; 664 } 665 } 666 /* Select MCU decoding routine */ 667 if (cinfo->Ah == 0) { 668 if (cinfo->Ss == 0) 669 entropy->pub.decode_mcu = decode_mcu_DC_first; 670 else 671 entropy->pub.decode_mcu = decode_mcu_AC_first; 672 } else { 673 if (cinfo->Ss == 0) 674 entropy->pub.decode_mcu = decode_mcu_DC_refine; 675 else 676 entropy->pub.decode_mcu = decode_mcu_AC_refine; 677 } 678 } else { 679 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 680 * This ought to be an error condition, but we make it a warning. 681 */ 682 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || 683 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1)) 684 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 685 /* Select MCU decoding routine */ 686 entropy->pub.decode_mcu = decode_mcu; 687 } 688 689 /* Allocate & initialize requested statistics areas */ 690 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 691 compptr = cinfo->cur_comp_info[ci]; 692 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { 693 tbl = compptr->dc_tbl_no; 694 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) 695 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); 696 if (entropy->dc_stats[tbl] == NULL) 697 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) 698 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); 699 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); 700 /* Initialize DC predictions to 0 */ 701 entropy->last_dc_val[ci] = 0; 702 entropy->dc_context[ci] = 0; 703 } 704 if (! cinfo->progressive_mode || cinfo->Ss) { 705 tbl = compptr->ac_tbl_no; 706 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) 707 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); 708 if (entropy->ac_stats[tbl] == NULL) 709 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) 710 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); 711 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); 712 } 713 } 714 715 /* Initialize arithmetic decoding variables */ 716 entropy->c = 0; 717 entropy->a = 0; 718 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ 719 720 /* Initialize restart counter */ 721 entropy->restarts_to_go = cinfo->restart_interval; 722 } 723 724 725 /* 726 * Module initialization routine for arithmetic entropy decoding. 727 */ 728 729 GLOBAL(void) 730 jinit_arith_decoder (j_decompress_ptr cinfo) 731 { 732 arith_entropy_ptr entropy; 733 int i; 734 735 entropy = (arith_entropy_ptr) 736 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 737 SIZEOF(arith_entropy_decoder)); 738 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 739 entropy->pub.start_pass = start_pass; 740 741 /* Mark tables unallocated */ 742 for (i = 0; i < NUM_ARITH_TBLS; i++) { 743 entropy->dc_stats[i] = NULL; 744 entropy->ac_stats[i] = NULL; 745 } 746 747 /* Initialize index for fixed probability estimation */ 748 entropy->fixed_bin[0] = 113; 749 750 if (cinfo->progressive_mode) { 751 /* Create progression status table */ 752 int *coef_bit_ptr, ci; 753 cinfo->coef_bits = (int (*)[DCTSIZE2]) 754 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 755 cinfo->num_components*DCTSIZE2*SIZEOF(int)); 756 coef_bit_ptr = & cinfo->coef_bits[0][0]; 757 for (ci = 0; ci < cinfo->num_components; ci++) 758 for (i = 0; i < DCTSIZE2; i++) 759 *coef_bit_ptr++ = -1; 760 } 761 } 762