istereo

annotate libs/libjpeg/jdhuff.c @ 35:23e5d274b2a2

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