OSDN Git Service

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