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