media/libjpeg/jdhuff.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial