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