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