Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenjdhuff.c
Go to the documentation of this file.
00001 /* 00002 * jdhuff.c 00003 * 00004 * Copyright (C) 1991-1997, Thomas G. Lane. 00005 * Modified 2006-2009 by Guido Vollbeding. 00006 * This file is part of the Independent JPEG Group's software. 00007 * For conditions of distribution and use, see the accompanying README file. 00008 * 00009 * This file contains Huffman entropy decoding routines. 00010 * Both sequential and progressive modes are supported in this single module. 00011 * 00012 * Much of the complexity here has to do with supporting input suspension. 00013 * If the data source module demands suspension, we want to be able to back 00014 * up to the start of the current MCU. To do this, we copy state variables 00015 * into local working storage, and update them back to the permanent 00016 * storage only upon successful completion of an MCU. 00017 */ 00018 00019 #define JPEG_INTERNALS 00020 #include "jinclude.h" 00021 #include "jpeglib.h" 00022 00023 00024 /* Derived data constructed for each Huffman table */ 00025 00026 #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ 00027 00028 typedef struct { 00029 /* Basic tables: (element [0] of each array is unused) */ 00030 INT32 maxcode[18]; /* largest code of length k (-1 if none) */ 00031 /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ 00032 INT32 valoffset[17]; /* huffval[] offset for codes of length k */ 00033 /* valoffset[k] = huffval[] index of 1st symbol of code length k, less 00034 * the smallest code of length k; so given a code of length k, the 00035 * corresponding symbol is huffval[code + valoffset[k]] 00036 */ 00037 00038 /* Link to public Huffman table (needed only in jpeg_huff_decode) */ 00039 JHUFF_TBL *pub; 00040 00041 /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of 00042 * the input data stream. If the next Huffman code is no more 00043 * than HUFF_LOOKAHEAD bits long, we can obtain its length and 00044 * the corresponding symbol directly from these tables. 00045 */ 00046 int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */ 00047 UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */ 00048 } d_derived_tbl; 00049 00050 00051 /* 00052 * Fetching the next N bits from the input stream is a time-critical operation 00053 * for the Huffman decoders. We implement it with a combination of inline 00054 * macros and out-of-line subroutines. Note that N (the number of bits 00055 * demanded at one time) never exceeds 15 for JPEG use. 00056 * 00057 * We read source bytes into get_buffer and dole out bits as needed. 00058 * If get_buffer already contains enough bits, they are fetched in-line 00059 * by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough 00060 * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer 00061 * as full as possible (not just to the number of bits needed; this 00062 * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer). 00063 * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension. 00064 * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains 00065 * at least the requested number of bits --- dummy zeroes are inserted if 00066 * necessary. 00067 */ 00068 00069 typedef INT32 bit_buf_type; /* type of bit-extraction buffer */ 00070 #define BIT_BUF_SIZE 32 /* size of buffer in bits */ 00071 00072 /* If long is > 32 bits on your machine, and shifting/masking longs is 00073 * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE 00074 * appropriately should be a win. Unfortunately we can't define the size 00075 * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) 00076 * because not all machines measure sizeof in 8-bit bytes. 00077 */ 00078 00079 typedef struct { /* Bitreading state saved across MCUs */ 00080 bit_buf_type get_buffer; /* current bit-extraction buffer */ 00081 int bits_left; /* # of unused bits in it */ 00082 } bitread_perm_state; 00083 00084 typedef struct { /* Bitreading working state within an MCU */ 00085 /* Current data source location */ 00086 /* We need a copy, rather than munging the original, in case of suspension */ 00087 const JOCTET * next_input_byte; /* => next byte to read from source */ 00088 size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ 00089 /* Bit input buffer --- note these values are kept in register variables, 00090 * not in this struct, inside the inner loops. 00091 */ 00092 bit_buf_type get_buffer; /* current bit-extraction buffer */ 00093 int bits_left; /* # of unused bits in it */ 00094 /* Pointer needed by jpeg_fill_bit_buffer. */ 00095 j_decompress_ptr cinfo; /* back link to decompress master record */ 00096 } bitread_working_state; 00097 00098 /* Macros to declare and load/save bitread local variables. */ 00099 #define BITREAD_STATE_VARS \ 00100 register bit_buf_type get_buffer; \ 00101 register int bits_left; \ 00102 bitread_working_state br_state 00103 00104 #define BITREAD_LOAD_STATE(cinfop,permstate) \ 00105 br_state.cinfo = cinfop; \ 00106 br_state.next_input_byte = cinfop->src->next_input_byte; \ 00107 br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ 00108 get_buffer = permstate.get_buffer; \ 00109 bits_left = permstate.bits_left; 00110 00111 #define BITREAD_SAVE_STATE(cinfop,permstate) \ 00112 cinfop->src->next_input_byte = br_state.next_input_byte; \ 00113 cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ 00114 permstate.get_buffer = get_buffer; \ 00115 permstate.bits_left = bits_left 00116 00117 /* 00118 * These macros provide the in-line portion of bit fetching. 00119 * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer 00120 * before using GET_BITS, PEEK_BITS, or DROP_BITS. 00121 * The variables get_buffer and bits_left are assumed to be locals, 00122 * but the state struct might not be (jpeg_huff_decode needs this). 00123 * CHECK_BIT_BUFFER(state,n,action); 00124 * Ensure there are N bits in get_buffer; if suspend, take action. 00125 * val = GET_BITS(n); 00126 * Fetch next N bits. 00127 * val = PEEK_BITS(n); 00128 * Fetch next N bits without removing them from the buffer. 00129 * DROP_BITS(n); 00130 * Discard next N bits. 00131 * The value N should be a simple variable, not an expression, because it 00132 * is evaluated multiple times. 00133 */ 00134 00135 #define CHECK_BIT_BUFFER(state,nbits,action) \ 00136 { if (bits_left < (nbits)) { \ 00137 if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \ 00138 { action; } \ 00139 get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } 00140 00141 #define GET_BITS(nbits) \ 00142 (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits)) 00143 00144 #define PEEK_BITS(nbits) \ 00145 (((int) (get_buffer >> (bits_left - (nbits)))) & BIT_MASK(nbits)) 00146 00147 #define DROP_BITS(nbits) \ 00148 (bits_left -= (nbits)) 00149 00150 00151 /* 00152 * Code for extracting next Huffman-coded symbol from input bit stream. 00153 * Again, this is time-critical and we make the main paths be macros. 00154 * 00155 * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits 00156 * without looping. Usually, more than 95% of the Huffman codes will be 8 00157 * or fewer bits long. The few overlength codes are handled with a loop, 00158 * which need not be inline code. 00159 * 00160 * Notes about the HUFF_DECODE macro: 00161 * 1. Near the end of the data segment, we may fail to get enough bits 00162 * for a lookahead. In that case, we do it the hard way. 00163 * 2. If the lookahead table contains no entry, the next code must be 00164 * more than HUFF_LOOKAHEAD bits long. 00165 * 3. jpeg_huff_decode returns -1 if forced to suspend. 00166 */ 00167 00168 #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ 00169 { register int nb, look; \ 00170 if (bits_left < HUFF_LOOKAHEAD) { \ 00171 if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ 00172 get_buffer = state.get_buffer; bits_left = state.bits_left; \ 00173 if (bits_left < HUFF_LOOKAHEAD) { \ 00174 nb = 1; goto slowlabel; \ 00175 } \ 00176 } \ 00177 look = PEEK_BITS(HUFF_LOOKAHEAD); \ 00178 if ((nb = htbl->look_nbits[look]) != 0) { \ 00179 DROP_BITS(nb); \ 00180 result = htbl->look_sym[look]; \ 00181 } else { \ 00182 nb = HUFF_LOOKAHEAD+1; \ 00183 slowlabel: \ 00184 if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ 00185 { failaction; } \ 00186 get_buffer = state.get_buffer; bits_left = state.bits_left; \ 00187 } \ 00188 } 00189 00190 00191 /* 00192 * Expanded entropy decoder object for Huffman decoding. 00193 * 00194 * The savable_state subrecord contains fields that change within an MCU, 00195 * but must not be updated permanently until we complete the MCU. 00196 */ 00197 00198 typedef struct { 00199 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ 00200 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 00201 } savable_state; 00202 00203 /* This macro is to work around compilers with missing or broken 00204 * structure assignment. You'll need to fix this code if you have 00205 * such a compiler and you change MAX_COMPS_IN_SCAN. 00206 */ 00207 00208 #ifndef NO_STRUCT_ASSIGN 00209 #define ASSIGN_STATE(dest,src) ((dest) = (src)) 00210 #else 00211 #if MAX_COMPS_IN_SCAN == 4 00212 #define ASSIGN_STATE(dest,src) \ 00213 ((dest).EOBRUN = (src).EOBRUN, \ 00214 (dest).last_dc_val[0] = (src).last_dc_val[0], \ 00215 (dest).last_dc_val[1] = (src).last_dc_val[1], \ 00216 (dest).last_dc_val[2] = (src).last_dc_val[2], \ 00217 (dest).last_dc_val[3] = (src).last_dc_val[3]) 00218 #endif 00219 #endif 00220 00221 00222 typedef struct { 00223 struct jpeg_entropy_decoder pub; /* public fields */ 00224 00225 /* These fields are loaded into local variables at start of each MCU. 00226 * In case of suspension, we exit WITHOUT updating them. 00227 */ 00228 bitread_perm_state bitstate; /* Bit buffer at start of MCU */ 00229 savable_state saved; /* Other state at start of MCU */ 00230 00231 /* These fields are NOT loaded into local working state. */ 00232 boolean insufficient_data; /* set TRUE after emitting warning */ 00233 unsigned int restarts_to_go; /* MCUs left in this restart interval */ 00234 00235 /* Following two fields used only in progressive mode */ 00236 00237 /* Pointers to derived tables (these workspaces have image lifespan) */ 00238 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; 00239 00240 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ 00241 00242 /* Following fields used only in sequential mode */ 00243 00244 /* Pointers to derived tables (these workspaces have image lifespan) */ 00245 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; 00246 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; 00247 00248 /* Precalculated info set up by start_pass for use in decode_mcu: */ 00249 00250 /* Pointers to derived tables to be used for each block within an MCU */ 00251 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 00252 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 00253 /* Whether we care about the DC and AC coefficient values for each block */ 00254 int coef_limit[D_MAX_BLOCKS_IN_MCU]; 00255 } huff_entropy_decoder; 00256 00257 typedef huff_entropy_decoder * huff_entropy_ptr; 00258 00259 00260 static const int jpeg_zigzag_order[8][8] = { 00261 { 0, 1, 5, 6, 14, 15, 27, 28 }, 00262 { 2, 4, 7, 13, 16, 26, 29, 42 }, 00263 { 3, 8, 12, 17, 25, 30, 41, 43 }, 00264 { 9, 11, 18, 24, 31, 40, 44, 53 }, 00265 { 10, 19, 23, 32, 39, 45, 52, 54 }, 00266 { 20, 22, 33, 38, 46, 51, 55, 60 }, 00267 { 21, 34, 37, 47, 50, 56, 59, 61 }, 00268 { 35, 36, 48, 49, 57, 58, 62, 63 } 00269 }; 00270 00271 static const int jpeg_zigzag_order7[7][7] = { 00272 { 0, 1, 5, 6, 14, 15, 27 }, 00273 { 2, 4, 7, 13, 16, 26, 28 }, 00274 { 3, 8, 12, 17, 25, 29, 38 }, 00275 { 9, 11, 18, 24, 30, 37, 39 }, 00276 { 10, 19, 23, 31, 36, 40, 45 }, 00277 { 20, 22, 32, 35, 41, 44, 46 }, 00278 { 21, 33, 34, 42, 43, 47, 48 } 00279 }; 00280 00281 static const int jpeg_zigzag_order6[6][6] = { 00282 { 0, 1, 5, 6, 14, 15 }, 00283 { 2, 4, 7, 13, 16, 25 }, 00284 { 3, 8, 12, 17, 24, 26 }, 00285 { 9, 11, 18, 23, 27, 32 }, 00286 { 10, 19, 22, 28, 31, 33 }, 00287 { 20, 21, 29, 30, 34, 35 } 00288 }; 00289 00290 static const int jpeg_zigzag_order5[5][5] = { 00291 { 0, 1, 5, 6, 14 }, 00292 { 2, 4, 7, 13, 15 }, 00293 { 3, 8, 12, 16, 21 }, 00294 { 9, 11, 17, 20, 22 }, 00295 { 10, 18, 19, 23, 24 } 00296 }; 00297 00298 static const int jpeg_zigzag_order4[4][4] = { 00299 { 0, 1, 5, 6 }, 00300 { 2, 4, 7, 12 }, 00301 { 3, 8, 11, 13 }, 00302 { 9, 10, 14, 15 } 00303 }; 00304 00305 static const int jpeg_zigzag_order3[3][3] = { 00306 { 0, 1, 5 }, 00307 { 2, 4, 6 }, 00308 { 3, 7, 8 } 00309 }; 00310 00311 static const int jpeg_zigzag_order2[2][2] = { 00312 { 0, 1 }, 00313 { 2, 3 } 00314 }; 00315 00316 00317 /* 00318 * Compute the derived values for a Huffman table. 00319 * This routine also performs some validation checks on the table. 00320 */ 00321 00322 LOCAL(void) 00323 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, 00324 d_derived_tbl ** pdtbl) 00325 { 00326 JHUFF_TBL *htbl; 00327 d_derived_tbl *dtbl; 00328 int p, i, l, si, numsymbols; 00329 int lookbits, ctr; 00330 char huffsize[257]; 00331 unsigned int huffcode[257]; 00332 unsigned int code; 00333 00334 /* Note that huffsize[] and huffcode[] are filled in code-length order, 00335 * paralleling the order of the symbols themselves in htbl->huffval[]. 00336 */ 00337 00338 /* Find the input Huffman table */ 00339 if (tblno < 0 || tblno >= NUM_HUFF_TBLS) 00340 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 00341 htbl = 00342 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; 00343 if (htbl == NULL) 00344 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 00345 00346 /* Allocate a workspace if we haven't already done so. */ 00347 if (*pdtbl == NULL) 00348 *pdtbl = (d_derived_tbl *) 00349 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00350 SIZEOF(d_derived_tbl)); 00351 dtbl = *pdtbl; 00352 dtbl->pub = htbl; /* fill in back link */ 00353 00354 /* Figure C.1: make table of Huffman code length for each symbol */ 00355 00356 p = 0; 00357 for (l = 1; l <= 16; l++) { 00358 i = (int) htbl->bits[l]; 00359 if (i < 0 || p + i > 256) /* protect against table overrun */ 00360 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00361 while (i--) 00362 huffsize[p++] = (char) l; 00363 } 00364 huffsize[p] = 0; 00365 numsymbols = p; 00366 00367 /* Figure C.2: generate the codes themselves */ 00368 /* We also validate that the counts represent a legal Huffman code tree. */ 00369 00370 code = 0; 00371 si = huffsize[0]; 00372 p = 0; 00373 while (huffsize[p]) { 00374 while (((int) huffsize[p]) == si) { 00375 huffcode[p++] = code; 00376 code++; 00377 } 00378 /* code is now 1 more than the last code used for codelength si; but 00379 * it must still fit in si bits, since no code is allowed to be all ones. 00380 */ 00381 if (((INT32) code) >= (((INT32) 1) << si)) 00382 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00383 code <<= 1; 00384 si++; 00385 } 00386 00387 /* Figure F.15: generate decoding tables for bit-sequential decoding */ 00388 00389 p = 0; 00390 for (l = 1; l <= 16; l++) { 00391 if (htbl->bits[l]) { 00392 /* valoffset[l] = huffval[] index of 1st symbol of code length l, 00393 * minus the minimum code of length l 00394 */ 00395 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; 00396 p += htbl->bits[l]; 00397 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ 00398 } else { 00399 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ 00400 } 00401 } 00402 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ 00403 00404 /* Compute lookahead tables to speed up decoding. 00405 * First we set all the table entries to 0, indicating "too long"; 00406 * then we iterate through the Huffman codes that are short enough and 00407 * fill in all the entries that correspond to bit sequences starting 00408 * with that code. 00409 */ 00410 00411 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits)); 00412 00413 p = 0; 00414 for (l = 1; l <= HUFF_LOOKAHEAD; l++) { 00415 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { 00416 /* l = current code's length, p = its index in huffcode[] & huffval[]. */ 00417 /* Generate left-justified code followed by all possible bit sequences */ 00418 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); 00419 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { 00420 dtbl->look_nbits[lookbits] = l; 00421 dtbl->look_sym[lookbits] = htbl->huffval[p]; 00422 lookbits++; 00423 } 00424 } 00425 } 00426 00427 /* Validate symbols as being reasonable. 00428 * For AC tables, we make no check, but accept all byte values 0..255. 00429 * For DC tables, we require the symbols to be in range 0..15. 00430 * (Tighter bounds could be applied depending on the data depth and mode, 00431 * but this is sufficient to ensure safe decoding.) 00432 */ 00433 if (isDC) { 00434 for (i = 0; i < numsymbols; i++) { 00435 int sym = htbl->huffval[i]; 00436 if (sym < 0 || sym > 15) 00437 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00438 } 00439 } 00440 } 00441 00442 00443 /* 00444 * Out-of-line code for bit fetching. 00445 * Note: current values of get_buffer and bits_left are passed as parameters, 00446 * but are returned in the corresponding fields of the state struct. 00447 * 00448 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 00449 * of get_buffer to be used. (On machines with wider words, an even larger 00450 * buffer could be used.) However, on some machines 32-bit shifts are 00451 * quite slow and take time proportional to the number of places shifted. 00452 * (This is true with most PC compilers, for instance.) In this case it may 00453 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the 00454 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. 00455 */ 00456 00457 #ifdef SLOW_SHIFT_32 00458 #define MIN_GET_BITS 15 /* minimum allowable value */ 00459 #else 00460 #define MIN_GET_BITS (BIT_BUF_SIZE-7) 00461 #endif 00462 00463 00464 LOCAL(boolean) 00465 jpeg_fill_bit_buffer (bitread_working_state * state, 00466 register bit_buf_type get_buffer, register int bits_left, 00467 int nbits) 00468 /* Load up the bit buffer to a depth of at least nbits */ 00469 { 00470 /* Copy heavily used state fields into locals (hopefully registers) */ 00471 register const JOCTET * next_input_byte = state->next_input_byte; 00472 register size_t bytes_in_buffer = state->bytes_in_buffer; 00473 j_decompress_ptr cinfo = state->cinfo; 00474 00475 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ 00476 /* (It is assumed that no request will be for more than that many bits.) */ 00477 /* We fail to do so only if we hit a marker or are forced to suspend. */ 00478 00479 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ 00480 while (bits_left < MIN_GET_BITS) { 00481 register int c; 00482 00483 /* Attempt to read a byte */ 00484 if (bytes_in_buffer == 0) { 00485 if (! (*cinfo->src->fill_input_buffer) (cinfo)) 00486 return FALSE; 00487 next_input_byte = cinfo->src->next_input_byte; 00488 bytes_in_buffer = cinfo->src->bytes_in_buffer; 00489 } 00490 bytes_in_buffer--; 00491 c = GETJOCTET(*next_input_byte++); 00492 00493 /* If it's 0xFF, check and discard stuffed zero byte */ 00494 if (c == 0xFF) { 00495 /* Loop here to discard any padding FF's on terminating marker, 00496 * so that we can save a valid unread_marker value. NOTE: we will 00497 * accept multiple FF's followed by a 0 as meaning a single FF data 00498 * byte. This data pattern is not valid according to the standard. 00499 */ 00500 do { 00501 if (bytes_in_buffer == 0) { 00502 if (! (*cinfo->src->fill_input_buffer) (cinfo)) 00503 return FALSE; 00504 next_input_byte = cinfo->src->next_input_byte; 00505 bytes_in_buffer = cinfo->src->bytes_in_buffer; 00506 } 00507 bytes_in_buffer--; 00508 c = GETJOCTET(*next_input_byte++); 00509 } while (c == 0xFF); 00510 00511 if (c == 0) { 00512 /* Found FF/00, which represents an FF data byte */ 00513 c = 0xFF; 00514 } else { 00515 /* Oops, it's actually a marker indicating end of compressed data. 00516 * Save the marker code for later use. 00517 * Fine point: it might appear that we should save the marker into 00518 * bitread working state, not straight into permanent state. But 00519 * once we have hit a marker, we cannot need to suspend within the 00520 * current MCU, because we will read no more bytes from the data 00521 * source. So it is OK to update permanent state right away. 00522 */ 00523 cinfo->unread_marker = c; 00524 /* See if we need to insert some fake zero bits. */ 00525 goto no_more_bytes; 00526 } 00527 } 00528 00529 /* OK, load c into get_buffer */ 00530 get_buffer = (get_buffer << 8) | c; 00531 bits_left += 8; 00532 } /* end while */ 00533 } else { 00534 no_more_bytes: 00535 /* We get here if we've read the marker that terminates the compressed 00536 * data segment. There should be enough bits in the buffer register 00537 * to satisfy the request; if so, no problem. 00538 */ 00539 if (nbits > bits_left) { 00540 /* Uh-oh. Report corrupted data to user and stuff zeroes into 00541 * the data stream, so that we can produce some kind of image. 00542 * We use a nonvolatile flag to ensure that only one warning message 00543 * appears per data segment. 00544 */ 00545 if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) { 00546 WARNMS(cinfo, JWRN_HIT_MARKER); 00547 ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE; 00548 } 00549 /* Fill the buffer with zero bits */ 00550 get_buffer <<= MIN_GET_BITS - bits_left; 00551 bits_left = MIN_GET_BITS; 00552 } 00553 } 00554 00555 /* Unload the local registers */ 00556 state->next_input_byte = next_input_byte; 00557 state->bytes_in_buffer = bytes_in_buffer; 00558 state->get_buffer = get_buffer; 00559 state->bits_left = bits_left; 00560 00561 return TRUE; 00562 } 00563 00564 00565 /* 00566 * Figure F.12: extend sign bit. 00567 * On some machines, a shift and sub will be faster than a table lookup. 00568 */ 00569 00570 #ifdef AVOID_TABLES 00571 00572 #define BIT_MASK(nbits) ((1<<(nbits))-1) 00573 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) - ((1<<(s))-1) : (x)) 00574 00575 #else 00576 00577 #define BIT_MASK(nbits) bmask[nbits] 00578 #define HUFF_EXTEND(x,s) ((x) <= bmask[(s) - 1] ? (x) - bmask[s] : (x)) 00579 00580 static const int bmask[16] = /* bmask[n] is mask for n rightmost bits */ 00581 { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 00582 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF }; 00583 00584 #endif /* AVOID_TABLES */ 00585 00586 00587 /* 00588 * Out-of-line code for Huffman code decoding. 00589 */ 00590 00591 LOCAL(int) 00592 jpeg_huff_decode (bitread_working_state * state, 00593 register bit_buf_type get_buffer, register int bits_left, 00594 d_derived_tbl * htbl, int min_bits) 00595 { 00596 register int l = min_bits; 00597 register INT32 code; 00598 00599 /* HUFF_DECODE has determined that the code is at least min_bits */ 00600 /* bits long, so fetch that many bits in one swoop. */ 00601 00602 CHECK_BIT_BUFFER(*state, l, return -1); 00603 code = GET_BITS(l); 00604 00605 /* Collect the rest of the Huffman code one bit at a time. */ 00606 /* This is per Figure F.16 in the JPEG spec. */ 00607 00608 while (code > htbl->maxcode[l]) { 00609 code <<= 1; 00610 CHECK_BIT_BUFFER(*state, 1, return -1); 00611 code |= GET_BITS(1); 00612 l++; 00613 } 00614 00615 /* Unload the local registers */ 00616 state->get_buffer = get_buffer; 00617 state->bits_left = bits_left; 00618 00619 /* With garbage input we may reach the sentinel value l = 17. */ 00620 00621 if (l > 16) { 00622 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); 00623 return 0; /* fake a zero as the safest result */ 00624 } 00625 00626 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; 00627 } 00628 00629 00630 /* 00631 * Check for a restart marker & resynchronize decoder. 00632 * Returns FALSE if must suspend. 00633 */ 00634 00635 LOCAL(boolean) 00636 process_restart (j_decompress_ptr cinfo) 00637 { 00638 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00639 int ci; 00640 00641 /* Throw away any unused bits remaining in bit buffer; */ 00642 /* include any full bytes in next_marker's count of discarded bytes */ 00643 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; 00644 entropy->bitstate.bits_left = 0; 00645 00646 /* Advance past the RSTn marker */ 00647 if (! (*cinfo->marker->read_restart_marker) (cinfo)) 00648 return FALSE; 00649 00650 /* Re-initialize DC predictions to 0 */ 00651 for (ci = 0; ci < cinfo->comps_in_scan; ci++) 00652 entropy->saved.last_dc_val[ci] = 0; 00653 /* Re-init EOB run count, too */ 00654 entropy->saved.EOBRUN = 0; 00655 00656 /* Reset restart counter */ 00657 entropy->restarts_to_go = cinfo->restart_interval; 00658 00659 /* Reset out-of-data flag, unless read_restart_marker left us smack up 00660 * against a marker. In that case we will end up treating the next data 00661 * segment as empty, and we can avoid producing bogus output pixels by 00662 * leaving the flag set. 00663 */ 00664 if (cinfo->unread_marker == 0) 00665 entropy->insufficient_data = FALSE; 00666 00667 return TRUE; 00668 } 00669 00670 00671 /* 00672 * Huffman MCU decoding. 00673 * Each of these routines decodes and returns one MCU's worth of 00674 * Huffman-compressed coefficients. 00675 * The coefficients are reordered from zigzag order into natural array order, 00676 * but are not dequantized. 00677 * 00678 * The i'th block of the MCU is stored into the block pointed to by 00679 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. 00680 * (Wholesale zeroing is usually a little faster than retail...) 00681 * 00682 * We return FALSE if data source requested suspension. In that case no 00683 * changes have been made to permanent state. (Exception: some output 00684 * coefficients may already have been assigned. This is harmless for 00685 * spectral selection, since we'll just re-assign them on the next call. 00686 * Successive approximation AC refinement has to be more careful, however.) 00687 */ 00688 00689 /* 00690 * MCU decoding for DC initial scan (either spectral selection, 00691 * or first pass of successive approximation). 00692 */ 00693 00694 METHODDEF(boolean) 00695 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 00696 { 00697 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00698 int Al = cinfo->Al; 00699 register int s, r; 00700 int blkn, ci; 00701 JBLOCKROW block; 00702 BITREAD_STATE_VARS; 00703 savable_state state; 00704 d_derived_tbl * tbl; 00705 jpeg_component_info * compptr; 00706 00707 /* Process restart marker if needed; may have to suspend */ 00708 if (cinfo->restart_interval) { 00709 if (entropy->restarts_to_go == 0) 00710 if (! process_restart(cinfo)) 00711 return FALSE; 00712 } 00713 00714 /* If we've run out of data, just leave the MCU set to zeroes. 00715 * This way, we return uniform gray for the remainder of the segment. 00716 */ 00717 if (! entropy->insufficient_data) { 00718 00719 /* Load up working state */ 00720 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 00721 ASSIGN_STATE(state, entropy->saved); 00722 00723 /* Outer loop handles each block in the MCU */ 00724 00725 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 00726 block = MCU_data[blkn]; 00727 ci = cinfo->MCU_membership[blkn]; 00728 compptr = cinfo->cur_comp_info[ci]; 00729 tbl = entropy->derived_tbls[compptr->dc_tbl_no]; 00730 00731 /* Decode a single block's worth of coefficients */ 00732 00733 /* Section F.2.2.1: decode the DC coefficient difference */ 00734 HUFF_DECODE(s, br_state, tbl, return FALSE, label1); 00735 if (s) { 00736 CHECK_BIT_BUFFER(br_state, s, return FALSE); 00737 r = GET_BITS(s); 00738 s = HUFF_EXTEND(r, s); 00739 } 00740 00741 /* Convert DC difference to actual value, update last_dc_val */ 00742 s += state.last_dc_val[ci]; 00743 state.last_dc_val[ci] = s; 00744 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ 00745 (*block)[0] = (JCOEF) (s << Al); 00746 } 00747 00748 /* Completed MCU, so update state */ 00749 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 00750 ASSIGN_STATE(entropy->saved, state); 00751 } 00752 00753 /* Account for restart interval (no-op if not using restarts) */ 00754 entropy->restarts_to_go--; 00755 00756 return TRUE; 00757 } 00758 00759 00760 /* 00761 * MCU decoding for AC initial scan (either spectral selection, 00762 * or first pass of successive approximation). 00763 */ 00764 00765 METHODDEF(boolean) 00766 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 00767 { 00768 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00769 register int s, k, r; 00770 unsigned int EOBRUN; 00771 int Se, Al; 00772 const int * natural_order; 00773 JBLOCKROW block; 00774 BITREAD_STATE_VARS; 00775 d_derived_tbl * tbl; 00776 00777 /* Process restart marker if needed; may have to suspend */ 00778 if (cinfo->restart_interval) { 00779 if (entropy->restarts_to_go == 0) 00780 if (! process_restart(cinfo)) 00781 return FALSE; 00782 } 00783 00784 /* If we've run out of data, just leave the MCU set to zeroes. 00785 * This way, we return uniform gray for the remainder of the segment. 00786 */ 00787 if (! entropy->insufficient_data) { 00788 00789 Se = cinfo->Se; 00790 Al = cinfo->Al; 00791 natural_order = cinfo->natural_order; 00792 00793 /* Load up working state. 00794 * We can avoid loading/saving bitread state if in an EOB run. 00795 */ 00796 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ 00797 00798 /* There is always only one block per MCU */ 00799 00800 if (EOBRUN > 0) /* if it's a band of zeroes... */ 00801 EOBRUN--; /* ...process it now (we do nothing) */ 00802 else { 00803 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 00804 block = MCU_data[0]; 00805 tbl = entropy->ac_derived_tbl; 00806 00807 for (k = cinfo->Ss; k <= Se; k++) { 00808 HUFF_DECODE(s, br_state, tbl, return FALSE, label2); 00809 r = s >> 4; 00810 s &= 15; 00811 if (s) { 00812 k += r; 00813 CHECK_BIT_BUFFER(br_state, s, return FALSE); 00814 r = GET_BITS(s); 00815 s = HUFF_EXTEND(r, s); 00816 /* Scale and output coefficient in natural (dezigzagged) order */ 00817 (*block)[natural_order[k]] = (JCOEF) (s << Al); 00818 } else { 00819 if (r == 15) { /* ZRL */ 00820 k += 15; /* skip 15 zeroes in band */ 00821 } else { /* EOBr, run length is 2^r + appended bits */ 00822 EOBRUN = 1 << r; 00823 if (r) { /* EOBr, r > 0 */ 00824 CHECK_BIT_BUFFER(br_state, r, return FALSE); 00825 r = GET_BITS(r); 00826 EOBRUN += r; 00827 } 00828 EOBRUN--; /* this band is processed at this moment */ 00829 break; /* force end-of-band */ 00830 } 00831 } 00832 } 00833 00834 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 00835 } 00836 00837 /* Completed MCU, so update state */ 00838 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ 00839 } 00840 00841 /* Account for restart interval (no-op if not using restarts) */ 00842 entropy->restarts_to_go--; 00843 00844 return TRUE; 00845 } 00846 00847 00848 /* 00849 * MCU decoding for DC successive approximation refinement scan. 00850 * Note: we assume such scans can be multi-component, although the spec 00851 * is not very clear on the point. 00852 */ 00853 00854 METHODDEF(boolean) 00855 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 00856 { 00857 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00858 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 00859 int blkn; 00860 JBLOCKROW block; 00861 BITREAD_STATE_VARS; 00862 00863 /* Process restart marker if needed; may have to suspend */ 00864 if (cinfo->restart_interval) { 00865 if (entropy->restarts_to_go == 0) 00866 if (! process_restart(cinfo)) 00867 return FALSE; 00868 } 00869 00870 /* Not worth the cycles to check insufficient_data here, 00871 * since we will not change the data anyway if we read zeroes. 00872 */ 00873 00874 /* Load up working state */ 00875 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 00876 00877 /* Outer loop handles each block in the MCU */ 00878 00879 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 00880 block = MCU_data[blkn]; 00881 00882 /* Encoded data is simply the next bit of the two's-complement DC value */ 00883 CHECK_BIT_BUFFER(br_state, 1, return FALSE); 00884 if (GET_BITS(1)) 00885 (*block)[0] |= p1; 00886 /* Note: since we use |=, repeating the assignment later is safe */ 00887 } 00888 00889 /* Completed MCU, so update state */ 00890 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 00891 00892 /* Account for restart interval (no-op if not using restarts) */ 00893 entropy->restarts_to_go--; 00894 00895 return TRUE; 00896 } 00897 00898 00899 /* 00900 * MCU decoding for AC successive approximation refinement scan. 00901 */ 00902 00903 METHODDEF(boolean) 00904 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 00905 { 00906 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00907 register int s, k, r; 00908 unsigned int EOBRUN; 00909 int Se, p1, m1; 00910 const int * natural_order; 00911 JBLOCKROW block; 00912 JCOEFPTR thiscoef; 00913 BITREAD_STATE_VARS; 00914 d_derived_tbl * tbl; 00915 int num_newnz; 00916 int newnz_pos[DCTSIZE2]; 00917 00918 /* Process restart marker if needed; may have to suspend */ 00919 if (cinfo->restart_interval) { 00920 if (entropy->restarts_to_go == 0) 00921 if (! process_restart(cinfo)) 00922 return FALSE; 00923 } 00924 00925 /* If we've run out of data, don't modify the MCU. 00926 */ 00927 if (! entropy->insufficient_data) { 00928 00929 Se = cinfo->Se; 00930 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 00931 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ 00932 natural_order = cinfo->natural_order; 00933 00934 /* Load up working state */ 00935 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 00936 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ 00937 00938 /* There is always only one block per MCU */ 00939 block = MCU_data[0]; 00940 tbl = entropy->ac_derived_tbl; 00941 00942 /* If we are forced to suspend, we must undo the assignments to any newly 00943 * nonzero coefficients in the block, because otherwise we'd get confused 00944 * next time about which coefficients were already nonzero. 00945 * But we need not undo addition of bits to already-nonzero coefficients; 00946 * instead, we can test the current bit to see if we already did it. 00947 */ 00948 num_newnz = 0; 00949 00950 /* initialize coefficient loop counter to start of band */ 00951 k = cinfo->Ss; 00952 00953 if (EOBRUN == 0) { 00954 for (; k <= Se; k++) { 00955 HUFF_DECODE(s, br_state, tbl, goto undoit, label3); 00956 r = s >> 4; 00957 s &= 15; 00958 if (s) { 00959 if (s != 1) /* size of new coef should always be 1 */ 00960 WARNMS(cinfo, JWRN_HUFF_BAD_CODE); 00961 CHECK_BIT_BUFFER(br_state, 1, goto undoit); 00962 if (GET_BITS(1)) 00963 s = p1; /* newly nonzero coef is positive */ 00964 else 00965 s = m1; /* newly nonzero coef is negative */ 00966 } else { 00967 if (r != 15) { 00968 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ 00969 if (r) { 00970 CHECK_BIT_BUFFER(br_state, r, goto undoit); 00971 r = GET_BITS(r); 00972 EOBRUN += r; 00973 } 00974 break; /* rest of block is handled by EOB logic */ 00975 } 00976 /* note s = 0 for processing ZRL */ 00977 } 00978 /* Advance over already-nonzero coefs and r still-zero coefs, 00979 * appending correction bits to the nonzeroes. A correction bit is 1 00980 * if the absolute value of the coefficient must be increased. 00981 */ 00982 do { 00983 thiscoef = *block + natural_order[k]; 00984 if (*thiscoef != 0) { 00985 CHECK_BIT_BUFFER(br_state, 1, goto undoit); 00986 if (GET_BITS(1)) { 00987 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ 00988 if (*thiscoef >= 0) 00989 *thiscoef += p1; 00990 else 00991 *thiscoef += m1; 00992 } 00993 } 00994 } else { 00995 if (--r < 0) 00996 break; /* reached target zero coefficient */ 00997 } 00998 k++; 00999 } while (k <= Se); 01000 if (s) { 01001 int pos = natural_order[k]; 01002 /* Output newly nonzero coefficient */ 01003 (*block)[pos] = (JCOEF) s; 01004 /* Remember its position in case we have to suspend */ 01005 newnz_pos[num_newnz++] = pos; 01006 } 01007 } 01008 } 01009 01010 if (EOBRUN > 0) { 01011 /* Scan any remaining coefficient positions after the end-of-band 01012 * (the last newly nonzero coefficient, if any). Append a correction 01013 * bit to each already-nonzero coefficient. A correction bit is 1 01014 * if the absolute value of the coefficient must be increased. 01015 */ 01016 for (; k <= Se; k++) { 01017 thiscoef = *block + natural_order[k]; 01018 if (*thiscoef != 0) { 01019 CHECK_BIT_BUFFER(br_state, 1, goto undoit); 01020 if (GET_BITS(1)) { 01021 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ 01022 if (*thiscoef >= 0) 01023 *thiscoef += p1; 01024 else 01025 *thiscoef += m1; 01026 } 01027 } 01028 } 01029 } 01030 /* Count one block completed in EOB run */ 01031 EOBRUN--; 01032 } 01033 01034 /* Completed MCU, so update state */ 01035 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 01036 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ 01037 } 01038 01039 /* Account for restart interval (no-op if not using restarts) */ 01040 entropy->restarts_to_go--; 01041 01042 return TRUE; 01043 01044 undoit: 01045 /* Re-zero any output coefficients that we made newly nonzero */ 01046 while (num_newnz > 0) 01047 (*block)[newnz_pos[--num_newnz]] = 0; 01048 01049 return FALSE; 01050 } 01051 01052 01053 /* 01054 * Decode one MCU's worth of Huffman-compressed coefficients, 01055 * partial blocks. 01056 */ 01057 01058 METHODDEF(boolean) 01059 decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 01060 { 01061 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 01062 const int * natural_order; 01063 int Se, blkn; 01064 BITREAD_STATE_VARS; 01065 savable_state state; 01066 01067 /* Process restart marker if needed; may have to suspend */ 01068 if (cinfo->restart_interval) { 01069 if (entropy->restarts_to_go == 0) 01070 if (! process_restart(cinfo)) 01071 return FALSE; 01072 } 01073 01074 /* If we've run out of data, just leave the MCU set to zeroes. 01075 * This way, we return uniform gray for the remainder of the segment. 01076 */ 01077 if (! entropy->insufficient_data) { 01078 01079 natural_order = cinfo->natural_order; 01080 Se = cinfo->lim_Se; 01081 01082 /* Load up working state */ 01083 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 01084 ASSIGN_STATE(state, entropy->saved); 01085 01086 /* Outer loop handles each block in the MCU */ 01087 01088 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 01089 JBLOCKROW block = MCU_data[blkn]; 01090 d_derived_tbl * htbl; 01091 register int s, k, r; 01092 int coef_limit, ci; 01093 01094 /* Decode a single block's worth of coefficients */ 01095 01096 /* Section F.2.2.1: decode the DC coefficient difference */ 01097 htbl = entropy->dc_cur_tbls[blkn]; 01098 HUFF_DECODE(s, br_state, htbl, return FALSE, label1); 01099 01100 htbl = entropy->ac_cur_tbls[blkn]; 01101 k = 1; 01102 coef_limit = entropy->coef_limit[blkn]; 01103 if (coef_limit) { 01104 /* Convert DC difference to actual value, update last_dc_val */ 01105 if (s) { 01106 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01107 r = GET_BITS(s); 01108 s = HUFF_EXTEND(r, s); 01109 } 01110 ci = cinfo->MCU_membership[blkn]; 01111 s += state.last_dc_val[ci]; 01112 state.last_dc_val[ci] = s; 01113 /* Output the DC coefficient */ 01114 (*block)[0] = (JCOEF) s; 01115 01116 /* Section F.2.2.2: decode the AC coefficients */ 01117 /* Since zeroes are skipped, output area must be cleared beforehand */ 01118 for (; k < coef_limit; k++) { 01119 HUFF_DECODE(s, br_state, htbl, return FALSE, label2); 01120 01121 r = s >> 4; 01122 s &= 15; 01123 01124 if (s) { 01125 k += r; 01126 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01127 r = GET_BITS(s); 01128 s = HUFF_EXTEND(r, s); 01129 /* Output coefficient in natural (dezigzagged) order. 01130 * Note: the extra entries in natural_order[] will save us 01131 * if k > Se, which could happen if the data is corrupted. 01132 */ 01133 (*block)[natural_order[k]] = (JCOEF) s; 01134 } else { 01135 if (r != 15) 01136 goto EndOfBlock; 01137 k += 15; 01138 } 01139 } 01140 } else { 01141 if (s) { 01142 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01143 DROP_BITS(s); 01144 } 01145 } 01146 01147 /* Section F.2.2.2: decode the AC coefficients */ 01148 /* In this path we just discard the values */ 01149 for (; k <= Se; k++) { 01150 HUFF_DECODE(s, br_state, htbl, return FALSE, label3); 01151 01152 r = s >> 4; 01153 s &= 15; 01154 01155 if (s) { 01156 k += r; 01157 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01158 DROP_BITS(s); 01159 } else { 01160 if (r != 15) 01161 break; 01162 k += 15; 01163 } 01164 } 01165 01166 EndOfBlock: ; 01167 } 01168 01169 /* Completed MCU, so update state */ 01170 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 01171 ASSIGN_STATE(entropy->saved, state); 01172 } 01173 01174 /* Account for restart interval (no-op if not using restarts) */ 01175 entropy->restarts_to_go--; 01176 01177 return TRUE; 01178 } 01179 01180 01181 /* 01182 * Decode one MCU's worth of Huffman-compressed coefficients, 01183 * full-size blocks. 01184 */ 01185 01186 METHODDEF(boolean) 01187 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 01188 { 01189 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 01190 int blkn; 01191 BITREAD_STATE_VARS; 01192 savable_state state; 01193 01194 /* Process restart marker if needed; may have to suspend */ 01195 if (cinfo->restart_interval) { 01196 if (entropy->restarts_to_go == 0) 01197 if (! process_restart(cinfo)) 01198 return FALSE; 01199 } 01200 01201 /* If we've run out of data, just leave the MCU set to zeroes. 01202 * This way, we return uniform gray for the remainder of the segment. 01203 */ 01204 if (! entropy->insufficient_data) { 01205 01206 /* Load up working state */ 01207 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 01208 ASSIGN_STATE(state, entropy->saved); 01209 01210 /* Outer loop handles each block in the MCU */ 01211 01212 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 01213 JBLOCKROW block = MCU_data[blkn]; 01214 d_derived_tbl * htbl; 01215 register int s, k, r; 01216 int coef_limit, ci; 01217 01218 /* Decode a single block's worth of coefficients */ 01219 01220 /* Section F.2.2.1: decode the DC coefficient difference */ 01221 htbl = entropy->dc_cur_tbls[blkn]; 01222 HUFF_DECODE(s, br_state, htbl, return FALSE, label1); 01223 01224 htbl = entropy->ac_cur_tbls[blkn]; 01225 k = 1; 01226 coef_limit = entropy->coef_limit[blkn]; 01227 if (coef_limit) { 01228 /* Convert DC difference to actual value, update last_dc_val */ 01229 if (s) { 01230 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01231 r = GET_BITS(s); 01232 s = HUFF_EXTEND(r, s); 01233 } 01234 ci = cinfo->MCU_membership[blkn]; 01235 s += state.last_dc_val[ci]; 01236 state.last_dc_val[ci] = s; 01237 /* Output the DC coefficient */ 01238 (*block)[0] = (JCOEF) s; 01239 01240 /* Section F.2.2.2: decode the AC coefficients */ 01241 /* Since zeroes are skipped, output area must be cleared beforehand */ 01242 for (; k < coef_limit; k++) { 01243 HUFF_DECODE(s, br_state, htbl, return FALSE, label2); 01244 01245 r = s >> 4; 01246 s &= 15; 01247 01248 if (s) { 01249 k += r; 01250 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01251 r = GET_BITS(s); 01252 s = HUFF_EXTEND(r, s); 01253 /* Output coefficient in natural (dezigzagged) order. 01254 * Note: the extra entries in jpeg_natural_order[] will save us 01255 * if k >= DCTSIZE2, which could happen if the data is corrupted. 01256 */ 01257 (*block)[jpeg_natural_order[k]] = (JCOEF) s; 01258 } else { 01259 if (r != 15) 01260 goto EndOfBlock; 01261 k += 15; 01262 } 01263 } 01264 } else { 01265 if (s) { 01266 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01267 DROP_BITS(s); 01268 } 01269 } 01270 01271 /* Section F.2.2.2: decode the AC coefficients */ 01272 /* In this path we just discard the values */ 01273 for (; k < DCTSIZE2; k++) { 01274 HUFF_DECODE(s, br_state, htbl, return FALSE, label3); 01275 01276 r = s >> 4; 01277 s &= 15; 01278 01279 if (s) { 01280 k += r; 01281 CHECK_BIT_BUFFER(br_state, s, return FALSE); 01282 DROP_BITS(s); 01283 } else { 01284 if (r != 15) 01285 break; 01286 k += 15; 01287 } 01288 } 01289 01290 EndOfBlock: ; 01291 } 01292 01293 /* Completed MCU, so update state */ 01294 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 01295 ASSIGN_STATE(entropy->saved, state); 01296 } 01297 01298 /* Account for restart interval (no-op if not using restarts) */ 01299 entropy->restarts_to_go--; 01300 01301 return TRUE; 01302 } 01303 01304 01305 /* 01306 * Initialize for a Huffman-compressed scan. 01307 */ 01308 01309 METHODDEF(void) 01310 start_pass_huff_decoder (j_decompress_ptr cinfo) 01311 { 01312 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 01313 int ci, blkn, tbl, i; 01314 jpeg_component_info * compptr; 01315 01316 if (cinfo->progressive_mode) { 01317 /* Validate progressive scan parameters */ 01318 if (cinfo->Ss == 0) { 01319 if (cinfo->Se != 0) 01320 goto bad; 01321 } else { 01322 /* need not check Ss/Se < 0 since they came from unsigned bytes */ 01323 if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se) 01324 goto bad; 01325 /* AC scans may have only one component */ 01326 if (cinfo->comps_in_scan != 1) 01327 goto bad; 01328 } 01329 if (cinfo->Ah != 0) { 01330 /* Successive approximation refinement scan: must have Al = Ah-1. */ 01331 if (cinfo->Ah-1 != cinfo->Al) 01332 goto bad; 01333 } 01334 if (cinfo->Al > 13) { /* need not check for < 0 */ 01335 /* Arguably the maximum Al value should be less than 13 for 8-bit precision, 01336 * but the spec doesn't say so, and we try to be liberal about what we 01337 * accept. Note: large Al values could result in out-of-range DC 01338 * coefficients during early scans, leading to bizarre displays due to 01339 * overflows in the IDCT math. But we won't crash. 01340 */ 01341 bad: 01342 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 01343 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); 01344 } 01345 /* Update progression status, and verify that scan order is legal. 01346 * Note that inter-scan inconsistencies are treated as warnings 01347 * not fatal errors ... not clear if this is right way to behave. 01348 */ 01349 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 01350 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; 01351 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; 01352 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ 01353 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 01354 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { 01355 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; 01356 if (cinfo->Ah != expected) 01357 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); 01358 coef_bit_ptr[coefi] = cinfo->Al; 01359 } 01360 } 01361 01362 /* Select MCU decoding routine */ 01363 if (cinfo->Ah == 0) { 01364 if (cinfo->Ss == 0) 01365 entropy->pub.decode_mcu = decode_mcu_DC_first; 01366 else 01367 entropy->pub.decode_mcu = decode_mcu_AC_first; 01368 } else { 01369 if (cinfo->Ss == 0) 01370 entropy->pub.decode_mcu = decode_mcu_DC_refine; 01371 else 01372 entropy->pub.decode_mcu = decode_mcu_AC_refine; 01373 } 01374 01375 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 01376 compptr = cinfo->cur_comp_info[ci]; 01377 /* Make sure requested tables are present, and compute derived tables. 01378 * We may build same derived table more than once, but it's not expensive. 01379 */ 01380 if (cinfo->Ss == 0) { 01381 if (cinfo->Ah == 0) { /* DC refinement needs no table */ 01382 tbl = compptr->dc_tbl_no; 01383 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, 01384 & entropy->derived_tbls[tbl]); 01385 } 01386 } else { 01387 tbl = compptr->ac_tbl_no; 01388 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, 01389 & entropy->derived_tbls[tbl]); 01390 /* remember the single active table */ 01391 entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; 01392 } 01393 /* Initialize DC predictions to 0 */ 01394 entropy->saved.last_dc_val[ci] = 0; 01395 } 01396 01397 /* Initialize private state variables */ 01398 entropy->saved.EOBRUN = 0; 01399 } else { 01400 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 01401 * This ought to be an error condition, but we make it a warning because 01402 * there are some baseline files out there with all zeroes in these bytes. 01403 */ 01404 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || 01405 ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) && 01406 cinfo->Se != cinfo->lim_Se)) 01407 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 01408 01409 /* Select MCU decoding routine */ 01410 /* We retain the hard-coded case for full-size blocks. 01411 * This is not necessary, but it appears that this version is slightly 01412 * more performant in the given implementation. 01413 * With an improved implementation we would prefer a single optimized 01414 * function. 01415 */ 01416 if (cinfo->lim_Se != DCTSIZE2-1) 01417 entropy->pub.decode_mcu = decode_mcu_sub; 01418 else 01419 entropy->pub.decode_mcu = decode_mcu; 01420 01421 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 01422 compptr = cinfo->cur_comp_info[ci]; 01423 /* Compute derived values for Huffman tables */ 01424 /* We may do this more than once for a table, but it's not expensive */ 01425 tbl = compptr->dc_tbl_no; 01426 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, 01427 & entropy->dc_derived_tbls[tbl]); 01428 if (cinfo->lim_Se) { /* AC needs no table when not present */ 01429 tbl = compptr->ac_tbl_no; 01430 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, 01431 & entropy->ac_derived_tbls[tbl]); 01432 } 01433 /* Initialize DC predictions to 0 */ 01434 entropy->saved.last_dc_val[ci] = 0; 01435 } 01436 01437 /* Precalculate decoding info for each block in an MCU of this scan */ 01438 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 01439 ci = cinfo->MCU_membership[blkn]; 01440 compptr = cinfo->cur_comp_info[ci]; 01441 /* Precalculate which table to use for each block */ 01442 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; 01443 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; 01444 /* Decide whether we really care about the coefficient values */ 01445 if (compptr->component_needed) { 01446 ci = compptr->DCT_v_scaled_size; 01447 i = compptr->DCT_h_scaled_size; 01448 switch (cinfo->lim_Se) { 01449 case (1*1-1): 01450 entropy->coef_limit[blkn] = 1; 01451 break; 01452 case (2*2-1): 01453 if (ci <= 0 || ci > 2) ci = 2; 01454 if (i <= 0 || i > 2) i = 2; 01455 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1]; 01456 break; 01457 case (3*3-1): 01458 if (ci <= 0 || ci > 3) ci = 3; 01459 if (i <= 0 || i > 3) i = 3; 01460 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1]; 01461 break; 01462 case (4*4-1): 01463 if (ci <= 0 || ci > 4) ci = 4; 01464 if (i <= 0 || i > 4) i = 4; 01465 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1]; 01466 break; 01467 case (5*5-1): 01468 if (ci <= 0 || ci > 5) ci = 5; 01469 if (i <= 0 || i > 5) i = 5; 01470 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1]; 01471 break; 01472 case (6*6-1): 01473 if (ci <= 0 || ci > 6) ci = 6; 01474 if (i <= 0 || i > 6) i = 6; 01475 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1]; 01476 break; 01477 case (7*7-1): 01478 if (ci <= 0 || ci > 7) ci = 7; 01479 if (i <= 0 || i > 7) i = 7; 01480 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1]; 01481 break; 01482 default: 01483 if (ci <= 0 || ci > 8) ci = 8; 01484 if (i <= 0 || i > 8) i = 8; 01485 entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1]; 01486 break; 01487 } 01488 } else { 01489 entropy->coef_limit[blkn] = 0; 01490 } 01491 } 01492 } 01493 01494 /* Initialize bitread state variables */ 01495 entropy->bitstate.bits_left = 0; 01496 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ 01497 entropy->insufficient_data = FALSE; 01498 01499 /* Initialize restart counter */ 01500 entropy->restarts_to_go = cinfo->restart_interval; 01501 } 01502 01503 01504 /* 01505 * Module initialization routine for Huffman entropy decoding. 01506 */ 01507 01508 GLOBAL(void) 01509 jinit_huff_decoder (j_decompress_ptr cinfo) 01510 { 01511 huff_entropy_ptr entropy; 01512 int i; 01513 01514 entropy = (huff_entropy_ptr) 01515 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 01516 SIZEOF(huff_entropy_decoder)); 01517 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 01518 entropy->pub.start_pass = start_pass_huff_decoder; 01519 01520 if (cinfo->progressive_mode) { 01521 /* Create progression status table */ 01522 int *coef_bit_ptr, ci; 01523 cinfo->coef_bits = (int (*)[DCTSIZE2]) 01524 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 01525 cinfo->num_components*DCTSIZE2*SIZEOF(int)); 01526 coef_bit_ptr = & cinfo->coef_bits[0][0]; 01527 for (ci = 0; ci < cinfo->num_components; ci++) 01528 for (i = 0; i < DCTSIZE2; i++) 01529 *coef_bit_ptr++ = -1; 01530 01531 /* Mark derived tables unallocated */ 01532 for (i = 0; i < NUM_HUFF_TBLS; i++) { 01533 entropy->derived_tbls[i] = NULL; 01534 } 01535 } else { 01536 /* Mark tables unallocated */ 01537 for (i = 0; i < NUM_HUFF_TBLS; i++) { 01538 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 01539 } 01540 } 01541 } Generated on Sun May 27 2012 04:19:25 for ReactOS by
1.7.6.1
|