blob: a9a6ec6b6102acb00c7440855c239623a83a57be [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.
DRCeb32cc12015-06-25 03:44:36 +00006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000010 *
11 * This file contains portable arithmetic entropy decoding routines for JPEG
12 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
13 *
14 * Both sequential and progressive modes are supported in this single module.
15 *
16 * Suspension is not currently supported in this module.
17 */
18
19#define JPEG_INTERNALS
20#include "jinclude.h"
21#include "jpeglib.h"
22
23
24/* Expanded entropy decoder object for arithmetic decoding. */
25
26typedef struct {
27 struct jpeg_entropy_decoder pub; /* public fields */
28
DRC1e32fe32015-10-14 17:32:39 -050029 JLONG c; /* C register, base of coding interval + input bit buffer */
30 JLONG a; /* A register, normalized size of coding interval */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000031 int ct; /* bit shift counter, # of bits left in bit buffer part of C */
32 /* init: ct = -16 */
33 /* run: ct = 0..7 */
34 /* error: ct = -1 */
35 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
36 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
37
DRCe5eaf372014-05-09 18:00:32 +000038 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000039
40 /* Pointers to statistics areas (these workspaces have image lifespan) */
41 unsigned char * dc_stats[NUM_ARITH_TBLS];
42 unsigned char * ac_stats[NUM_ARITH_TBLS];
Guido Vollbeding989630f2010-01-10 00:00:00 +000043
44 /* Statistics bin for coding with fixed probability 0.5 */
45 unsigned char fixed_bin[4];
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000046} arith_entropy_decoder;
47
48typedef arith_entropy_decoder * arith_entropy_ptr;
49
50/* The following two definitions specify the allocation chunk size
51 * for the statistics area.
52 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
53 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +000054 *
55 * We use a compact representation with 1 byte per statistics bin,
56 * thus the numbers directly represent byte sizes.
57 * This 1 byte per statistics bin contains the meaning of the MPS
58 * (more probable symbol) in the highest bit (mask 0x80), and the
59 * index into the probability estimation state machine table
60 * in the lower bits (mask 0x7F).
61 */
62
63#define DC_STAT_BINS 64
64#define AC_STAT_BINS 256
65
66
67LOCAL(int)
68get_byte (j_decompress_ptr cinfo)
69/* Read next input byte; we do not support suspension in this module. */
70{
71 struct jpeg_source_mgr * src = cinfo->src;
72
73 if (src->bytes_in_buffer == 0)
74 if (! (*src->fill_input_buffer) (cinfo))
75 ERREXIT(cinfo, JERR_CANT_SUSPEND);
76 src->bytes_in_buffer--;
77 return GETJOCTET(*src->next_input_byte++);
78}
79
80
81/*
82 * The core arithmetic decoding routine (common in JPEG and JBIG).
83 * This needs to go as fast as possible.
84 * Machine-dependent optimization facilities
85 * are not utilized in this portable implementation.
86 * However, this code should be fairly efficient and
87 * may be a good base for further optimizations anyway.
88 *
89 * Return value is 0 or 1 (binary decision).
90 *
91 * Note: I've changed the handling of the code base & bit
92 * buffer register C compared to other implementations
93 * based on the standards layout & procedures.
94 * While it also contains both the actual base of the
95 * coding interval (16 bits) and the next-bits buffer,
96 * the cut-point between these two parts is floating
97 * (instead of fixed) with the bit shift counter CT.
98 * Thus, we also need only one (variable instead of
99 * fixed size) shift for the LPS/MPS decision, and
100 * we can get away with any renormalization update
101 * of C (except for new data insertion, of course).
102 *
103 * I've also introduced a new scheme for accessing
104 * the probability estimation state machine table,
105 * derived from Markus Kuhn's JBIG implementation.
106 */
107
108LOCAL(int)
109arith_decode (j_decompress_ptr cinfo, unsigned char *st)
110{
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000111 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
112 register unsigned char nl, nm;
DRC1e32fe32015-10-14 17:32:39 -0500113 register JLONG qe, temp;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000114 register int sv, data;
115
116 /* Renormalization & data input per section D.2.6 */
117 while (e->a < 0x8000L) {
118 if (--e->ct < 0) {
119 /* Need to fetch next data byte */
120 if (cinfo->unread_marker)
DRCe5eaf372014-05-09 18:00:32 +0000121 data = 0; /* stuff zero data */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000122 else {
DRCe5eaf372014-05-09 18:00:32 +0000123 data = get_byte(cinfo); /* read next input byte */
124 if (data == 0xFF) { /* zero stuff or marker code */
125 do data = get_byte(cinfo);
126 while (data == 0xFF); /* swallow extra 0xFF bytes */
127 if (data == 0)
128 data = 0xFF; /* discard stuffed zero byte */
129 else {
130 /* Note: Different from the Huffman decoder, hitting
131 * a marker while processing the compressed data
132 * segment is legal in arithmetic coding.
133 * The convention is to supply zero data
134 * then until decoding is complete.
135 */
136 cinfo->unread_marker = data;
137 data = 0;
138 }
139 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000140 }
141 e->c = (e->c << 8) | data; /* insert data into C register */
DRCe5eaf372014-05-09 18:00:32 +0000142 if ((e->ct += 8) < 0) /* update bit shift counter */
143 /* Need more initial bytes */
144 if (++e->ct == 0)
145 /* Got 2 initial bytes -> re-init A and exit loop */
146 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000147 }
148 e->a <<= 1;
149 }
150
151 /* Fetch values from our compact representation of Table D.2:
152 * Qe values and probability estimation state machine
153 */
154 sv = *st;
DRCe5eaf372014-05-09 18:00:32 +0000155 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
156 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
157 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000158
159 /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
160 temp = e->a - qe;
161 e->a = temp;
162 temp <<= e->ct;
163 if (e->c >= temp) {
164 e->c -= temp;
165 /* Conditional LPS (less probable symbol) exchange */
166 if (e->a < qe) {
167 e->a = qe;
DRCe5eaf372014-05-09 18:00:32 +0000168 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000169 } else {
170 e->a = qe;
DRCe5eaf372014-05-09 18:00:32 +0000171 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
172 sv ^= 0x80; /* Exchange LPS/MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000173 }
174 } else if (e->a < 0x8000L) {
175 /* Conditional MPS (more probable symbol) exchange */
176 if (e->a < qe) {
DRCe5eaf372014-05-09 18:00:32 +0000177 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
178 sv ^= 0x80; /* Exchange LPS/MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000179 } else {
DRCe5eaf372014-05-09 18:00:32 +0000180 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000181 }
182 }
183
184 return sv >> 7;
185}
186
187
188/*
189 * Check for a restart marker & resynchronize decoder.
190 */
191
192LOCAL(void)
193process_restart (j_decompress_ptr cinfo)
194{
195 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
196 int ci;
197 jpeg_component_info * compptr;
198
199 /* Advance past the RSTn marker */
200 if (! (*cinfo->marker->read_restart_marker) (cinfo))
201 ERREXIT(cinfo, JERR_CANT_SUSPEND);
202
Guido Vollbeding989630f2010-01-10 00:00:00 +0000203 /* Re-initialize statistics areas */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000204 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
205 compptr = cinfo->cur_comp_info[ci];
Guido Vollbeding989630f2010-01-10 00:00:00 +0000206 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000207 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
208 /* Reset DC predictions to 0 */
209 entropy->last_dc_val[ci] = 0;
210 entropy->dc_context[ci] = 0;
211 }
DRC66f97e62010-11-23 05:49:54 +0000212 if (! cinfo->progressive_mode || cinfo->Ss) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000213 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
214 }
215 }
216
217 /* Reset arithmetic decoding variables */
218 entropy->c = 0;
219 entropy->a = 0;
DRCe5eaf372014-05-09 18:00:32 +0000220 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000221
222 /* Reset restart counter */
223 entropy->restarts_to_go = cinfo->restart_interval;
224}
225
226
227/*
228 * Arithmetic MCU decoding.
229 * Each of these routines decodes and returns one MCU's worth of
230 * arithmetic-compressed coefficients.
231 * The coefficients are reordered from zigzag order into natural array order,
232 * but are not dequantized.
233 *
234 * The i'th block of the MCU is stored into the block pointed to by
235 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
236 */
237
238/*
239 * MCU decoding for DC initial scan (either spectral selection,
240 * or first pass of successive approximation).
241 */
242
243METHODDEF(boolean)
244decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
245{
246 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
247 JBLOCKROW block;
248 unsigned char *st;
249 int blkn, ci, tbl, sign;
250 int v, m;
251
252 /* Process restart marker if needed */
253 if (cinfo->restart_interval) {
254 if (entropy->restarts_to_go == 0)
255 process_restart(cinfo);
256 entropy->restarts_to_go--;
257 }
258
DRCe5eaf372014-05-09 18:00:32 +0000259 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000260
261 /* Outer loop handles each block in the MCU */
262
263 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
264 block = MCU_data[blkn];
265 ci = cinfo->MCU_membership[blkn];
266 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
267
268 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
269
270 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
271 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
272
273 /* Figure F.19: Decode_DC_DIFF */
274 if (arith_decode(cinfo, st) == 0)
275 entropy->dc_context[ci] = 0;
276 else {
277 /* Figure F.21: Decoding nonzero value v */
278 /* Figure F.22: Decoding the sign of v */
279 sign = arith_decode(cinfo, st + 1);
280 st += 2; st += sign;
281 /* Figure F.23: Decoding the magnitude category of v */
282 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000283 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
284 while (arith_decode(cinfo, st)) {
285 if ((m <<= 1) == 0x8000) {
286 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
287 entropy->ct = -1; /* magnitude overflow */
288 return TRUE;
289 }
290 st += 1;
291 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000292 }
293 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000294 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000295 entropy->dc_context[ci] = 0; /* zero diff category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000296 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000297 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000298 else
DRCe5eaf372014-05-09 18:00:32 +0000299 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000300 v = m;
301 /* Figure F.24: Decoding the magnitude bit pattern of v */
302 st += 14;
303 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000304 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000305 v += 1; if (sign) v = -v;
306 entropy->last_dc_val[ci] += v;
307 }
308
309 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
DRC8e9cef22015-09-21 12:57:41 -0500310 (*block)[0] = (JCOEF) LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000311 }
312
313 return TRUE;
314}
315
316
317/*
318 * MCU decoding for AC initial scan (either spectral selection,
319 * or first pass of successive approximation).
320 */
321
322METHODDEF(boolean)
323decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
324{
325 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
326 JBLOCKROW block;
327 unsigned char *st;
328 int tbl, sign, k;
329 int v, m;
330
331 /* Process restart marker if needed */
332 if (cinfo->restart_interval) {
333 if (entropy->restarts_to_go == 0)
334 process_restart(cinfo);
335 entropy->restarts_to_go--;
336 }
337
DRCe5eaf372014-05-09 18:00:32 +0000338 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000339
340 /* There is always only one block per MCU */
341 block = MCU_data[0];
342 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
343
344 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
345
346 /* Figure F.20: Decode_AC_coefficients */
347 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
348 st = entropy->ac_stats[tbl] + 3 * (k - 1);
DRCe5eaf372014-05-09 18:00:32 +0000349 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000350 while (arith_decode(cinfo, st + 1) == 0) {
351 st += 3; k++;
352 if (k > cinfo->Se) {
DRCe5eaf372014-05-09 18:00:32 +0000353 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
354 entropy->ct = -1; /* spectral overflow */
355 return TRUE;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000356 }
357 }
358 /* Figure F.21: Decoding nonzero value v */
359 /* Figure F.22: Decoding the sign of v */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000360 sign = arith_decode(cinfo, entropy->fixed_bin);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000361 st += 2;
362 /* Figure F.23: Decoding the magnitude category of v */
363 if ((m = arith_decode(cinfo, st)) != 0) {
364 if (arith_decode(cinfo, st)) {
DRCe5eaf372014-05-09 18:00:32 +0000365 m <<= 1;
366 st = entropy->ac_stats[tbl] +
367 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
368 while (arith_decode(cinfo, st)) {
369 if ((m <<= 1) == 0x8000) {
370 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
371 entropy->ct = -1; /* magnitude overflow */
372 return TRUE;
373 }
374 st += 1;
375 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000376 }
377 }
378 v = m;
379 /* Figure F.24: Decoding the magnitude bit pattern of v */
380 st += 14;
381 while (m >>= 1)
382 if (arith_decode(cinfo, st)) v |= m;
383 v += 1; if (sign) v = -v;
384 /* Scale and output coefficient in natural (dezigzagged) order */
DRC66f97e62010-11-23 05:49:54 +0000385 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000386 }
387
388 return TRUE;
389}
390
391
392/*
393 * MCU decoding for DC successive approximation refinement scan.
394 */
395
396METHODDEF(boolean)
397decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
398{
399 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
Guido Vollbeding989630f2010-01-10 00:00:00 +0000400 unsigned char *st;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000401 int p1, blkn;
402
403 /* Process restart marker if needed */
404 if (cinfo->restart_interval) {
405 if (entropy->restarts_to_go == 0)
406 process_restart(cinfo);
407 entropy->restarts_to_go--;
408 }
409
DRCe5eaf372014-05-09 18:00:32 +0000410 st = entropy->fixed_bin; /* use fixed probability estimation */
411 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000412
413 /* Outer loop handles each block in the MCU */
414
415 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000416 /* Encoded data is simply the next bit of the two's-complement DC value */
417 if (arith_decode(cinfo, st))
418 MCU_data[blkn][0][0] |= p1;
419 }
420
421 return TRUE;
422}
423
424
425/*
426 * MCU decoding for AC successive approximation refinement scan.
427 */
428
429METHODDEF(boolean)
430decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
431{
432 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
433 JBLOCKROW block;
434 JCOEFPTR thiscoef;
435 unsigned char *st;
436 int tbl, k, kex;
437 int p1, m1;
438
439 /* Process restart marker if needed */
440 if (cinfo->restart_interval) {
441 if (entropy->restarts_to_go == 0)
442 process_restart(cinfo);
443 entropy->restarts_to_go--;
444 }
445
DRCe5eaf372014-05-09 18:00:32 +0000446 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000447
448 /* There is always only one block per MCU */
449 block = MCU_data[0];
450 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
451
DRCe5eaf372014-05-09 18:00:32 +0000452 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
453 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000454
455 /* Establish EOBx (previous stage end-of-block) index */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000456 for (kex = cinfo->Se; kex > 0; kex--)
DRC66f97e62010-11-23 05:49:54 +0000457 if ((*block)[jpeg_natural_order[kex]]) break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000458
459 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
460 st = entropy->ac_stats[tbl] + 3 * (k - 1);
Guido Vollbeding989630f2010-01-10 00:00:00 +0000461 if (k > kex)
DRCe5eaf372014-05-09 18:00:32 +0000462 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000463 for (;;) {
DRC66f97e62010-11-23 05:49:54 +0000464 thiscoef = *block + jpeg_natural_order[k];
DRCe5eaf372014-05-09 18:00:32 +0000465 if (*thiscoef) { /* previously nonzero coef */
466 if (arith_decode(cinfo, st + 2)) {
467 if (*thiscoef < 0)
468 *thiscoef += m1;
469 else
470 *thiscoef += p1;
471 }
472 break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000473 }
DRCe5eaf372014-05-09 18:00:32 +0000474 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
475 if (arith_decode(cinfo, entropy->fixed_bin))
476 *thiscoef = m1;
477 else
478 *thiscoef = p1;
479 break;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000480 }
481 st += 3; k++;
482 if (k > cinfo->Se) {
DRCe5eaf372014-05-09 18:00:32 +0000483 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
484 entropy->ct = -1; /* spectral overflow */
485 return TRUE;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000486 }
487 }
488 }
489
490 return TRUE;
491}
492
493
494/*
495 * Decode one MCU's worth of arithmetic-compressed coefficients.
496 */
497
498METHODDEF(boolean)
499decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
500{
501 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
502 jpeg_component_info * compptr;
503 JBLOCKROW block;
504 unsigned char *st;
505 int blkn, ci, tbl, sign, k;
506 int v, m;
507
508 /* Process restart marker if needed */
509 if (cinfo->restart_interval) {
510 if (entropy->restarts_to_go == 0)
511 process_restart(cinfo);
512 entropy->restarts_to_go--;
513 }
514
DRCe5eaf372014-05-09 18:00:32 +0000515 if (entropy->ct == -1) return TRUE; /* if error do nothing */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000516
517 /* Outer loop handles each block in the MCU */
518
519 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
DRCeb32cc12015-06-25 03:44:36 +0000520 block = MCU_data ? MCU_data[blkn] : NULL;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000521 ci = cinfo->MCU_membership[blkn];
522 compptr = cinfo->cur_comp_info[ci];
523
524 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
525
526 tbl = compptr->dc_tbl_no;
527
528 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
529 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
530
531 /* Figure F.19: Decode_DC_DIFF */
532 if (arith_decode(cinfo, st) == 0)
533 entropy->dc_context[ci] = 0;
534 else {
535 /* Figure F.21: Decoding nonzero value v */
536 /* Figure F.22: Decoding the sign of v */
537 sign = arith_decode(cinfo, st + 1);
538 st += 2; st += sign;
539 /* Figure F.23: Decoding the magnitude category of v */
540 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000541 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
542 while (arith_decode(cinfo, st)) {
543 if ((m <<= 1) == 0x8000) {
544 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
545 entropy->ct = -1; /* magnitude overflow */
546 return TRUE;
547 }
548 st += 1;
549 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000550 }
551 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000552 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000553 entropy->dc_context[ci] = 0; /* zero diff category */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000554 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
DRCe5eaf372014-05-09 18:00:32 +0000555 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000556 else
DRCe5eaf372014-05-09 18:00:32 +0000557 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000558 v = m;
559 /* Figure F.24: Decoding the magnitude bit pattern of v */
560 st += 14;
561 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000562 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000563 v += 1; if (sign) v = -v;
564 entropy->last_dc_val[ci] += v;
565 }
566
DRCeb32cc12015-06-25 03:44:36 +0000567 if (block)
568 (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000569
570 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
571
572 tbl = compptr->ac_tbl_no;
573
574 /* Figure F.20: Decode_AC_coefficients */
DRC66f97e62010-11-23 05:49:54 +0000575 for (k = 1; k <= DCTSIZE2 - 1; k++) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000576 st = entropy->ac_stats[tbl] + 3 * (k - 1);
DRCe5eaf372014-05-09 18:00:32 +0000577 if (arith_decode(cinfo, st)) break; /* EOB flag */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000578 while (arith_decode(cinfo, st + 1) == 0) {
DRCe5eaf372014-05-09 18:00:32 +0000579 st += 3; k++;
580 if (k > DCTSIZE2 - 1) {
581 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
582 entropy->ct = -1; /* spectral overflow */
583 return TRUE;
584 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000585 }
586 /* Figure F.21: Decoding nonzero value v */
587 /* Figure F.22: Decoding the sign of v */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000588 sign = arith_decode(cinfo, entropy->fixed_bin);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000589 st += 2;
590 /* Figure F.23: Decoding the magnitude category of v */
591 if ((m = arith_decode(cinfo, st)) != 0) {
DRCe5eaf372014-05-09 18:00:32 +0000592 if (arith_decode(cinfo, st)) {
593 m <<= 1;
594 st = entropy->ac_stats[tbl] +
595 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
596 while (arith_decode(cinfo, st)) {
597 if ((m <<= 1) == 0x8000) {
598 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
599 entropy->ct = -1; /* magnitude overflow */
600 return TRUE;
601 }
602 st += 1;
603 }
604 }
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000605 }
606 v = m;
607 /* Figure F.24: Decoding the magnitude bit pattern of v */
608 st += 14;
609 while (m >>= 1)
DRCe5eaf372014-05-09 18:00:32 +0000610 if (arith_decode(cinfo, st)) v |= m;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000611 v += 1; if (sign) v = -v;
DRCeb32cc12015-06-25 03:44:36 +0000612 if (block)
613 (*block)[jpeg_natural_order[k]] = (JCOEF) v;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000614 }
615 }
616
617 return TRUE;
618}
619
620
621/*
622 * Initialize for an arithmetic-compressed scan.
623 */
624
625METHODDEF(void)
626start_pass (j_decompress_ptr cinfo)
627{
628 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
629 int ci, tbl;
630 jpeg_component_info * compptr;
631
632 if (cinfo->progressive_mode) {
633 /* Validate progressive scan parameters */
634 if (cinfo->Ss == 0) {
635 if (cinfo->Se != 0)
DRCe5eaf372014-05-09 18:00:32 +0000636 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000637 } else {
638 /* need not check Ss/Se < 0 since they came from unsigned bytes */
DRC66f97e62010-11-23 05:49:54 +0000639 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
DRCe5eaf372014-05-09 18:00:32 +0000640 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000641 /* AC scans may have only one component */
642 if (cinfo->comps_in_scan != 1)
DRCe5eaf372014-05-09 18:00:32 +0000643 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000644 }
645 if (cinfo->Ah != 0) {
646 /* Successive approximation refinement scan: must have Al = Ah-1. */
647 if (cinfo->Ah-1 != cinfo->Al)
DRCe5eaf372014-05-09 18:00:32 +0000648 goto bad;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000649 }
DRCe5eaf372014-05-09 18:00:32 +0000650 if (cinfo->Al > 13) { /* need not check for < 0 */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000651 bad:
652 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
DRCe5eaf372014-05-09 18:00:32 +0000653 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000654 }
655 /* Update progression status, and verify that scan order is legal.
656 * Note that inter-scan inconsistencies are treated as warnings
657 * not fatal errors ... not clear if this is right way to behave.
658 */
659 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
660 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
661 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
662 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
DRCe5eaf372014-05-09 18:00:32 +0000663 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000664 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
DRCe5eaf372014-05-09 18:00:32 +0000665 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
666 if (cinfo->Ah != expected)
667 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
668 coef_bit_ptr[coefi] = cinfo->Al;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000669 }
670 }
671 /* Select MCU decoding routine */
672 if (cinfo->Ah == 0) {
673 if (cinfo->Ss == 0)
DRCe5eaf372014-05-09 18:00:32 +0000674 entropy->pub.decode_mcu = decode_mcu_DC_first;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000675 else
DRCe5eaf372014-05-09 18:00:32 +0000676 entropy->pub.decode_mcu = decode_mcu_AC_first;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000677 } else {
678 if (cinfo->Ss == 0)
DRCe5eaf372014-05-09 18:00:32 +0000679 entropy->pub.decode_mcu = decode_mcu_DC_refine;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000680 else
DRCe5eaf372014-05-09 18:00:32 +0000681 entropy->pub.decode_mcu = decode_mcu_AC_refine;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000682 }
683 } else {
684 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
Guido Vollbeding989630f2010-01-10 00:00:00 +0000685 * This ought to be an error condition, but we make it a warning.
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000686 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000687 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
DRCe5eaf372014-05-09 18:00:32 +0000688 (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000689 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
690 /* Select MCU decoding routine */
691 entropy->pub.decode_mcu = decode_mcu;
692 }
693
Guido Vollbeding989630f2010-01-10 00:00:00 +0000694 /* Allocate & initialize requested statistics areas */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000695 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
696 compptr = cinfo->cur_comp_info[ci];
Guido Vollbeding989630f2010-01-10 00:00:00 +0000697 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000698 tbl = compptr->dc_tbl_no;
699 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
DRCe5eaf372014-05-09 18:00:32 +0000700 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000701 if (entropy->dc_stats[tbl] == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000702 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
703 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000704 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
705 /* Initialize DC predictions to 0 */
706 entropy->last_dc_val[ci] = 0;
707 entropy->dc_context[ci] = 0;
708 }
DRC66f97e62010-11-23 05:49:54 +0000709 if (! cinfo->progressive_mode || cinfo->Ss) {
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000710 tbl = compptr->ac_tbl_no;
711 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
DRCe5eaf372014-05-09 18:00:32 +0000712 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000713 if (entropy->ac_stats[tbl] == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000714 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
715 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000716 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
717 }
718 }
719
720 /* Initialize arithmetic decoding variables */
721 entropy->c = 0;
722 entropy->a = 0;
DRCe5eaf372014-05-09 18:00:32 +0000723 entropy->ct = -16; /* force reading 2 initial bytes to fill C */
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000724
725 /* Initialize restart counter */
726 entropy->restarts_to_go = cinfo->restart_interval;
727}
728
729
730/*
731 * Module initialization routine for arithmetic entropy decoding.
732 */
733
734GLOBAL(void)
735jinit_arith_decoder (j_decompress_ptr cinfo)
736{
737 arith_entropy_ptr entropy;
738 int i;
739
740 entropy = (arith_entropy_ptr)
741 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000742 sizeof(arith_entropy_decoder));
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000743 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
744 entropy->pub.start_pass = start_pass;
745
746 /* Mark tables unallocated */
747 for (i = 0; i < NUM_ARITH_TBLS; i++) {
748 entropy->dc_stats[i] = NULL;
749 entropy->ac_stats[i] = NULL;
750 }
751
Guido Vollbeding989630f2010-01-10 00:00:00 +0000752 /* Initialize index for fixed probability estimation */
753 entropy->fixed_bin[0] = 113;
754
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000755 if (cinfo->progressive_mode) {
756 /* Create progression status table */
757 int *coef_bit_ptr, ci;
758 cinfo->coef_bits = (int (*)[DCTSIZE2])
759 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000760 cinfo->num_components*DCTSIZE2*sizeof(int));
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000761 coef_bit_ptr = & cinfo->coef_bits[0][0];
DRCe5eaf372014-05-09 18:00:32 +0000762 for (ci = 0; ci < cinfo->num_components; ci++)
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000763 for (i = 0; i < DCTSIZE2; i++)
DRCe5eaf372014-05-09 18:00:32 +0000764 *coef_bit_ptr++ = -1;
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000765 }
766}