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