istereo2

annotate libs/libjpeg/jdhuff.c @ 13:ea928c313344

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