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