| OLD | NEW |
| 1 /* | 1 /* |
| 2 * jdarith.c | 2 * jdarith.c |
| 3 * | 3 * |
| 4 * This file is part of the Independent JPEG Group's software: | 4 * This file was part of the Independent JPEG Group's software: |
| 5 * Developed 1997-2009 by Guido Vollbeding. | 5 * Developed 1997-2015 by Guido Vollbeding. |
| 6 * libjpeg-turbo Modifications: | 6 * libjpeg-turbo Modifications: |
| 7 * Copyright (C) 2015, D. R. Commander. | 7 * Copyright (C) 2015, D. R. Commander. |
| 8 * For conditions of distribution and use, see the accompanying README file. | 8 * For conditions of distribution and use, see the accompanying README.ijg |
| 9 * file. |
| 9 * | 10 * |
| 10 * This file contains portable arithmetic entropy decoding routines for JPEG | 11 * This file contains portable arithmetic entropy decoding routines for JPEG |
| 11 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). | 12 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). |
| 12 * | 13 * |
| 13 * Both sequential and progressive modes are supported in this single module. | 14 * Both sequential and progressive modes are supported in this single module. |
| 14 * | 15 * |
| 15 * Suspension is not currently supported in this module. | 16 * Suspension is not currently supported in this module. |
| 16 */ | 17 */ |
| 17 | 18 |
| 18 #define JPEG_INTERNALS | 19 #define JPEG_INTERNALS |
| 19 #include "jinclude.h" | 20 #include "jinclude.h" |
| 20 #include "jpeglib.h" | 21 #include "jpeglib.h" |
| 21 | 22 |
| 22 | 23 |
| 23 /* Expanded entropy decoder object for arithmetic decoding. */ | 24 /* Expanded entropy decoder object for arithmetic decoding. */ |
| 24 | 25 |
| 25 typedef struct { | 26 typedef struct { |
| 26 struct jpeg_entropy_decoder pub; /* public fields */ | 27 struct jpeg_entropy_decoder pub; /* public fields */ |
| 27 | 28 |
| 28 INT32 c; /* C register, base of coding interval + input bit buffer */ | 29 JLONG c; /* C register, base of coding interval + input bit buffer */ |
| 29 INT32 a; /* A register, normalized size of coding interval */ | 30 JLONG a; /* A register, normalized size of coding interval */ |
| 30 int ct; /* bit shift counter, # of bits left in bit buffer part of C */ | 31 int ct; /* bit shift counter, # of bits left in bit buffer part of C */ |
| 31 /* init: ct = -16 */ | 32 /* init: ct = -16 */ |
| 32 /* run: ct = 0..7 */ | 33 /* run: ct = 0..7 */ |
| 33 /* error: ct = -1 */ | 34 /* error: ct = -1 */ |
| 34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | 35 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
| 35 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ | 36 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ |
| 36 | 37 |
| 37 unsigned int restarts_to_go;» /* MCUs left in this restart interval */ | 38 unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
| 38 | 39 |
| 39 /* Pointers to statistics areas (these workspaces have image lifespan) */ | 40 /* Pointers to statistics areas (these workspaces have image lifespan) */ |
| 40 unsigned char * dc_stats[NUM_ARITH_TBLS]; | 41 unsigned char *dc_stats[NUM_ARITH_TBLS]; |
| 41 unsigned char * ac_stats[NUM_ARITH_TBLS]; | 42 unsigned char *ac_stats[NUM_ARITH_TBLS]; |
| 42 | 43 |
| 43 /* Statistics bin for coding with fixed probability 0.5 */ | 44 /* Statistics bin for coding with fixed probability 0.5 */ |
| 44 unsigned char fixed_bin[4]; | 45 unsigned char fixed_bin[4]; |
| 45 } arith_entropy_decoder; | 46 } arith_entropy_decoder; |
| 46 | 47 |
| 47 typedef arith_entropy_decoder * arith_entropy_ptr; | 48 typedef arith_entropy_decoder *arith_entropy_ptr; |
| 48 | 49 |
| 49 /* The following two definitions specify the allocation chunk size | 50 /* The following two definitions specify the allocation chunk size |
| 50 * for the statistics area. | 51 * for the statistics area. |
| 51 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least | 52 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least |
| 52 * 49 statistics bins for DC, and 245 statistics bins for AC coding. | 53 * 49 statistics bins for DC, and 245 statistics bins for AC coding. |
| 53 * | 54 * |
| 54 * We use a compact representation with 1 byte per statistics bin, | 55 * We use a compact representation with 1 byte per statistics bin, |
| 55 * thus the numbers directly represent byte sizes. | 56 * thus the numbers directly represent byte sizes. |
| 56 * This 1 byte per statistics bin contains the meaning of the MPS | 57 * This 1 byte per statistics bin contains the meaning of the MPS |
| 57 * (more probable symbol) in the highest bit (mask 0x80), and the | 58 * (more probable symbol) in the highest bit (mask 0x80), and the |
| 58 * index into the probability estimation state machine table | 59 * index into the probability estimation state machine table |
| 59 * in the lower bits (mask 0x7F). | 60 * in the lower bits (mask 0x7F). |
| 60 */ | 61 */ |
| 61 | 62 |
| 62 #define DC_STAT_BINS 64 | 63 #define DC_STAT_BINS 64 |
| 63 #define AC_STAT_BINS 256 | 64 #define AC_STAT_BINS 256 |
| 64 | 65 |
| 65 | 66 |
| 66 LOCAL(int) | 67 LOCAL(int) |
| 67 get_byte (j_decompress_ptr cinfo) | 68 get_byte (j_decompress_ptr cinfo) |
| 68 /* Read next input byte; we do not support suspension in this module. */ | 69 /* Read next input byte; we do not support suspension in this module. */ |
| 69 { | 70 { |
| 70 struct jpeg_source_mgr * src = cinfo->src; | 71 struct jpeg_source_mgr *src = cinfo->src; |
| 71 | 72 |
| 72 if (src->bytes_in_buffer == 0) | 73 if (src->bytes_in_buffer == 0) |
| 73 if (! (*src->fill_input_buffer) (cinfo)) | 74 if (! (*src->fill_input_buffer) (cinfo)) |
| 74 ERREXIT(cinfo, JERR_CANT_SUSPEND); | 75 ERREXIT(cinfo, JERR_CANT_SUSPEND); |
| 75 src->bytes_in_buffer--; | 76 src->bytes_in_buffer--; |
| 76 return GETJOCTET(*src->next_input_byte++); | 77 return GETJOCTET(*src->next_input_byte++); |
| 77 } | 78 } |
| 78 | 79 |
| 79 | 80 |
| 80 /* | 81 /* |
| 81 * The core arithmetic decoding routine (common in JPEG and JBIG). | 82 * The core arithmetic decoding routine (common in JPEG and JBIG). |
| 82 * This needs to go as fast as possible. | 83 * This needs to go as fast as possible. |
| 83 * Machine-dependent optimization facilities | 84 * Machine-dependent optimization facilities |
| 84 * are not utilized in this portable implementation. | 85 * are not utilized in this portable implementation. |
| 85 * However, this code should be fairly efficient and | 86 * However, this code should be fairly efficient and |
| 86 * may be a good base for further optimizations anyway. | 87 * may be a good base for further optimizations anyway. |
| 87 * | 88 * |
| 88 * Return value is 0 or 1 (binary decision). | 89 * Return value is 0 or 1 (binary decision). |
| 89 * | 90 * |
| 90 * Note: I've changed the handling of the code base & bit | 91 * Note: I've changed the handling of the code base & bit |
| 91 * buffer register C compared to other implementations | 92 * buffer register C compared to other implementations |
| 92 * based on the standards layout & procedures. | 93 * based on the standards layout & procedures. |
| 93 * While it also contains both the actual base of the | 94 * While it also contains both the actual base of the |
| 94 * coding interval (16 bits) and the next-bits buffer, | 95 * coding interval (16 bits) and the next-bits buffer, |
| 95 * the cut-point between these two parts is floating | 96 * the cut-point between these two parts is floating |
| 96 * (instead of fixed) with the bit shift counter CT. | 97 * (instead of fixed) with the bit shift counter CT. |
| 97 * Thus, we also need only one (variable instead of | 98 * Thus, we also need only one (variable instead of |
| 98 * fixed size) shift for the LPS/MPS decision, and | 99 * fixed size) shift for the LPS/MPS decision, and |
| 99 * we can get away with any renormalization update | 100 * we can do away with any renormalization update |
| 100 * of C (except for new data insertion, of course). | 101 * of C (except for new data insertion, of course). |
| 101 * | 102 * |
| 102 * I've also introduced a new scheme for accessing | 103 * I've also introduced a new scheme for accessing |
| 103 * the probability estimation state machine table, | 104 * the probability estimation state machine table, |
| 104 * derived from Markus Kuhn's JBIG implementation. | 105 * derived from Markus Kuhn's JBIG implementation. |
| 105 */ | 106 */ |
| 106 | 107 |
| 107 LOCAL(int) | 108 LOCAL(int) |
| 108 arith_decode (j_decompress_ptr cinfo, unsigned char *st) | 109 arith_decode (j_decompress_ptr cinfo, unsigned char *st) |
| 109 { | 110 { |
| 110 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; | 111 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; |
| 111 register unsigned char nl, nm; | 112 register unsigned char nl, nm; |
| 112 register INT32 qe, temp; | 113 register JLONG qe, temp; |
| 113 register int sv, data; | 114 register int sv, data; |
| 114 | 115 |
| 115 /* Renormalization & data input per section D.2.6 */ | 116 /* Renormalization & data input per section D.2.6 */ |
| 116 while (e->a < 0x8000L) { | 117 while (e->a < 0x8000L) { |
| 117 if (--e->ct < 0) { | 118 if (--e->ct < 0) { |
| 118 /* Need to fetch next data byte */ | 119 /* Need to fetch next data byte */ |
| 119 if (cinfo->unread_marker) | 120 if (cinfo->unread_marker) |
| 120 » data = 0;» » /* stuff zero data */ | 121 data = 0; /* stuff zero data */ |
| 121 else { | 122 else { |
| 122 » data = get_byte(cinfo);»/* read next input byte */ | 123 data = get_byte(cinfo); /* read next input byte */ |
| 123 » if (data == 0xFF) {» /* zero stuff or marker code */ | 124 if (data == 0xFF) { /* zero stuff or marker code */ |
| 124 » do data = get_byte(cinfo); | 125 do data = get_byte(cinfo); |
| 125 » while (data == 0xFF);»/* swallow extra 0xFF bytes */ | 126 while (data == 0xFF); /* swallow extra 0xFF bytes */ |
| 126 » if (data == 0) | 127 if (data == 0) |
| 127 » data = 0xFF;» /* discard stuffed zero byte */ | 128 data = 0xFF; /* discard stuffed zero byte */ |
| 128 » else { | 129 else { |
| 129 » /* Note: Different from the Huffman decoder, hitting | 130 /* Note: Different from the Huffman decoder, hitting |
| 130 » * a marker while processing the compressed data | 131 * a marker while processing the compressed data |
| 131 » * segment is legal in arithmetic coding. | 132 * segment is legal in arithmetic coding. |
| 132 » * The convention is to supply zero data | 133 * The convention is to supply zero data |
| 133 » * then until decoding is complete. | 134 * then until decoding is complete. |
| 134 » */ | 135 */ |
| 135 » cinfo->unread_marker = data; | 136 cinfo->unread_marker = data; |
| 136 » data = 0; | 137 data = 0; |
| 137 » } | 138 } |
| 138 » } | 139 } |
| 139 } | 140 } |
| 140 e->c = (e->c << 8) | data; /* insert data into C register */ | 141 e->c = (e->c << 8) | data; /* insert data into C register */ |
| 141 if ((e->ct += 8) < 0)» /* update bit shift counter */ | 142 if ((e->ct += 8) < 0) /* update bit shift counter */ |
| 142 » /* Need more initial bytes */ | 143 /* Need more initial bytes */ |
| 143 » if (++e->ct == 0) | 144 if (++e->ct == 0) |
| 144 » /* Got 2 initial bytes -> re-init A and exit loop */ | 145 /* Got 2 initial bytes -> re-init A and exit loop */ |
| 145 » e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ | 146 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ |
| 146 } | 147 } |
| 147 e->a <<= 1; | 148 e->a <<= 1; |
| 148 } | 149 } |
| 149 | 150 |
| 150 /* Fetch values from our compact representation of Table D.2: | 151 /* Fetch values from our compact representation of Table D.2: |
| 151 * Qe values and probability estimation state machine | 152 * Qe values and probability estimation state machine |
| 152 */ | 153 */ |
| 153 sv = *st; | 154 sv = *st; |
| 154 qe = jpeg_aritab[sv & 0x7F];» /* => Qe_Value */ | 155 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ |
| 155 nl = (unsigned char) qe & 0xFF; qe >>= 8;» /* Next_Index_LPS + Switch_MPS *
/ | 156 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ |
| 156 nm = (unsigned char) qe & 0xFF; qe >>= 8;» /* Next_Index_MPS */ | 157 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ |
| 157 | 158 |
| 158 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ | 159 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ |
| 159 temp = e->a - qe; | 160 temp = e->a - qe; |
| 160 e->a = temp; | 161 e->a = temp; |
| 161 temp <<= e->ct; | 162 temp <<= e->ct; |
| 162 if (e->c >= temp) { | 163 if (e->c >= temp) { |
| 163 e->c -= temp; | 164 e->c -= temp; |
| 164 /* Conditional LPS (less probable symbol) exchange */ | 165 /* Conditional LPS (less probable symbol) exchange */ |
| 165 if (e->a < qe) { | 166 if (e->a < qe) { |
| 166 e->a = qe; | 167 e->a = qe; |
| 167 *st = (sv & 0x80) ^ nm;» /* Estimate_after_MPS */ | 168 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
| 168 } else { | 169 } else { |
| 169 e->a = qe; | 170 e->a = qe; |
| 170 *st = (sv & 0x80) ^ nl;» /* Estimate_after_LPS */ | 171 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
| 171 sv ^= 0x80;» » /* Exchange LPS/MPS */ | 172 sv ^= 0x80; /* Exchange LPS/MPS */ |
| 172 } | 173 } |
| 173 } else if (e->a < 0x8000L) { | 174 } else if (e->a < 0x8000L) { |
| 174 /* Conditional MPS (more probable symbol) exchange */ | 175 /* Conditional MPS (more probable symbol) exchange */ |
| 175 if (e->a < qe) { | 176 if (e->a < qe) { |
| 176 *st = (sv & 0x80) ^ nl;» /* Estimate_after_LPS */ | 177 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
| 177 sv ^= 0x80;» » /* Exchange LPS/MPS */ | 178 sv ^= 0x80; /* Exchange LPS/MPS */ |
| 178 } else { | 179 } else { |
| 179 *st = (sv & 0x80) ^ nm;» /* Estimate_after_MPS */ | 180 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
| 180 } | 181 } |
| 181 } | 182 } |
| 182 | 183 |
| 183 return sv >> 7; | 184 return sv >> 7; |
| 184 } | 185 } |
| 185 | 186 |
| 186 | 187 |
| 187 /* | 188 /* |
| 188 * Check for a restart marker & resynchronize decoder. | 189 * Check for a restart marker & resynchronize decoder. |
| 189 */ | 190 */ |
| 190 | 191 |
| 191 LOCAL(void) | 192 LOCAL(void) |
| 192 process_restart (j_decompress_ptr cinfo) | 193 process_restart (j_decompress_ptr cinfo) |
| 193 { | 194 { |
| 194 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | 195 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
| 195 int ci; | 196 int ci; |
| 196 jpeg_component_info * compptr; | 197 jpeg_component_info *compptr; |
| 197 | 198 |
| 198 /* Advance past the RSTn marker */ | 199 /* Advance past the RSTn marker */ |
| 199 if (! (*cinfo->marker->read_restart_marker) (cinfo)) | 200 if (! (*cinfo->marker->read_restart_marker) (cinfo)) |
| 200 ERREXIT(cinfo, JERR_CANT_SUSPEND); | 201 ERREXIT(cinfo, JERR_CANT_SUSPEND); |
| 201 | 202 |
| 202 /* Re-initialize statistics areas */ | 203 /* Re-initialize statistics areas */ |
| 203 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 204 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 204 compptr = cinfo->cur_comp_info[ci]; | 205 compptr = cinfo->cur_comp_info[ci]; |
| 205 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | 206 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
| 206 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); | 207 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); |
| 207 /* Reset DC predictions to 0 */ | 208 /* Reset DC predictions to 0 */ |
| 208 entropy->last_dc_val[ci] = 0; | 209 entropy->last_dc_val[ci] = 0; |
| 209 entropy->dc_context[ci] = 0; | 210 entropy->dc_context[ci] = 0; |
| 210 } | 211 } |
| 211 if (! cinfo->progressive_mode || cinfo->Ss) { | 212 if (!cinfo->progressive_mode || cinfo->Ss) { |
| 212 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); | 213 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); |
| 213 } | 214 } |
| 214 } | 215 } |
| 215 | 216 |
| 216 /* Reset arithmetic decoding variables */ | 217 /* Reset arithmetic decoding variables */ |
| 217 entropy->c = 0; | 218 entropy->c = 0; |
| 218 entropy->a = 0; | 219 entropy->a = 0; |
| 219 entropy->ct = -16;» /* force reading 2 initial bytes to fill C */ | 220 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
| 220 | 221 |
| 221 /* Reset restart counter */ | 222 /* Reset restart counter */ |
| 222 entropy->restarts_to_go = cinfo->restart_interval; | 223 entropy->restarts_to_go = cinfo->restart_interval; |
| 223 } | 224 } |
| 224 | 225 |
| 225 | 226 |
| 226 /* | 227 /* |
| 227 * Arithmetic MCU decoding. | 228 * Arithmetic MCU decoding. |
| 228 * Each of these routines decodes and returns one MCU's worth of | 229 * Each of these routines decodes and returns one MCU's worth of |
| 229 * arithmetic-compressed coefficients. | 230 * arithmetic-compressed coefficients. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 248 int blkn, ci, tbl, sign; | 249 int blkn, ci, tbl, sign; |
| 249 int v, m; | 250 int v, m; |
| 250 | 251 |
| 251 /* Process restart marker if needed */ | 252 /* Process restart marker if needed */ |
| 252 if (cinfo->restart_interval) { | 253 if (cinfo->restart_interval) { |
| 253 if (entropy->restarts_to_go == 0) | 254 if (entropy->restarts_to_go == 0) |
| 254 process_restart(cinfo); | 255 process_restart(cinfo); |
| 255 entropy->restarts_to_go--; | 256 entropy->restarts_to_go--; |
| 256 } | 257 } |
| 257 | 258 |
| 258 if (entropy->ct == -1) return TRUE;» /* if error do nothing */ | 259 if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
| 259 | 260 |
| 260 /* Outer loop handles each block in the MCU */ | 261 /* Outer loop handles each block in the MCU */ |
| 261 | 262 |
| 262 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 263 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 263 block = MCU_data[blkn]; | 264 block = MCU_data[blkn]; |
| 264 ci = cinfo->MCU_membership[blkn]; | 265 ci = cinfo->MCU_membership[blkn]; |
| 265 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; | 266 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; |
| 266 | 267 |
| 267 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ | 268 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
| 268 | 269 |
| 269 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | 270 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
| 270 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | 271 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
| 271 | 272 |
| 272 /* Figure F.19: Decode_DC_DIFF */ | 273 /* Figure F.19: Decode_DC_DIFF */ |
| 273 if (arith_decode(cinfo, st) == 0) | 274 if (arith_decode(cinfo, st) == 0) |
| 274 entropy->dc_context[ci] = 0; | 275 entropy->dc_context[ci] = 0; |
| 275 else { | 276 else { |
| 276 /* Figure F.21: Decoding nonzero value v */ | 277 /* Figure F.21: Decoding nonzero value v */ |
| 277 /* Figure F.22: Decoding the sign of v */ | 278 /* Figure F.22: Decoding the sign of v */ |
| 278 sign = arith_decode(cinfo, st + 1); | 279 sign = arith_decode(cinfo, st + 1); |
| 279 st += 2; st += sign; | 280 st += 2; st += sign; |
| 280 /* Figure F.23: Decoding the magnitude category of v */ | 281 /* Figure F.23: Decoding the magnitude category of v */ |
| 281 if ((m = arith_decode(cinfo, st)) != 0) { | 282 if ((m = arith_decode(cinfo, st)) != 0) { |
| 282 » st = entropy->dc_stats[tbl] + 20;» /* Table F.4: X1 = 20 */ | 283 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
| 283 » while (arith_decode(cinfo, st)) { | 284 while (arith_decode(cinfo, st)) { |
| 284 » if ((m <<= 1) == 0x8000) { | 285 if ((m <<= 1) == 0x8000) { |
| 285 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 286 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 286 » entropy->ct = -1;» » » /* magnitude overflow */ | 287 entropy->ct = -1; /* magnitude overflow */ |
| 287 » return TRUE; | 288 return TRUE; |
| 288 » } | 289 } |
| 289 » st += 1; | 290 st += 1; |
| 290 » } | 291 } |
| 291 } | 292 } |
| 292 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | 293 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
| 293 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) | 294 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
| 294 » entropy->dc_context[ci] = 0;» » /* zero diff category */ | 295 entropy->dc_context[ci] = 0; /* zero diff category */ |
| 295 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) | 296 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
| 296 » entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ | 297 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
| 297 else | 298 else |
| 298 » entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ | 299 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
| 299 v = m; | 300 v = m; |
| 300 /* Figure F.24: Decoding the magnitude bit pattern of v */ | 301 /* Figure F.24: Decoding the magnitude bit pattern of v */ |
| 301 st += 14; | 302 st += 14; |
| 302 while (m >>= 1) | 303 while (m >>= 1) |
| 303 » if (arith_decode(cinfo, st)) v |= m; | 304 if (arith_decode(cinfo, st)) v |= m; |
| 304 v += 1; if (sign) v = -v; | 305 v += 1; if (sign) v = -v; |
| 305 entropy->last_dc_val[ci] += v; | 306 entropy->last_dc_val[ci] += v; |
| 306 } | 307 } |
| 307 | 308 |
| 308 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ | 309 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ |
| 309 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); | 310 (*block)[0] = (JCOEF) LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al); |
| 310 } | 311 } |
| 311 | 312 |
| 312 return TRUE; | 313 return TRUE; |
| 313 } | 314 } |
| 314 | 315 |
| 315 | 316 |
| 316 /* | 317 /* |
| 317 * MCU decoding for AC initial scan (either spectral selection, | 318 * MCU decoding for AC initial scan (either spectral selection, |
| 318 * or first pass of successive approximation). | 319 * or first pass of successive approximation). |
| 319 */ | 320 */ |
| 320 | 321 |
| 321 METHODDEF(boolean) | 322 METHODDEF(boolean) |
| 322 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | 323 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
| 323 { | 324 { |
| 324 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | 325 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
| 325 JBLOCKROW block; | 326 JBLOCKROW block; |
| 326 unsigned char *st; | 327 unsigned char *st; |
| 327 int tbl, sign, k; | 328 int tbl, sign, k; |
| 328 int v, m; | 329 int v, m; |
| 329 | 330 |
| 330 /* Process restart marker if needed */ | 331 /* Process restart marker if needed */ |
| 331 if (cinfo->restart_interval) { | 332 if (cinfo->restart_interval) { |
| 332 if (entropy->restarts_to_go == 0) | 333 if (entropy->restarts_to_go == 0) |
| 333 process_restart(cinfo); | 334 process_restart(cinfo); |
| 334 entropy->restarts_to_go--; | 335 entropy->restarts_to_go--; |
| 335 } | 336 } |
| 336 | 337 |
| 337 if (entropy->ct == -1) return TRUE;» /* if error do nothing */ | 338 if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
| 338 | 339 |
| 339 /* There is always only one block per MCU */ | 340 /* There is always only one block per MCU */ |
| 340 block = MCU_data[0]; | 341 block = MCU_data[0]; |
| 341 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | 342 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
| 342 | 343 |
| 343 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ | 344 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
| 344 | 345 |
| 345 /* Figure F.20: Decode_AC_coefficients */ | 346 /* Figure F.20: Decode_AC_coefficients */ |
| 346 for (k = cinfo->Ss; k <= cinfo->Se; k++) { | 347 for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
| 347 st = entropy->ac_stats[tbl] + 3 * (k - 1); | 348 st = entropy->ac_stats[tbl] + 3 * (k - 1); |
| 348 if (arith_decode(cinfo, st)) break;»» /* EOB flag */ | 349 if (arith_decode(cinfo, st)) break; /* EOB flag */ |
| 349 while (arith_decode(cinfo, st + 1) == 0) { | 350 while (arith_decode(cinfo, st + 1) == 0) { |
| 350 st += 3; k++; | 351 st += 3; k++; |
| 351 if (k > cinfo->Se) { | 352 if (k > cinfo->Se) { |
| 352 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 353 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 353 » entropy->ct = -1;» » » /* spectral overflow */ | 354 entropy->ct = -1; /* spectral overflow */ |
| 354 » return TRUE; | 355 return TRUE; |
| 355 } | 356 } |
| 356 } | 357 } |
| 357 /* Figure F.21: Decoding nonzero value v */ | 358 /* Figure F.21: Decoding nonzero value v */ |
| 358 /* Figure F.22: Decoding the sign of v */ | 359 /* Figure F.22: Decoding the sign of v */ |
| 359 sign = arith_decode(cinfo, entropy->fixed_bin); | 360 sign = arith_decode(cinfo, entropy->fixed_bin); |
| 360 st += 2; | 361 st += 2; |
| 361 /* Figure F.23: Decoding the magnitude category of v */ | 362 /* Figure F.23: Decoding the magnitude category of v */ |
| 362 if ((m = arith_decode(cinfo, st)) != 0) { | 363 if ((m = arith_decode(cinfo, st)) != 0) { |
| 363 if (arith_decode(cinfo, st)) { | 364 if (arith_decode(cinfo, st)) { |
| 364 » m <<= 1; | 365 m <<= 1; |
| 365 » st = entropy->ac_stats[tbl] + | 366 st = entropy->ac_stats[tbl] + |
| 366 » (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | 367 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
| 367 » while (arith_decode(cinfo, st)) { | 368 while (arith_decode(cinfo, st)) { |
| 368 » if ((m <<= 1) == 0x8000) { | 369 if ((m <<= 1) == 0x8000) { |
| 369 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 370 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 370 » entropy->ct = -1;» » » /* magnitude overflow */ | 371 entropy->ct = -1; /* magnitude overflow */ |
| 371 » return TRUE; | 372 return TRUE; |
| 372 » } | 373 } |
| 373 » st += 1; | 374 st += 1; |
| 374 » } | 375 } |
| 375 } | 376 } |
| 376 } | 377 } |
| 377 v = m; | 378 v = m; |
| 378 /* Figure F.24: Decoding the magnitude bit pattern of v */ | 379 /* Figure F.24: Decoding the magnitude bit pattern of v */ |
| 379 st += 14; | 380 st += 14; |
| 380 while (m >>= 1) | 381 while (m >>= 1) |
| 381 if (arith_decode(cinfo, st)) v |= m; | 382 if (arith_decode(cinfo, st)) v |= m; |
| 382 v += 1; if (sign) v = -v; | 383 v += 1; if (sign) v = -v; |
| 383 /* Scale and output coefficient in natural (dezigzagged) order */ | 384 /* Scale and output coefficient in natural (dezigzagged) order */ |
| 384 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); | 385 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 399 unsigned char *st; | 400 unsigned char *st; |
| 400 int p1, blkn; | 401 int p1, blkn; |
| 401 | 402 |
| 402 /* Process restart marker if needed */ | 403 /* Process restart marker if needed */ |
| 403 if (cinfo->restart_interval) { | 404 if (cinfo->restart_interval) { |
| 404 if (entropy->restarts_to_go == 0) | 405 if (entropy->restarts_to_go == 0) |
| 405 process_restart(cinfo); | 406 process_restart(cinfo); |
| 406 entropy->restarts_to_go--; | 407 entropy->restarts_to_go--; |
| 407 } | 408 } |
| 408 | 409 |
| 409 st = entropy->fixed_bin;» /* use fixed probability estimation */ | 410 st = entropy->fixed_bin; /* use fixed probability estimation */ |
| 410 p1 = 1 << cinfo->Al;» » /* 1 in the bit position being coded */ | 411 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
| 411 | 412 |
| 412 /* Outer loop handles each block in the MCU */ | 413 /* Outer loop handles each block in the MCU */ |
| 413 | 414 |
| 414 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 415 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 415 /* Encoded data is simply the next bit of the two's-complement DC value */ | 416 /* Encoded data is simply the next bit of the two's-complement DC value */ |
| 416 if (arith_decode(cinfo, st)) | 417 if (arith_decode(cinfo, st)) |
| 417 MCU_data[blkn][0][0] |= p1; | 418 MCU_data[blkn][0][0] |= p1; |
| 418 } | 419 } |
| 419 | 420 |
| 420 return TRUE; | 421 return TRUE; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 435 int tbl, k, kex; | 436 int tbl, k, kex; |
| 436 int p1, m1; | 437 int p1, m1; |
| 437 | 438 |
| 438 /* Process restart marker if needed */ | 439 /* Process restart marker if needed */ |
| 439 if (cinfo->restart_interval) { | 440 if (cinfo->restart_interval) { |
| 440 if (entropy->restarts_to_go == 0) | 441 if (entropy->restarts_to_go == 0) |
| 441 process_restart(cinfo); | 442 process_restart(cinfo); |
| 442 entropy->restarts_to_go--; | 443 entropy->restarts_to_go--; |
| 443 } | 444 } |
| 444 | 445 |
| 445 if (entropy->ct == -1) return TRUE;» /* if error do nothing */ | 446 if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
| 446 | 447 |
| 447 /* There is always only one block per MCU */ | 448 /* There is always only one block per MCU */ |
| 448 block = MCU_data[0]; | 449 block = MCU_data[0]; |
| 449 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | 450 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
| 450 | 451 |
| 451 p1 = 1 << cinfo->Al;» » /* 1 in the bit position being coded */ | 452 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
| 452 m1 = (-1) << cinfo->Al;» /* -1 in the bit position being coded */ | 453 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ |
| 453 | 454 |
| 454 /* Establish EOBx (previous stage end-of-block) index */ | 455 /* Establish EOBx (previous stage end-of-block) index */ |
| 455 for (kex = cinfo->Se; kex > 0; kex--) | 456 for (kex = cinfo->Se; kex > 0; kex--) |
| 456 if ((*block)[jpeg_natural_order[kex]]) break; | 457 if ((*block)[jpeg_natural_order[kex]]) break; |
| 457 | 458 |
| 458 for (k = cinfo->Ss; k <= cinfo->Se; k++) { | 459 for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
| 459 st = entropy->ac_stats[tbl] + 3 * (k - 1); | 460 st = entropy->ac_stats[tbl] + 3 * (k - 1); |
| 460 if (k > kex) | 461 if (k > kex) |
| 461 if (arith_decode(cinfo, st)) break;» /* EOB flag */ | 462 if (arith_decode(cinfo, st)) break; /* EOB flag */ |
| 462 for (;;) { | 463 for (;;) { |
| 463 thiscoef = *block + jpeg_natural_order[k]; | 464 thiscoef = *block + jpeg_natural_order[k]; |
| 464 if (*thiscoef) {» » » » /* previously nonzero coef */ | 465 if (*thiscoef) { /* previously nonzero coef */ |
| 465 » if (arith_decode(cinfo, st + 2)) { | 466 if (arith_decode(cinfo, st + 2)) { |
| 466 » if (*thiscoef < 0) | 467 if (*thiscoef < 0) |
| 467 » *thiscoef += m1; | 468 *thiscoef += m1; |
| 468 » else | 469 else |
| 469 » *thiscoef += p1; | 470 *thiscoef += p1; |
| 470 » } | 471 } |
| 471 » break; | 472 break; |
| 472 } | 473 } |
| 473 if (arith_decode(cinfo, st + 1)) {» /* newly nonzero coef */ | 474 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ |
| 474 » if (arith_decode(cinfo, entropy->fixed_bin)) | 475 if (arith_decode(cinfo, entropy->fixed_bin)) |
| 475 » *thiscoef = m1; | 476 *thiscoef = m1; |
| 476 » else | 477 else |
| 477 » *thiscoef = p1; | 478 *thiscoef = p1; |
| 478 » break; | 479 break; |
| 479 } | 480 } |
| 480 st += 3; k++; | 481 st += 3; k++; |
| 481 if (k > cinfo->Se) { | 482 if (k > cinfo->Se) { |
| 482 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 483 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 483 » entropy->ct = -1;» » » /* spectral overflow */ | 484 entropy->ct = -1; /* spectral overflow */ |
| 484 » return TRUE; | 485 return TRUE; |
| 485 } | 486 } |
| 486 } | 487 } |
| 487 } | 488 } |
| 488 | 489 |
| 489 return TRUE; | 490 return TRUE; |
| 490 } | 491 } |
| 491 | 492 |
| 492 | 493 |
| 493 /* | 494 /* |
| 494 * Decode one MCU's worth of arithmetic-compressed coefficients. | 495 * Decode one MCU's worth of arithmetic-compressed coefficients. |
| 495 */ | 496 */ |
| 496 | 497 |
| 497 METHODDEF(boolean) | 498 METHODDEF(boolean) |
| 498 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | 499 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
| 499 { | 500 { |
| 500 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | 501 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
| 501 jpeg_component_info * compptr; | 502 jpeg_component_info *compptr; |
| 502 JBLOCKROW block; | 503 JBLOCKROW block; |
| 503 unsigned char *st; | 504 unsigned char *st; |
| 504 int blkn, ci, tbl, sign, k; | 505 int blkn, ci, tbl, sign, k; |
| 505 int v, m; | 506 int v, m; |
| 506 | 507 |
| 507 /* Process restart marker if needed */ | 508 /* Process restart marker if needed */ |
| 508 if (cinfo->restart_interval) { | 509 if (cinfo->restart_interval) { |
| 509 if (entropy->restarts_to_go == 0) | 510 if (entropy->restarts_to_go == 0) |
| 510 process_restart(cinfo); | 511 process_restart(cinfo); |
| 511 entropy->restarts_to_go--; | 512 entropy->restarts_to_go--; |
| 512 } | 513 } |
| 513 | 514 |
| 514 if (entropy->ct == -1) return TRUE;» /* if error do nothing */ | 515 if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
| 515 | 516 |
| 516 /* Outer loop handles each block in the MCU */ | 517 /* Outer loop handles each block in the MCU */ |
| 517 | 518 |
| 518 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 519 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 519 block = MCU_data ? MCU_data[blkn] : NULL; | 520 block = MCU_data ? MCU_data[blkn] : NULL; |
| 520 ci = cinfo->MCU_membership[blkn]; | 521 ci = cinfo->MCU_membership[blkn]; |
| 521 compptr = cinfo->cur_comp_info[ci]; | 522 compptr = cinfo->cur_comp_info[ci]; |
| 522 | 523 |
| 523 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ | 524 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
| 524 | 525 |
| 525 tbl = compptr->dc_tbl_no; | 526 tbl = compptr->dc_tbl_no; |
| 526 | 527 |
| 527 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | 528 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
| 528 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | 529 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
| 529 | 530 |
| 530 /* Figure F.19: Decode_DC_DIFF */ | 531 /* Figure F.19: Decode_DC_DIFF */ |
| 531 if (arith_decode(cinfo, st) == 0) | 532 if (arith_decode(cinfo, st) == 0) |
| 532 entropy->dc_context[ci] = 0; | 533 entropy->dc_context[ci] = 0; |
| 533 else { | 534 else { |
| 534 /* Figure F.21: Decoding nonzero value v */ | 535 /* Figure F.21: Decoding nonzero value v */ |
| 535 /* Figure F.22: Decoding the sign of v */ | 536 /* Figure F.22: Decoding the sign of v */ |
| 536 sign = arith_decode(cinfo, st + 1); | 537 sign = arith_decode(cinfo, st + 1); |
| 537 st += 2; st += sign; | 538 st += 2; st += sign; |
| 538 /* Figure F.23: Decoding the magnitude category of v */ | 539 /* Figure F.23: Decoding the magnitude category of v */ |
| 539 if ((m = arith_decode(cinfo, st)) != 0) { | 540 if ((m = arith_decode(cinfo, st)) != 0) { |
| 540 » st = entropy->dc_stats[tbl] + 20;» /* Table F.4: X1 = 20 */ | 541 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
| 541 » while (arith_decode(cinfo, st)) { | 542 while (arith_decode(cinfo, st)) { |
| 542 » if ((m <<= 1) == 0x8000) { | 543 if ((m <<= 1) == 0x8000) { |
| 543 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 544 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 544 » entropy->ct = -1;» » » /* magnitude overflow */ | 545 entropy->ct = -1; /* magnitude overflow */ |
| 545 » return TRUE; | 546 return TRUE; |
| 546 » } | 547 } |
| 547 » st += 1; | 548 st += 1; |
| 548 » } | 549 } |
| 549 } | 550 } |
| 550 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | 551 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
| 551 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) | 552 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
| 552 » entropy->dc_context[ci] = 0;» » /* zero diff category */ | 553 entropy->dc_context[ci] = 0; /* zero diff category */ |
| 553 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) | 554 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
| 554 » entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ | 555 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
| 555 else | 556 else |
| 556 » entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ | 557 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
| 557 v = m; | 558 v = m; |
| 558 /* Figure F.24: Decoding the magnitude bit pattern of v */ | 559 /* Figure F.24: Decoding the magnitude bit pattern of v */ |
| 559 st += 14; | 560 st += 14; |
| 560 while (m >>= 1) | 561 while (m >>= 1) |
| 561 » if (arith_decode(cinfo, st)) v |= m; | 562 if (arith_decode(cinfo, st)) v |= m; |
| 562 v += 1; if (sign) v = -v; | 563 v += 1; if (sign) v = -v; |
| 563 entropy->last_dc_val[ci] += v; | 564 entropy->last_dc_val[ci] += v; |
| 564 } | 565 } |
| 565 | 566 |
| 566 if (block) | 567 if (block) |
| 567 (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; | 568 (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; |
| 568 | 569 |
| 569 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ | 570 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
| 570 | 571 |
| 571 tbl = compptr->ac_tbl_no; | 572 tbl = compptr->ac_tbl_no; |
| 572 | 573 |
| 573 /* Figure F.20: Decode_AC_coefficients */ | 574 /* Figure F.20: Decode_AC_coefficients */ |
| 574 for (k = 1; k <= DCTSIZE2 - 1; k++) { | 575 for (k = 1; k <= DCTSIZE2 - 1; k++) { |
| 575 st = entropy->ac_stats[tbl] + 3 * (k - 1); | 576 st = entropy->ac_stats[tbl] + 3 * (k - 1); |
| 576 if (arith_decode(cinfo, st)) break;» /* EOB flag */ | 577 if (arith_decode(cinfo, st)) break; /* EOB flag */ |
| 577 while (arith_decode(cinfo, st + 1) == 0) { | 578 while (arith_decode(cinfo, st + 1) == 0) { |
| 578 » st += 3; k++; | 579 st += 3; k++; |
| 579 » if (k > DCTSIZE2 - 1) { | 580 if (k > DCTSIZE2 - 1) { |
| 580 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 581 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 581 » entropy->ct = -1;» » » /* spectral overflow */ | 582 entropy->ct = -1; /* spectral overflow */ |
| 582 » return TRUE; | 583 return TRUE; |
| 583 » } | 584 } |
| 584 } | 585 } |
| 585 /* Figure F.21: Decoding nonzero value v */ | 586 /* Figure F.21: Decoding nonzero value v */ |
| 586 /* Figure F.22: Decoding the sign of v */ | 587 /* Figure F.22: Decoding the sign of v */ |
| 587 sign = arith_decode(cinfo, entropy->fixed_bin); | 588 sign = arith_decode(cinfo, entropy->fixed_bin); |
| 588 st += 2; | 589 st += 2; |
| 589 /* Figure F.23: Decoding the magnitude category of v */ | 590 /* Figure F.23: Decoding the magnitude category of v */ |
| 590 if ((m = arith_decode(cinfo, st)) != 0) { | 591 if ((m = arith_decode(cinfo, st)) != 0) { |
| 591 » if (arith_decode(cinfo, st)) { | 592 if (arith_decode(cinfo, st)) { |
| 592 » m <<= 1; | 593 m <<= 1; |
| 593 » st = entropy->ac_stats[tbl] + | 594 st = entropy->ac_stats[tbl] + |
| 594 » (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | 595 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
| 595 » while (arith_decode(cinfo, st)) { | 596 while (arith_decode(cinfo, st)) { |
| 596 » if ((m <<= 1) == 0x8000) { | 597 if ((m <<= 1) == 0x8000) { |
| 597 » WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | 598 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
| 598 » entropy->ct = -1;»» » /* magnitude overflow */ | 599 entropy->ct = -1; /* magnitude overflow */ |
| 599 » return TRUE; | 600 return TRUE; |
| 600 » } | 601 } |
| 601 » st += 1; | 602 st += 1; |
| 602 » } | 603 } |
| 603 » } | 604 } |
| 604 } | 605 } |
| 605 v = m; | 606 v = m; |
| 606 /* Figure F.24: Decoding the magnitude bit pattern of v */ | 607 /* Figure F.24: Decoding the magnitude bit pattern of v */ |
| 607 st += 14; | 608 st += 14; |
| 608 while (m >>= 1) | 609 while (m >>= 1) |
| 609 » if (arith_decode(cinfo, st)) v |= m; | 610 if (arith_decode(cinfo, st)) v |= m; |
| 610 v += 1; if (sign) v = -v; | 611 v += 1; if (sign) v = -v; |
| 611 if (block) | 612 if (block) |
| 612 (*block)[jpeg_natural_order[k]] = (JCOEF) v; | 613 (*block)[jpeg_natural_order[k]] = (JCOEF) v; |
| 613 } | 614 } |
| 614 } | 615 } |
| 615 | 616 |
| 616 return TRUE; | 617 return TRUE; |
| 617 } | 618 } |
| 618 | 619 |
| 619 | 620 |
| 620 /* | 621 /* |
| 621 * Initialize for an arithmetic-compressed scan. | 622 * Initialize for an arithmetic-compressed scan. |
| 622 */ | 623 */ |
| 623 | 624 |
| 624 METHODDEF(void) | 625 METHODDEF(void) |
| 625 start_pass (j_decompress_ptr cinfo) | 626 start_pass (j_decompress_ptr cinfo) |
| 626 { | 627 { |
| 627 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | 628 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
| 628 int ci, tbl; | 629 int ci, tbl; |
| 629 jpeg_component_info * compptr; | 630 jpeg_component_info *compptr; |
| 630 | 631 |
| 631 if (cinfo->progressive_mode) { | 632 if (cinfo->progressive_mode) { |
| 632 /* Validate progressive scan parameters */ | 633 /* Validate progressive scan parameters */ |
| 633 if (cinfo->Ss == 0) { | 634 if (cinfo->Ss == 0) { |
| 634 if (cinfo->Se != 0) | 635 if (cinfo->Se != 0) |
| 635 » goto bad; | 636 goto bad; |
| 636 } else { | 637 } else { |
| 637 /* need not check Ss/Se < 0 since they came from unsigned bytes */ | 638 /* need not check Ss/Se < 0 since they came from unsigned bytes */ |
| 638 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) | 639 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) |
| 639 » goto bad; | 640 goto bad; |
| 640 /* AC scans may have only one component */ | 641 /* AC scans may have only one component */ |
| 641 if (cinfo->comps_in_scan != 1) | 642 if (cinfo->comps_in_scan != 1) |
| 642 » goto bad; | 643 goto bad; |
| 643 } | 644 } |
| 644 if (cinfo->Ah != 0) { | 645 if (cinfo->Ah != 0) { |
| 645 /* Successive approximation refinement scan: must have Al = Ah-1. */ | 646 /* Successive approximation refinement scan: must have Al = Ah-1. */ |
| 646 if (cinfo->Ah-1 != cinfo->Al) | 647 if (cinfo->Ah-1 != cinfo->Al) |
| 647 » goto bad; | 648 goto bad; |
| 648 } | 649 } |
| 649 if (cinfo->Al > 13) {» /* need not check for < 0 */ | 650 if (cinfo->Al > 13) { /* need not check for < 0 */ |
| 650 bad: | 651 bad: |
| 651 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, | 652 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, |
| 652 » cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); | 653 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); |
| 653 } | 654 } |
| 654 /* Update progression status, and verify that scan order is legal. | 655 /* Update progression status, and verify that scan order is legal. |
| 655 * Note that inter-scan inconsistencies are treated as warnings | 656 * Note that inter-scan inconsistencies are treated as warnings |
| 656 * not fatal errors ... not clear if this is right way to behave. | 657 * not fatal errors ... not clear if this is right way to behave. |
| 657 */ | 658 */ |
| 658 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 659 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 659 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; | 660 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; |
| 660 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; | 661 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; |
| 661 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ | 662 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ |
| 662 » WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); | 663 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); |
| 663 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { | 664 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { |
| 664 » int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; | 665 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; |
| 665 » if (cinfo->Ah != expected) | 666 if (cinfo->Ah != expected) |
| 666 » WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); | 667 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); |
| 667 » coef_bit_ptr[coefi] = cinfo->Al; | 668 coef_bit_ptr[coefi] = cinfo->Al; |
| 668 } | 669 } |
| 669 } | 670 } |
| 670 /* Select MCU decoding routine */ | 671 /* Select MCU decoding routine */ |
| 671 if (cinfo->Ah == 0) { | 672 if (cinfo->Ah == 0) { |
| 672 if (cinfo->Ss == 0) | 673 if (cinfo->Ss == 0) |
| 673 » entropy->pub.decode_mcu = decode_mcu_DC_first; | 674 entropy->pub.decode_mcu = decode_mcu_DC_first; |
| 674 else | 675 else |
| 675 » entropy->pub.decode_mcu = decode_mcu_AC_first; | 676 entropy->pub.decode_mcu = decode_mcu_AC_first; |
| 676 } else { | 677 } else { |
| 677 if (cinfo->Ss == 0) | 678 if (cinfo->Ss == 0) |
| 678 » entropy->pub.decode_mcu = decode_mcu_DC_refine; | 679 entropy->pub.decode_mcu = decode_mcu_DC_refine; |
| 679 else | 680 else |
| 680 » entropy->pub.decode_mcu = decode_mcu_AC_refine; | 681 entropy->pub.decode_mcu = decode_mcu_AC_refine; |
| 681 } | 682 } |
| 682 } else { | 683 } else { |
| 683 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. | 684 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. |
| 684 * This ought to be an error condition, but we make it a warning. | 685 * This ought to be an error condition, but we make it a warning. |
| 685 */ | 686 */ |
| 686 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || | 687 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || |
| 687 » (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1)) | 688 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1)) |
| 688 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); | 689 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
| 689 /* Select MCU decoding routine */ | 690 /* Select MCU decoding routine */ |
| 690 entropy->pub.decode_mcu = decode_mcu; | 691 entropy->pub.decode_mcu = decode_mcu; |
| 691 } | 692 } |
| 692 | 693 |
| 693 /* Allocate & initialize requested statistics areas */ | 694 /* Allocate & initialize requested statistics areas */ |
| 694 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 695 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 695 compptr = cinfo->cur_comp_info[ci]; | 696 compptr = cinfo->cur_comp_info[ci]; |
| 696 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | 697 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
| 697 tbl = compptr->dc_tbl_no; | 698 tbl = compptr->dc_tbl_no; |
| 698 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | 699 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
| 699 » ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | 700 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
| 700 if (entropy->dc_stats[tbl] == NULL) | 701 if (entropy->dc_stats[tbl] == NULL) |
| 701 » entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | 702 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
| 702 » ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); | 703 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); |
| 703 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); | 704 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); |
| 704 /* Initialize DC predictions to 0 */ | 705 /* Initialize DC predictions to 0 */ |
| 705 entropy->last_dc_val[ci] = 0; | 706 entropy->last_dc_val[ci] = 0; |
| 706 entropy->dc_context[ci] = 0; | 707 entropy->dc_context[ci] = 0; |
| 707 } | 708 } |
| 708 if (! cinfo->progressive_mode || cinfo->Ss) { | 709 if (!cinfo->progressive_mode || cinfo->Ss) { |
| 709 tbl = compptr->ac_tbl_no; | 710 tbl = compptr->ac_tbl_no; |
| 710 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | 711 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
| 711 » ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | 712 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
| 712 if (entropy->ac_stats[tbl] == NULL) | 713 if (entropy->ac_stats[tbl] == NULL) |
| 713 » entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | 714 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
| 714 » ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); | 715 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); |
| 715 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); | 716 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); |
| 716 } | 717 } |
| 717 } | 718 } |
| 718 | 719 |
| 719 /* Initialize arithmetic decoding variables */ | 720 /* Initialize arithmetic decoding variables */ |
| 720 entropy->c = 0; | 721 entropy->c = 0; |
| 721 entropy->a = 0; | 722 entropy->a = 0; |
| 722 entropy->ct = -16;» /* force reading 2 initial bytes to fill C */ | 723 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
| 723 | 724 |
| 724 /* Initialize restart counter */ | 725 /* Initialize restart counter */ |
| 725 entropy->restarts_to_go = cinfo->restart_interval; | 726 entropy->restarts_to_go = cinfo->restart_interval; |
| 726 } | 727 } |
| 727 | 728 |
| 728 | 729 |
| 729 /* | 730 /* |
| 730 * Module initialization routine for arithmetic entropy decoding. | 731 * Module initialization routine for arithmetic entropy decoding. |
| 731 */ | 732 */ |
| 732 | 733 |
| 733 GLOBAL(void) | 734 GLOBAL(void) |
| 734 jinit_arith_decoder (j_decompress_ptr cinfo) | 735 jinit_arith_decoder (j_decompress_ptr cinfo) |
| 735 { | 736 { |
| 736 arith_entropy_ptr entropy; | 737 arith_entropy_ptr entropy; |
| 737 int i; | 738 int i; |
| 738 | 739 |
| 739 entropy = (arith_entropy_ptr) | 740 entropy = (arith_entropy_ptr) |
| 740 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 741 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 741 » » » » SIZEOF(arith_entropy_decoder)); | 742 sizeof(arith_entropy_decoder)); |
| 742 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; | 743 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; |
| 743 entropy->pub.start_pass = start_pass; | 744 entropy->pub.start_pass = start_pass; |
| 744 | 745 |
| 745 /* Mark tables unallocated */ | 746 /* Mark tables unallocated */ |
| 746 for (i = 0; i < NUM_ARITH_TBLS; i++) { | 747 for (i = 0; i < NUM_ARITH_TBLS; i++) { |
| 747 entropy->dc_stats[i] = NULL; | 748 entropy->dc_stats[i] = NULL; |
| 748 entropy->ac_stats[i] = NULL; | 749 entropy->ac_stats[i] = NULL; |
| 749 } | 750 } |
| 750 | 751 |
| 751 /* Initialize index for fixed probability estimation */ | 752 /* Initialize index for fixed probability estimation */ |
| 752 entropy->fixed_bin[0] = 113; | 753 entropy->fixed_bin[0] = 113; |
| 753 | 754 |
| 754 if (cinfo->progressive_mode) { | 755 if (cinfo->progressive_mode) { |
| 755 /* Create progression status table */ | 756 /* Create progression status table */ |
| 756 int *coef_bit_ptr, ci; | 757 int *coef_bit_ptr, ci; |
| 757 cinfo->coef_bits = (int (*)[DCTSIZE2]) | 758 cinfo->coef_bits = (int (*)[DCTSIZE2]) |
| 758 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 759 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 759 » » » » cinfo->num_components*DCTSIZE2*SIZEOF(int)); | 760 cinfo->num_components*DCTSIZE2*sizeof(int)); |
| 760 coef_bit_ptr = & cinfo->coef_bits[0][0]; | 761 coef_bit_ptr = & cinfo->coef_bits[0][0]; |
| 761 for (ci = 0; ci < cinfo->num_components; ci++) | 762 for (ci = 0; ci < cinfo->num_components; ci++) |
| 762 for (i = 0; i < DCTSIZE2; i++) | 763 for (i = 0; i < DCTSIZE2; i++) |
| 763 » *coef_bit_ptr++ = -1; | 764 *coef_bit_ptr++ = -1; |
| 764 } | 765 } |
| 765 } | 766 } |
| OLD | NEW |