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