vrshoot

annotate libs/libjpeg/jdhuff.c @ 2:334d17aed7de

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