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