blob: 7f0d3a785c392b465ddc154f1668f6defa5c6801 [file] [log] [blame]
hbono@chromium.org98626972011-08-03 03:13:08 +00001/*
2 * jdarith.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
5 * Developed 1997-2015 by Guido Vollbeding.
Aaron Gablec9c87552015-08-03 09:34:32 -07006 * libjpeg-turbo Modifications:
Jonathan Wrightbbb82822020-11-25 13:36:43 +00007 * Copyright (C) 2015-2020, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
hbono@chromium.org98626972011-08-03 03:13:08 +000010 *
Chris Blumecca8c4d2019-03-01 01:09:50 -080011 * This file contains portable arithmetic entropy encoding routines for JPEG
12 * (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1).
hbono@chromium.org98626972011-08-03 03:13:08 +000013 *
14 * Both sequential and progressive modes are supported in this single module.
15 *
16 * Suspension is not currently supported in this module.
Chris Blumecca8c4d2019-03-01 01:09:50 -080017 *
18 * NOTE: All referenced figures are from
19 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
hbono@chromium.org98626972011-08-03 03:13:08 +000020 */
21
22#define JPEG_INTERNALS
23#include "jinclude.h"
24#include "jpeglib.h"
25
26
Chris Blumecca8c4d2019-03-01 01:09:50 -080027#define NEG_1 ((unsigned int)-1)
28
29
hbono@chromium.org98626972011-08-03 03:13:08 +000030/* Expanded entropy decoder object for arithmetic decoding. */
31
32typedef struct {
33 struct jpeg_entropy_decoder pub; /* public fields */
34
Tom Hudson0d47d2d2016-05-04 13:22:56 -040035 JLONG c; /* C register, base of coding interval + input bit buffer */
36 JLONG a; /* A register, normalized size of coding interval */
hbono@chromium.org98626972011-08-03 03:13:08 +000037 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -040044 unsigned int restarts_to_go; /* MCUs left in this restart interval */
hbono@chromium.org98626972011-08-03 03:13:08 +000045
46 /* Pointers to statistics areas (these workspaces have image lifespan) */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040047 unsigned char *dc_stats[NUM_ARITH_TBLS];
48 unsigned char *ac_stats[NUM_ARITH_TBLS];
hbono@chromium.org98626972011-08-03 03:13:08 +000049
50 /* Statistics bin for coding with fixed probability 0.5 */
51 unsigned char fixed_bin[4];
52} arith_entropy_decoder;
53
Tom Hudson0d47d2d2016-05-04 13:22:56 -040054typedef arith_entropy_decoder *arith_entropy_ptr;
hbono@chromium.org98626972011-08-03 03:13:08 +000055
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.
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
Chris Blumecca8c4d2019-03-01 01:09:50 -080069#define DC_STAT_BINS 64
70#define AC_STAT_BINS 256
hbono@chromium.org98626972011-08-03 03:13:08 +000071
72
73LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -080074get_byte(j_decompress_ptr cinfo)
hbono@chromium.org98626972011-08-03 03:13:08 +000075/* Read next input byte; we do not support suspension in this module. */
76{
Tom Hudson0d47d2d2016-05-04 13:22:56 -040077 struct jpeg_source_mgr *src = cinfo->src;
hbono@chromium.org98626972011-08-03 03:13:08 +000078
79 if (src->bytes_in_buffer == 0)
Chris Blumecca8c4d2019-03-01 01:09:50 -080080 if (!(*src->fill_input_buffer) (cinfo))
hbono@chromium.org98626972011-08-03 03:13:08 +000081 ERREXIT(cinfo, JERR_CANT_SUSPEND);
82 src->bytes_in_buffer--;
Jonathan Wrightbbb82822020-11-25 13:36:43 +000083 return *src->next_input_byte++;
hbono@chromium.org98626972011-08-03 03:13:08 +000084}
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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400106 * we can do away with any renormalization update
hbono@chromium.org98626972011-08-03 03:13:08 +0000107 * 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
114LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800115arith_decode(j_decompress_ptr cinfo, unsigned char *st)
hbono@chromium.org98626972011-08-03 03:13:08 +0000116{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800117 register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000118 register unsigned char nl, nm;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400119 register JLONG qe, temp;
hbono@chromium.org98626972011-08-03 03:13:08 +0000120 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)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400127 data = 0; /* stuff zero data */
hbono@chromium.org98626972011-08-03 03:13:08 +0000128 else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400129 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 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000146 }
147 e->c = (e->c << 8) | data; /* insert data into C register */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400148 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 */
hbono@chromium.org98626972011-08-03 03:13:08 +0000153 }
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;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400161 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800162 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
163 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
hbono@chromium.org98626972011-08-03 03:13:08 +0000164
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;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400174 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
hbono@chromium.org98626972011-08-03 03:13:08 +0000175 } else {
176 e->a = qe;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400177 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
178 sv ^= 0x80; /* Exchange LPS/MPS */
hbono@chromium.org98626972011-08-03 03:13:08 +0000179 }
180 } else if (e->a < 0x8000L) {
181 /* Conditional MPS (more probable symbol) exchange */
182 if (e->a < qe) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400183 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
184 sv ^= 0x80; /* Exchange LPS/MPS */
hbono@chromium.org98626972011-08-03 03:13:08 +0000185 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400186 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
hbono@chromium.org98626972011-08-03 03:13:08 +0000187 }
188 }
189
190 return sv >> 7;
191}
192
193
194/*
195 * Check for a restart marker & resynchronize decoder.
196 */
197
198LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800199process_restart(j_decompress_ptr cinfo)
hbono@chromium.org98626972011-08-03 03:13:08 +0000200{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800201 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000202 int ci;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400203 jpeg_component_info *compptr;
hbono@chromium.org98626972011-08-03 03:13:08 +0000204
205 /* Advance past the RSTn marker */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800206 if (!(*cinfo->marker->read_restart_marker) (cinfo))
hbono@chromium.org98626972011-08-03 03:13:08 +0000207 ERREXIT(cinfo, JERR_CANT_SUSPEND);
208
209 /* Re-initialize statistics areas */
210 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
211 compptr = cinfo->cur_comp_info[ci];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400212 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000213 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 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400218 if (!cinfo->progressive_mode || cinfo->Ss) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000219 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;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400226 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
hbono@chromium.org98626972011-08-03 03:13:08 +0000227
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
249METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800250decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.org98626972011-08-03 03:13:08 +0000251{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800252 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000253 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400265 if (entropy->ct == -1) return TRUE; /* if error do nothing */
hbono@chromium.org98626972011-08-03 03:13:08 +0000266
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);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800286 st += 2; st += sign;
hbono@chromium.org98626972011-08-03 03:13:08 +0000287 /* Figure F.23: Decoding the magnitude category of v */
288 if ((m = arith_decode(cinfo, st)) != 0) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400289 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 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000298 }
299 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800300 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400301 entropy->dc_context[ci] = 0; /* zero diff category */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800302 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400303 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
hbono@chromium.org98626972011-08-03 03:13:08 +0000304 else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400305 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
hbono@chromium.org98626972011-08-03 03:13:08 +0000306 v = m;
307 /* Figure F.24: Decoding the magnitude bit pattern of v */
308 st += 14;
309 while (m >>= 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400310 if (arith_decode(cinfo, st)) v |= m;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800311 v += 1; if (sign) v = -v;
312 entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
hbono@chromium.org98626972011-08-03 03:13:08 +0000313 }
314
315 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800316 (*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
hbono@chromium.org98626972011-08-03 03:13:08 +0000317 }
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
328METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800329decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.org98626972011-08-03 03:13:08 +0000330{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800331 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000332 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400344 if (entropy->ct == -1) return TRUE; /* if error do nothing */
hbono@chromium.org98626972011-08-03 03:13:08 +0000345
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);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400355 if (arith_decode(cinfo, st)) break; /* EOB flag */
hbono@chromium.org98626972011-08-03 03:13:08 +0000356 while (arith_decode(cinfo, st + 1) == 0) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800357 st += 3; k++;
hbono@chromium.org98626972011-08-03 03:13:08 +0000358 if (k > cinfo->Se) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400359 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
360 entropy->ct = -1; /* spectral overflow */
361 return TRUE;
hbono@chromium.org98626972011-08-03 03:13:08 +0000362 }
363 }
364 /* Figure F.21: Decoding nonzero value v */
365 /* Figure F.22: Decoding the sign of v */
366 sign = arith_decode(cinfo, entropy->fixed_bin);
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)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400371 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 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000382 }
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;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800389 v += 1; if (sign) v = -v;
hbono@chromium.org98626972011-08-03 03:13:08 +0000390 /* Scale and output coefficient in natural (dezigzagged) order */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800391 (*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al);
hbono@chromium.org98626972011-08-03 03:13:08 +0000392 }
393
394 return TRUE;
395}
396
397
398/*
399 * MCU decoding for DC successive approximation refinement scan.
400 */
401
402METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800403decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.org98626972011-08-03 03:13:08 +0000404{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800405 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000406 unsigned char *st;
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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400416 st = entropy->fixed_bin; /* use fixed probability estimation */
417 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
hbono@chromium.org98626972011-08-03 03:13:08 +0000418
419 /* Outer loop handles each block in the MCU */
420
421 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
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
435METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800436decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.org98626972011-08-03 03:13:08 +0000437{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800438 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000439 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400452 if (entropy->ct == -1) return TRUE; /* if error do nothing */
hbono@chromium.org98626972011-08-03 03:13:08 +0000453
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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400458 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800459 m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
hbono@chromium.org98626972011-08-03 03:13:08 +0000460
461 /* Establish EOBx (previous stage end-of-block) index */
462 for (kex = cinfo->Se; kex > 0; kex--)
463 if ((*block)[jpeg_natural_order[kex]]) break;
464
465 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
466 st = entropy->ac_stats[tbl] + 3 * (k - 1);
467 if (k > kex)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400468 if (arith_decode(cinfo, st)) break; /* EOB flag */
hbono@chromium.org98626972011-08-03 03:13:08 +0000469 for (;;) {
470 thiscoef = *block + jpeg_natural_order[k];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400471 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;
hbono@chromium.org98626972011-08-03 03:13:08 +0000479 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400480 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;
hbono@chromium.org98626972011-08-03 03:13:08 +0000486 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800487 st += 3; k++;
hbono@chromium.org98626972011-08-03 03:13:08 +0000488 if (k > cinfo->Se) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400489 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
490 entropy->ct = -1; /* spectral overflow */
491 return TRUE;
hbono@chromium.org98626972011-08-03 03:13:08 +0000492 }
493 }
494 }
495
496 return TRUE;
497}
498
499
500/*
501 * Decode one MCU's worth of arithmetic-compressed coefficients.
502 */
503
504METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800505decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.org98626972011-08-03 03:13:08 +0000506{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800507 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400508 jpeg_component_info *compptr;
hbono@chromium.org98626972011-08-03 03:13:08 +0000509 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400521 if (entropy->ct == -1) return TRUE; /* if error do nothing */
hbono@chromium.org98626972011-08-03 03:13:08 +0000522
523 /* Outer loop handles each block in the MCU */
524
525 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
Aaron Gablec9c87552015-08-03 09:34:32 -0700526 block = MCU_data ? MCU_data[blkn] : NULL;
hbono@chromium.org98626972011-08-03 03:13:08 +0000527 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);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800544 st += 2; st += sign;
hbono@chromium.org98626972011-08-03 03:13:08 +0000545 /* Figure F.23: Decoding the magnitude category of v */
546 if ((m = arith_decode(cinfo, st)) != 0) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400547 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 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000556 }
557 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800558 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400559 entropy->dc_context[ci] = 0; /* zero diff category */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800560 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400561 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
hbono@chromium.org98626972011-08-03 03:13:08 +0000562 else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400563 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
hbono@chromium.org98626972011-08-03 03:13:08 +0000564 v = m;
565 /* Figure F.24: Decoding the magnitude bit pattern of v */
566 st += 14;
567 while (m >>= 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400568 if (arith_decode(cinfo, st)) v |= m;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800569 v += 1; if (sign) v = -v;
570 entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
hbono@chromium.org98626972011-08-03 03:13:08 +0000571 }
572
Aaron Gablec9c87552015-08-03 09:34:32 -0700573 if (block)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800574 (*block)[0] = (JCOEF)entropy->last_dc_val[ci];
hbono@chromium.org98626972011-08-03 03:13:08 +0000575
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 */
581 for (k = 1; k <= DCTSIZE2 - 1; k++) {
582 st = entropy->ac_stats[tbl] + 3 * (k - 1);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400583 if (arith_decode(cinfo, st)) break; /* EOB flag */
hbono@chromium.org98626972011-08-03 03:13:08 +0000584 while (arith_decode(cinfo, st + 1) == 0) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800585 st += 3; k++;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400586 if (k > DCTSIZE2 - 1) {
587 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
588 entropy->ct = -1; /* spectral overflow */
589 return TRUE;
590 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000591 }
592 /* Figure F.21: Decoding nonzero value v */
593 /* Figure F.22: Decoding the sign of v */
594 sign = arith_decode(cinfo, entropy->fixed_bin);
595 st += 2;
596 /* Figure F.23: Decoding the magnitude category of v */
597 if ((m = arith_decode(cinfo, st)) != 0) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400598 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 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000611 }
612 v = m;
613 /* Figure F.24: Decoding the magnitude bit pattern of v */
614 st += 14;
615 while (m >>= 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400616 if (arith_decode(cinfo, st)) v |= m;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800617 v += 1; if (sign) v = -v;
Aaron Gablec9c87552015-08-03 09:34:32 -0700618 if (block)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800619 (*block)[jpeg_natural_order[k]] = (JCOEF)v;
hbono@chromium.org98626972011-08-03 03:13:08 +0000620 }
621 }
622
623 return TRUE;
624}
625
626
627/*
628 * Initialize for an arithmetic-compressed scan.
629 */
630
631METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800632start_pass(j_decompress_ptr cinfo)
hbono@chromium.org98626972011-08-03 03:13:08 +0000633{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800634 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000635 int ci, tbl;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400636 jpeg_component_info *compptr;
hbono@chromium.org98626972011-08-03 03:13:08 +0000637
638 if (cinfo->progressive_mode) {
639 /* Validate progressive scan parameters */
640 if (cinfo->Ss == 0) {
641 if (cinfo->Se != 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400642 goto bad;
hbono@chromium.org98626972011-08-03 03:13:08 +0000643 } else {
644 /* need not check Ss/Se < 0 since they came from unsigned bytes */
645 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400646 goto bad;
hbono@chromium.org98626972011-08-03 03:13:08 +0000647 /* AC scans may have only one component */
648 if (cinfo->comps_in_scan != 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400649 goto bad;
hbono@chromium.org98626972011-08-03 03:13:08 +0000650 }
651 if (cinfo->Ah != 0) {
652 /* Successive approximation refinement scan: must have Al = Ah-1. */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800653 if (cinfo->Ah - 1 != cinfo->Al)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400654 goto bad;
hbono@chromium.org98626972011-08-03 03:13:08 +0000655 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400656 if (cinfo->Al > 13) { /* need not check for < 0 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800657bad:
hbono@chromium.org98626972011-08-03 03:13:08 +0000658 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400659 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
hbono@chromium.org98626972011-08-03 03:13:08 +0000660 }
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;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800667 int *coef_bit_ptr = &cinfo->coef_bits[cindex][0];
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000668 int *prev_coef_bit_ptr =
669 &cinfo->coef_bits[cindex + cinfo->num_components][0];
hbono@chromium.org98626972011-08-03 03:13:08 +0000670 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400671 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000672 for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {
673 if (cinfo->input_scan_number > 1)
674 prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];
Jonathan Wrighte9a659a2020-12-08 16:53:51 +0000675 else
676 prev_coef_bit_ptr[coefi] = 0;
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000677 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000678 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400679 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
680 if (cinfo->Ah != expected)
681 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
682 coef_bit_ptr[coefi] = cinfo->Al;
hbono@chromium.org98626972011-08-03 03:13:08 +0000683 }
684 }
685 /* Select MCU decoding routine */
686 if (cinfo->Ah == 0) {
687 if (cinfo->Ss == 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400688 entropy->pub.decode_mcu = decode_mcu_DC_first;
hbono@chromium.org98626972011-08-03 03:13:08 +0000689 else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400690 entropy->pub.decode_mcu = decode_mcu_AC_first;
hbono@chromium.org98626972011-08-03 03:13:08 +0000691 } else {
692 if (cinfo->Ss == 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400693 entropy->pub.decode_mcu = decode_mcu_DC_refine;
hbono@chromium.org98626972011-08-03 03:13:08 +0000694 else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400695 entropy->pub.decode_mcu = decode_mcu_AC_refine;
hbono@chromium.org98626972011-08-03 03:13:08 +0000696 }
697 } else {
698 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
699 * This ought to be an error condition, but we make it a warning.
700 */
701 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400702 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
hbono@chromium.org98626972011-08-03 03:13:08 +0000703 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
704 /* Select MCU decoding routine */
705 entropy->pub.decode_mcu = decode_mcu;
706 }
707
708 /* Allocate & initialize requested statistics areas */
709 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
710 compptr = cinfo->cur_comp_info[ci];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400711 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000712 tbl = compptr->dc_tbl_no;
713 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400714 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
hbono@chromium.org98626972011-08-03 03:13:08 +0000715 if (entropy->dc_stats[tbl] == NULL)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800716 entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
717 ((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS);
hbono@chromium.org98626972011-08-03 03:13:08 +0000718 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
719 /* Initialize DC predictions to 0 */
720 entropy->last_dc_val[ci] = 0;
721 entropy->dc_context[ci] = 0;
722 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400723 if (!cinfo->progressive_mode || cinfo->Ss) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000724 tbl = compptr->ac_tbl_no;
725 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400726 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
hbono@chromium.org98626972011-08-03 03:13:08 +0000727 if (entropy->ac_stats[tbl] == NULL)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800728 entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
729 ((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS);
hbono@chromium.org98626972011-08-03 03:13:08 +0000730 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
731 }
732 }
733
734 /* Initialize arithmetic decoding variables */
735 entropy->c = 0;
736 entropy->a = 0;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400737 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000738 entropy->pub.insufficient_data = FALSE;
hbono@chromium.org98626972011-08-03 03:13:08 +0000739
740 /* Initialize restart counter */
741 entropy->restarts_to_go = cinfo->restart_interval;
742}
743
744
745/*
746 * Module initialization routine for arithmetic entropy decoding.
747 */
748
749GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800750jinit_arith_decoder(j_decompress_ptr cinfo)
hbono@chromium.org98626972011-08-03 03:13:08 +0000751{
752 arith_entropy_ptr entropy;
753 int i;
754
755 entropy = (arith_entropy_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800756 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400757 sizeof(arith_entropy_decoder));
Chris Blumecca8c4d2019-03-01 01:09:50 -0800758 cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
hbono@chromium.org98626972011-08-03 03:13:08 +0000759 entropy->pub.start_pass = start_pass;
760
761 /* Mark tables unallocated */
762 for (i = 0; i < NUM_ARITH_TBLS; i++) {
763 entropy->dc_stats[i] = NULL;
764 entropy->ac_stats[i] = NULL;
765 }
766
767 /* Initialize index for fixed probability estimation */
768 entropy->fixed_bin[0] = 113;
769
770 if (cinfo->progressive_mode) {
771 /* Create progression status table */
772 int *coef_bit_ptr, ci;
773 cinfo->coef_bits = (int (*)[DCTSIZE2])
Chris Blumecca8c4d2019-03-01 01:09:50 -0800774 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000775 cinfo->num_components * 2 * DCTSIZE2 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800776 sizeof(int));
777 coef_bit_ptr = &cinfo->coef_bits[0][0];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400778 for (ci = 0; ci < cinfo->num_components; ci++)
hbono@chromium.org98626972011-08-03 03:13:08 +0000779 for (i = 0; i < DCTSIZE2; i++)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400780 *coef_bit_ptr++ = -1;
hbono@chromium.org98626972011-08-03 03:13:08 +0000781 }
782}