jdhuff.cpp (20233B)
1 /* 2 * jdhuff.c 3 * 4 * Copyright (C) 1991-1995, 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 30 typedef 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 52 typedef 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 } huff_entropy_decoder; 68 69 typedef huff_entropy_decoder * huff_entropy_ptr; 70 71 72 /* 73 * Initialize for a Huffman-compressed scan. 74 */ 75 76 METHODDEF void 77 start_pass_huff_decoder( j_decompress_ptr cinfo ) { 78 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 79 int ci, dctbl, actbl; 80 jpeg_component_info * compptr; 81 82 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 83 * This ought to be an error condition, but we make it a warning because 84 * there are some baseline files out there with all zeroes in these bytes. 85 */ 86 if ( ( cinfo->Ss != 0 ) || ( cinfo->Se != DCTSIZE2 - 1 ) || 87 ( cinfo->Ah != 0 ) || ( cinfo->Al != 0 ) ) { 88 WARNMS( cinfo, JWRN_NOT_SEQUENTIAL ); 89 } 90 91 for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) { 92 compptr = cinfo->cur_comp_info[ci]; 93 dctbl = compptr->dc_tbl_no; 94 actbl = compptr->ac_tbl_no; 95 /* Make sure requested tables are present */ 96 if ( ( dctbl < 0 ) || ( dctbl >= NUM_HUFF_TBLS ) || 97 ( cinfo->dc_huff_tbl_ptrs[dctbl] == NULL ) ) { 98 ERREXIT1( cinfo, JERR_NO_HUFF_TABLE, dctbl ); 99 } 100 if ( ( actbl < 0 ) || ( actbl >= NUM_HUFF_TBLS ) || 101 ( cinfo->ac_huff_tbl_ptrs[actbl] == NULL ) ) { 102 ERREXIT1( cinfo, JERR_NO_HUFF_TABLE, actbl ); 103 } 104 /* Compute derived values for Huffman tables */ 105 /* We may do this more than once for a table, but it's not expensive */ 106 jpeg_make_d_derived_tbl( cinfo, cinfo->dc_huff_tbl_ptrs[dctbl], 107 &entropy->dc_derived_tbls[dctbl] ); 108 jpeg_make_d_derived_tbl( cinfo, cinfo->ac_huff_tbl_ptrs[actbl], 109 &entropy->ac_derived_tbls[actbl] ); 110 /* Initialize DC predictions to 0 */ 111 entropy->saved.last_dc_val[ci] = 0; 112 } 113 114 /* Initialize bitread state variables */ 115 entropy->bitstate.bits_left = 0; 116 entropy->bitstate.get_buffer = 0;/* unnecessary, but keeps Purify quiet */ 117 entropy->bitstate.printed_eod = FALSE; 118 119 /* Initialize restart counter */ 120 entropy->restarts_to_go = cinfo->restart_interval; 121 } 122 123 124 /* 125 * Compute the derived values for a Huffman table. 126 * Note this is also used by jdphuff.c. 127 */ 128 129 GLOBAL void 130 jpeg_make_d_derived_tbl( j_decompress_ptr cinfo, JHUFF_TBL * htbl, 131 d_derived_tbl ** pdtbl ) { 132 d_derived_tbl * dtbl; 133 int p, i, l, si; 134 int lookbits, ctr; 135 char huffsize[257]; 136 unsigned int huffcode[257]; 137 unsigned int code; 138 139 /* Allocate a workspace if we haven't already done so. */ 140 if ( *pdtbl == NULL ) { 141 *pdtbl = (d_derived_tbl *) 142 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE, 143 SIZEOF( d_derived_tbl ) ); 144 } 145 dtbl = *pdtbl; 146 dtbl->pub = htbl; /* fill in back link */ 147 148 /* Figure C.1: make table of Huffman code length for each symbol */ 149 /* Note that this is in code-length order. */ 150 151 p = 0; 152 for ( l = 1; l <= 16; l++ ) { 153 for ( i = 1; i <= (int) htbl->bits[l]; i++ ) { 154 huffsize[p++] = (char) l; 155 } 156 } 157 huffsize[p] = 0; 158 159 /* Figure C.2: generate the codes themselves */ 160 /* Note that this is in code-length order. */ 161 162 code = 0; 163 si = huffsize[0]; 164 p = 0; 165 while ( huffsize[p] ) { 166 while ( ( (int) huffsize[p] ) == si ) { 167 huffcode[p++] = code; 168 code++; 169 } 170 code <<= 1; 171 si++; 172 } 173 174 /* Figure F.15: generate decoding tables for bit-sequential decoding */ 175 176 p = 0; 177 for ( l = 1; l <= 16; l++ ) { 178 if ( htbl->bits[l] ) { 179 dtbl->valptr[l] = p;/* huffval[] index of 1st symbol of code length l */ 180 dtbl->mincode[l] = huffcode[p];/* minimum code of length l */ 181 p += htbl->bits[l]; 182 dtbl->maxcode[l] = huffcode[p - 1];/* maximum code of length l */ 183 } else { 184 dtbl->maxcode[l] = -1;/* -1 if no codes of this length */ 185 } 186 } 187 dtbl->maxcode[17] = 0xFFFFFL;/* ensures jpeg_huff_decode terminates */ 188 189 /* Compute lookahead tables to speed up decoding. 190 * First we set all the table entries to 0, indicating "too long"; 191 * then we iterate through the Huffman codes that are short enough and 192 * fill in all the entries that correspond to bit sequences starting 193 * with that code. 194 */ 195 196 MEMZERO( dtbl->look_nbits, SIZEOF( dtbl->look_nbits ) ); 197 198 p = 0; 199 for ( l = 1; l <= HUFF_LOOKAHEAD; l++ ) { 200 for ( i = 1; i <= (int) htbl->bits[l]; i++, p++ ) { 201 /* l = current code's length, p = its index in huffcode[] & huffval[]. */ 202 /* Generate left-justified code followed by all possible bit sequences */ 203 lookbits = huffcode[p] << ( HUFF_LOOKAHEAD - l ); 204 for ( ctr = 1 << ( HUFF_LOOKAHEAD - l ); ctr > 0; ctr-- ) { 205 dtbl->look_nbits[lookbits] = l; 206 dtbl->look_sym[lookbits] = htbl->huffval[p]; 207 lookbits++; 208 } 209 } 210 } 211 } 212 213 214 /* 215 * Out-of-line code for bit fetching (shared with jdphuff.c). 216 * See jdhuff.h for info about usage. 217 * Note: current values of get_buffer and bits_left are passed as parameters, 218 * but are returned in the corresponding fields of the state struct. 219 * 220 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 221 * of get_buffer to be used. (On machines with wider words, an even larger 222 * buffer could be used.) However, on some machines 32-bit shifts are 223 * quite slow and take time proportional to the number of places shifted. 224 * (This is true with most PC compilers, for instance.) In this case it may 225 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the 226 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. 227 */ 228 229 #ifdef SLOW_SHIFT_32 230 #define MIN_GET_BITS 15 /* minimum allowable value */ 231 #else 232 #define MIN_GET_BITS ( BIT_BUF_SIZE - 7 ) 233 #endif 234 235 236 GLOBAL boolean 237 jpeg_fill_bit_buffer( bitread_working_state * state, 238 register bit_buf_type get_buffer, register int bits_left, 239 int nbits ) { 240 /* Load up the bit buffer to a depth of at least nbits */ 241 /* Copy heavily used state fields into locals (hopefully registers) */ 242 register const JOCTET * next_input_byte = state->next_input_byte; 243 register size_t bytes_in_buffer = state->bytes_in_buffer; 244 register int c; 245 246 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ 247 /* (It is assumed that no request will be for more than that many bits.) */ 248 249 while ( bits_left < MIN_GET_BITS ) { 250 /* Attempt to read a byte */ 251 if ( state->unread_marker != 0 ) { 252 goto no_more_data; 253 } /* can't advance past a marker */ 254 255 if ( bytes_in_buffer == 0 ) { 256 if ( !( *state->cinfo->src->fill_input_buffer )( state->cinfo ) ) { 257 return FALSE; 258 } 259 next_input_byte = state->cinfo->src->next_input_byte; 260 bytes_in_buffer = state->cinfo->src->bytes_in_buffer; 261 } 262 bytes_in_buffer--; 263 c = GETJOCTET( *next_input_byte++ ); 264 265 /* If it's 0xFF, check and discard stuffed zero byte */ 266 if ( c == 0xFF ) { 267 do { 268 if ( bytes_in_buffer == 0 ) { 269 if ( !( *state->cinfo->src->fill_input_buffer )( state->cinfo ) ) { 270 return FALSE; 271 } 272 next_input_byte = state->cinfo->src->next_input_byte; 273 bytes_in_buffer = state->cinfo->src->bytes_in_buffer; 274 } 275 bytes_in_buffer--; 276 c = GETJOCTET( *next_input_byte++ ); 277 } while ( c == 0xFF ); 278 279 if ( c == 0 ) { 280 /* Found FF/00, which represents an FF data byte */ 281 c = 0xFF; 282 } else { 283 /* Oops, it's actually a marker indicating end of compressed data. */ 284 /* Better put it back for use later */ 285 state->unread_marker = c; 286 287 no_more_data: 288 /* There should be enough bits still left in the data segment; */ 289 /* if so, just break out of the outer while loop. */ 290 if ( bits_left >= nbits ) { 291 break; 292 } 293 /* Uh-oh. Report corrupted data to user and stuff zeroes into 294 * the data stream, so that we can produce some kind of image. 295 * Note that this code will be repeated for each byte demanded 296 * for the rest of the segment. We use a nonvolatile flag to ensure 297 * that only one warning message appears. 298 */ 299 if ( ! * ( state->printed_eod_ptr ) ) { 300 WARNMS( state->cinfo, JWRN_HIT_MARKER ); 301 *( state->printed_eod_ptr ) = TRUE; 302 } 303 c = 0;/* insert a zero byte into bit buffer */ 304 } 305 } 306 307 /* OK, load c into get_buffer */ 308 get_buffer = ( get_buffer << 8 ) | c; 309 bits_left += 8; 310 } 311 312 /* Unload the local registers */ 313 state->next_input_byte = next_input_byte; 314 state->bytes_in_buffer = bytes_in_buffer; 315 state->get_buffer = get_buffer; 316 state->bits_left = bits_left; 317 318 return TRUE; 319 } 320 321 322 /* 323 * Out-of-line code for Huffman code decoding. 324 * See jdhuff.h for info about usage. 325 */ 326 327 GLOBAL int 328 jpeg_huff_decode( bitread_working_state * state, 329 register bit_buf_type get_buffer, register int bits_left, 330 d_derived_tbl * htbl, int min_bits ) { 331 register int l = min_bits; 332 register INT32 code; 333 334 /* HUFF_DECODE has determined that the code is at least min_bits */ 335 /* bits long, so fetch that many bits in one swoop. */ 336 337 CHECK_BIT_BUFFER( *state, l, return -1 ); 338 code = GET_BITS( l ); 339 340 /* Collect the rest of the Huffman code one bit at a time. */ 341 /* This is per Figure F.16 in the JPEG spec. */ 342 343 while ( code > htbl->maxcode[l] ) { 344 code <<= 1; 345 346 CHECK_BIT_BUFFER( *state, 1, return -1 ); 347 code |= GET_BITS( 1 ); 348 l++; 349 } 350 351 /* Unload the local registers */ 352 state->get_buffer = get_buffer; 353 state->bits_left = bits_left; 354 355 /* With garbage input we may reach the sentinel value l = 17. */ 356 357 if ( l > 16 ) { 358 WARNMS( state->cinfo, JWRN_HUFF_BAD_CODE ); 359 return 0; /* fake a zero as the safest result */ 360 } 361 362 return htbl->pub->huffval[ htbl->valptr[l] + 363 ( (int) ( code - htbl->mincode[l] ) ) ]; 364 } 365 366 367 /* 368 * Figure F.12: extend sign bit. 369 * On some machines, a shift and add will be faster than a table lookup. 370 */ 371 372 #ifdef AVOID_TABLES 373 374 #define HUFF_EXTEND( x, s ) ( ( x ) < ( 1 << ( ( s ) - 1 ) ) ? ( x ) + ( ( ( -1 ) << ( s ) ) + 1 ) : ( x ) ) 375 376 #else 377 378 #define HUFF_EXTEND( x, s ) ( ( x ) < extend_test[s] ? ( x ) + extend_offset[s] : ( x ) ) 379 380 static const int extend_test[16] = /* entry n is 2**(n-1) */ 381 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 382 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; 383 384 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ 385 { 0, ( ( -1 ) << 1 ) + 1, ( ( -1 ) << 2 ) + 1, ( ( -1 ) << 3 ) + 1, ( ( -1 ) << 4 ) + 1, 386 ( ( -1 ) << 5 ) + 1, ( ( -1 ) << 6 ) + 1, ( ( -1 ) << 7 ) + 1, ( ( -1 ) << 8 ) + 1, 387 ( ( -1 ) << 9 ) + 1, ( ( -1 ) << 10 ) + 1, ( ( -1 ) << 11 ) + 1, ( ( -1 ) << 12 ) + 1, 388 ( ( -1 ) << 13 ) + 1, ( ( -1 ) << 14 ) + 1, ( ( -1 ) << 15 ) + 1 }; 389 390 #endif /* AVOID_TABLES */ 391 392 393 /* 394 * Check for a restart marker & resynchronize decoder. 395 * Returns FALSE if must suspend. 396 */ 397 398 LOCAL boolean 399 process_restart( j_decompress_ptr cinfo ) { 400 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 401 int ci; 402 403 /* Throw away any unused bits remaining in bit buffer; */ 404 /* include any full bytes in next_marker's count of discarded bytes */ 405 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; 406 entropy->bitstate.bits_left = 0; 407 408 /* Advance past the RSTn marker */ 409 if ( !( *cinfo->marker->read_restart_marker )( cinfo ) ) { 410 return FALSE; 411 } 412 413 /* Re-initialize DC predictions to 0 */ 414 for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) { 415 entropy->saved.last_dc_val[ci] = 0; 416 } 417 418 /* Reset restart counter */ 419 entropy->restarts_to_go = cinfo->restart_interval; 420 421 /* Next segment can get another out-of-data warning */ 422 entropy->bitstate.printed_eod = FALSE; 423 424 return TRUE; 425 } 426 427 428 /* 429 * Decode and return one MCU's worth of Huffman-compressed coefficients. 430 * The coefficients are reordered from zigzag order into natural array order, 431 * but are not dequantized. 432 * 433 * The i'th block of the MCU is stored into the block pointed to by 434 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. 435 * (Wholesale zeroing is usually a little faster than retail...) 436 * 437 * Returns FALSE if data source requested suspension. In that case no 438 * changes have been made to permanent state. (Exception: some output 439 * coefficients may already have been assigned. This is harmless for 440 * this module, since we'll just re-assign them on the next call.) 441 */ 442 443 METHODDEF boolean 444 decode_mcu( j_decompress_ptr cinfo, JBLOCKROW * MCU_data ) { 445 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 446 register int s, k, r; 447 int blkn, ci; 448 JBLOCKROW block; 449 BITREAD_STATE_VARS; 450 savable_state state; 451 d_derived_tbl * dctbl; 452 d_derived_tbl * actbl; 453 jpeg_component_info * compptr; 454 455 /* Process restart marker if needed; may have to suspend */ 456 if ( cinfo->restart_interval ) { 457 if ( entropy->restarts_to_go == 0 ) { 458 if ( !process_restart( cinfo ) ) { 459 return FALSE; 460 } 461 } 462 } 463 464 /* Load up working state */ 465 BITREAD_LOAD_STATE( cinfo, entropy->bitstate ); 466 ASSIGN_STATE( state, entropy->saved ); 467 468 /* Outer loop handles each block in the MCU */ 469 470 for ( blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++ ) { 471 block = MCU_data[blkn]; 472 ci = cinfo->MCU_membership[blkn]; 473 compptr = cinfo->cur_comp_info[ci]; 474 dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no]; 475 actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no]; 476 477 /* Decode a single block's worth of coefficients */ 478 479 /* Section F.2.2.1: decode the DC coefficient difference */ 480 HUFF_DECODE( s, br_state, dctbl, return FALSE, label1 ); 481 if ( s ) { 482 CHECK_BIT_BUFFER( br_state, s, return FALSE ); 483 484 r = GET_BITS( s ); 485 s = HUFF_EXTEND( r, s ); 486 } 487 488 /* Shortcut if component's values are not interesting */ 489 if ( !compptr->component_needed ) { 490 goto skip_ACs; 491 } 492 493 /* Convert DC difference to actual value, update last_dc_val */ 494 s += state.last_dc_val[ci]; 495 state.last_dc_val[ci] = s; 496 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ 497 ( *block )[0] = (JCOEF) s; 498 499 /* Do we need to decode the AC coefficients for this component? */ 500 if ( compptr->DCT_scaled_size > 1 ) { 501 502 /* Section F.2.2.2: decode the AC coefficients */ 503 /* Since zeroes are skipped, output area must be cleared beforehand */ 504 for ( k = 1; k < DCTSIZE2; k++ ) { 505 HUFF_DECODE( s, br_state, actbl, return FALSE, label2 ); 506 507 r = s >> 4; 508 s &= 15; 509 510 if ( s ) { 511 k += r; 512 513 CHECK_BIT_BUFFER( br_state, s, return FALSE ); 514 r = GET_BITS( s ); 515 s = HUFF_EXTEND( r, s ); 516 /* Output coefficient in natural (dezigzagged) order. 517 * Note: the extra entries in jpeg_natural_order[] will save us 518 * if k >= DCTSIZE2, which could happen if the data is corrupted. 519 */ 520 ( *block )[jpeg_natural_order[k]] = (JCOEF) s; 521 } else { 522 if ( r != 15 ) { 523 break; 524 } 525 k += 15; 526 } 527 } 528 529 } else { 530 skip_ACs: 531 532 /* Section F.2.2.2: decode the AC coefficients */ 533 /* In this path we just discard the values */ 534 for ( k = 1; k < DCTSIZE2; k++ ) { 535 HUFF_DECODE( s, br_state, actbl, return FALSE, label3 ); 536 537 r = s >> 4; 538 s &= 15; 539 540 if ( s ) { 541 k += r; 542 543 CHECK_BIT_BUFFER( br_state, s, return FALSE ); 544 DROP_BITS( s ); 545 } else { 546 if ( r != 15 ) { 547 break; 548 } 549 k += 15; 550 } 551 } 552 553 } 554 } 555 556 /* Completed MCU, so update state */ 557 BITREAD_SAVE_STATE( cinfo, entropy->bitstate ); 558 ASSIGN_STATE( entropy->saved, state ); 559 560 /* Account for restart interval (no-op if not using restarts) */ 561 entropy->restarts_to_go--; 562 563 return TRUE; 564 } 565 566 567 /* 568 * Module initialization routine for Huffman entropy decoding. 569 */ 570 571 GLOBAL void 572 jinit_huff_decoder( j_decompress_ptr cinfo ) { 573 huff_entropy_ptr entropy; 574 int i; 575 576 entropy = (huff_entropy_ptr) 577 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE, 578 SIZEOF( huff_entropy_decoder ) ); 579 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 580 entropy->pub.start_pass = start_pass_huff_decoder; 581 entropy->pub.decode_mcu = decode_mcu; 582 583 /* Mark tables unallocated */ 584 for ( i = 0; i < NUM_HUFF_TBLS; i++ ) { 585 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 586 } 587 }