jdhuff.c revision 3147fbe7688fc353e6ae03825a37cf101a4ee01d
1/* 2 * jdhuff.c 3 * 4 * Copyright (C) 1991-1997, Thomas G. Lane. 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 Huffman entropy decoding routines. 9 * 10 * Much of the complexity here has to do with supporting input suspension. 11 * If the data source module demands suspension, we want to be able to back 12 * up to the start of the current MCU. To do this, we copy state variables 13 * into local working storage, and update them back to the permanent 14 * storage only upon successful completion of an MCU. 15 */ 16 17#define JPEG_INTERNALS 18#include "jinclude.h" 19#include "jpeglib.h" 20#include "jdhuff.h" /* Declarations shared with jdphuff.c */ 21 22 23/* 24 * Expanded entropy decoder object for Huffman decoding. 25 * 26 * The savable_state subrecord contains fields that change within an MCU, 27 * but must not be updated permanently until we complete the MCU. 28 */ 29 30typedef struct { 31 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 32} savable_state; 33 34/* This macro is to work around compilers with missing or broken 35 * structure assignment. You'll need to fix this code if you have 36 * such a compiler and you change MAX_COMPS_IN_SCAN. 37 */ 38 39#ifndef NO_STRUCT_ASSIGN 40#define ASSIGN_STATE(dest,src) ((dest) = (src)) 41#else 42#if MAX_COMPS_IN_SCAN == 4 43#define ASSIGN_STATE(dest,src) \ 44 ((dest).last_dc_val[0] = (src).last_dc_val[0], \ 45 (dest).last_dc_val[1] = (src).last_dc_val[1], \ 46 (dest).last_dc_val[2] = (src).last_dc_val[2], \ 47 (dest).last_dc_val[3] = (src).last_dc_val[3]) 48#endif 49#endif 50 51 52typedef struct { 53 struct jpeg_entropy_decoder pub; /* public fields */ 54 55 /* These fields are loaded into local variables at start of each MCU. 56 * In case of suspension, we exit WITHOUT updating them. 57 */ 58 bitread_perm_state bitstate; /* Bit buffer at start of MCU */ 59 savable_state saved; /* Other state at start of MCU */ 60 61 /* These fields are NOT loaded into local working state. */ 62 unsigned int restarts_to_go; /* MCUs left in this restart interval */ 63 64 /* Pointers to derived tables (these workspaces have image lifespan) */ 65 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; 66 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; 67 68 /* Precalculated info set up by start_pass for use in decode_mcu: */ 69 70 /* Pointers to derived tables to be used for each block within an MCU */ 71 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 72 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 73 /* Whether we care about the DC and AC coefficient values for each block */ 74 boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; 75 boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; 76} huff_entropy_decoder; 77 78typedef huff_entropy_decoder * huff_entropy_ptr; 79 80/* 81 * Initialize for a Huffman-compressed scan. 82 */ 83 84METHODDEF(void) 85start_pass_huff_decoder (j_decompress_ptr cinfo) 86{ 87 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 88 int ci, blkn, dctbl, actbl; 89 jpeg_component_info * compptr; 90 91 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 92 * This ought to be an error condition, but we make it a warning because 93 * there are some baseline files out there with all zeroes in these bytes. 94 */ 95 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || 96 cinfo->Ah != 0 || cinfo->Al != 0) 97 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 98 99 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 100 compptr = cinfo->cur_comp_info[ci]; 101 dctbl = compptr->dc_tbl_no; 102 actbl = compptr->ac_tbl_no; 103 /* Compute derived values for Huffman tables */ 104 /* We may do this more than once for a table, but it's not expensive */ 105 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, 106 & entropy->dc_derived_tbls[dctbl]); 107 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, 108 & entropy->ac_derived_tbls[actbl]); 109 /* Initialize DC predictions to 0 */ 110 entropy->saved.last_dc_val[ci] = 0; 111 } 112 113 /* Precalculate decoding info for each block in an MCU of this scan */ 114 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 115 ci = cinfo->MCU_membership[blkn]; 116 compptr = cinfo->cur_comp_info[ci]; 117 /* Precalculate which table to use for each block */ 118 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; 119 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; 120 /* Decide whether we really care about the coefficient values */ 121 if (compptr->component_needed) { 122 entropy->dc_needed[blkn] = TRUE; 123 /* we don't need the ACs if producing a 1/8th-size image */ 124 entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1); 125 } else { 126 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; 127 } 128 } 129 130 /* Initialize bitread state variables */ 131 entropy->bitstate.bits_left = 0; 132 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ 133 entropy->pub.insufficient_data = FALSE; 134 135 /* Initialize restart counter */ 136 entropy->restarts_to_go = cinfo->restart_interval; 137} 138 139 140/* 141 * Compute the derived values for a Huffman table. 142 * This routine also performs some validation checks on the table. 143 * 144 * Note this is also used by jdphuff.c. 145 */ 146 147GLOBAL(void) 148jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, 149 d_derived_tbl ** pdtbl) 150{ 151 JHUFF_TBL *htbl; 152 d_derived_tbl *dtbl; 153 int p, i, l, si, numsymbols; 154 int lookbits, ctr; 155 char huffsize[257]; 156 unsigned int huffcode[257]; 157 unsigned int code; 158 159 /* Note that huffsize[] and huffcode[] are filled in code-length order, 160 * paralleling the order of the symbols themselves in htbl->huffval[]. 161 */ 162 163 /* Find the input Huffman table */ 164 if (tblno < 0 || tblno >= NUM_HUFF_TBLS) 165 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 166 htbl = 167 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; 168 if (htbl == NULL) 169 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 170 171 /* Allocate a workspace if we haven't already done so. */ 172 if (*pdtbl == NULL) 173 *pdtbl = (d_derived_tbl *) 174 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 175 SIZEOF(d_derived_tbl)); 176 dtbl = *pdtbl; 177 dtbl->pub = htbl; /* fill in back link */ 178 179 /* Figure C.1: make table of Huffman code length for each symbol */ 180 181 p = 0; 182 for (l = 1; l <= 16; l++) { 183 i = (int) htbl->bits[l]; 184 if (i < 0 || p + i > 256) /* protect against table overrun */ 185 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 186 while (i--) 187 huffsize[p++] = (char) l; 188 } 189 huffsize[p] = 0; 190 numsymbols = p; 191 192 /* Figure C.2: generate the codes themselves */ 193 /* We also validate that the counts represent a legal Huffman code tree. */ 194 195 code = 0; 196 si = huffsize[0]; 197 p = 0; 198 while (huffsize[p]) { 199 while (((int) huffsize[p]) == si) { 200 huffcode[p++] = code; 201 code++; 202 } 203 /* code is now 1 more than the last code used for codelength si; but 204 * it must still fit in si bits, since no code is allowed to be all ones. 205 */ 206 if (((INT32) code) >= (((INT32) 1) << si)) 207 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 208 code <<= 1; 209 si++; 210 } 211 212 /* Figure F.15: generate decoding tables for bit-sequential decoding */ 213 214 p = 0; 215 for (l = 1; l <= 16; l++) { 216 if (htbl->bits[l]) { 217 /* valoffset[l] = huffval[] index of 1st symbol of code length l, 218 * minus the minimum code of length l 219 */ 220 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; 221 p += htbl->bits[l]; 222 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ 223 } else { 224 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ 225 } 226 } 227 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ 228 229 /* Compute lookahead tables to speed up decoding. 230 * First we set all the table entries to 0, indicating "too long"; 231 * then we iterate through the Huffman codes that are short enough and 232 * fill in all the entries that correspond to bit sequences starting 233 * with that code. 234 */ 235 236 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits)); 237 238 p = 0; 239 for (l = 1; l <= HUFF_LOOKAHEAD; l++) { 240 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { 241 /* l = current code's length, p = its index in huffcode[] & huffval[]. */ 242 /* Generate left-justified code followed by all possible bit sequences */ 243 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); 244 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { 245 dtbl->look_nbits[lookbits] = l; 246 dtbl->look_sym[lookbits] = htbl->huffval[p]; 247 lookbits++; 248 } 249 } 250 } 251 252 /* Validate symbols as being reasonable. 253 * For AC tables, we make no check, but accept all byte values 0..255. 254 * For DC tables, we require the symbols to be in range 0..15. 255 * (Tighter bounds could be applied depending on the data depth and mode, 256 * but this is sufficient to ensure safe decoding.) 257 */ 258 if (isDC) { 259 for (i = 0; i < numsymbols; i++) { 260 int sym = htbl->huffval[i]; 261 if (sym < 0 || sym > 15) 262 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 263 } 264 } 265} 266 267 268/* 269 * Out-of-line code for bit fetching (shared with jdphuff.c). 270 * See jdhuff.h for info about usage. 271 * Note: current values of get_buffer and bits_left are passed as parameters, 272 * but are returned in the corresponding fields of the state struct. 273 * 274 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 275 * of get_buffer to be used. (On machines with wider words, an even larger 276 * buffer could be used.) However, on some machines 32-bit shifts are 277 * quite slow and take time proportional to the number of places shifted. 278 * (This is true with most PC compilers, for instance.) In this case it may 279 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the 280 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. 281 */ 282 283#ifdef SLOW_SHIFT_32 284#define MIN_GET_BITS 15 /* minimum allowable value */ 285#else 286#define MIN_GET_BITS (BIT_BUF_SIZE-7) 287#endif 288 289 290GLOBAL(boolean) 291jpeg_fill_bit_buffer (bitread_working_state * state, 292 register bit_buf_type get_buffer, register int bits_left, 293 int nbits) 294/* Load up the bit buffer to a depth of at least nbits */ 295{ 296 /* Copy heavily used state fields into locals (hopefully registers) */ 297 register const JOCTET * next_input_byte = state->next_input_byte; 298 register size_t bytes_in_buffer = state->bytes_in_buffer; 299 j_decompress_ptr cinfo = state->cinfo; 300 301 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ 302 /* (It is assumed that no request will be for more than that many bits.) */ 303 /* We fail to do so only if we hit a marker or are forced to suspend. */ 304 305 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ 306 while (bits_left < MIN_GET_BITS) { 307 register int c; 308 309 /* Attempt to read a byte */ 310 if (bytes_in_buffer == 0) { 311 if (! (*cinfo->src->fill_input_buffer) (cinfo)) 312 return FALSE; 313 next_input_byte = cinfo->src->next_input_byte; 314 bytes_in_buffer = cinfo->src->bytes_in_buffer; 315 } 316 bytes_in_buffer--; 317 c = GETJOCTET(*next_input_byte++); 318 319 /* If it's 0xFF, check and discard stuffed zero byte */ 320 if (c == 0xFF) { 321 /* Loop here to discard any padding FF's on terminating marker, 322 * so that we can save a valid unread_marker value. NOTE: we will 323 * accept multiple FF's followed by a 0 as meaning a single FF data 324 * byte. This data pattern is not valid according to the standard. 325 */ 326 do { 327 if (bytes_in_buffer == 0) { 328 if (! (*cinfo->src->fill_input_buffer) (cinfo)) 329 return FALSE; 330 next_input_byte = cinfo->src->next_input_byte; 331 bytes_in_buffer = cinfo->src->bytes_in_buffer; 332 } 333 bytes_in_buffer--; 334 c = GETJOCTET(*next_input_byte++); 335 } while (c == 0xFF); 336 337 if (c == 0) { 338 /* Found FF/00, which represents an FF data byte */ 339 c = 0xFF; 340 } else { 341 /* Oops, it's actually a marker indicating end of compressed data. 342 * Save the marker code for later use. 343 * Fine point: it might appear that we should save the marker into 344 * bitread working state, not straight into permanent state. But 345 * once we have hit a marker, we cannot need to suspend within the 346 * current MCU, because we will read no more bytes from the data 347 * source. So it is OK to update permanent state right away. 348 */ 349 cinfo->unread_marker = c; 350 /* See if we need to insert some fake zero bits. */ 351 goto no_more_bytes; 352 } 353 } 354 355 /* OK, load c into get_buffer */ 356 get_buffer = (get_buffer << 8) | c; 357 bits_left += 8; 358 } /* end while */ 359 } else { 360 no_more_bytes: 361 /* We get here if we've read the marker that terminates the compressed 362 * data segment. There should be enough bits in the buffer register 363 * to satisfy the request; if so, no problem. 364 */ 365 if (nbits > bits_left) { 366 /* Uh-oh. Report corrupted data to user and stuff zeroes into 367 * the data stream, so that we can produce some kind of image. 368 * We use a nonvolatile flag to ensure that only one warning message 369 * appears per data segment. 370 */ 371 if (! cinfo->entropy->insufficient_data) { 372 WARNMS(cinfo, JWRN_HIT_MARKER); 373 cinfo->entropy->insufficient_data = TRUE; 374 } 375 /* Fill the buffer with zero bits */ 376 get_buffer <<= MIN_GET_BITS - bits_left; 377 bits_left = MIN_GET_BITS; 378 } 379 } 380 381 /* Unload the local registers */ 382 state->next_input_byte = next_input_byte; 383 state->bytes_in_buffer = bytes_in_buffer; 384 state->get_buffer = get_buffer; 385 state->bits_left = bits_left; 386 387 return TRUE; 388} 389 390 391/* 392 * Out-of-line code for Huffman code decoding. 393 * See jdhuff.h for info about usage. 394 */ 395 396GLOBAL(int) 397jpeg_huff_decode (bitread_working_state * state, 398 register bit_buf_type get_buffer, register int bits_left, 399 d_derived_tbl * htbl, int min_bits) 400{ 401 register int l = min_bits; 402 register INT32 code; 403 404 /* HUFF_DECODE has determined that the code is at least min_bits */ 405 /* bits long, so fetch that many bits in one swoop. */ 406 407 CHECK_BIT_BUFFER(*state, l, return -1); 408 code = GET_BITS(l); 409 410 /* Collect the rest of the Huffman code one bit at a time. */ 411 /* This is per Figure F.16 in the JPEG spec. */ 412 413 while (code > htbl->maxcode[l]) { 414 code <<= 1; 415 CHECK_BIT_BUFFER(*state, 1, return -1); 416 code |= GET_BITS(1); 417 l++; 418 } 419 420 /* Unload the local registers */ 421 state->get_buffer = get_buffer; 422 state->bits_left = bits_left; 423 424 /* With garbage input we may reach the sentinel value l = 17. */ 425 426 if (l > 16) { 427 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); 428 return 0; /* fake a zero as the safest result */ 429 } 430 431 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; 432} 433 434 435/* 436 * Figure F.12: extend sign bit. 437 * On some machines, a shift and add will be faster than a table lookup. 438 */ 439 440#ifdef AVOID_TABLES 441 442#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) 443 444#else 445 446#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) 447 448static const int extend_test[16] = /* entry n is 2**(n-1) */ 449 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 450 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; 451 452static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ 453 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, 454 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, 455 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, 456 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; 457 458#endif /* AVOID_TABLES */ 459 460 461/* 462 * Check for a restart marker & resynchronize decoder. 463 * Returns FALSE if must suspend. 464 */ 465 466LOCAL(boolean) 467process_restart (j_decompress_ptr cinfo) 468{ 469 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 470 int ci; 471 472 /* Throw away any unused bits remaining in bit buffer; */ 473 /* include any full bytes in next_marker's count of discarded bytes */ 474 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; 475 entropy->bitstate.bits_left = 0; 476 477 /* Advance past the RSTn marker */ 478 if (! (*cinfo->marker->read_restart_marker) (cinfo)) 479 return FALSE; 480 481 /* Re-initialize DC predictions to 0 */ 482 for (ci = 0; ci < cinfo->comps_in_scan; ci++) 483 entropy->saved.last_dc_val[ci] = 0; 484 485 /* Reset restart counter */ 486 entropy->restarts_to_go = cinfo->restart_interval; 487 488 /* Reset out-of-data flag, unless read_restart_marker left us smack up 489 * against a marker. In that case we will end up treating the next data 490 * segment as empty, and we can avoid producing bogus output pixels by 491 * leaving the flag set. 492 */ 493 if (cinfo->unread_marker == 0) 494 entropy->pub.insufficient_data = FALSE; 495 496 return TRUE; 497} 498 499/* 500 * Configure the Huffman decoder to decode the image 501 * starting from (iMCU_row_offset, iMCU_col_offset). 502 */ 503 504GLOBAL(void) 505jpeg_configure_huffman_decoder(j_decompress_ptr cinfo, 506 unsigned int bitstream_offset, short int *dc_info) 507{ 508 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 509 int blkn, i; 510 511 BITREAD_STATE_VARS; 512 savable_state state; 513 514 unsigned int byte_offset = bitstream_offset >> LOG_TWO_BIT_BUF_SIZE; 515 unsigned int bit_in_bit_buffer = 516 bitstream_offset & ((1 << LOG_TWO_BIT_BUF_SIZE) - 1); 517 518 cinfo->src->next_input_byte = cinfo->src->start_input_byte + byte_offset; 519 cinfo->src->bytes_in_buffer = cinfo->src->total_byte - byte_offset; 520 521 entropy->bitstate.bits_left = 0; 522 523 /* 524 * When byte_offset points to the middle of a JPEG marker (2-bytes data 525 * starting with 0xFF), we need to shift the byte_offset backward so that 526 * CHECK_BIT_BUFFER can handle it properly. 527 */ 528 for (i = 0; i < 5 || *(cinfo->src->next_input_byte - 1) == 0xFF; i++) { 529 if (cinfo->src->next_input_byte <= cinfo->src->start_input_byte) 530 break; 531 cinfo->src->next_input_byte--; 532 cinfo->src->bytes_in_buffer++; 533 } 534 535 BITREAD_LOAD_STATE(cinfo, entropy->bitstate); 536 CHECK_BIT_BUFFER(br_state, BIT_BUF_SIZE, return); 537 while (cinfo->src->total_byte - br_state.bytes_in_buffer < byte_offset) { 538 DROP_BITS(8); 539 CHECK_BIT_BUFFER(br_state, BIT_BUF_SIZE, return); 540 } 541 DROP_BITS(bits_left - bit_in_bit_buffer); 542 BITREAD_SAVE_STATE(cinfo, entropy->bitstate); 543 544 for (i = 0; i < cinfo->comps_in_scan; i++) { 545 entropy->saved.last_dc_val[i] = dc_info[i]; 546 } 547} 548 549/* 550 * Save the current Huffman deocde position and the DC coefficients 551 * for each component into bitstream_offset and dc_info[], respectively. 552 */ 553 554GLOBAL(void) 555jpeg_get_huffman_decoder_configuration(j_decompress_ptr cinfo, 556 unsigned int *bitstream_offset, short int *dc_info) 557{ 558 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 559 560 BITREAD_STATE_VARS; 561 savable_state state; 562 int i; 563 564 BITREAD_LOAD_STATE(cinfo, entropy->bitstate); 565 ASSIGN_STATE(state, entropy->saved); 566 567 *bitstream_offset = ((cinfo->src->total_byte - cinfo->src->bytes_in_buffer) 568 << LOG_TWO_BIT_BUF_SIZE) + bits_left; 569 for (i = 0; i < cinfo->comps_in_scan; i++) { 570 dc_info[i] = state.last_dc_val[i]; 571 } 572} 573 574/* 575 * Decode and return one MCU's worth of Huffman-compressed coefficients. 576 * The coefficients are reordered from zigzag order into natural array order, 577 * but are not dequantized. 578 * 579 * The i'th block of the MCU is stored into the block pointed to by 580 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. 581 * (Wholesale zeroing is usually a little faster than retail...) 582 * 583 * Returns FALSE if data source requested suspension. In that case no 584 * changes have been made to permanent state. (Exception: some output 585 * coefficients may already have been assigned. This is harmless for 586 * this module, since we'll just re-assign them on the next call.) 587 */ 588 589METHODDEF(boolean) 590decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 591{ 592 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 593 int blkn; 594 BITREAD_STATE_VARS; 595 savable_state state; 596 597 /* Process restart marker if needed; may have to suspend */ 598 if (cinfo->restart_interval) { 599 if (entropy->restarts_to_go == 0) 600 if (! process_restart(cinfo)) 601 return FALSE; 602 } 603 604 /* If we've run out of data, just leave the MCU set to zeroes. 605 * This way, we return uniform gray for the remainder of the segment. 606 */ 607 if (! entropy->pub.insufficient_data) { 608 /* Load up working state */ 609 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 610 ASSIGN_STATE(state, entropy->saved); 611 612 /* Outer loop handles each block in the MCU */ 613 614 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 615 JBLOCKROW block = MCU_data[blkn]; 616 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; 617 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; 618 register int s, k, r; 619 620 /* Decode a single block's worth of coefficients */ 621 622 /* Section F.2.2.1: decode the DC coefficient difference */ 623 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); 624 if (s) { 625 CHECK_BIT_BUFFER(br_state, s, return FALSE); 626 r = GET_BITS(s); 627 s = HUFF_EXTEND(r, s); 628 } 629 630 if (entropy->dc_needed[blkn]) { 631 /* Convert DC difference to actual value, update last_dc_val */ 632 int ci = cinfo->MCU_membership[blkn]; 633 s += state.last_dc_val[ci]; 634 state.last_dc_val[ci] = s; 635 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ 636 (*block)[0] = (JCOEF) s; 637 } 638 639 if (entropy->ac_needed[blkn]) { 640 641 /* Section F.2.2.2: decode the AC coefficients */ 642 /* Since zeroes are skipped, output area must be cleared beforehand */ 643 for (k = 1; k < DCTSIZE2; k++) { 644 HUFF_DECODE(s, br_state, actbl, return FALSE, label2); 645 646 r = s >> 4; 647 s &= 15; 648 649 if (s) { 650 k += r; 651 CHECK_BIT_BUFFER(br_state, s, return FALSE); 652 r = GET_BITS(s); 653 s = HUFF_EXTEND(r, s); 654 /* Output coefficient in natural (dezigzagged) order. 655 * Note: the extra entries in jpeg_natural_order[] will save us 656 * if k >= DCTSIZE2, which could happen if the data is corrupted. 657 */ 658 (*block)[jpeg_natural_order[k]] = (JCOEF) s; 659 } else { 660 if (r != 15) 661 break; 662 k += 15; 663 } 664 } 665 666 } else { 667 668 /* Section F.2.2.2: decode the AC coefficients */ 669 /* In this path we just discard the values */ 670 for (k = 1; k < DCTSIZE2; k++) { 671 HUFF_DECODE(s, br_state, actbl, return FALSE, label3); 672 673 r = s >> 4; 674 s &= 15; 675 676 if (s) { 677 k += r; 678 CHECK_BIT_BUFFER(br_state, s, return FALSE); 679 DROP_BITS(s); 680 } else { 681 if (r != 15) 682 break; 683 k += 15; 684 } 685 } 686 687 } 688 } 689 690 /* Completed MCU, so update state */ 691 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 692 ASSIGN_STATE(entropy->saved, state); 693 } 694 695 /* Account for restart interval (no-op if not using restarts) */ 696 entropy->restarts_to_go--; 697 698 return TRUE; 699} 700 701/* 702 * Decode one MCU's worth of Huffman-compressed coefficients. 703 * The propose of this method is to calculate the 704 * data length of one MCU in Huffman-coded format. 705 * Therefore, all coefficients are discarded. 706 */ 707 708METHODDEF(boolean) 709decode_mcu_discard_coef (j_decompress_ptr cinfo) 710{ 711 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 712 int blkn; 713 BITREAD_STATE_VARS; 714 savable_state state; 715 716 /* Process restart marker if needed; may have to suspend */ 717 if (cinfo->restart_interval) { 718 if (entropy->restarts_to_go == 0) 719 if (! process_restart(cinfo)) 720 return FALSE; 721 } 722 723 if (! entropy->pub.insufficient_data) { 724 725 /* Load up working state */ 726 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 727 ASSIGN_STATE(state, entropy->saved); 728 729 /* Outer loop handles each block in the MCU */ 730 731 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 732 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; 733 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; 734 register int s, k, r; 735 736 /* Decode a single block's worth of coefficients */ 737 738 /* Section F.2.2.1: decode the DC coefficient difference */ 739 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); 740 if (s) { 741 CHECK_BIT_BUFFER(br_state, s, return FALSE); 742 r = GET_BITS(s); 743 s = HUFF_EXTEND(r, s); 744 } 745 746 /* discard all coefficients */ 747 if (entropy->dc_needed[blkn]) { 748 /* Convert DC difference to actual value, update last_dc_val */ 749 int ci = cinfo->MCU_membership[blkn]; 750 s += state.last_dc_val[ci]; 751 state.last_dc_val[ci] = s; 752 } 753 for (k = 1; k < DCTSIZE2; k++) { 754 HUFF_DECODE(s, br_state, actbl, return FALSE, label3); 755 756 r = s >> 4; 757 s &= 15; 758 759 if (s) { 760 k += r; 761 CHECK_BIT_BUFFER(br_state, s, return FALSE); 762 DROP_BITS(s); 763 } else { 764 if (r != 15) 765 break; 766 k += 15; 767 } 768 } 769 } 770 771 /* Completed MCU, so update state */ 772 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 773 ASSIGN_STATE(entropy->saved, state); 774 } 775 776 /* Account for restart interval (no-op if not using restarts) */ 777 entropy->restarts_to_go--; 778 779 return TRUE; 780} 781 782 783/* 784 * Module initialization routine for Huffman entropy decoding. 785 */ 786 787GLOBAL(void) 788jinit_huff_decoder (j_decompress_ptr cinfo) 789{ 790 huff_entropy_ptr entropy; 791 int i; 792 793 entropy = (huff_entropy_ptr) 794 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 795 SIZEOF(huff_entropy_decoder)); 796 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 797 entropy->pub.start_pass = start_pass_huff_decoder; 798 entropy->pub.decode_mcu = decode_mcu; 799 800 /* Mark tables unallocated */ 801 for (i = 0; i < NUM_HUFF_TBLS; i++) { 802 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 803 } 804} 805 806GLOBAL(void) 807jinit_huff_decoder_no_data (j_decompress_ptr cinfo) 808{ 809 huff_entropy_ptr entropy; 810 int i; 811 812 entropy = (huff_entropy_ptr) 813 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 814 SIZEOF(huff_entropy_decoder)); 815 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 816 entropy->pub.start_pass = start_pass_huff_decoder; 817 entropy->pub.decode_mcu = decode_mcu; 818 entropy->pub.decode_mcu_discard_coef = decode_mcu_discard_coef; 819 820 /* Mark tables unallocated */ 821 for (i = 0; i < NUM_HUFF_TBLS; i++) { 822 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 823 } 824} 825 826/* 827 * Call after jpeg_read_header 828 */ 829GLOBAL(void) 830jpeg_create_huffman_index(j_decompress_ptr cinfo, huffman_index *index) 831{ 832 int i, s; 833 index->scan_count = 1; 834 index->scan = (huffman_scan_header*)malloc(index->scan_count 835 * sizeof(huffman_scan_header)); 836 index->total_iMCU_rows = cinfo->total_iMCU_rows; 837 index->scan[0].offset = (huffman_offset_data**)malloc(cinfo->total_iMCU_rows 838 * sizeof(huffman_offset_data*)); 839 index->MCU_sample_size = DEFAULT_MCU_SAMPLE_SIZE; 840 841 index->mem_used = sizeof(huffman_scan_header) 842 + cinfo->total_iMCU_rows*sizeof(huffman_offset_data*); 843} 844 845GLOBAL(void) 846jpeg_destroy_huffman_index(huffman_index *index) 847{ 848 int i, j; 849 for (i = 0; i < index->scan_count; i++) { 850 for(j = 0; j < index->total_iMCU_rows; j++) { 851 free(index->scan[i].offset[j]); 852 } 853 free(index->scan[i].offset); 854 } 855 free(index->scan); 856} 857