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