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