3dphotoshoot

annotate libs/libjpeg/jdhuff.c @ 14:06dc8b9b4f89

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