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