blob: c6a1a99ac846ac7d895389291c0b70e146eca0b2 [file] [log] [blame]
Guido Vollbeding1e247ac1998-03-28 00:00:00 +00001/*
2 * jdarith.c
3 *
DRC5de454b2014-05-18 19:04:03 +00004 * This file was part of the Independent JPEG Group's software:
Guido Vollbeding989630f2010-01-10 00:00:00 +00005 * Developed 1997-2009 by Guido Vollbeding.
DRC5de454b2014-05-18 19:04:03 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Guido Vollbeding5996a252009-06-27 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +00009 *
10 * This file contains portable arithmetic entropy decoding routines for JPEG
11 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
12 *
13 * Both sequential and progressive modes are supported in this single module.
14 *
15 * Suspension is not currently supported in this module.
16 */
17
18#define JPEG_INTERNALS
19#include "jinclude.h"
20#include "jpeglib.h"
21
22
23/* Expanded entropy decoder object for arithmetic decoding. */
24
25typedef struct {
26 struct jpeg_entropy_decoder pub; /* public fields */
27
28 INT32 c; /* C register, base of coding interval + input bit buffer */
29 INT32 a; /* A register, normalized size of coding interval */
30 int ct; /* bit shift counter, # of bits left in bit buffer part of C */
31 /* init: ct = -16 */
32 /* run: ct = 0..7 */
33 /* error: ct = -1 */
34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
36
DRCe5eaf372014-05-09 18:00:32 +000037 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000038
39 /* Pointers to statistics areas (these workspaces have image lifespan) */
40 unsigned char * dc_stats[NUM_ARITH_TBLS];
41 unsigned char * ac_stats[NUM_ARITH_TBLS];
Guido Vollbeding989630f2010-01-10 00:00:00 +000042
43 /* Statistics bin for coding with fixed probability 0.5 */
44 unsigned char fixed_bin[4];
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000045} arith_entropy_decoder;
46
47typedef arith_entropy_decoder * arith_entropy_ptr;
48
49/* The following two definitions specify the allocation chunk size
50 * for the statistics area.
51 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
52 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000053 *
54 * We use a compact representation with 1 byte per statistics bin,
55 * thus the numbers directly represent byte sizes.
56 * This 1 byte per statistics bin contains the meaning of the MPS
57 * (more probable symbol) in the highest bit (mask 0x80), and the
58 * index into the probability estimation state machine table
59 * in the lower bits (mask 0x7F).
60 */
61
62#define DC_STAT_BINS 64
63#define AC_STAT_BINS 256
64
65
66LOCAL(int)
67get_byte (j_decompress_ptr cinfo)
68/* Read next input byte; we do not support suspension in this module. */
69{
70 struct jpeg_source_mgr * src = cinfo->src;
71
72 if (src->bytes_in_buffer == 0)
73 if (! (*src->fill_input_buffer) (cinfo))
74 ERREXIT(cinfo, JERR_CANT_SUSPEND);
75 src->bytes_in_buffer--;
76 return GETJOCTET(*src->next_input_byte++);
77}
78
79
80/*
81 * The core arithmetic decoding routine (common in JPEG and JBIG).
82 * This needs to go as fast as possible.
83 * Machine-dependent optimization facilities
84 * are not utilized in this portable implementation.
85 * However, this code should be fairly efficient and
86 * may be a good base for further optimizations anyway.
87 *
88 * Return value is 0 or 1 (binary decision).
89 *
90 * Note: I've changed the handling of the code base & bit
91 * buffer register C compared to other implementations
92 * based on the standards layout & procedures.
93 * While it also contains both the actual base of the
94 * coding interval (16 bits) and the next-bits buffer,
95 * the cut-point between these two parts is floating
96 * (instead of fixed) with the bit shift counter CT.
97 * Thus, we also need only one (variable instead of
98 * fixed size) shift for the LPS/MPS decision, and
99 * we can get away with any renormalization update
100 * of C (except for new data insertion, of course).
101 *
102 * I've also introduced a new scheme for accessing
103 * the probability estimation state machine table,
104 * derived from Markus Kuhn's JBIG implementation.
105 */
106
107LOCAL(int)
108arith_decode (j_decompress_ptr cinfo, unsigned char *st)
109{
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000110 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
111 register unsigned char nl, nm;
112 register INT32 qe, temp;
113 register int sv, data;
114
115 /* Renormalization & data input per section D.2.6 */
116 while (e->a < 0x8000L) {
117 if (--e->ct < 0) {
118 /* Need to fetch next data byte */
119 if (cinfo->unread_marker)
DRCe5eaf372014-05-09 18:00:32 +0000120 data = 0; /* stuff zero data */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000121 else {
DRCe5eaf372014-05-09 18:00:32 +0000122 data = get_byte(cinfo); /* read next input byte */
123 if (data == 0xFF) { /* zero stuff or marker code */
124 do data = get_byte(cinfo);
125 while (data == 0xFF); /* swallow extra 0xFF bytes */
126 if (data == 0)
127 data = 0xFF; /* discard stuffed zero byte */
128 else {
129 /* Note: Different from the Huffman decoder, hitting
130 * a marker while processing the compressed data
131 * segment is legal in arithmetic coding.
132 * The convention is to supply zero data
133 * then until decoding is complete.
134 */
135 cinfo->unread_marker = data;
136 data = 0;
137 }
138 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000139 }
140 e->c = (e->c << 8) | data; /* insert data into C register */
DRCe5eaf372014-05-09 18:00:32 +0000141 if ((e->ct += 8) < 0) /* update bit shift counter */
142 /* Need more initial bytes */
143 if (++e->ct == 0)
144 /* Got 2 initial bytes -> re-init A and exit loop */
145 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000146 }
147 e->a <<= 1;
148 }
149
150 /* Fetch values from our compact representation of Table D.2:
151 * Qe values and probability estimation state machine
152 */
153 sv = *st;
DRCe5eaf372014-05-09 18:00:32 +0000154 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
155 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
156 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000157
158 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
159 temp = e->a - qe;
160 e->a = temp;
161 temp <<= e->ct;
162 if (e->c >= temp) {
163 e->c -= temp;
164 /* Conditional LPS (less probable symbol) exchange */
165 if (e->a < qe) {
166 e->a = qe;
DRCe5eaf372014-05-09 18:00:32 +0000167 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000168 } else {
169 e->a = qe;
DRCe5eaf372014-05-09 18:00:32 +0000170 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
171 sv ^= 0x80; /* Exchange LPS/MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000172 }
173 } else if (e->a < 0x8000L) {
174 /* Conditional MPS (more probable symbol) exchange */
175 if (e->a < qe) {
DRCe5eaf372014-05-09 18:00:32 +0000176 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
177 sv ^= 0x80; /* Exchange LPS/MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000178 } else {
DRCe5eaf372014-05-09 18:00:32 +0000179 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000180 }
181 }
182
183 return sv >> 7;
184}
185
186
187/*
188 * Check for a restart marker & resynchronize decoder.
189 */
190
191LOCAL(void)
192process_restart (j_decompress_ptr cinfo)
193{
194 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
195 int ci;
196 jpeg_component_info * compptr;
197
198 /* Advance past the RSTn marker */
199 if (! (*cinfo->marker->read_restart_marker) (cinfo))
200 ERREXIT(cinfo, JERR_CANT_SUSPEND);
201
Guido Vollbeding989630f2010-01-10 00:00:00 +0000202 /* Re-initialize statistics areas */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000203 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
204 compptr = cinfo->cur_comp_info[ci];
Guido Vollbeding989630f2010-01-10 00:00:00 +0000205 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000206 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
207 /* Reset DC predictions to 0 */
208 entropy->last_dc_val[ci] = 0;
209 entropy->dc_context[ci] = 0;
210 }
DRC66f97e62010-11-23 05:49:54 +0000211 if (! cinfo->progressive_mode || cinfo->Ss) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000212 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
213 }
214 }
215
216 /* Reset arithmetic decoding variables */
217 entropy->c = 0;
218 entropy->a = 0;
DRCe5eaf372014-05-09 18:00:32 +0000219 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000220
221 /* Reset restart counter */
222 entropy->restarts_to_go = cinfo->restart_interval;
223}
224
225
226/*
227 * Arithmetic MCU decoding.
228 * Each of these routines decodes and returns one MCU's worth of
229 * arithmetic-compressed coefficients.
230 * The coefficients are reordered from zigzag order into natural array order,
231 * but are not dequantized.
232 *
233 * The i'th block of the MCU is stored into the block pointed to by
234 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
235 */
236
237/*
238 * MCU decoding for DC initial scan (either spectral selection,
239 * or first pass of successive approximation).
240 */
241
242METHODDEF(boolean)
243decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
244{
245 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
246 JBLOCKROW block;
247 unsigned char *st;
248 int blkn, ci, tbl, sign;
249 int v, m;
250
251 /* Process restart marker if needed */
252 if (cinfo->restart_interval) {
253 if (entropy->restarts_to_go == 0)
254 process_restart(cinfo);
255 entropy->restarts_to_go--;
256 }
257
DRCe5eaf372014-05-09 18:00:32 +0000258 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000259
260 /* Outer loop handles each block in the MCU */
261
262 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
263 block = MCU_data[blkn];
264 ci = cinfo->MCU_membership[blkn];
265 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
266
267 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
268
269 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
270 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
271
272 /* Figure F.19: Decode_DC_DIFF */
273 if (arith_decode(cinfo, st) == 0)
274 entropy->dc_context[ci] = 0;
275 else {
276 /* Figure F.21: Decoding nonzero value v */
277 /* Figure F.22: Decoding the sign of v */
278 sign = arith_decode(cinfo, st + 1);
279 st += 2; st += sign;
280 /* Figure F.23: Decoding the magnitude category of v */
281 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000282 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
283 while (arith_decode(cinfo, st)) {
284 if ((m <<= 1) == 0x8000) {
285 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
286 entropy->ct = -1; /* magnitude overflow */
287 return TRUE;
288 }
289 st += 1;
290 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000291 }
292 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000293 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000294 entropy->dc_context[ci] = 0; /* zero diff category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000295 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000296 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000297 else
DRCe5eaf372014-05-09 18:00:32 +0000298 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000299 v = m;
300 /* Figure F.24: Decoding the magnitude bit pattern of v */
301 st += 14;
302 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000303 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000304 v += 1; if (sign) v = -v;
305 entropy->last_dc_val[ci] += v;
306 }
307
308 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
309 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
310 }
311
312 return TRUE;
313}
314
315
316/*
317 * MCU decoding for AC initial scan (either spectral selection,
318 * or first pass of successive approximation).
319 */
320
321METHODDEF(boolean)
322decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
323{
324 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
325 JBLOCKROW block;
326 unsigned char *st;
327 int tbl, sign, k;
328 int v, m;
329
330 /* Process restart marker if needed */
331 if (cinfo->restart_interval) {
332 if (entropy->restarts_to_go == 0)
333 process_restart(cinfo);
334 entropy->restarts_to_go--;
335 }
336
DRCe5eaf372014-05-09 18:00:32 +0000337 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000338
339 /* There is always only one block per MCU */
340 block = MCU_data[0];
341 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
342
343 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
344
345 /* Figure F.20: Decode_AC_coefficients */
346 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
347 st = entropy->ac_stats[tbl] + 3 * (k - 1);
DRCe5eaf372014-05-09 18:00:32 +0000348 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000349 while (arith_decode(cinfo, st + 1) == 0) {
350 st += 3; k++;
351 if (k > cinfo->Se) {
DRCe5eaf372014-05-09 18:00:32 +0000352 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
353 entropy->ct = -1; /* spectral overflow */
354 return TRUE;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000355 }
356 }
357 /* Figure F.21: Decoding nonzero value v */
358 /* Figure F.22: Decoding the sign of v */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000359 sign = arith_decode(cinfo, entropy->fixed_bin);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000360 st += 2;
361 /* Figure F.23: Decoding the magnitude category of v */
362 if ((m = arith_decode(cinfo, st)) != 0) {
363 if (arith_decode(cinfo, st)) {
DRCe5eaf372014-05-09 18:00:32 +0000364 m <<= 1;
365 st = entropy->ac_stats[tbl] +
366 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
367 while (arith_decode(cinfo, st)) {
368 if ((m <<= 1) == 0x8000) {
369 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
370 entropy->ct = -1; /* magnitude overflow */
371 return TRUE;
372 }
373 st += 1;
374 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000375 }
376 }
377 v = m;
378 /* Figure F.24: Decoding the magnitude bit pattern of v */
379 st += 14;
380 while (m >>= 1)
381 if (arith_decode(cinfo, st)) v |= m;
382 v += 1; if (sign) v = -v;
383 /* Scale and output coefficient in natural (dezigzagged) order */
DRC66f97e62010-11-23 05:49:54 +0000384 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000385 }
386
387 return TRUE;
388}
389
390
391/*
392 * MCU decoding for DC successive approximation refinement scan.
393 */
394
395METHODDEF(boolean)
396decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
397{
398 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
Guido Vollbeding989630f2010-01-10 00:00:00 +0000399 unsigned char *st;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000400 int p1, blkn;
401
402 /* Process restart marker if needed */
403 if (cinfo->restart_interval) {
404 if (entropy->restarts_to_go == 0)
405 process_restart(cinfo);
406 entropy->restarts_to_go--;
407 }
408
DRCe5eaf372014-05-09 18:00:32 +0000409 st = entropy->fixed_bin; /* use fixed probability estimation */
410 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000411
412 /* Outer loop handles each block in the MCU */
413
414 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000415 /* Encoded data is simply the next bit of the two's-complement DC value */
416 if (arith_decode(cinfo, st))
417 MCU_data[blkn][0][0] |= p1;
418 }
419
420 return TRUE;
421}
422
423
424/*
425 * MCU decoding for AC successive approximation refinement scan.
426 */
427
428METHODDEF(boolean)
429decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
430{
431 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
432 JBLOCKROW block;
433 JCOEFPTR thiscoef;
434 unsigned char *st;
435 int tbl, k, kex;
436 int p1, m1;
437
438 /* Process restart marker if needed */
439 if (cinfo->restart_interval) {
440 if (entropy->restarts_to_go == 0)
441 process_restart(cinfo);
442 entropy->restarts_to_go--;
443 }
444
DRCe5eaf372014-05-09 18:00:32 +0000445 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000446
447 /* There is always only one block per MCU */
448 block = MCU_data[0];
449 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
450
DRCe5eaf372014-05-09 18:00:32 +0000451 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
452 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000453
454 /* Establish EOBx (previous stage end-of-block) index */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000455 for (kex = cinfo->Se; kex > 0; kex--)
DRC66f97e62010-11-23 05:49:54 +0000456 if ((*block)[jpeg_natural_order[kex]]) break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000457
458 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
459 st = entropy->ac_stats[tbl] + 3 * (k - 1);
Guido Vollbeding989630f2010-01-10 00:00:00 +0000460 if (k > kex)
DRCe5eaf372014-05-09 18:00:32 +0000461 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000462 for (;;) {
DRC66f97e62010-11-23 05:49:54 +0000463 thiscoef = *block + jpeg_natural_order[k];
DRCe5eaf372014-05-09 18:00:32 +0000464 if (*thiscoef) { /* previously nonzero coef */
465 if (arith_decode(cinfo, st + 2)) {
466 if (*thiscoef < 0)
467 *thiscoef += m1;
468 else
469 *thiscoef += p1;
470 }
471 break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000472 }
DRCe5eaf372014-05-09 18:00:32 +0000473 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
474 if (arith_decode(cinfo, entropy->fixed_bin))
475 *thiscoef = m1;
476 else
477 *thiscoef = p1;
478 break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000479 }
480 st += 3; k++;
481 if (k > cinfo->Se) {
DRCe5eaf372014-05-09 18:00:32 +0000482 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
483 entropy->ct = -1; /* spectral overflow */
484 return TRUE;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000485 }
486 }
487 }
488
489 return TRUE;
490}
491
492
493/*
494 * Decode one MCU's worth of arithmetic-compressed coefficients.
495 */
496
497METHODDEF(boolean)
498decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
499{
500 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
501 jpeg_component_info * compptr;
502 JBLOCKROW block;
503 unsigned char *st;
504 int blkn, ci, tbl, sign, k;
505 int v, m;
506
507 /* Process restart marker if needed */
508 if (cinfo->restart_interval) {
509 if (entropy->restarts_to_go == 0)
510 process_restart(cinfo);
511 entropy->restarts_to_go--;
512 }
513
DRCe5eaf372014-05-09 18:00:32 +0000514 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000515
516 /* Outer loop handles each block in the MCU */
517
518 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
519 block = MCU_data[blkn];
520 ci = cinfo->MCU_membership[blkn];
521 compptr = cinfo->cur_comp_info[ci];
522
523 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
524
525 tbl = compptr->dc_tbl_no;
526
527 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
528 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
529
530 /* Figure F.19: Decode_DC_DIFF */
531 if (arith_decode(cinfo, st) == 0)
532 entropy->dc_context[ci] = 0;
533 else {
534 /* Figure F.21: Decoding nonzero value v */
535 /* Figure F.22: Decoding the sign of v */
536 sign = arith_decode(cinfo, st + 1);
537 st += 2; st += sign;
538 /* Figure F.23: Decoding the magnitude category of v */
539 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000540 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
541 while (arith_decode(cinfo, st)) {
542 if ((m <<= 1) == 0x8000) {
543 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
544 entropy->ct = -1; /* magnitude overflow */
545 return TRUE;
546 }
547 st += 1;
548 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000549 }
550 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000551 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000552 entropy->dc_context[ci] = 0; /* zero diff category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000553 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000554 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000555 else
DRCe5eaf372014-05-09 18:00:32 +0000556 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000557 v = m;
558 /* Figure F.24: Decoding the magnitude bit pattern of v */
559 st += 14;
560 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000561 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000562 v += 1; if (sign) v = -v;
563 entropy->last_dc_val[ci] += v;
564 }
565
566 (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
567
568 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
569
570 tbl = compptr->ac_tbl_no;
571
572 /* Figure F.20: Decode_AC_coefficients */
DRC66f97e62010-11-23 05:49:54 +0000573 for (k = 1; k <= DCTSIZE2 - 1; k++) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000574 st = entropy->ac_stats[tbl] + 3 * (k - 1);
DRCe5eaf372014-05-09 18:00:32 +0000575 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000576 while (arith_decode(cinfo, st + 1) == 0) {
DRCe5eaf372014-05-09 18:00:32 +0000577 st += 3; k++;
578 if (k > DCTSIZE2 - 1) {
579 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
580 entropy->ct = -1; /* spectral overflow */
581 return TRUE;
582 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000583 }
584 /* Figure F.21: Decoding nonzero value v */
585 /* Figure F.22: Decoding the sign of v */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000586 sign = arith_decode(cinfo, entropy->fixed_bin);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000587 st += 2;
588 /* Figure F.23: Decoding the magnitude category of v */
589 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000590 if (arith_decode(cinfo, st)) {
591 m <<= 1;
592 st = entropy->ac_stats[tbl] +
593 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
594 while (arith_decode(cinfo, st)) {
595 if ((m <<= 1) == 0x8000) {
596 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
597 entropy->ct = -1; /* magnitude overflow */
598 return TRUE;
599 }
600 st += 1;
601 }
602 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000603 }
604 v = m;
605 /* Figure F.24: Decoding the magnitude bit pattern of v */
606 st += 14;
607 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000608 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000609 v += 1; if (sign) v = -v;
DRC66f97e62010-11-23 05:49:54 +0000610 (*block)[jpeg_natural_order[k]] = (JCOEF) v;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000611 }
612 }
613
614 return TRUE;
615}
616
617
618/*
619 * Initialize for an arithmetic-compressed scan.
620 */
621
622METHODDEF(void)
623start_pass (j_decompress_ptr cinfo)
624{
625 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
626 int ci, tbl;
627 jpeg_component_info * compptr;
628
629 if (cinfo->progressive_mode) {
630 /* Validate progressive scan parameters */
631 if (cinfo->Ss == 0) {
632 if (cinfo->Se != 0)
DRCe5eaf372014-05-09 18:00:32 +0000633 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000634 } else {
635 /* need not check Ss/Se < 0 since they came from unsigned bytes */
DRC66f97e62010-11-23 05:49:54 +0000636 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
DRCe5eaf372014-05-09 18:00:32 +0000637 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000638 /* AC scans may have only one component */
639 if (cinfo->comps_in_scan != 1)
DRCe5eaf372014-05-09 18:00:32 +0000640 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000641 }
642 if (cinfo->Ah != 0) {
643 /* Successive approximation refinement scan: must have Al = Ah-1. */
644 if (cinfo->Ah-1 != cinfo->Al)
DRCe5eaf372014-05-09 18:00:32 +0000645 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000646 }
DRCe5eaf372014-05-09 18:00:32 +0000647 if (cinfo->Al > 13) { /* need not check for < 0 */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000648 bad:
649 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
DRCe5eaf372014-05-09 18:00:32 +0000650 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000651 }
652 /* Update progression status, and verify that scan order is legal.
653 * Note that inter-scan inconsistencies are treated as warnings
654 * not fatal errors ... not clear if this is right way to behave.
655 */
656 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
657 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
658 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
659 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
DRCe5eaf372014-05-09 18:00:32 +0000660 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000661 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
DRCe5eaf372014-05-09 18:00:32 +0000662 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
663 if (cinfo->Ah != expected)
664 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
665 coef_bit_ptr[coefi] = cinfo->Al;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000666 }
667 }
668 /* Select MCU decoding routine */
669 if (cinfo->Ah == 0) {
670 if (cinfo->Ss == 0)
DRCe5eaf372014-05-09 18:00:32 +0000671 entropy->pub.decode_mcu = decode_mcu_DC_first;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000672 else
DRCe5eaf372014-05-09 18:00:32 +0000673 entropy->pub.decode_mcu = decode_mcu_AC_first;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000674 } else {
675 if (cinfo->Ss == 0)
DRCe5eaf372014-05-09 18:00:32 +0000676 entropy->pub.decode_mcu = decode_mcu_DC_refine;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000677 else
DRCe5eaf372014-05-09 18:00:32 +0000678 entropy->pub.decode_mcu = decode_mcu_AC_refine;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000679 }
680 } else {
681 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
Guido Vollbeding989630f2010-01-10 00:00:00 +0000682 * This ought to be an error condition, but we make it a warning.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000683 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000684 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
DRCe5eaf372014-05-09 18:00:32 +0000685 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000686 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
687 /* Select MCU decoding routine */
688 entropy->pub.decode_mcu = decode_mcu;
689 }
690
Guido Vollbeding989630f2010-01-10 00:00:00 +0000691 /* Allocate & initialize requested statistics areas */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000692 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
693 compptr = cinfo->cur_comp_info[ci];
Guido Vollbeding989630f2010-01-10 00:00:00 +0000694 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000695 tbl = compptr->dc_tbl_no;
696 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
DRCe5eaf372014-05-09 18:00:32 +0000697 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000698 if (entropy->dc_stats[tbl] == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000699 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
700 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000701 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
702 /* Initialize DC predictions to 0 */
703 entropy->last_dc_val[ci] = 0;
704 entropy->dc_context[ci] = 0;
705 }
DRC66f97e62010-11-23 05:49:54 +0000706 if (! cinfo->progressive_mode || cinfo->Ss) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000707 tbl = compptr->ac_tbl_no;
708 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
DRCe5eaf372014-05-09 18:00:32 +0000709 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000710 if (entropy->ac_stats[tbl] == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000711 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
712 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000713 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
714 }
715 }
716
717 /* Initialize arithmetic decoding variables */
718 entropy->c = 0;
719 entropy->a = 0;
DRCe5eaf372014-05-09 18:00:32 +0000720 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000721
722 /* Initialize restart counter */
723 entropy->restarts_to_go = cinfo->restart_interval;
724}
725
726
727/*
728 * Module initialization routine for arithmetic entropy decoding.
729 */
730
731GLOBAL(void)
732jinit_arith_decoder (j_decompress_ptr cinfo)
733{
734 arith_entropy_ptr entropy;
735 int i;
736
737 entropy = (arith_entropy_ptr)
738 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000739 sizeof(arith_entropy_decoder));
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000740 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
741 entropy->pub.start_pass = start_pass;
742
743 /* Mark tables unallocated */
744 for (i = 0; i < NUM_ARITH_TBLS; i++) {
745 entropy->dc_stats[i] = NULL;
746 entropy->ac_stats[i] = NULL;
747 }
748
Guido Vollbeding989630f2010-01-10 00:00:00 +0000749 /* Initialize index for fixed probability estimation */
750 entropy->fixed_bin[0] = 113;
751
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000752 if (cinfo->progressive_mode) {
753 /* Create progression status table */
754 int *coef_bit_ptr, ci;
755 cinfo->coef_bits = (int (*)[DCTSIZE2])
756 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000757 cinfo->num_components*DCTSIZE2*sizeof(int));
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000758 coef_bit_ptr = & cinfo->coef_bits[0][0];
DRCe5eaf372014-05-09 18:00:32 +0000759 for (ci = 0; ci < cinfo->num_components; ci++)
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000760 for (i = 0; i < DCTSIZE2; i++)
DRCe5eaf372014-05-09 18:00:32 +0000761 *coef_bit_ptr++ = -1;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000762 }
763}