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