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