Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: jdphuff.c

Issue 1953443002: Update to libjpeg_turbo 1.4.90 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libjpeg_turbo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jdmrgext.c ('k') | jdpostct.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * jdphuff.c 2 * jdphuff.c
3 * 3 *
4 * This file was part of the Independent JPEG Group's software:
4 * Copyright (C) 1995-1997, Thomas G. Lane. 5 * Copyright (C) 1995-1997, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software. 6 * libjpeg-turbo Modifications:
6 * For conditions of distribution and use, see the accompanying README file. 7 * Copyright (C) 2015, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
7 * 10 *
8 * This file contains Huffman entropy decoding routines for progressive JPEG. 11 * This file contains Huffman entropy decoding routines for progressive JPEG.
9 * 12 *
10 * 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.
11 * 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
12 * 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
13 * into local working storage, and update them back to the permanent 16 * into local working storage, and update them back to the permanent
14 * storage only upon successful completion of an MCU. 17 * storage only upon successful completion of an MCU.
15 */ 18 */
16 19
17 #define JPEG_INTERNALS 20 #define JPEG_INTERNALS
18 #include "jinclude.h" 21 #include "jinclude.h"
19 #include "jpeglib.h" 22 #include "jpeglib.h"
20 #include "jdhuff.h"» » /* Declarations shared with jdhuff.c */ 23 #include "jdhuff.h" /* Declarations shared with jdhuff.c */
21 24
22 25
23 #ifdef D_PROGRESSIVE_SUPPORTED 26 #ifdef D_PROGRESSIVE_SUPPORTED
24 27
25 /* 28 /*
26 * Expanded entropy decoder object for progressive Huffman decoding. 29 * Expanded entropy decoder object for progressive Huffman decoding.
27 * 30 *
28 * The savable_state subrecord contains fields that change within an MCU, 31 * The savable_state subrecord contains fields that change within an MCU,
29 * but must not be updated permanently until we complete the MCU. 32 * but must not be updated permanently until we complete the MCU.
30 */ 33 */
31 34
32 typedef struct { 35 typedef struct {
33 unsigned int EOBRUN;» » » /* remaining EOBs in EOBRUN */ 36 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
34 int last_dc_val[MAX_COMPS_IN_SCAN];» /* last DC coef for each component */ 37 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35 } savable_state; 38 } savable_state;
36 39
37 /* This macro is to work around compilers with missing or broken 40 /* This macro is to work around compilers with missing or broken
38 * structure assignment. You'll need to fix this code if you have 41 * structure assignment. You'll need to fix this code if you have
39 * such a compiler and you change MAX_COMPS_IN_SCAN. 42 * such a compiler and you change MAX_COMPS_IN_SCAN.
40 */ 43 */
41 44
42 #ifndef NO_STRUCT_ASSIGN 45 #ifndef NO_STRUCT_ASSIGN
43 #define ASSIGN_STATE(dest,src) ((dest) = (src)) 46 #define ASSIGN_STATE(dest,src) ((dest) = (src))
44 #else 47 #else
45 #if MAX_COMPS_IN_SCAN == 4 48 #if MAX_COMPS_IN_SCAN == 4
46 #define ASSIGN_STATE(dest,src) \ 49 #define ASSIGN_STATE(dest,src) \
47 » ((dest).EOBRUN = (src).EOBRUN, \ 50 ((dest).EOBRUN = (src).EOBRUN, \
48 » (dest).last_dc_val[0] = (src).last_dc_val[0], \ 51 (dest).last_dc_val[0] = (src).last_dc_val[0], \
49 » (dest).last_dc_val[1] = (src).last_dc_val[1], \ 52 (dest).last_dc_val[1] = (src).last_dc_val[1], \
50 » (dest).last_dc_val[2] = (src).last_dc_val[2], \ 53 (dest).last_dc_val[2] = (src).last_dc_val[2], \
51 » (dest).last_dc_val[3] = (src).last_dc_val[3]) 54 (dest).last_dc_val[3] = (src).last_dc_val[3])
52 #endif 55 #endif
53 #endif 56 #endif
54 57
55 58
56 typedef struct { 59 typedef struct {
57 struct jpeg_entropy_decoder pub; /* public fields */ 60 struct jpeg_entropy_decoder pub; /* public fields */
58 61
59 /* These fields are loaded into local variables at start of each MCU. 62 /* These fields are loaded into local variables at start of each MCU.
60 * In case of suspension, we exit WITHOUT updating them. 63 * In case of suspension, we exit WITHOUT updating them.
61 */ 64 */
62 bitread_perm_state bitstate;» /* Bit buffer at start of MCU */ 65 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
63 savable_state saved;» » /* Other state at start of MCU */ 66 savable_state saved; /* Other state at start of MCU */
64 67
65 /* These fields are NOT loaded into local working state. */ 68 /* These fields are NOT loaded into local working state. */
66 unsigned int restarts_to_go;» /* MCUs left in this restart interval */ 69 unsigned int restarts_to_go; /* MCUs left in this restart interval */
67 70
68 /* Pointers to derived tables (these workspaces have image lifespan) */ 71 /* Pointers to derived tables (these workspaces have image lifespan) */
69 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; 72 d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
70 73
71 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ 74 d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
72 } phuff_entropy_decoder; 75 } phuff_entropy_decoder;
73 76
74 typedef phuff_entropy_decoder * phuff_entropy_ptr; 77 typedef phuff_entropy_decoder *phuff_entropy_ptr;
75 78
76 /* Forward declarations */ 79 /* Forward declarations */
77 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, 80 METHODDEF(boolean) decode_mcu_DC_first (j_decompress_ptr cinfo,
78 » » » » » JBLOCKROW *MCU_data)); 81 JBLOCKROW *MCU_data);
79 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, 82 METHODDEF(boolean) decode_mcu_AC_first (j_decompress_ptr cinfo,
80 » » » » » JBLOCKROW *MCU_data)); 83 JBLOCKROW *MCU_data);
81 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, 84 METHODDEF(boolean) decode_mcu_DC_refine (j_decompress_ptr cinfo,
82 » » » » » JBLOCKROW *MCU_data)); 85 JBLOCKROW *MCU_data);
83 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, 86 METHODDEF(boolean) decode_mcu_AC_refine (j_decompress_ptr cinfo,
84 » » » » » JBLOCKROW *MCU_data)); 87 JBLOCKROW *MCU_data);
85 88
86 89
87 /* 90 /*
88 * Initialize for a Huffman-compressed scan. 91 * Initialize for a Huffman-compressed scan.
89 */ 92 */
90 93
91 METHODDEF(void) 94 METHODDEF(void)
92 start_pass_phuff_decoder (j_decompress_ptr cinfo) 95 start_pass_phuff_decoder (j_decompress_ptr cinfo)
93 { 96 {
94 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 97 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
95 boolean is_DC_band, bad; 98 boolean is_DC_band, bad;
96 int ci, coefi, tbl; 99 int ci, coefi, tbl;
100 d_derived_tbl **pdtbl;
97 int *coef_bit_ptr; 101 int *coef_bit_ptr;
98 jpeg_component_info * compptr; 102 jpeg_component_info *compptr;
99 103
100 is_DC_band = (cinfo->Ss == 0); 104 is_DC_band = (cinfo->Ss == 0);
101 105
102 /* Validate scan parameters */ 106 /* Validate scan parameters */
103 bad = FALSE; 107 bad = FALSE;
104 if (is_DC_band) { 108 if (is_DC_band) {
105 if (cinfo->Se != 0) 109 if (cinfo->Se != 0)
106 bad = TRUE; 110 bad = TRUE;
107 } else { 111 } else {
108 /* need not check Ss/Se < 0 since they came from unsigned bytes */ 112 /* need not check Ss/Se < 0 since they came from unsigned bytes */
109 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) 113 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
110 bad = TRUE; 114 bad = TRUE;
111 /* AC scans may have only one component */ 115 /* AC scans may have only one component */
112 if (cinfo->comps_in_scan != 1) 116 if (cinfo->comps_in_scan != 1)
113 bad = TRUE; 117 bad = TRUE;
114 } 118 }
115 if (cinfo->Ah != 0) { 119 if (cinfo->Ah != 0) {
116 /* Successive approximation refinement scan: must have Al = Ah-1. */ 120 /* Successive approximation refinement scan: must have Al = Ah-1. */
117 if (cinfo->Al != cinfo->Ah-1) 121 if (cinfo->Al != cinfo->Ah-1)
118 bad = TRUE; 122 bad = TRUE;
119 } 123 }
120 if (cinfo->Al > 13)» » /* need not check for < 0 */ 124 if (cinfo->Al > 13) /* need not check for < 0 */
121 bad = TRUE; 125 bad = TRUE;
122 /* Arguably the maximum Al value should be less than 13 for 8-bit precision, 126 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
123 * but the spec doesn't say so, and we try to be liberal about what we 127 * but the spec doesn't say so, and we try to be liberal about what we
124 * accept. Note: large Al values could result in out-of-range DC 128 * accept. Note: large Al values could result in out-of-range DC
125 * coefficients during early scans, leading to bizarre displays due to 129 * coefficients during early scans, leading to bizarre displays due to
126 * overflows in the IDCT math. But we won't crash. 130 * overflows in the IDCT math. But we won't crash.
127 */ 131 */
128 if (bad) 132 if (bad)
129 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 133 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
130 » cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); 134 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
131 /* Update progression status, and verify that scan order is legal. 135 /* Update progression status, and verify that scan order is legal.
132 * Note that inter-scan inconsistencies are treated as warnings 136 * Note that inter-scan inconsistencies are treated as warnings
133 * not fatal errors ... not clear if this is right way to behave. 137 * not fatal errors ... not clear if this is right way to behave.
134 */ 138 */
135 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 139 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
136 int cindex = cinfo->cur_comp_info[ci]->component_index; 140 int cindex = cinfo->cur_comp_info[ci]->component_index;
137 coef_bit_ptr = & cinfo->coef_bits[cindex][0]; 141 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
138 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ 142 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
139 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 143 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
140 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { 144 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
141 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; 145 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
142 if (cinfo->Ah != expected) 146 if (cinfo->Ah != expected)
143 » WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); 147 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
144 coef_bit_ptr[coefi] = cinfo->Al; 148 coef_bit_ptr[coefi] = cinfo->Al;
145 } 149 }
146 } 150 }
147 151
148 /* Select MCU decoding routine */ 152 /* Select MCU decoding routine */
149 if (cinfo->Ah == 0) { 153 if (cinfo->Ah == 0) {
150 if (is_DC_band) 154 if (is_DC_band)
151 entropy->pub.decode_mcu = decode_mcu_DC_first; 155 entropy->pub.decode_mcu = decode_mcu_DC_first;
152 else 156 else
153 entropy->pub.decode_mcu = decode_mcu_AC_first; 157 entropy->pub.decode_mcu = decode_mcu_AC_first;
154 } else { 158 } else {
155 if (is_DC_band) 159 if (is_DC_band)
156 entropy->pub.decode_mcu = decode_mcu_DC_refine; 160 entropy->pub.decode_mcu = decode_mcu_DC_refine;
157 else 161 else
158 entropy->pub.decode_mcu = decode_mcu_AC_refine; 162 entropy->pub.decode_mcu = decode_mcu_AC_refine;
159 } 163 }
160 164
161 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 165 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
162 compptr = cinfo->cur_comp_info[ci]; 166 compptr = cinfo->cur_comp_info[ci];
163 /* Make sure requested tables are present, and compute derived tables. 167 /* Make sure requested tables are present, and compute derived tables.
164 * We may build same derived table more than once, but it's not expensive. 168 * We may build same derived table more than once, but it's not expensive.
165 */ 169 */
166 if (is_DC_band) { 170 if (is_DC_band) {
167 if (cinfo->Ah == 0) {» /* DC refinement needs no table */ 171 if (cinfo->Ah == 0) { /* DC refinement needs no table */
168 » tbl = compptr->dc_tbl_no; 172 tbl = compptr->dc_tbl_no;
169 » jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, 173 pdtbl = entropy->derived_tbls + tbl;
170 » » » » & entropy->derived_tbls[tbl]); 174 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
171 } 175 }
172 } else { 176 } else {
173 tbl = compptr->ac_tbl_no; 177 tbl = compptr->ac_tbl_no;
174 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, 178 pdtbl = entropy->derived_tbls + tbl;
175 » » » & entropy->derived_tbls[tbl]); 179 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
176 /* remember the single active table */ 180 /* remember the single active table */
177 entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; 181 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
178 } 182 }
179 /* Initialize DC predictions to 0 */ 183 /* Initialize DC predictions to 0 */
180 entropy->saved.last_dc_val[ci] = 0; 184 entropy->saved.last_dc_val[ci] = 0;
181 } 185 }
182 186
183 /* Initialize bitread state variables */ 187 /* Initialize bitread state variables */
184 entropy->bitstate.bits_left = 0; 188 entropy->bitstate.bits_left = 0;
185 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ 189 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
186 entropy->pub.insufficient_data = FALSE; 190 entropy->pub.insufficient_data = FALSE;
187 191
188 /* Initialize private state variables */ 192 /* Initialize private state variables */
189 entropy->saved.EOBRUN = 0; 193 entropy->saved.EOBRUN = 0;
190 194
191 /* Initialize restart counter */ 195 /* Initialize restart counter */
192 entropy->restarts_to_go = cinfo->restart_interval; 196 entropy->restarts_to_go = cinfo->restart_interval;
193 } 197 }
194 198
195 199
196 /* 200 /*
197 * Figure F.12: extend sign bit. 201 * Figure F.12: extend sign bit.
198 * On some machines, a shift and add will be faster than a table lookup. 202 * On some machines, a shift and add will be faster than a table lookup.
199 */ 203 */
200 204
201 #define AVOID_TABLES 205 #define AVOID_TABLES
202 #ifdef AVOID_TABLES 206 #ifdef AVOID_TABLES
203 207
204 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) 208 #define NEG_1 ((unsigned)-1)
209 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((NEG_1)<<(s)) + 1) : (x) )
205 210
206 #else 211 #else
207 212
208 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) 213 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
209 214
210 static const int extend_test[16] = /* entry n is 2**(n-1) */ 215 static const int extend_test[16] = /* entry n is 2**(n-1) */
211 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 216 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
212 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; 217 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
213 218
214 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ 219 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 if (cinfo->unread_marker == 0) 262 if (cinfo->unread_marker == 0)
258 entropy->pub.insufficient_data = FALSE; 263 entropy->pub.insufficient_data = FALSE;
259 264
260 return TRUE; 265 return TRUE;
261 } 266 }
262 267
263 268
264 /* 269 /*
265 * Huffman MCU decoding. 270 * Huffman MCU decoding.
266 * Each of these routines decodes and returns one MCU's worth of 271 * Each of these routines decodes and returns one MCU's worth of
267 * Huffman-compressed coefficients. 272 * Huffman-compressed coefficients.
268 * The coefficients are reordered from zigzag order into natural array order, 273 * The coefficients are reordered from zigzag order into natural array order,
269 * but are not dequantized. 274 * but are not dequantized.
270 * 275 *
271 * The i'th block of the MCU is stored into the block pointed to by 276 * The i'th block of the MCU is stored into the block pointed to by
272 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. 277 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
273 * 278 *
274 * We return FALSE if data source requested suspension. In that case no 279 * We return FALSE if data source requested suspension. In that case no
275 * changes have been made to permanent state. (Exception: some output 280 * changes have been made to permanent state. (Exception: some output
276 * coefficients may already have been assigned. This is harmless for 281 * coefficients may already have been assigned. This is harmless for
277 * spectral selection, since we'll just re-assign them on the next call. 282 * spectral selection, since we'll just re-assign them on the next call.
278 * Successive approximation AC refinement has to be more careful, however.) 283 * Successive approximation AC refinement has to be more careful, however.)
279 */ 284 */
280 285
281 /* 286 /*
282 * MCU decoding for DC initial scan (either spectral selection, 287 * MCU decoding for DC initial scan (either spectral selection,
283 * or first pass of successive approximation). 288 * or first pass of successive approximation).
284 */ 289 */
285 290
286 METHODDEF(boolean) 291 METHODDEF(boolean)
287 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 292 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
288 { 293 {
289 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 294 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
290 int Al = cinfo->Al; 295 int Al = cinfo->Al;
291 register int s, r; 296 register int s, r;
292 int blkn, ci; 297 int blkn, ci;
293 JBLOCKROW block; 298 JBLOCKROW block;
294 BITREAD_STATE_VARS; 299 BITREAD_STATE_VARS;
295 savable_state state; 300 savable_state state;
296 d_derived_tbl * tbl; 301 d_derived_tbl *tbl;
297 jpeg_component_info * compptr; 302 jpeg_component_info *compptr;
298 303
299 /* Process restart marker if needed; may have to suspend */ 304 /* Process restart marker if needed; may have to suspend */
300 if (cinfo->restart_interval) { 305 if (cinfo->restart_interval) {
301 if (entropy->restarts_to_go == 0) 306 if (entropy->restarts_to_go == 0)
302 if (! process_restart(cinfo)) 307 if (! process_restart(cinfo))
303 » return FALSE; 308 return FALSE;
304 } 309 }
305 310
306 /* If we've run out of data, just leave the MCU set to zeroes. 311 /* If we've run out of data, just leave the MCU set to zeroes.
307 * This way, we return uniform gray for the remainder of the segment. 312 * This way, we return uniform gray for the remainder of the segment.
308 */ 313 */
309 if (! entropy->pub.insufficient_data) { 314 if (! entropy->pub.insufficient_data) {
310 315
311 /* Load up working state */ 316 /* Load up working state */
312 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 317 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
313 ASSIGN_STATE(state, entropy->saved); 318 ASSIGN_STATE(state, entropy->saved);
314 319
315 /* Outer loop handles each block in the MCU */ 320 /* Outer loop handles each block in the MCU */
316 321
317 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 322 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
318 block = MCU_data[blkn]; 323 block = MCU_data[blkn];
319 ci = cinfo->MCU_membership[blkn]; 324 ci = cinfo->MCU_membership[blkn];
320 compptr = cinfo->cur_comp_info[ci]; 325 compptr = cinfo->cur_comp_info[ci];
321 tbl = entropy->derived_tbls[compptr->dc_tbl_no]; 326 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
322 327
323 /* Decode a single block's worth of coefficients */ 328 /* Decode a single block's worth of coefficients */
324 329
325 /* Section F.2.2.1: decode the DC coefficient difference */ 330 /* Section F.2.2.1: decode the DC coefficient difference */
326 HUFF_DECODE(s, br_state, tbl, return FALSE, label1); 331 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
327 if (s) { 332 if (s) {
328 » CHECK_BIT_BUFFER(br_state, s, return FALSE); 333 CHECK_BIT_BUFFER(br_state, s, return FALSE);
329 » r = GET_BITS(s); 334 r = GET_BITS(s);
330 » s = HUFF_EXTEND(r, s); 335 s = HUFF_EXTEND(r, s);
331 } 336 }
332 337
333 /* Convert DC difference to actual value, update last_dc_val */ 338 /* Convert DC difference to actual value, update last_dc_val */
334 s += state.last_dc_val[ci]; 339 s += state.last_dc_val[ci];
335 state.last_dc_val[ci] = s; 340 state.last_dc_val[ci] = s;
336 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ 341 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
337 (*block)[0] = (JCOEF) (s << Al); 342 (*block)[0] = (JCOEF) LEFT_SHIFT(s, Al);
338 } 343 }
339 344
340 /* Completed MCU, so update state */ 345 /* Completed MCU, so update state */
341 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 346 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
342 ASSIGN_STATE(entropy->saved, state); 347 ASSIGN_STATE(entropy->saved, state);
343 } 348 }
344 349
345 /* Account for restart interval (no-op if not using restarts) */ 350 /* Account for restart interval (no-op if not using restarts) */
346 entropy->restarts_to_go--; 351 entropy->restarts_to_go--;
347 352
348 return TRUE; 353 return TRUE;
349 } 354 }
350 355
351 356
352 /* 357 /*
353 * MCU decoding for AC initial scan (either spectral selection, 358 * MCU decoding for AC initial scan (either spectral selection,
354 * or first pass of successive approximation). 359 * or first pass of successive approximation).
355 */ 360 */
356 361
357 METHODDEF(boolean) 362 METHODDEF(boolean)
358 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 363 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
359 { 364 {
360 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 365 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
361 int Se = cinfo->Se; 366 int Se = cinfo->Se;
362 int Al = cinfo->Al; 367 int Al = cinfo->Al;
363 register int s, k, r; 368 register int s, k, r;
364 unsigned int EOBRUN; 369 unsigned int EOBRUN;
365 JBLOCKROW block; 370 JBLOCKROW block;
366 BITREAD_STATE_VARS; 371 BITREAD_STATE_VARS;
367 d_derived_tbl * tbl; 372 d_derived_tbl *tbl;
368 373
369 /* Process restart marker if needed; may have to suspend */ 374 /* Process restart marker if needed; may have to suspend */
370 if (cinfo->restart_interval) { 375 if (cinfo->restart_interval) {
371 if (entropy->restarts_to_go == 0) 376 if (entropy->restarts_to_go == 0)
372 if (! process_restart(cinfo)) 377 if (! process_restart(cinfo))
373 » return FALSE; 378 return FALSE;
374 } 379 }
375 380
376 /* If we've run out of data, just leave the MCU set to zeroes. 381 /* If we've run out of data, just leave the MCU set to zeroes.
377 * This way, we return uniform gray for the remainder of the segment. 382 * This way, we return uniform gray for the remainder of the segment.
378 */ 383 */
379 if (! entropy->pub.insufficient_data) { 384 if (! entropy->pub.insufficient_data) {
380 385
381 /* Load up working state. 386 /* Load up working state.
382 * We can avoid loading/saving bitread state if in an EOB run. 387 * We can avoid loading/saving bitread state if in an EOB run.
383 */ 388 */
384 EOBRUN = entropy->saved.EOBRUN;» /* only part of saved state we need */ 389 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
385 390
386 /* There is always only one block per MCU */ 391 /* There is always only one block per MCU */
387 392
388 if (EOBRUN > 0)» » /* if it's a band of zeroes... */ 393 if (EOBRUN > 0) /* if it's a band of zeroes... */
389 EOBRUN--;»» » /* ...process it now (we do nothing) */ 394 EOBRUN--; /* ...process it now (we do nothing) */
390 else { 395 else {
391 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 396 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
392 block = MCU_data[0]; 397 block = MCU_data[0];
393 tbl = entropy->ac_derived_tbl; 398 tbl = entropy->ac_derived_tbl;
394 399
395 for (k = cinfo->Ss; k <= Se; k++) { 400 for (k = cinfo->Ss; k <= Se; k++) {
396 » HUFF_DECODE(s, br_state, tbl, return FALSE, label2); 401 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
397 » r = s >> 4; 402 r = s >> 4;
398 » s &= 15; 403 s &= 15;
399 » if (s) { 404 if (s) {
400 » k += r; 405 k += r;
401 » CHECK_BIT_BUFFER(br_state, s, return FALSE); 406 CHECK_BIT_BUFFER(br_state, s, return FALSE);
402 » r = GET_BITS(s); 407 r = GET_BITS(s);
403 » s = HUFF_EXTEND(r, s); 408 s = HUFF_EXTEND(r, s);
404 » /* Scale and output coefficient in natural (dezigzagged) order */ 409 /* Scale and output coefficient in natural (dezigzagged) order */
405 » (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); 410 (*block)[jpeg_natural_order[k]] = (JCOEF) LEFT_SHIFT(s, Al);
406 » } else { 411 } else {
407 » if (r == 15) {» /* ZRL */ 412 if (r == 15) { /* ZRL */
408 » k += 15;» » /* skip 15 zeroes in band */ 413 k += 15; /* skip 15 zeroes in band */
409 » } else {» » /* EOBr, run length is 2^r + appended bits */ 414 } else { /* EOBr, run length is 2^r + appended bits */
410 » EOBRUN = 1 << r; 415 EOBRUN = 1 << r;
411 » if (r) {» » /* EOBr, r > 0 */ 416 if (r) { /* EOBr, r > 0 */
412 » CHECK_BIT_BUFFER(br_state, r, return FALSE); 417 CHECK_BIT_BUFFER(br_state, r, return FALSE);
413 » r = GET_BITS(r); 418 r = GET_BITS(r);
414 » EOBRUN += r; 419 EOBRUN += r;
415 » } 420 }
416 » EOBRUN--;» » /* this band is processed at this moment */ 421 EOBRUN--; /* this band is processed at this moment */
417 » break;» » /* force end-of-band */ 422 break; /* force end-of-band */
418 » } 423 }
419 » } 424 }
420 } 425 }
421 426
422 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 427 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
423 } 428 }
424 429
425 /* Completed MCU, so update state */ 430 /* Completed MCU, so update state */
426 entropy->saved.EOBRUN = EOBRUN;» /* only part of saved state we need */ 431 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
427 } 432 }
428 433
429 /* Account for restart interval (no-op if not using restarts) */ 434 /* Account for restart interval (no-op if not using restarts) */
430 entropy->restarts_to_go--; 435 entropy->restarts_to_go--;
431 436
432 return TRUE; 437 return TRUE;
433 } 438 }
434 439
435 440
436 /* 441 /*
437 * MCU decoding for DC successive approximation refinement scan. 442 * MCU decoding for DC successive approximation refinement scan.
438 * Note: we assume such scans can be multi-component, although the spec 443 * Note: we assume such scans can be multi-component, although the spec
439 * is not very clear on the point. 444 * is not very clear on the point.
440 */ 445 */
441 446
442 METHODDEF(boolean) 447 METHODDEF(boolean)
443 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 448 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
444 { 449 {
445 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 450 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
446 int p1 = 1 << cinfo->Al;» /* 1 in the bit position being coded */ 451 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
447 int blkn; 452 int blkn;
448 JBLOCKROW block; 453 JBLOCKROW block;
449 BITREAD_STATE_VARS; 454 BITREAD_STATE_VARS;
450 455
451 /* Process restart marker if needed; may have to suspend */ 456 /* Process restart marker if needed; may have to suspend */
452 if (cinfo->restart_interval) { 457 if (cinfo->restart_interval) {
453 if (entropy->restarts_to_go == 0) 458 if (entropy->restarts_to_go == 0)
454 if (! process_restart(cinfo)) 459 if (! process_restart(cinfo))
455 » return FALSE; 460 return FALSE;
456 } 461 }
457 462
458 /* Not worth the cycles to check insufficient_data here, 463 /* Not worth the cycles to check insufficient_data here,
459 * since we will not change the data anyway if we read zeroes. 464 * since we will not change the data anyway if we read zeroes.
460 */ 465 */
461 466
462 /* Load up working state */ 467 /* Load up working state */
463 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 468 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
464 469
465 /* Outer loop handles each block in the MCU */ 470 /* Outer loop handles each block in the MCU */
(...skipping 17 matching lines...) Expand all
483 return TRUE; 488 return TRUE;
484 } 489 }
485 490
486 491
487 /* 492 /*
488 * MCU decoding for AC successive approximation refinement scan. 493 * MCU decoding for AC successive approximation refinement scan.
489 */ 494 */
490 495
491 METHODDEF(boolean) 496 METHODDEF(boolean)
492 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 497 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
493 { 498 {
494 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 499 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
495 int Se = cinfo->Se; 500 int Se = cinfo->Se;
496 int p1 = 1 << cinfo->Al;» /* 1 in the bit position being coded */ 501 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
497 int m1 = (-1) << cinfo->Al;» /* -1 in the bit position being coded */ 502 int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
498 register int s, k, r; 503 register int s, k, r;
499 unsigned int EOBRUN; 504 unsigned int EOBRUN;
500 JBLOCKROW block; 505 JBLOCKROW block;
501 JCOEFPTR thiscoef; 506 JCOEFPTR thiscoef;
502 BITREAD_STATE_VARS; 507 BITREAD_STATE_VARS;
503 d_derived_tbl * tbl; 508 d_derived_tbl *tbl;
504 int num_newnz; 509 int num_newnz;
505 int newnz_pos[DCTSIZE2]; 510 int newnz_pos[DCTSIZE2];
506 511
507 /* Process restart marker if needed; may have to suspend */ 512 /* Process restart marker if needed; may have to suspend */
508 if (cinfo->restart_interval) { 513 if (cinfo->restart_interval) {
509 if (entropy->restarts_to_go == 0) 514 if (entropy->restarts_to_go == 0)
510 if (! process_restart(cinfo)) 515 if (! process_restart(cinfo))
511 » return FALSE; 516 return FALSE;
512 } 517 }
513 518
514 /* If we've run out of data, don't modify the MCU. 519 /* If we've run out of data, don't modify the MCU.
515 */ 520 */
516 if (! entropy->pub.insufficient_data) { 521 if (! entropy->pub.insufficient_data) {
517 522
518 /* Load up working state */ 523 /* Load up working state */
519 BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 524 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
520 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ 525 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
521 526
522 /* There is always only one block per MCU */ 527 /* There is always only one block per MCU */
523 block = MCU_data[0]; 528 block = MCU_data[0];
524 tbl = entropy->ac_derived_tbl; 529 tbl = entropy->ac_derived_tbl;
525 530
526 /* If we are forced to suspend, we must undo the assignments to any newly 531 /* If we are forced to suspend, we must undo the assignments to any newly
527 * nonzero coefficients in the block, because otherwise we'd get confused 532 * nonzero coefficients in the block, because otherwise we'd get confused
528 * next time about which coefficients were already nonzero. 533 * next time about which coefficients were already nonzero.
529 * But we need not undo addition of bits to already-nonzero coefficients; 534 * But we need not undo addition of bits to already-nonzero coefficients;
530 * instead, we can test the current bit to see if we already did it. 535 * instead, we can test the current bit to see if we already did it.
531 */ 536 */
532 num_newnz = 0; 537 num_newnz = 0;
533 538
534 /* initialize coefficient loop counter to start of band */ 539 /* initialize coefficient loop counter to start of band */
535 k = cinfo->Ss; 540 k = cinfo->Ss;
536 541
537 if (EOBRUN == 0) { 542 if (EOBRUN == 0) {
538 for (; k <= Se; k++) { 543 for (; k <= Se; k++) {
539 » HUFF_DECODE(s, br_state, tbl, goto undoit, label3); 544 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
540 » r = s >> 4; 545 r = s >> 4;
541 » s &= 15; 546 s &= 15;
542 » if (s) { 547 if (s) {
543 » if (s != 1)» » /* size of new coef should always be 1 */ 548 if (s != 1) /* size of new coef should always be 1 */
544 » WARNMS(cinfo, JWRN_HUFF_BAD_CODE); 549 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
545 » CHECK_BIT_BUFFER(br_state, 1, goto undoit); 550 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
546 » if (GET_BITS(1)) 551 if (GET_BITS(1))
547 » s = p1;» » /* newly nonzero coef is positive */ 552 s = p1; /* newly nonzero coef is positive */
548 » else 553 else
549 » s = m1;» » /* newly nonzero coef is negative */ 554 s = m1; /* newly nonzero coef is negative */
550 » } else { 555 } else {
551 » if (r != 15) { 556 if (r != 15) {
552 » EOBRUN = 1 << r;» /* EOBr, run length is 2^r + appended bits */ 557 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
553 » if (r) { 558 if (r) {
554 » CHECK_BIT_BUFFER(br_state, r, goto undoit); 559 CHECK_BIT_BUFFER(br_state, r, goto undoit);
555 » r = GET_BITS(r); 560 r = GET_BITS(r);
556 » EOBRUN += r; 561 EOBRUN += r;
557 » } 562 }
558 » break;» » /* rest of block is handled by EOB logic */ 563 break; /* rest of block is handled by EOB logic */
559 » } 564 }
560 » /* note s = 0 for processing ZRL */ 565 /* note s = 0 for processing ZRL */
561 » } 566 }
562 » /* Advance over already-nonzero coefs and r still-zero coefs, 567 /* Advance over already-nonzero coefs and r still-zero coefs,
563 » * appending correction bits to the nonzeroes. A correction bit is 1 568 * appending correction bits to the nonzeroes. A correction bit is 1
564 » * if the absolute value of the coefficient must be increased. 569 * if the absolute value of the coefficient must be increased.
565 » */ 570 */
566 » do { 571 do {
567 » thiscoef = *block + jpeg_natural_order[k]; 572 thiscoef = *block + jpeg_natural_order[k];
568 » if (*thiscoef != 0) { 573 if (*thiscoef != 0) {
569 » CHECK_BIT_BUFFER(br_state, 1, goto undoit); 574 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
570 » if (GET_BITS(1)) { 575 if (GET_BITS(1)) {
571 » if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ 576 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
572 » » if (*thiscoef >= 0) 577 if (*thiscoef >= 0)
573 » » *thiscoef += p1; 578 *thiscoef += p1;
574 » » else 579 else
575 » » *thiscoef += m1; 580 *thiscoef += m1;
576 » } 581 }
577 » } 582 }
578 » } else { 583 } else {
579 » if (--r < 0) 584 if (--r < 0)
580 » break;» » /* reached target zero coefficient */ 585 break; /* reached target zero coefficient */
581 » } 586 }
582 » k++; 587 k++;
583 » } while (k <= Se); 588 } while (k <= Se);
584 » if (s) { 589 if (s) {
585 » int pos = jpeg_natural_order[k]; 590 int pos = jpeg_natural_order[k];
586 » /* Output newly nonzero coefficient */ 591 /* Output newly nonzero coefficient */
587 » (*block)[pos] = (JCOEF) s; 592 (*block)[pos] = (JCOEF) s;
588 » /* Remember its position in case we have to suspend */ 593 /* Remember its position in case we have to suspend */
589 » newnz_pos[num_newnz++] = pos; 594 newnz_pos[num_newnz++] = pos;
590 » } 595 }
591 } 596 }
592 } 597 }
593 598
594 if (EOBRUN > 0) { 599 if (EOBRUN > 0) {
595 /* Scan any remaining coefficient positions after the end-of-band 600 /* Scan any remaining coefficient positions after the end-of-band
596 * (the last newly nonzero coefficient, if any). Append a correction 601 * (the last newly nonzero coefficient, if any). Append a correction
597 * bit to each already-nonzero coefficient. A correction bit is 1 602 * bit to each already-nonzero coefficient. A correction bit is 1
598 * if the absolute value of the coefficient must be increased. 603 * if the absolute value of the coefficient must be increased.
599 */ 604 */
600 for (; k <= Se; k++) { 605 for (; k <= Se; k++) {
601 » thiscoef = *block + jpeg_natural_order[k]; 606 thiscoef = *block + jpeg_natural_order[k];
602 » if (*thiscoef != 0) { 607 if (*thiscoef != 0) {
603 » CHECK_BIT_BUFFER(br_state, 1, goto undoit); 608 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
604 » if (GET_BITS(1)) { 609 if (GET_BITS(1)) {
605 » if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ 610 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
606 » if (*thiscoef >= 0) 611 if (*thiscoef >= 0)
607 » » *thiscoef += p1; 612 *thiscoef += p1;
608 » else 613 else
609 » » *thiscoef += m1; 614 *thiscoef += m1;
610 » } 615 }
611 » } 616 }
612 » } 617 }
613 } 618 }
614 /* Count one block completed in EOB run */ 619 /* Count one block completed in EOB run */
615 EOBRUN--; 620 EOBRUN--;
616 } 621 }
617 622
618 /* Completed MCU, so update state */ 623 /* Completed MCU, so update state */
619 BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 624 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
620 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ 625 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
621 } 626 }
622 627
(...skipping 17 matching lines...) Expand all
640 645
641 GLOBAL(void) 646 GLOBAL(void)
642 jinit_phuff_decoder (j_decompress_ptr cinfo) 647 jinit_phuff_decoder (j_decompress_ptr cinfo)
643 { 648 {
644 phuff_entropy_ptr entropy; 649 phuff_entropy_ptr entropy;
645 int *coef_bit_ptr; 650 int *coef_bit_ptr;
646 int ci, i; 651 int ci, i;
647 652
648 entropy = (phuff_entropy_ptr) 653 entropy = (phuff_entropy_ptr)
649 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 654 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
650 » » » » SIZEOF(phuff_entropy_decoder)); 655 sizeof(phuff_entropy_decoder));
651 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 656 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
652 entropy->pub.start_pass = start_pass_phuff_decoder; 657 entropy->pub.start_pass = start_pass_phuff_decoder;
653 658
654 /* Mark derived tables unallocated */ 659 /* Mark derived tables unallocated */
655 for (i = 0; i < NUM_HUFF_TBLS; i++) { 660 for (i = 0; i < NUM_HUFF_TBLS; i++) {
656 entropy->derived_tbls[i] = NULL; 661 entropy->derived_tbls[i] = NULL;
657 } 662 }
658 663
659 /* Create progression status table */ 664 /* Create progression status table */
660 cinfo->coef_bits = (int (*)[DCTSIZE2]) 665 cinfo->coef_bits = (int (*)[DCTSIZE2])
661 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 666 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
662 » » » » cinfo->num_components*DCTSIZE2*SIZEOF(int)); 667 cinfo->num_components*DCTSIZE2*sizeof(int));
663 coef_bit_ptr = & cinfo->coef_bits[0][0]; 668 coef_bit_ptr = & cinfo->coef_bits[0][0];
664 for (ci = 0; ci < cinfo->num_components; ci++) 669 for (ci = 0; ci < cinfo->num_components; ci++)
665 for (i = 0; i < DCTSIZE2; i++) 670 for (i = 0; i < DCTSIZE2; i++)
666 *coef_bit_ptr++ = -1; 671 *coef_bit_ptr++ = -1;
667 } 672 }
668 673
669 #endif /* D_PROGRESSIVE_SUPPORTED */ 674 #endif /* D_PROGRESSIVE_SUPPORTED */
OLDNEW
« no previous file with comments | « jdmrgext.c ('k') | jdpostct.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698