1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jdhuff.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,816 @@ 1.4 +/* 1.5 + * jdhuff.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1991-1997, Thomas G. Lane. 1.9 + * libjpeg-turbo Modifications: 1.10 + * Copyright (C) 2009-2011, D. R. Commander. 1.11 + * For conditions of distribution and use, see the accompanying README file. 1.12 + * 1.13 + * This file contains Huffman entropy decoding routines. 1.14 + * 1.15 + * Much of the complexity here has to do with supporting input suspension. 1.16 + * If the data source module demands suspension, we want to be able to back 1.17 + * up to the start of the current MCU. To do this, we copy state variables 1.18 + * into local working storage, and update them back to the permanent 1.19 + * storage only upon successful completion of an MCU. 1.20 + */ 1.21 + 1.22 +#define JPEG_INTERNALS 1.23 +#include "jinclude.h" 1.24 +#include "jpeglib.h" 1.25 +#include "jdhuff.h" /* Declarations shared with jdphuff.c */ 1.26 +#include "jpegcomp.h" 1.27 +#include "jstdhuff.c" 1.28 + 1.29 + 1.30 +/* 1.31 + * Expanded entropy decoder object for Huffman decoding. 1.32 + * 1.33 + * The savable_state subrecord contains fields that change within an MCU, 1.34 + * but must not be updated permanently until we complete the MCU. 1.35 + */ 1.36 + 1.37 +typedef struct { 1.38 + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 1.39 +} savable_state; 1.40 + 1.41 +/* This macro is to work around compilers with missing or broken 1.42 + * structure assignment. You'll need to fix this code if you have 1.43 + * such a compiler and you change MAX_COMPS_IN_SCAN. 1.44 + */ 1.45 + 1.46 +#ifndef NO_STRUCT_ASSIGN 1.47 +#define ASSIGN_STATE(dest,src) ((dest) = (src)) 1.48 +#else 1.49 +#if MAX_COMPS_IN_SCAN == 4 1.50 +#define ASSIGN_STATE(dest,src) \ 1.51 + ((dest).last_dc_val[0] = (src).last_dc_val[0], \ 1.52 + (dest).last_dc_val[1] = (src).last_dc_val[1], \ 1.53 + (dest).last_dc_val[2] = (src).last_dc_val[2], \ 1.54 + (dest).last_dc_val[3] = (src).last_dc_val[3]) 1.55 +#endif 1.56 +#endif 1.57 + 1.58 + 1.59 +typedef struct { 1.60 + struct jpeg_entropy_decoder pub; /* public fields */ 1.61 + 1.62 + /* These fields are loaded into local variables at start of each MCU. 1.63 + * In case of suspension, we exit WITHOUT updating them. 1.64 + */ 1.65 + bitread_perm_state bitstate; /* Bit buffer at start of MCU */ 1.66 + savable_state saved; /* Other state at start of MCU */ 1.67 + 1.68 + /* These fields are NOT loaded into local working state. */ 1.69 + unsigned int restarts_to_go; /* MCUs left in this restart interval */ 1.70 + 1.71 + /* Pointers to derived tables (these workspaces have image lifespan) */ 1.72 + d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; 1.73 + d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; 1.74 + 1.75 + /* Precalculated info set up by start_pass for use in decode_mcu: */ 1.76 + 1.77 + /* Pointers to derived tables to be used for each block within an MCU */ 1.78 + d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 1.79 + d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 1.80 + /* Whether we care about the DC and AC coefficient values for each block */ 1.81 + boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; 1.82 + boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; 1.83 +} huff_entropy_decoder; 1.84 + 1.85 +typedef huff_entropy_decoder * huff_entropy_ptr; 1.86 + 1.87 + 1.88 +/* 1.89 + * Initialize for a Huffman-compressed scan. 1.90 + */ 1.91 + 1.92 +METHODDEF(void) 1.93 +start_pass_huff_decoder (j_decompress_ptr cinfo) 1.94 +{ 1.95 + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 1.96 + int ci, blkn, dctbl, actbl; 1.97 + jpeg_component_info * compptr; 1.98 + 1.99 + /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 1.100 + * This ought to be an error condition, but we make it a warning because 1.101 + * there are some baseline files out there with all zeroes in these bytes. 1.102 + */ 1.103 + if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || 1.104 + cinfo->Ah != 0 || cinfo->Al != 0) 1.105 + WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 1.106 + 1.107 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.108 + compptr = cinfo->cur_comp_info[ci]; 1.109 + dctbl = compptr->dc_tbl_no; 1.110 + actbl = compptr->ac_tbl_no; 1.111 + /* Compute derived values for Huffman tables */ 1.112 + /* We may do this more than once for a table, but it's not expensive */ 1.113 + jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, 1.114 + & entropy->dc_derived_tbls[dctbl]); 1.115 + jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, 1.116 + & entropy->ac_derived_tbls[actbl]); 1.117 + /* Initialize DC predictions to 0 */ 1.118 + entropy->saved.last_dc_val[ci] = 0; 1.119 + } 1.120 + 1.121 + /* Precalculate decoding info for each block in an MCU of this scan */ 1.122 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.123 + ci = cinfo->MCU_membership[blkn]; 1.124 + compptr = cinfo->cur_comp_info[ci]; 1.125 + /* Precalculate which table to use for each block */ 1.126 + entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; 1.127 + entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; 1.128 + /* Decide whether we really care about the coefficient values */ 1.129 + if (compptr->component_needed) { 1.130 + entropy->dc_needed[blkn] = TRUE; 1.131 + /* we don't need the ACs if producing a 1/8th-size image */ 1.132 + entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1); 1.133 + } else { 1.134 + entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; 1.135 + } 1.136 + } 1.137 + 1.138 + /* Initialize bitread state variables */ 1.139 + entropy->bitstate.bits_left = 0; 1.140 + entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ 1.141 + entropy->pub.insufficient_data = FALSE; 1.142 + 1.143 + /* Initialize restart counter */ 1.144 + entropy->restarts_to_go = cinfo->restart_interval; 1.145 +} 1.146 + 1.147 + 1.148 +/* 1.149 + * Compute the derived values for a Huffman table. 1.150 + * This routine also performs some validation checks on the table. 1.151 + * 1.152 + * Note this is also used by jdphuff.c. 1.153 + */ 1.154 + 1.155 +GLOBAL(void) 1.156 +jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, 1.157 + d_derived_tbl ** pdtbl) 1.158 +{ 1.159 + JHUFF_TBL *htbl; 1.160 + d_derived_tbl *dtbl; 1.161 + int p, i, l, si, numsymbols; 1.162 + int lookbits, ctr; 1.163 + char huffsize[257]; 1.164 + unsigned int huffcode[257]; 1.165 + unsigned int code; 1.166 + 1.167 + /* Note that huffsize[] and huffcode[] are filled in code-length order, 1.168 + * paralleling the order of the symbols themselves in htbl->huffval[]. 1.169 + */ 1.170 + 1.171 + /* Find the input Huffman table */ 1.172 + if (tblno < 0 || tblno >= NUM_HUFF_TBLS) 1.173 + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 1.174 + htbl = 1.175 + isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; 1.176 + if (htbl == NULL) 1.177 + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 1.178 + 1.179 + /* Allocate a workspace if we haven't already done so. */ 1.180 + if (*pdtbl == NULL) 1.181 + *pdtbl = (d_derived_tbl *) 1.182 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.183 + SIZEOF(d_derived_tbl)); 1.184 + dtbl = *pdtbl; 1.185 + dtbl->pub = htbl; /* fill in back link */ 1.186 + 1.187 + /* Figure C.1: make table of Huffman code length for each symbol */ 1.188 + 1.189 + p = 0; 1.190 + for (l = 1; l <= 16; l++) { 1.191 + i = (int) htbl->bits[l]; 1.192 + if (i < 0 || p + i > 256) /* protect against table overrun */ 1.193 + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 1.194 + while (i--) 1.195 + huffsize[p++] = (char) l; 1.196 + } 1.197 + huffsize[p] = 0; 1.198 + numsymbols = p; 1.199 + 1.200 + /* Figure C.2: generate the codes themselves */ 1.201 + /* We also validate that the counts represent a legal Huffman code tree. */ 1.202 + 1.203 + code = 0; 1.204 + si = huffsize[0]; 1.205 + p = 0; 1.206 + while (huffsize[p]) { 1.207 + while (((int) huffsize[p]) == si) { 1.208 + huffcode[p++] = code; 1.209 + code++; 1.210 + } 1.211 + /* code is now 1 more than the last code used for codelength si; but 1.212 + * it must still fit in si bits, since no code is allowed to be all ones. 1.213 + */ 1.214 + if (((INT32) code) >= (((INT32) 1) << si)) 1.215 + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 1.216 + code <<= 1; 1.217 + si++; 1.218 + } 1.219 + 1.220 + /* Figure F.15: generate decoding tables for bit-sequential decoding */ 1.221 + 1.222 + p = 0; 1.223 + for (l = 1; l <= 16; l++) { 1.224 + if (htbl->bits[l]) { 1.225 + /* valoffset[l] = huffval[] index of 1st symbol of code length l, 1.226 + * minus the minimum code of length l 1.227 + */ 1.228 + dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; 1.229 + p += htbl->bits[l]; 1.230 + dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ 1.231 + } else { 1.232 + dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ 1.233 + } 1.234 + } 1.235 + dtbl->valoffset[17] = 0; 1.236 + dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ 1.237 + 1.238 + /* Compute lookahead tables to speed up decoding. 1.239 + * First we set all the table entries to 0, indicating "too long"; 1.240 + * then we iterate through the Huffman codes that are short enough and 1.241 + * fill in all the entries that correspond to bit sequences starting 1.242 + * with that code. 1.243 + */ 1.244 + 1.245 + for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) 1.246 + dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; 1.247 + 1.248 + p = 0; 1.249 + for (l = 1; l <= HUFF_LOOKAHEAD; l++) { 1.250 + for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { 1.251 + /* l = current code's length, p = its index in huffcode[] & huffval[]. */ 1.252 + /* Generate left-justified code followed by all possible bit sequences */ 1.253 + lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); 1.254 + for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { 1.255 + dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; 1.256 + lookbits++; 1.257 + } 1.258 + } 1.259 + } 1.260 + 1.261 + /* Validate symbols as being reasonable. 1.262 + * For AC tables, we make no check, but accept all byte values 0..255. 1.263 + * For DC tables, we require the symbols to be in range 0..15. 1.264 + * (Tighter bounds could be applied depending on the data depth and mode, 1.265 + * but this is sufficient to ensure safe decoding.) 1.266 + */ 1.267 + if (isDC) { 1.268 + for (i = 0; i < numsymbols; i++) { 1.269 + int sym = htbl->huffval[i]; 1.270 + if (sym < 0 || sym > 15) 1.271 + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 1.272 + } 1.273 + } 1.274 +} 1.275 + 1.276 + 1.277 +/* 1.278 + * Out-of-line code for bit fetching (shared with jdphuff.c). 1.279 + * See jdhuff.h for info about usage. 1.280 + * Note: current values of get_buffer and bits_left are passed as parameters, 1.281 + * but are returned in the corresponding fields of the state struct. 1.282 + * 1.283 + * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 1.284 + * of get_buffer to be used. (On machines with wider words, an even larger 1.285 + * buffer could be used.) However, on some machines 32-bit shifts are 1.286 + * quite slow and take time proportional to the number of places shifted. 1.287 + * (This is true with most PC compilers, for instance.) In this case it may 1.288 + * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the 1.289 + * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. 1.290 + */ 1.291 + 1.292 +#ifdef SLOW_SHIFT_32 1.293 +#define MIN_GET_BITS 15 /* minimum allowable value */ 1.294 +#else 1.295 +#define MIN_GET_BITS (BIT_BUF_SIZE-7) 1.296 +#endif 1.297 + 1.298 + 1.299 +GLOBAL(boolean) 1.300 +jpeg_fill_bit_buffer (bitread_working_state * state, 1.301 + register bit_buf_type get_buffer, register int bits_left, 1.302 + int nbits) 1.303 +/* Load up the bit buffer to a depth of at least nbits */ 1.304 +{ 1.305 + /* Copy heavily used state fields into locals (hopefully registers) */ 1.306 + register const JOCTET * next_input_byte = state->next_input_byte; 1.307 + register size_t bytes_in_buffer = state->bytes_in_buffer; 1.308 + j_decompress_ptr cinfo = state->cinfo; 1.309 + 1.310 + /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ 1.311 + /* (It is assumed that no request will be for more than that many bits.) */ 1.312 + /* We fail to do so only if we hit a marker or are forced to suspend. */ 1.313 + 1.314 + if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ 1.315 + while (bits_left < MIN_GET_BITS) { 1.316 + register int c; 1.317 + 1.318 + /* Attempt to read a byte */ 1.319 + if (bytes_in_buffer == 0) { 1.320 + if (! (*cinfo->src->fill_input_buffer) (cinfo)) 1.321 + return FALSE; 1.322 + next_input_byte = cinfo->src->next_input_byte; 1.323 + bytes_in_buffer = cinfo->src->bytes_in_buffer; 1.324 + } 1.325 + bytes_in_buffer--; 1.326 + c = GETJOCTET(*next_input_byte++); 1.327 + 1.328 + /* If it's 0xFF, check and discard stuffed zero byte */ 1.329 + if (c == 0xFF) { 1.330 + /* Loop here to discard any padding FF's on terminating marker, 1.331 + * so that we can save a valid unread_marker value. NOTE: we will 1.332 + * accept multiple FF's followed by a 0 as meaning a single FF data 1.333 + * byte. This data pattern is not valid according to the standard. 1.334 + */ 1.335 + do { 1.336 + if (bytes_in_buffer == 0) { 1.337 + if (! (*cinfo->src->fill_input_buffer) (cinfo)) 1.338 + return FALSE; 1.339 + next_input_byte = cinfo->src->next_input_byte; 1.340 + bytes_in_buffer = cinfo->src->bytes_in_buffer; 1.341 + } 1.342 + bytes_in_buffer--; 1.343 + c = GETJOCTET(*next_input_byte++); 1.344 + } while (c == 0xFF); 1.345 + 1.346 + if (c == 0) { 1.347 + /* Found FF/00, which represents an FF data byte */ 1.348 + c = 0xFF; 1.349 + } else { 1.350 + /* Oops, it's actually a marker indicating end of compressed data. 1.351 + * Save the marker code for later use. 1.352 + * Fine point: it might appear that we should save the marker into 1.353 + * bitread working state, not straight into permanent state. But 1.354 + * once we have hit a marker, we cannot need to suspend within the 1.355 + * current MCU, because we will read no more bytes from the data 1.356 + * source. So it is OK to update permanent state right away. 1.357 + */ 1.358 + cinfo->unread_marker = c; 1.359 + /* See if we need to insert some fake zero bits. */ 1.360 + goto no_more_bytes; 1.361 + } 1.362 + } 1.363 + 1.364 + /* OK, load c into get_buffer */ 1.365 + get_buffer = (get_buffer << 8) | c; 1.366 + bits_left += 8; 1.367 + } /* end while */ 1.368 + } else { 1.369 + no_more_bytes: 1.370 + /* We get here if we've read the marker that terminates the compressed 1.371 + * data segment. There should be enough bits in the buffer register 1.372 + * to satisfy the request; if so, no problem. 1.373 + */ 1.374 + if (nbits > bits_left) { 1.375 + /* Uh-oh. Report corrupted data to user and stuff zeroes into 1.376 + * the data stream, so that we can produce some kind of image. 1.377 + * We use a nonvolatile flag to ensure that only one warning message 1.378 + * appears per data segment. 1.379 + */ 1.380 + if (! cinfo->entropy->insufficient_data) { 1.381 + WARNMS(cinfo, JWRN_HIT_MARKER); 1.382 + cinfo->entropy->insufficient_data = TRUE; 1.383 + } 1.384 + /* Fill the buffer with zero bits */ 1.385 + get_buffer <<= MIN_GET_BITS - bits_left; 1.386 + bits_left = MIN_GET_BITS; 1.387 + } 1.388 + } 1.389 + 1.390 + /* Unload the local registers */ 1.391 + state->next_input_byte = next_input_byte; 1.392 + state->bytes_in_buffer = bytes_in_buffer; 1.393 + state->get_buffer = get_buffer; 1.394 + state->bits_left = bits_left; 1.395 + 1.396 + return TRUE; 1.397 +} 1.398 + 1.399 + 1.400 +/* Macro version of the above, which performs much better but does not 1.401 + handle markers. We have to hand off any blocks with markers to the 1.402 + slower routines. */ 1.403 + 1.404 +#define GET_BYTE \ 1.405 +{ \ 1.406 + register int c0, c1; \ 1.407 + c0 = GETJOCTET(*buffer++); \ 1.408 + c1 = GETJOCTET(*buffer); \ 1.409 + /* Pre-execute most common case */ \ 1.410 + get_buffer = (get_buffer << 8) | c0; \ 1.411 + bits_left += 8; \ 1.412 + if (c0 == 0xFF) { \ 1.413 + /* Pre-execute case of FF/00, which represents an FF data byte */ \ 1.414 + buffer++; \ 1.415 + if (c1 != 0) { \ 1.416 + /* Oops, it's actually a marker indicating end of compressed data. */ \ 1.417 + cinfo->unread_marker = c1; \ 1.418 + /* Back out pre-execution and fill the buffer with zero bits */ \ 1.419 + buffer -= 2; \ 1.420 + get_buffer &= ~0xFF; \ 1.421 + } \ 1.422 + } \ 1.423 +} 1.424 + 1.425 +#if __WORDSIZE == 64 || defined(_WIN64) 1.426 + 1.427 +/* Pre-fetch 48 bytes, because the holding register is 64-bit */ 1.428 +#define FILL_BIT_BUFFER_FAST \ 1.429 + if (bits_left < 16) { \ 1.430 + GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \ 1.431 + } 1.432 + 1.433 +#else 1.434 + 1.435 +/* Pre-fetch 16 bytes, because the holding register is 32-bit */ 1.436 +#define FILL_BIT_BUFFER_FAST \ 1.437 + if (bits_left < 16) { \ 1.438 + GET_BYTE GET_BYTE \ 1.439 + } 1.440 + 1.441 +#endif 1.442 + 1.443 + 1.444 +/* 1.445 + * Out-of-line code for Huffman code decoding. 1.446 + * See jdhuff.h for info about usage. 1.447 + */ 1.448 + 1.449 +GLOBAL(int) 1.450 +jpeg_huff_decode (bitread_working_state * state, 1.451 + register bit_buf_type get_buffer, register int bits_left, 1.452 + d_derived_tbl * htbl, int min_bits) 1.453 +{ 1.454 + register int l = min_bits; 1.455 + register INT32 code; 1.456 + 1.457 + /* HUFF_DECODE has determined that the code is at least min_bits */ 1.458 + /* bits long, so fetch that many bits in one swoop. */ 1.459 + 1.460 + CHECK_BIT_BUFFER(*state, l, return -1); 1.461 + code = GET_BITS(l); 1.462 + 1.463 + /* Collect the rest of the Huffman code one bit at a time. */ 1.464 + /* This is per Figure F.16 in the JPEG spec. */ 1.465 + 1.466 + while (code > htbl->maxcode[l]) { 1.467 + code <<= 1; 1.468 + CHECK_BIT_BUFFER(*state, 1, return -1); 1.469 + code |= GET_BITS(1); 1.470 + l++; 1.471 + } 1.472 + 1.473 + /* Unload the local registers */ 1.474 + state->get_buffer = get_buffer; 1.475 + state->bits_left = bits_left; 1.476 + 1.477 + /* With garbage input we may reach the sentinel value l = 17. */ 1.478 + 1.479 + if (l > 16) { 1.480 + WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); 1.481 + return 0; /* fake a zero as the safest result */ 1.482 + } 1.483 + 1.484 + return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; 1.485 +} 1.486 + 1.487 + 1.488 +/* 1.489 + * Figure F.12: extend sign bit. 1.490 + * On some machines, a shift and add will be faster than a table lookup. 1.491 + */ 1.492 + 1.493 +#define AVOID_TABLES 1.494 +#ifdef AVOID_TABLES 1.495 + 1.496 +#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1))) 1.497 + 1.498 +#else 1.499 + 1.500 +#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) 1.501 + 1.502 +static const int extend_test[16] = /* entry n is 2**(n-1) */ 1.503 + { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 1.504 + 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; 1.505 + 1.506 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ 1.507 + { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, 1.508 + ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, 1.509 + ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, 1.510 + ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; 1.511 + 1.512 +#endif /* AVOID_TABLES */ 1.513 + 1.514 + 1.515 +/* 1.516 + * Check for a restart marker & resynchronize decoder. 1.517 + * Returns FALSE if must suspend. 1.518 + */ 1.519 + 1.520 +LOCAL(boolean) 1.521 +process_restart (j_decompress_ptr cinfo) 1.522 +{ 1.523 + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 1.524 + int ci; 1.525 + 1.526 + /* Throw away any unused bits remaining in bit buffer; */ 1.527 + /* include any full bytes in next_marker's count of discarded bytes */ 1.528 + cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; 1.529 + entropy->bitstate.bits_left = 0; 1.530 + 1.531 + /* Advance past the RSTn marker */ 1.532 + if (! (*cinfo->marker->read_restart_marker) (cinfo)) 1.533 + return FALSE; 1.534 + 1.535 + /* Re-initialize DC predictions to 0 */ 1.536 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) 1.537 + entropy->saved.last_dc_val[ci] = 0; 1.538 + 1.539 + /* Reset restart counter */ 1.540 + entropy->restarts_to_go = cinfo->restart_interval; 1.541 + 1.542 + /* Reset out-of-data flag, unless read_restart_marker left us smack up 1.543 + * against a marker. In that case we will end up treating the next data 1.544 + * segment as empty, and we can avoid producing bogus output pixels by 1.545 + * leaving the flag set. 1.546 + */ 1.547 + if (cinfo->unread_marker == 0) 1.548 + entropy->pub.insufficient_data = FALSE; 1.549 + 1.550 + return TRUE; 1.551 +} 1.552 + 1.553 + 1.554 +LOCAL(boolean) 1.555 +decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 1.556 +{ 1.557 + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 1.558 + BITREAD_STATE_VARS; 1.559 + int blkn; 1.560 + savable_state state; 1.561 + /* Outer loop handles each block in the MCU */ 1.562 + 1.563 + /* Load up working state */ 1.564 + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 1.565 + ASSIGN_STATE(state, entropy->saved); 1.566 + 1.567 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.568 + JBLOCKROW block = MCU_data[blkn]; 1.569 + d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; 1.570 + d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; 1.571 + register int s, k, r; 1.572 + 1.573 + /* Decode a single block's worth of coefficients */ 1.574 + 1.575 + /* Section F.2.2.1: decode the DC coefficient difference */ 1.576 + HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); 1.577 + if (s) { 1.578 + CHECK_BIT_BUFFER(br_state, s, return FALSE); 1.579 + r = GET_BITS(s); 1.580 + s = HUFF_EXTEND(r, s); 1.581 + } 1.582 + 1.583 + if (entropy->dc_needed[blkn]) { 1.584 + /* Convert DC difference to actual value, update last_dc_val */ 1.585 + int ci = cinfo->MCU_membership[blkn]; 1.586 + s += state.last_dc_val[ci]; 1.587 + state.last_dc_val[ci] = s; 1.588 + /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ 1.589 + (*block)[0] = (JCOEF) s; 1.590 + } 1.591 + 1.592 + if (entropy->ac_needed[blkn]) { 1.593 + 1.594 + /* Section F.2.2.2: decode the AC coefficients */ 1.595 + /* Since zeroes are skipped, output area must be cleared beforehand */ 1.596 + for (k = 1; k < DCTSIZE2; k++) { 1.597 + HUFF_DECODE(s, br_state, actbl, return FALSE, label2); 1.598 + 1.599 + r = s >> 4; 1.600 + s &= 15; 1.601 + 1.602 + if (s) { 1.603 + k += r; 1.604 + CHECK_BIT_BUFFER(br_state, s, return FALSE); 1.605 + r = GET_BITS(s); 1.606 + s = HUFF_EXTEND(r, s); 1.607 + /* Output coefficient in natural (dezigzagged) order. 1.608 + * Note: the extra entries in jpeg_natural_order[] will save us 1.609 + * if k >= DCTSIZE2, which could happen if the data is corrupted. 1.610 + */ 1.611 + (*block)[jpeg_natural_order[k]] = (JCOEF) s; 1.612 + } else { 1.613 + if (r != 15) 1.614 + break; 1.615 + k += 15; 1.616 + } 1.617 + } 1.618 + 1.619 + } else { 1.620 + 1.621 + /* Section F.2.2.2: decode the AC coefficients */ 1.622 + /* In this path we just discard the values */ 1.623 + for (k = 1; k < DCTSIZE2; k++) { 1.624 + HUFF_DECODE(s, br_state, actbl, return FALSE, label3); 1.625 + 1.626 + r = s >> 4; 1.627 + s &= 15; 1.628 + 1.629 + if (s) { 1.630 + k += r; 1.631 + CHECK_BIT_BUFFER(br_state, s, return FALSE); 1.632 + DROP_BITS(s); 1.633 + } else { 1.634 + if (r != 15) 1.635 + break; 1.636 + k += 15; 1.637 + } 1.638 + } 1.639 + } 1.640 + } 1.641 + 1.642 + /* Completed MCU, so update state */ 1.643 + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 1.644 + ASSIGN_STATE(entropy->saved, state); 1.645 + return TRUE; 1.646 +} 1.647 + 1.648 + 1.649 +LOCAL(boolean) 1.650 +decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 1.651 +{ 1.652 + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 1.653 + BITREAD_STATE_VARS; 1.654 + JOCTET *buffer; 1.655 + int blkn; 1.656 + savable_state state; 1.657 + /* Outer loop handles each block in the MCU */ 1.658 + 1.659 + /* Load up working state */ 1.660 + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 1.661 + buffer = (JOCTET *) br_state.next_input_byte; 1.662 + ASSIGN_STATE(state, entropy->saved); 1.663 + 1.664 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.665 + JBLOCKROW block = MCU_data[blkn]; 1.666 + d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; 1.667 + d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; 1.668 + register int s, k, r, l; 1.669 + 1.670 + HUFF_DECODE_FAST(s, l, dctbl); 1.671 + if (s) { 1.672 + FILL_BIT_BUFFER_FAST 1.673 + r = GET_BITS(s); 1.674 + s = HUFF_EXTEND(r, s); 1.675 + } 1.676 + 1.677 + if (entropy->dc_needed[blkn]) { 1.678 + int ci = cinfo->MCU_membership[blkn]; 1.679 + s += state.last_dc_val[ci]; 1.680 + state.last_dc_val[ci] = s; 1.681 + (*block)[0] = (JCOEF) s; 1.682 + } 1.683 + 1.684 + if (entropy->ac_needed[blkn]) { 1.685 + 1.686 + for (k = 1; k < DCTSIZE2; k++) { 1.687 + HUFF_DECODE_FAST(s, l, actbl); 1.688 + r = s >> 4; 1.689 + s &= 15; 1.690 + 1.691 + if (s) { 1.692 + k += r; 1.693 + FILL_BIT_BUFFER_FAST 1.694 + r = GET_BITS(s); 1.695 + s = HUFF_EXTEND(r, s); 1.696 + (*block)[jpeg_natural_order[k]] = (JCOEF) s; 1.697 + } else { 1.698 + if (r != 15) break; 1.699 + k += 15; 1.700 + } 1.701 + } 1.702 + 1.703 + } else { 1.704 + 1.705 + for (k = 1; k < DCTSIZE2; k++) { 1.706 + HUFF_DECODE_FAST(s, l, actbl); 1.707 + r = s >> 4; 1.708 + s &= 15; 1.709 + 1.710 + if (s) { 1.711 + k += r; 1.712 + FILL_BIT_BUFFER_FAST 1.713 + DROP_BITS(s); 1.714 + } else { 1.715 + if (r != 15) break; 1.716 + k += 15; 1.717 + } 1.718 + } 1.719 + } 1.720 + } 1.721 + 1.722 + if (cinfo->unread_marker != 0) { 1.723 + cinfo->unread_marker = 0; 1.724 + return FALSE; 1.725 + } 1.726 + 1.727 + br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte); 1.728 + br_state.next_input_byte = buffer; 1.729 + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 1.730 + ASSIGN_STATE(entropy->saved, state); 1.731 + return TRUE; 1.732 +} 1.733 + 1.734 + 1.735 +/* 1.736 + * Decode and return one MCU's worth of Huffman-compressed coefficients. 1.737 + * The coefficients are reordered from zigzag order into natural array order, 1.738 + * but are not dequantized. 1.739 + * 1.740 + * The i'th block of the MCU is stored into the block pointed to by 1.741 + * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. 1.742 + * (Wholesale zeroing is usually a little faster than retail...) 1.743 + * 1.744 + * Returns FALSE if data source requested suspension. In that case no 1.745 + * changes have been made to permanent state. (Exception: some output 1.746 + * coefficients may already have been assigned. This is harmless for 1.747 + * this module, since we'll just re-assign them on the next call.) 1.748 + */ 1.749 + 1.750 +#define BUFSIZE (DCTSIZE2 * 2) 1.751 + 1.752 +METHODDEF(boolean) 1.753 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 1.754 +{ 1.755 + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 1.756 + int usefast = 1; 1.757 + 1.758 + /* Process restart marker if needed; may have to suspend */ 1.759 + if (cinfo->restart_interval) { 1.760 + if (entropy->restarts_to_go == 0) 1.761 + if (! process_restart(cinfo)) 1.762 + return FALSE; 1.763 + usefast = 0; 1.764 + } 1.765 + 1.766 + if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU 1.767 + || cinfo->unread_marker != 0) 1.768 + usefast = 0; 1.769 + 1.770 + /* If we've run out of data, just leave the MCU set to zeroes. 1.771 + * This way, we return uniform gray for the remainder of the segment. 1.772 + */ 1.773 + if (! entropy->pub.insufficient_data) { 1.774 + 1.775 + if (usefast) { 1.776 + if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow; 1.777 + } 1.778 + else { 1.779 + use_slow: 1.780 + if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE; 1.781 + } 1.782 + 1.783 + } 1.784 + 1.785 + /* Account for restart interval (no-op if not using restarts) */ 1.786 + entropy->restarts_to_go--; 1.787 + 1.788 + return TRUE; 1.789 +} 1.790 + 1.791 + 1.792 +/* 1.793 + * Module initialization routine for Huffman entropy decoding. 1.794 + */ 1.795 + 1.796 +GLOBAL(void) 1.797 +jinit_huff_decoder (j_decompress_ptr cinfo) 1.798 +{ 1.799 + huff_entropy_ptr entropy; 1.800 + int i; 1.801 + 1.802 + /* Motion JPEG frames typically do not include the Huffman tables if they 1.803 + are the default tables. Thus, if the tables are not set by the time 1.804 + the Huffman decoder is initialized (usually within the body of 1.805 + jpeg_start_decompress()), we set them to default values. */ 1.806 + std_huff_tables((j_common_ptr) cinfo); 1.807 + 1.808 + entropy = (huff_entropy_ptr) 1.809 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.810 + SIZEOF(huff_entropy_decoder)); 1.811 + cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 1.812 + entropy->pub.start_pass = start_pass_huff_decoder; 1.813 + entropy->pub.decode_mcu = decode_mcu; 1.814 + 1.815 + /* Mark tables unallocated */ 1.816 + for (i = 0; i < NUM_HUFF_TBLS; i++) { 1.817 + entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 1.818 + } 1.819 +}