ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

jdarith.c
Go to the documentation of this file.
00001 /*
00002  * jdarith.c
00003  *
00004  * Developed 1997-2009 by Guido Vollbeding.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains portable arithmetic entropy decoding routines for JPEG
00009  * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
00010  *
00011  * Both sequential and progressive modes are supported in this single module.
00012  *
00013  * Suspension is not currently supported in this module.
00014  */
00015 
00016 #define JPEG_INTERNALS
00017 #include "jinclude.h"
00018 #include "jpeglib.h"
00019 
00020 
00021 /* Expanded entropy decoder object for arithmetic decoding. */
00022 
00023 typedef struct {
00024   struct jpeg_entropy_decoder pub; /* public fields */
00025 
00026   INT32 c;       /* C register, base of coding interval + input bit buffer */
00027   INT32 a;               /* A register, normalized size of coding interval */
00028   int ct;     /* bit shift counter, # of bits left in bit buffer part of C */
00029                                                          /* init: ct = -16 */
00030                                                          /* run: ct = 0..7 */
00031                                                          /* error: ct = -1 */
00032   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
00033   int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
00034 
00035   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
00036 
00037   /* Pointers to statistics areas (these workspaces have image lifespan) */
00038   unsigned char * dc_stats[NUM_ARITH_TBLS];
00039   unsigned char * ac_stats[NUM_ARITH_TBLS];
00040 
00041   /* Statistics bin for coding with fixed probability 0.5 */
00042   unsigned char fixed_bin[4];
00043 } arith_entropy_decoder;
00044 
00045 typedef arith_entropy_decoder * arith_entropy_ptr;
00046 
00047 /* The following two definitions specify the allocation chunk size
00048  * for the statistics area.
00049  * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
00050  * 49 statistics bins for DC, and 245 statistics bins for AC coding.
00051  *
00052  * We use a compact representation with 1 byte per statistics bin,
00053  * thus the numbers directly represent byte sizes.
00054  * This 1 byte per statistics bin contains the meaning of the MPS
00055  * (more probable symbol) in the highest bit (mask 0x80), and the
00056  * index into the probability estimation state machine table
00057  * in the lower bits (mask 0x7F).
00058  */
00059 
00060 #define DC_STAT_BINS 64
00061 #define AC_STAT_BINS 256
00062 
00063 
00064 LOCAL(int)
00065 get_byte (j_decompress_ptr cinfo)
00066 /* Read next input byte; we do not support suspension in this module. */
00067 {
00068   struct jpeg_source_mgr * src = cinfo->src;
00069 
00070   if (src->bytes_in_buffer == 0)
00071     if (! (*src->fill_input_buffer) (cinfo))
00072       ERREXIT(cinfo, JERR_CANT_SUSPEND);
00073   src->bytes_in_buffer--;
00074   return GETJOCTET(*src->next_input_byte++);
00075 }
00076 
00077 
00078 /*
00079  * The core arithmetic decoding routine (common in JPEG and JBIG).
00080  * This needs to go as fast as possible.
00081  * Machine-dependent optimization facilities
00082  * are not utilized in this portable implementation.
00083  * However, this code should be fairly efficient and
00084  * may be a good base for further optimizations anyway.
00085  *
00086  * Return value is 0 or 1 (binary decision).
00087  *
00088  * Note: I've changed the handling of the code base & bit
00089  * buffer register C compared to other implementations
00090  * based on the standards layout & procedures.
00091  * While it also contains both the actual base of the
00092  * coding interval (16 bits) and the next-bits buffer,
00093  * the cut-point between these two parts is floating
00094  * (instead of fixed) with the bit shift counter CT.
00095  * Thus, we also need only one (variable instead of
00096  * fixed size) shift for the LPS/MPS decision, and
00097  * we can get away with any renormalization update
00098  * of C (except for new data insertion, of course).
00099  *
00100  * I've also introduced a new scheme for accessing
00101  * the probability estimation state machine table,
00102  * derived from Markus Kuhn's JBIG implementation.
00103  */
00104 
00105 LOCAL(int)
00106 arith_decode (j_decompress_ptr cinfo, unsigned char *st)
00107 {
00108   register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
00109   register unsigned char nl, nm;
00110   register INT32 qe, temp;
00111   register int sv, data;
00112 
00113   /* Renormalization & data input per section D.2.6 */
00114   while (e->a < 0x8000L) {
00115     if (--e->ct < 0) {
00116       /* Need to fetch next data byte */
00117       if (cinfo->unread_marker)
00118     data = 0;       /* stuff zero data */
00119       else {
00120     data = get_byte(cinfo); /* read next input byte */
00121     if (data == 0xFF) { /* zero stuff or marker code */
00122       do data = get_byte(cinfo);
00123       while (data == 0xFF); /* swallow extra 0xFF bytes */
00124       if (data == 0)
00125         data = 0xFF;    /* discard stuffed zero byte */
00126       else {
00127         /* Note: Different from the Huffman decoder, hitting
00128          * a marker while processing the compressed data
00129          * segment is legal in arithmetic coding.
00130          * The convention is to supply zero data
00131          * then until decoding is complete.
00132          */
00133         cinfo->unread_marker = data;
00134         data = 0;
00135       }
00136     }
00137       }
00138       e->c = (e->c << 8) | data; /* insert data into C register */
00139       if ((e->ct += 8) < 0)  /* update bit shift counter */
00140     /* Need more initial bytes */
00141     if (++e->ct == 0)
00142       /* Got 2 initial bytes -> re-init A and exit loop */
00143       e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
00144     }
00145     e->a <<= 1;
00146   }
00147 
00148   /* Fetch values from our compact representation of Table D.2:
00149    * Qe values and probability estimation state machine
00150    */
00151   sv = *st;
00152   qe = jpeg_aritab[sv & 0x7F];  /* => Qe_Value */
00153   nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
00154   nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
00155 
00156   /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
00157   temp = e->a - qe;
00158   e->a = temp;
00159   temp <<= e->ct;
00160   if (e->c >= temp) {
00161     e->c -= temp;
00162     /* Conditional LPS (less probable symbol) exchange */
00163     if (e->a < qe) {
00164       e->a = qe;
00165       *st = (sv & 0x80) ^ nm;   /* Estimate_after_MPS */
00166     } else {
00167       e->a = qe;
00168       *st = (sv & 0x80) ^ nl;   /* Estimate_after_LPS */
00169       sv ^= 0x80;       /* Exchange LPS/MPS */
00170     }
00171   } else if (e->a < 0x8000L) {
00172     /* Conditional MPS (more probable symbol) exchange */
00173     if (e->a < qe) {
00174       *st = (sv & 0x80) ^ nl;   /* Estimate_after_LPS */
00175       sv ^= 0x80;       /* Exchange LPS/MPS */
00176     } else {
00177       *st = (sv & 0x80) ^ nm;   /* Estimate_after_MPS */
00178     }
00179   }
00180 
00181   return sv >> 7;
00182 }
00183 
00184 
00185 /*
00186  * Check for a restart marker & resynchronize decoder.
00187  */
00188 
00189 LOCAL(void)
00190 process_restart (j_decompress_ptr cinfo)
00191 {
00192   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00193   int ci;
00194   jpeg_component_info * compptr;
00195 
00196   /* Advance past the RSTn marker */
00197   if (! (*cinfo->marker->read_restart_marker) (cinfo))
00198     ERREXIT(cinfo, JERR_CANT_SUSPEND);
00199 
00200   /* Re-initialize statistics areas */
00201   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00202     compptr = cinfo->cur_comp_info[ci];
00203     if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
00204       MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
00205       /* Reset DC predictions to 0 */
00206       entropy->last_dc_val[ci] = 0;
00207       entropy->dc_context[ci] = 0;
00208     }
00209     if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
00210     (cinfo->progressive_mode && cinfo->Ss)) {
00211       MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
00212     }
00213   }
00214 
00215   /* Reset arithmetic decoding variables */
00216   entropy->c = 0;
00217   entropy->a = 0;
00218   entropy->ct = -16;    /* force reading 2 initial bytes to fill C */
00219 
00220   /* Reset restart counter */
00221   entropy->restarts_to_go = cinfo->restart_interval;
00222 }
00223 
00224 
00225 /*
00226  * Arithmetic MCU decoding.
00227  * Each of these routines decodes and returns one MCU's worth of
00228  * arithmetic-compressed coefficients.
00229  * The coefficients are reordered from zigzag order into natural array order,
00230  * but are not dequantized.
00231  *
00232  * The i'th block of the MCU is stored into the block pointed to by
00233  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
00234  */
00235 
00236 /*
00237  * MCU decoding for DC initial scan (either spectral selection,
00238  * or first pass of successive approximation).
00239  */
00240 
00241 METHODDEF(boolean)
00242 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00243 {
00244   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00245   JBLOCKROW block;
00246   unsigned char *st;
00247   int blkn, ci, tbl, sign;
00248   int v, m;
00249 
00250   /* Process restart marker if needed */
00251   if (cinfo->restart_interval) {
00252     if (entropy->restarts_to_go == 0)
00253       process_restart(cinfo);
00254     entropy->restarts_to_go--;
00255   }
00256 
00257   if (entropy->ct == -1) return TRUE;   /* if error do nothing */
00258 
00259   /* Outer loop handles each block in the MCU */
00260 
00261   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00262     block = MCU_data[blkn];
00263     ci = cinfo->MCU_membership[blkn];
00264     tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
00265 
00266     /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
00267 
00268     /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
00269     st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
00270 
00271     /* Figure F.19: Decode_DC_DIFF */
00272     if (arith_decode(cinfo, st) == 0)
00273       entropy->dc_context[ci] = 0;
00274     else {
00275       /* Figure F.21: Decoding nonzero value v */
00276       /* Figure F.22: Decoding the sign of v */
00277       sign = arith_decode(cinfo, st + 1);
00278       st += 2; st += sign;
00279       /* Figure F.23: Decoding the magnitude category of v */
00280       if ((m = arith_decode(cinfo, st)) != 0) {
00281     st = entropy->dc_stats[tbl] + 20;   /* Table F.4: X1 = 20 */
00282     while (arith_decode(cinfo, st)) {
00283       if ((m <<= 1) == 0x8000) {
00284         WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00285         entropy->ct = -1;           /* magnitude overflow */
00286         return TRUE;
00287       }
00288       st += 1;
00289     }
00290       }
00291       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
00292       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
00293     entropy->dc_context[ci] = 0;           /* zero diff category */
00294       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
00295     entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
00296       else
00297     entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
00298       v = m;
00299       /* Figure F.24: Decoding the magnitude bit pattern of v */
00300       st += 14;
00301       while (m >>= 1)
00302     if (arith_decode(cinfo, st)) v |= m;
00303       v += 1; if (sign) v = -v;
00304       entropy->last_dc_val[ci] += v;
00305     }
00306 
00307     /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
00308     (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
00309   }
00310 
00311   return TRUE;
00312 }
00313 
00314 
00315 /*
00316  * MCU decoding for AC initial scan (either spectral selection,
00317  * or first pass of successive approximation).
00318  */
00319 
00320 METHODDEF(boolean)
00321 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00322 {
00323   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00324   JBLOCKROW block;
00325   unsigned char *st;
00326   int tbl, sign, k;
00327   int v, m;
00328   const int * natural_order;
00329 
00330   /* Process restart marker if needed */
00331   if (cinfo->restart_interval) {
00332     if (entropy->restarts_to_go == 0)
00333       process_restart(cinfo);
00334     entropy->restarts_to_go--;
00335   }
00336 
00337   if (entropy->ct == -1) return TRUE;   /* if error do nothing */
00338 
00339   natural_order = cinfo->natural_order;
00340 
00341   /* There is always only one block per MCU */
00342   block = MCU_data[0];
00343   tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
00344 
00345   /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
00346 
00347   /* Figure F.20: Decode_AC_coefficients */
00348   for (k = cinfo->Ss; k <= cinfo->Se; k++) {
00349     st = entropy->ac_stats[tbl] + 3 * (k - 1);
00350     if (arith_decode(cinfo, st)) break;     /* EOB flag */
00351     while (arith_decode(cinfo, st + 1) == 0) {
00352       st += 3; k++;
00353       if (k > cinfo->Se) {
00354     WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00355     entropy->ct = -1;           /* spectral overflow */
00356     return TRUE;
00357       }
00358     }
00359     /* Figure F.21: Decoding nonzero value v */
00360     /* Figure F.22: Decoding the sign of v */
00361     sign = arith_decode(cinfo, entropy->fixed_bin);
00362     st += 2;
00363     /* Figure F.23: Decoding the magnitude category of v */
00364     if ((m = arith_decode(cinfo, st)) != 0) {
00365       if (arith_decode(cinfo, st)) {
00366     m <<= 1;
00367     st = entropy->ac_stats[tbl] +
00368          (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
00369     while (arith_decode(cinfo, st)) {
00370       if ((m <<= 1) == 0x8000) {
00371         WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00372         entropy->ct = -1;           /* magnitude overflow */
00373         return TRUE;
00374       }
00375       st += 1;
00376     }
00377       }
00378     }
00379     v = m;
00380     /* Figure F.24: Decoding the magnitude bit pattern of v */
00381     st += 14;
00382     while (m >>= 1)
00383       if (arith_decode(cinfo, st)) v |= m;
00384     v += 1; if (sign) v = -v;
00385     /* Scale and output coefficient in natural (dezigzagged) order */
00386     (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
00387   }
00388 
00389   return TRUE;
00390 }
00391 
00392 
00393 /*
00394  * MCU decoding for DC successive approximation refinement scan.
00395  */
00396 
00397 METHODDEF(boolean)
00398 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00399 {
00400   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00401   unsigned char *st;
00402   int p1, blkn;
00403 
00404   /* Process restart marker if needed */
00405   if (cinfo->restart_interval) {
00406     if (entropy->restarts_to_go == 0)
00407       process_restart(cinfo);
00408     entropy->restarts_to_go--;
00409   }
00410 
00411   st = entropy->fixed_bin;  /* use fixed probability estimation */
00412   p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
00413 
00414   /* Outer loop handles each block in the MCU */
00415 
00416   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00417     /* Encoded data is simply the next bit of the two's-complement DC value */
00418     if (arith_decode(cinfo, st))
00419       MCU_data[blkn][0][0] |= p1;
00420   }
00421 
00422   return TRUE;
00423 }
00424 
00425 
00426 /*
00427  * MCU decoding for AC successive approximation refinement scan.
00428  */
00429 
00430 METHODDEF(boolean)
00431 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00432 {
00433   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00434   JBLOCKROW block;
00435   JCOEFPTR thiscoef;
00436   unsigned char *st;
00437   int tbl, k, kex;
00438   int p1, m1;
00439   const int * natural_order;
00440 
00441   /* Process restart marker if needed */
00442   if (cinfo->restart_interval) {
00443     if (entropy->restarts_to_go == 0)
00444       process_restart(cinfo);
00445     entropy->restarts_to_go--;
00446   }
00447 
00448   if (entropy->ct == -1) return TRUE;   /* if error do nothing */
00449 
00450   natural_order = cinfo->natural_order;
00451 
00452   /* There is always only one block per MCU */
00453   block = MCU_data[0];
00454   tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
00455 
00456   p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
00457   m1 = (-1) << cinfo->Al;   /* -1 in the bit position being coded */
00458 
00459   /* Establish EOBx (previous stage end-of-block) index */
00460   for (kex = cinfo->Se; kex > 0; kex--)
00461     if ((*block)[natural_order[kex]]) break;
00462 
00463   for (k = cinfo->Ss; k <= cinfo->Se; k++) {
00464     st = entropy->ac_stats[tbl] + 3 * (k - 1);
00465     if (k > kex)
00466       if (arith_decode(cinfo, st)) break;   /* EOB flag */
00467     for (;;) {
00468       thiscoef = *block + natural_order[k];
00469       if (*thiscoef) {              /* previously nonzero coef */
00470     if (arith_decode(cinfo, st + 2)) {
00471       if (*thiscoef < 0)
00472         *thiscoef += m1;
00473       else
00474         *thiscoef += p1;
00475     }
00476     break;
00477       }
00478       if (arith_decode(cinfo, st + 1)) {    /* newly nonzero coef */
00479     if (arith_decode(cinfo, entropy->fixed_bin))
00480       *thiscoef = m1;
00481     else
00482       *thiscoef = p1;
00483     break;
00484       }
00485       st += 3; k++;
00486       if (k > cinfo->Se) {
00487     WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00488     entropy->ct = -1;           /* spectral overflow */
00489     return TRUE;
00490       }
00491     }
00492   }
00493 
00494   return TRUE;
00495 }
00496 
00497 
00498 /*
00499  * Decode one MCU's worth of arithmetic-compressed coefficients.
00500  */
00501 
00502 METHODDEF(boolean)
00503 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00504 {
00505   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00506   jpeg_component_info * compptr;
00507   JBLOCKROW block;
00508   unsigned char *st;
00509   int blkn, ci, tbl, sign, k;
00510   int v, m;
00511   const int * natural_order;
00512 
00513   /* Process restart marker if needed */
00514   if (cinfo->restart_interval) {
00515     if (entropy->restarts_to_go == 0)
00516       process_restart(cinfo);
00517     entropy->restarts_to_go--;
00518   }
00519 
00520   if (entropy->ct == -1) return TRUE;   /* if error do nothing */
00521 
00522   natural_order = cinfo->natural_order;
00523 
00524   /* Outer loop handles each block in the MCU */
00525 
00526   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00527     block = MCU_data[blkn];
00528     ci = cinfo->MCU_membership[blkn];
00529     compptr = cinfo->cur_comp_info[ci];
00530 
00531     /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
00532 
00533     tbl = compptr->dc_tbl_no;
00534 
00535     /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
00536     st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
00537 
00538     /* Figure F.19: Decode_DC_DIFF */
00539     if (arith_decode(cinfo, st) == 0)
00540       entropy->dc_context[ci] = 0;
00541     else {
00542       /* Figure F.21: Decoding nonzero value v */
00543       /* Figure F.22: Decoding the sign of v */
00544       sign = arith_decode(cinfo, st + 1);
00545       st += 2; st += sign;
00546       /* Figure F.23: Decoding the magnitude category of v */
00547       if ((m = arith_decode(cinfo, st)) != 0) {
00548     st = entropy->dc_stats[tbl] + 20;   /* Table F.4: X1 = 20 */
00549     while (arith_decode(cinfo, st)) {
00550       if ((m <<= 1) == 0x8000) {
00551         WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00552         entropy->ct = -1;           /* magnitude overflow */
00553         return TRUE;
00554       }
00555       st += 1;
00556     }
00557       }
00558       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
00559       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
00560     entropy->dc_context[ci] = 0;           /* zero diff category */
00561       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
00562     entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
00563       else
00564     entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
00565       v = m;
00566       /* Figure F.24: Decoding the magnitude bit pattern of v */
00567       st += 14;
00568       while (m >>= 1)
00569     if (arith_decode(cinfo, st)) v |= m;
00570       v += 1; if (sign) v = -v;
00571       entropy->last_dc_val[ci] += v;
00572     }
00573 
00574     (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
00575 
00576     /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
00577 
00578     tbl = compptr->ac_tbl_no;
00579 
00580     /* Figure F.20: Decode_AC_coefficients */
00581     for (k = 1; k <= cinfo->lim_Se; k++) {
00582       st = entropy->ac_stats[tbl] + 3 * (k - 1);
00583       if (arith_decode(cinfo, st)) break;   /* EOB flag */
00584       while (arith_decode(cinfo, st + 1) == 0) {
00585     st += 3; k++;
00586     if (k > cinfo->lim_Se) {
00587       WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00588       entropy->ct = -1;         /* spectral overflow */
00589       return TRUE;
00590     }
00591       }
00592       /* Figure F.21: Decoding nonzero value v */
00593       /* Figure F.22: Decoding the sign of v */
00594       sign = arith_decode(cinfo, entropy->fixed_bin);
00595       st += 2;
00596       /* Figure F.23: Decoding the magnitude category of v */
00597       if ((m = arith_decode(cinfo, st)) != 0) {
00598     if (arith_decode(cinfo, st)) {
00599       m <<= 1;
00600       st = entropy->ac_stats[tbl] +
00601            (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
00602       while (arith_decode(cinfo, st)) {
00603         if ((m <<= 1) == 0x8000) {
00604           WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00605           entropy->ct = -1;         /* magnitude overflow */
00606           return TRUE;
00607         }
00608         st += 1;
00609       }
00610     }
00611       }
00612       v = m;
00613       /* Figure F.24: Decoding the magnitude bit pattern of v */
00614       st += 14;
00615       while (m >>= 1)
00616     if (arith_decode(cinfo, st)) v |= m;
00617       v += 1; if (sign) v = -v;
00618       (*block)[natural_order[k]] = (JCOEF) v;
00619     }
00620   }
00621 
00622   return TRUE;
00623 }
00624 
00625 
00626 /*
00627  * Initialize for an arithmetic-compressed scan.
00628  */
00629 
00630 METHODDEF(void)
00631 start_pass (j_decompress_ptr cinfo)
00632 {
00633   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00634   int ci, tbl;
00635   jpeg_component_info * compptr;
00636 
00637   if (cinfo->progressive_mode) {
00638     /* Validate progressive scan parameters */
00639     if (cinfo->Ss == 0) {
00640       if (cinfo->Se != 0)
00641     goto bad;
00642     } else {
00643       /* need not check Ss/Se < 0 since they came from unsigned bytes */
00644       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
00645     goto bad;
00646       /* AC scans may have only one component */
00647       if (cinfo->comps_in_scan != 1)
00648     goto bad;
00649     }
00650     if (cinfo->Ah != 0) {
00651       /* Successive approximation refinement scan: must have Al = Ah-1. */
00652       if (cinfo->Ah-1 != cinfo->Al)
00653     goto bad;
00654     }
00655     if (cinfo->Al > 13) {   /* need not check for < 0 */
00656       bad:
00657       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
00658            cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
00659     }
00660     /* Update progression status, and verify that scan order is legal.
00661      * Note that inter-scan inconsistencies are treated as warnings
00662      * not fatal errors ... not clear if this is right way to behave.
00663      */
00664     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00665       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
00666       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
00667       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
00668     WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
00669       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
00670     int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
00671     if (cinfo->Ah != expected)
00672       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
00673     coef_bit_ptr[coefi] = cinfo->Al;
00674       }
00675     }
00676     /* Select MCU decoding routine */
00677     if (cinfo->Ah == 0) {
00678       if (cinfo->Ss == 0)
00679     entropy->pub.decode_mcu = decode_mcu_DC_first;
00680       else
00681     entropy->pub.decode_mcu = decode_mcu_AC_first;
00682     } else {
00683       if (cinfo->Ss == 0)
00684     entropy->pub.decode_mcu = decode_mcu_DC_refine;
00685       else
00686     entropy->pub.decode_mcu = decode_mcu_AC_refine;
00687     }
00688   } else {
00689     /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
00690      * This ought to be an error condition, but we make it a warning.
00691      */
00692     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
00693     (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
00694       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
00695     /* Select MCU decoding routine */
00696     entropy->pub.decode_mcu = decode_mcu;
00697   }
00698 
00699   /* Allocate & initialize requested statistics areas */
00700   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00701     compptr = cinfo->cur_comp_info[ci];
00702     if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
00703       tbl = compptr->dc_tbl_no;
00704       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
00705     ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
00706       if (entropy->dc_stats[tbl] == NULL)
00707     entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
00708       ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
00709       MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
00710       /* Initialize DC predictions to 0 */
00711       entropy->last_dc_val[ci] = 0;
00712       entropy->dc_context[ci] = 0;
00713     }
00714     if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
00715     (cinfo->progressive_mode && cinfo->Ss)) {
00716       tbl = compptr->ac_tbl_no;
00717       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
00718     ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
00719       if (entropy->ac_stats[tbl] == NULL)
00720     entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
00721       ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
00722       MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
00723     }
00724   }
00725 
00726   /* Initialize arithmetic decoding variables */
00727   entropy->c = 0;
00728   entropy->a = 0;
00729   entropy->ct = -16;    /* force reading 2 initial bytes to fill C */
00730 
00731   /* Initialize restart counter */
00732   entropy->restarts_to_go = cinfo->restart_interval;
00733 }
00734 
00735 
00736 /*
00737  * Module initialization routine for arithmetic entropy decoding.
00738  */
00739 
00740 GLOBAL(void)
00741 jinit_arith_decoder (j_decompress_ptr cinfo)
00742 {
00743   arith_entropy_ptr entropy;
00744   int i;
00745 
00746   entropy = (arith_entropy_ptr)
00747     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00748                 SIZEOF(arith_entropy_decoder));
00749   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
00750   entropy->pub.start_pass = start_pass;
00751 
00752   /* Mark tables unallocated */
00753   for (i = 0; i < NUM_ARITH_TBLS; i++) {
00754     entropy->dc_stats[i] = NULL;
00755     entropy->ac_stats[i] = NULL;
00756   }
00757 
00758   /* Initialize index for fixed probability estimation */
00759   entropy->fixed_bin[0] = 113;
00760 
00761   if (cinfo->progressive_mode) {
00762     /* Create progression status table */
00763     int *coef_bit_ptr, ci;
00764     cinfo->coef_bits = (int (*)[DCTSIZE2])
00765       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00766                   cinfo->num_components*DCTSIZE2*SIZEOF(int));
00767     coef_bit_ptr = & cinfo->coef_bits[0][0];
00768     for (ci = 0; ci < cinfo->num_components; ci++) 
00769       for (i = 0; i < DCTSIZE2; i++)
00770     *coef_bit_ptr++ = -1;
00771   }
00772 }

Generated on Sun May 27 2012 04:19:24 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.