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