media/libjpeg/jdarith.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdarith.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,761 @@
     1.4 +/*
     1.5 + * jdarith.c
     1.6 + *
     1.7 + * Developed 1997-2009 by Guido Vollbeding.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains portable arithmetic entropy decoding routines for JPEG
    1.12 + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
    1.13 + *
    1.14 + * Both sequential and progressive modes are supported in this single module.
    1.15 + *
    1.16 + * Suspension is not currently supported in this module.
    1.17 + */
    1.18 +
    1.19 +#define JPEG_INTERNALS
    1.20 +#include "jinclude.h"
    1.21 +#include "jpeglib.h"
    1.22 +
    1.23 +
    1.24 +/* Expanded entropy decoder object for arithmetic decoding. */
    1.25 +
    1.26 +typedef struct {
    1.27 +  struct jpeg_entropy_decoder pub; /* public fields */
    1.28 +
    1.29 +  INT32 c;       /* C register, base of coding interval + input bit buffer */
    1.30 +  INT32 a;               /* A register, normalized size of coding interval */
    1.31 +  int ct;     /* bit shift counter, # of bits left in bit buffer part of C */
    1.32 +                                                         /* init: ct = -16 */
    1.33 +                                                         /* run: ct = 0..7 */
    1.34 +                                                         /* error: ct = -1 */
    1.35 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
    1.36 +  int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
    1.37 +
    1.38 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
    1.39 +
    1.40 +  /* Pointers to statistics areas (these workspaces have image lifespan) */
    1.41 +  unsigned char * dc_stats[NUM_ARITH_TBLS];
    1.42 +  unsigned char * ac_stats[NUM_ARITH_TBLS];
    1.43 +
    1.44 +  /* Statistics bin for coding with fixed probability 0.5 */
    1.45 +  unsigned char fixed_bin[4];
    1.46 +} arith_entropy_decoder;
    1.47 +
    1.48 +typedef arith_entropy_decoder * arith_entropy_ptr;
    1.49 +
    1.50 +/* The following two definitions specify the allocation chunk size
    1.51 + * for the statistics area.
    1.52 + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
    1.53 + * 49 statistics bins for DC, and 245 statistics bins for AC coding.
    1.54 + *
    1.55 + * We use a compact representation with 1 byte per statistics bin,
    1.56 + * thus the numbers directly represent byte sizes.
    1.57 + * This 1 byte per statistics bin contains the meaning of the MPS
    1.58 + * (more probable symbol) in the highest bit (mask 0x80), and the
    1.59 + * index into the probability estimation state machine table
    1.60 + * in the lower bits (mask 0x7F).
    1.61 + */
    1.62 +
    1.63 +#define DC_STAT_BINS 64
    1.64 +#define AC_STAT_BINS 256
    1.65 +
    1.66 +
    1.67 +LOCAL(int)
    1.68 +get_byte (j_decompress_ptr cinfo)
    1.69 +/* Read next input byte; we do not support suspension in this module. */
    1.70 +{
    1.71 +  struct jpeg_source_mgr * src = cinfo->src;
    1.72 +
    1.73 +  if (src->bytes_in_buffer == 0)
    1.74 +    if (! (*src->fill_input_buffer) (cinfo))
    1.75 +      ERREXIT(cinfo, JERR_CANT_SUSPEND);
    1.76 +  src->bytes_in_buffer--;
    1.77 +  return GETJOCTET(*src->next_input_byte++);
    1.78 +}
    1.79 +
    1.80 +
    1.81 +/*
    1.82 + * The core arithmetic decoding routine (common in JPEG and JBIG).
    1.83 + * This needs to go as fast as possible.
    1.84 + * Machine-dependent optimization facilities
    1.85 + * are not utilized in this portable implementation.
    1.86 + * However, this code should be fairly efficient and
    1.87 + * may be a good base for further optimizations anyway.
    1.88 + *
    1.89 + * Return value is 0 or 1 (binary decision).
    1.90 + *
    1.91 + * Note: I've changed the handling of the code base & bit
    1.92 + * buffer register C compared to other implementations
    1.93 + * based on the standards layout & procedures.
    1.94 + * While it also contains both the actual base of the
    1.95 + * coding interval (16 bits) and the next-bits buffer,
    1.96 + * the cut-point between these two parts is floating
    1.97 + * (instead of fixed) with the bit shift counter CT.
    1.98 + * Thus, we also need only one (variable instead of
    1.99 + * fixed size) shift for the LPS/MPS decision, and
   1.100 + * we can get away with any renormalization update
   1.101 + * of C (except for new data insertion, of course).
   1.102 + *
   1.103 + * I've also introduced a new scheme for accessing
   1.104 + * the probability estimation state machine table,
   1.105 + * derived from Markus Kuhn's JBIG implementation.
   1.106 + */
   1.107 +
   1.108 +LOCAL(int)
   1.109 +arith_decode (j_decompress_ptr cinfo, unsigned char *st)
   1.110 +{
   1.111 +  register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
   1.112 +  register unsigned char nl, nm;
   1.113 +  register INT32 qe, temp;
   1.114 +  register int sv, data;
   1.115 +
   1.116 +  /* Renormalization & data input per section D.2.6 */
   1.117 +  while (e->a < 0x8000L) {
   1.118 +    if (--e->ct < 0) {
   1.119 +      /* Need to fetch next data byte */
   1.120 +      if (cinfo->unread_marker)
   1.121 +	data = 0;		/* stuff zero data */
   1.122 +      else {
   1.123 +	data = get_byte(cinfo);	/* read next input byte */
   1.124 +	if (data == 0xFF) {	/* zero stuff or marker code */
   1.125 +	  do data = get_byte(cinfo);
   1.126 +	  while (data == 0xFF);	/* swallow extra 0xFF bytes */
   1.127 +	  if (data == 0)
   1.128 +	    data = 0xFF;	/* discard stuffed zero byte */
   1.129 +	  else {
   1.130 +	    /* Note: Different from the Huffman decoder, hitting
   1.131 +	     * a marker while processing the compressed data
   1.132 +	     * segment is legal in arithmetic coding.
   1.133 +	     * The convention is to supply zero data
   1.134 +	     * then until decoding is complete.
   1.135 +	     */
   1.136 +	    cinfo->unread_marker = data;
   1.137 +	    data = 0;
   1.138 +	  }
   1.139 +	}
   1.140 +      }
   1.141 +      e->c = (e->c << 8) | data; /* insert data into C register */
   1.142 +      if ((e->ct += 8) < 0)	 /* update bit shift counter */
   1.143 +	/* Need more initial bytes */
   1.144 +	if (++e->ct == 0)
   1.145 +	  /* Got 2 initial bytes -> re-init A and exit loop */
   1.146 +	  e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
   1.147 +    }
   1.148 +    e->a <<= 1;
   1.149 +  }
   1.150 +
   1.151 +  /* Fetch values from our compact representation of Table D.2:
   1.152 +   * Qe values and probability estimation state machine
   1.153 +   */
   1.154 +  sv = *st;
   1.155 +  qe = jpeg_aritab[sv & 0x7F];	/* => Qe_Value */
   1.156 +  nl = qe & 0xFF; qe >>= 8;	/* Next_Index_LPS + Switch_MPS */
   1.157 +  nm = qe & 0xFF; qe >>= 8;	/* Next_Index_MPS */
   1.158 +
   1.159 +  /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
   1.160 +  temp = e->a - qe;
   1.161 +  e->a = temp;
   1.162 +  temp <<= e->ct;
   1.163 +  if (e->c >= temp) {
   1.164 +    e->c -= temp;
   1.165 +    /* Conditional LPS (less probable symbol) exchange */
   1.166 +    if (e->a < qe) {
   1.167 +      e->a = qe;
   1.168 +      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
   1.169 +    } else {
   1.170 +      e->a = qe;
   1.171 +      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
   1.172 +      sv ^= 0x80;		/* Exchange LPS/MPS */
   1.173 +    }
   1.174 +  } else if (e->a < 0x8000L) {
   1.175 +    /* Conditional MPS (more probable symbol) exchange */
   1.176 +    if (e->a < qe) {
   1.177 +      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
   1.178 +      sv ^= 0x80;		/* Exchange LPS/MPS */
   1.179 +    } else {
   1.180 +      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
   1.181 +    }
   1.182 +  }
   1.183 +
   1.184 +  return sv >> 7;
   1.185 +}
   1.186 +
   1.187 +
   1.188 +/*
   1.189 + * Check for a restart marker & resynchronize decoder.
   1.190 + */
   1.191 +
   1.192 +LOCAL(void)
   1.193 +process_restart (j_decompress_ptr cinfo)
   1.194 +{
   1.195 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.196 +  int ci;
   1.197 +  jpeg_component_info * compptr;
   1.198 +
   1.199 +  /* Advance past the RSTn marker */
   1.200 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
   1.201 +    ERREXIT(cinfo, JERR_CANT_SUSPEND);
   1.202 +
   1.203 +  /* Re-initialize statistics areas */
   1.204 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.205 +    compptr = cinfo->cur_comp_info[ci];
   1.206 +    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
   1.207 +      MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
   1.208 +      /* Reset DC predictions to 0 */
   1.209 +      entropy->last_dc_val[ci] = 0;
   1.210 +      entropy->dc_context[ci] = 0;
   1.211 +    }
   1.212 +    if (! cinfo->progressive_mode || cinfo->Ss) {
   1.213 +      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
   1.214 +    }
   1.215 +  }
   1.216 +
   1.217 +  /* Reset arithmetic decoding variables */
   1.218 +  entropy->c = 0;
   1.219 +  entropy->a = 0;
   1.220 +  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */
   1.221 +
   1.222 +  /* Reset restart counter */
   1.223 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.224 +}
   1.225 +
   1.226 +
   1.227 +/*
   1.228 + * Arithmetic MCU decoding.
   1.229 + * Each of these routines decodes and returns one MCU's worth of
   1.230 + * arithmetic-compressed coefficients.
   1.231 + * The coefficients are reordered from zigzag order into natural array order,
   1.232 + * but are not dequantized.
   1.233 + *
   1.234 + * The i'th block of the MCU is stored into the block pointed to by
   1.235 + * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
   1.236 + */
   1.237 +
   1.238 +/*
   1.239 + * MCU decoding for DC initial scan (either spectral selection,
   1.240 + * or first pass of successive approximation).
   1.241 + */
   1.242 +
   1.243 +METHODDEF(boolean)
   1.244 +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.245 +{
   1.246 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.247 +  JBLOCKROW block;
   1.248 +  unsigned char *st;
   1.249 +  int blkn, ci, tbl, sign;
   1.250 +  int v, m;
   1.251 +
   1.252 +  /* Process restart marker if needed */
   1.253 +  if (cinfo->restart_interval) {
   1.254 +    if (entropy->restarts_to_go == 0)
   1.255 +      process_restart(cinfo);
   1.256 +    entropy->restarts_to_go--;
   1.257 +  }
   1.258 +
   1.259 +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
   1.260 +
   1.261 +  /* Outer loop handles each block in the MCU */
   1.262 +
   1.263 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.264 +    block = MCU_data[blkn];
   1.265 +    ci = cinfo->MCU_membership[blkn];
   1.266 +    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
   1.267 +
   1.268 +    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
   1.269 +
   1.270 +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
   1.271 +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
   1.272 +
   1.273 +    /* Figure F.19: Decode_DC_DIFF */
   1.274 +    if (arith_decode(cinfo, st) == 0)
   1.275 +      entropy->dc_context[ci] = 0;
   1.276 +    else {
   1.277 +      /* Figure F.21: Decoding nonzero value v */
   1.278 +      /* Figure F.22: Decoding the sign of v */
   1.279 +      sign = arith_decode(cinfo, st + 1);
   1.280 +      st += 2; st += sign;
   1.281 +      /* Figure F.23: Decoding the magnitude category of v */
   1.282 +      if ((m = arith_decode(cinfo, st)) != 0) {
   1.283 +	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */
   1.284 +	while (arith_decode(cinfo, st)) {
   1.285 +	  if ((m <<= 1) == 0x8000) {
   1.286 +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.287 +	    entropy->ct = -1;			/* magnitude overflow */
   1.288 +	    return TRUE;
   1.289 +	  }
   1.290 +	  st += 1;
   1.291 +	}
   1.292 +      }
   1.293 +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
   1.294 +      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
   1.295 +	entropy->dc_context[ci] = 0;		   /* zero diff category */
   1.296 +      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
   1.297 +	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
   1.298 +      else
   1.299 +	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
   1.300 +      v = m;
   1.301 +      /* Figure F.24: Decoding the magnitude bit pattern of v */
   1.302 +      st += 14;
   1.303 +      while (m >>= 1)
   1.304 +	if (arith_decode(cinfo, st)) v |= m;
   1.305 +      v += 1; if (sign) v = -v;
   1.306 +      entropy->last_dc_val[ci] += v;
   1.307 +    }
   1.308 +
   1.309 +    /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
   1.310 +    (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
   1.311 +  }
   1.312 +
   1.313 +  return TRUE;
   1.314 +}
   1.315 +
   1.316 +
   1.317 +/*
   1.318 + * MCU decoding for AC initial scan (either spectral selection,
   1.319 + * or first pass of successive approximation).
   1.320 + */
   1.321 +
   1.322 +METHODDEF(boolean)
   1.323 +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.324 +{
   1.325 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.326 +  JBLOCKROW block;
   1.327 +  unsigned char *st;
   1.328 +  int tbl, sign, k;
   1.329 +  int v, m;
   1.330 +
   1.331 +  /* Process restart marker if needed */
   1.332 +  if (cinfo->restart_interval) {
   1.333 +    if (entropy->restarts_to_go == 0)
   1.334 +      process_restart(cinfo);
   1.335 +    entropy->restarts_to_go--;
   1.336 +  }
   1.337 +
   1.338 +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
   1.339 +
   1.340 +  /* There is always only one block per MCU */
   1.341 +  block = MCU_data[0];
   1.342 +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
   1.343 +
   1.344 +  /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
   1.345 +
   1.346 +  /* Figure F.20: Decode_AC_coefficients */
   1.347 +  for (k = cinfo->Ss; k <= cinfo->Se; k++) {
   1.348 +    st = entropy->ac_stats[tbl] + 3 * (k - 1);
   1.349 +    if (arith_decode(cinfo, st)) break;		/* EOB flag */
   1.350 +    while (arith_decode(cinfo, st + 1) == 0) {
   1.351 +      st += 3; k++;
   1.352 +      if (k > cinfo->Se) {
   1.353 +	WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.354 +	entropy->ct = -1;			/* spectral overflow */
   1.355 +	return TRUE;
   1.356 +      }
   1.357 +    }
   1.358 +    /* Figure F.21: Decoding nonzero value v */
   1.359 +    /* Figure F.22: Decoding the sign of v */
   1.360 +    sign = arith_decode(cinfo, entropy->fixed_bin);
   1.361 +    st += 2;
   1.362 +    /* Figure F.23: Decoding the magnitude category of v */
   1.363 +    if ((m = arith_decode(cinfo, st)) != 0) {
   1.364 +      if (arith_decode(cinfo, st)) {
   1.365 +	m <<= 1;
   1.366 +	st = entropy->ac_stats[tbl] +
   1.367 +	     (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
   1.368 +	while (arith_decode(cinfo, st)) {
   1.369 +	  if ((m <<= 1) == 0x8000) {
   1.370 +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.371 +	    entropy->ct = -1;			/* magnitude overflow */
   1.372 +	    return TRUE;
   1.373 +	  }
   1.374 +	  st += 1;
   1.375 +	}
   1.376 +      }
   1.377 +    }
   1.378 +    v = m;
   1.379 +    /* Figure F.24: Decoding the magnitude bit pattern of v */
   1.380 +    st += 14;
   1.381 +    while (m >>= 1)
   1.382 +      if (arith_decode(cinfo, st)) v |= m;
   1.383 +    v += 1; if (sign) v = -v;
   1.384 +    /* Scale and output coefficient in natural (dezigzagged) order */
   1.385 +    (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
   1.386 +  }
   1.387 +
   1.388 +  return TRUE;
   1.389 +}
   1.390 +
   1.391 +
   1.392 +/*
   1.393 + * MCU decoding for DC successive approximation refinement scan.
   1.394 + */
   1.395 +
   1.396 +METHODDEF(boolean)
   1.397 +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.398 +{
   1.399 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.400 +  unsigned char *st;
   1.401 +  int p1, blkn;
   1.402 +
   1.403 +  /* Process restart marker if needed */
   1.404 +  if (cinfo->restart_interval) {
   1.405 +    if (entropy->restarts_to_go == 0)
   1.406 +      process_restart(cinfo);
   1.407 +    entropy->restarts_to_go--;
   1.408 +  }
   1.409 +
   1.410 +  st = entropy->fixed_bin;	/* use fixed probability estimation */
   1.411 +  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */
   1.412 +
   1.413 +  /* Outer loop handles each block in the MCU */
   1.414 +
   1.415 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.416 +    /* Encoded data is simply the next bit of the two's-complement DC value */
   1.417 +    if (arith_decode(cinfo, st))
   1.418 +      MCU_data[blkn][0][0] |= p1;
   1.419 +  }
   1.420 +
   1.421 +  return TRUE;
   1.422 +}
   1.423 +
   1.424 +
   1.425 +/*
   1.426 + * MCU decoding for AC successive approximation refinement scan.
   1.427 + */
   1.428 +
   1.429 +METHODDEF(boolean)
   1.430 +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.431 +{
   1.432 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.433 +  JBLOCKROW block;
   1.434 +  JCOEFPTR thiscoef;
   1.435 +  unsigned char *st;
   1.436 +  int tbl, k, kex;
   1.437 +  int p1, m1;
   1.438 +
   1.439 +  /* Process restart marker if needed */
   1.440 +  if (cinfo->restart_interval) {
   1.441 +    if (entropy->restarts_to_go == 0)
   1.442 +      process_restart(cinfo);
   1.443 +    entropy->restarts_to_go--;
   1.444 +  }
   1.445 +
   1.446 +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
   1.447 +
   1.448 +  /* There is always only one block per MCU */
   1.449 +  block = MCU_data[0];
   1.450 +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
   1.451 +
   1.452 +  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */
   1.453 +  m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
   1.454 +
   1.455 +  /* Establish EOBx (previous stage end-of-block) index */
   1.456 +  for (kex = cinfo->Se; kex > 0; kex--)
   1.457 +    if ((*block)[jpeg_natural_order[kex]]) break;
   1.458 +
   1.459 +  for (k = cinfo->Ss; k <= cinfo->Se; k++) {
   1.460 +    st = entropy->ac_stats[tbl] + 3 * (k - 1);
   1.461 +    if (k > kex)
   1.462 +      if (arith_decode(cinfo, st)) break;	/* EOB flag */
   1.463 +    for (;;) {
   1.464 +      thiscoef = *block + jpeg_natural_order[k];
   1.465 +      if (*thiscoef) {				/* previously nonzero coef */
   1.466 +	if (arith_decode(cinfo, st + 2)) {
   1.467 +	  if (*thiscoef < 0)
   1.468 +	    *thiscoef += m1;
   1.469 +	  else
   1.470 +	    *thiscoef += p1;
   1.471 +	}
   1.472 +	break;
   1.473 +      }
   1.474 +      if (arith_decode(cinfo, st + 1)) {	/* newly nonzero coef */
   1.475 +	if (arith_decode(cinfo, entropy->fixed_bin))
   1.476 +	  *thiscoef = m1;
   1.477 +	else
   1.478 +	  *thiscoef = p1;
   1.479 +	break;
   1.480 +      }
   1.481 +      st += 3; k++;
   1.482 +      if (k > cinfo->Se) {
   1.483 +	WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.484 +	entropy->ct = -1;			/* spectral overflow */
   1.485 +	return TRUE;
   1.486 +      }
   1.487 +    }
   1.488 +  }
   1.489 +
   1.490 +  return TRUE;
   1.491 +}
   1.492 +
   1.493 +
   1.494 +/*
   1.495 + * Decode one MCU's worth of arithmetic-compressed coefficients.
   1.496 + */
   1.497 +
   1.498 +METHODDEF(boolean)
   1.499 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.500 +{
   1.501 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.502 +  jpeg_component_info * compptr;
   1.503 +  JBLOCKROW block;
   1.504 +  unsigned char *st;
   1.505 +  int blkn, ci, tbl, sign, k;
   1.506 +  int v, m;
   1.507 +
   1.508 +  /* Process restart marker if needed */
   1.509 +  if (cinfo->restart_interval) {
   1.510 +    if (entropy->restarts_to_go == 0)
   1.511 +      process_restart(cinfo);
   1.512 +    entropy->restarts_to_go--;
   1.513 +  }
   1.514 +
   1.515 +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
   1.516 +
   1.517 +  /* Outer loop handles each block in the MCU */
   1.518 +
   1.519 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.520 +    block = MCU_data[blkn];
   1.521 +    ci = cinfo->MCU_membership[blkn];
   1.522 +    compptr = cinfo->cur_comp_info[ci];
   1.523 +
   1.524 +    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
   1.525 +
   1.526 +    tbl = compptr->dc_tbl_no;
   1.527 +
   1.528 +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
   1.529 +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
   1.530 +
   1.531 +    /* Figure F.19: Decode_DC_DIFF */
   1.532 +    if (arith_decode(cinfo, st) == 0)
   1.533 +      entropy->dc_context[ci] = 0;
   1.534 +    else {
   1.535 +      /* Figure F.21: Decoding nonzero value v */
   1.536 +      /* Figure F.22: Decoding the sign of v */
   1.537 +      sign = arith_decode(cinfo, st + 1);
   1.538 +      st += 2; st += sign;
   1.539 +      /* Figure F.23: Decoding the magnitude category of v */
   1.540 +      if ((m = arith_decode(cinfo, st)) != 0) {
   1.541 +	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */
   1.542 +	while (arith_decode(cinfo, st)) {
   1.543 +	  if ((m <<= 1) == 0x8000) {
   1.544 +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.545 +	    entropy->ct = -1;			/* magnitude overflow */
   1.546 +	    return TRUE;
   1.547 +	  }
   1.548 +	  st += 1;
   1.549 +	}
   1.550 +      }
   1.551 +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
   1.552 +      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
   1.553 +	entropy->dc_context[ci] = 0;		   /* zero diff category */
   1.554 +      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
   1.555 +	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
   1.556 +      else
   1.557 +	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
   1.558 +      v = m;
   1.559 +      /* Figure F.24: Decoding the magnitude bit pattern of v */
   1.560 +      st += 14;
   1.561 +      while (m >>= 1)
   1.562 +	if (arith_decode(cinfo, st)) v |= m;
   1.563 +      v += 1; if (sign) v = -v;
   1.564 +      entropy->last_dc_val[ci] += v;
   1.565 +    }
   1.566 +
   1.567 +    (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
   1.568 +
   1.569 +    /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
   1.570 +
   1.571 +    tbl = compptr->ac_tbl_no;
   1.572 +
   1.573 +    /* Figure F.20: Decode_AC_coefficients */
   1.574 +    for (k = 1; k <= DCTSIZE2 - 1; k++) {
   1.575 +      st = entropy->ac_stats[tbl] + 3 * (k - 1);
   1.576 +      if (arith_decode(cinfo, st)) break;	/* EOB flag */
   1.577 +      while (arith_decode(cinfo, st + 1) == 0) {
   1.578 +	st += 3; k++;
   1.579 +	if (k > DCTSIZE2 - 1) {
   1.580 +	  WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.581 +	  entropy->ct = -1;			/* spectral overflow */
   1.582 +	  return TRUE;
   1.583 +	}
   1.584 +      }
   1.585 +      /* Figure F.21: Decoding nonzero value v */
   1.586 +      /* Figure F.22: Decoding the sign of v */
   1.587 +      sign = arith_decode(cinfo, entropy->fixed_bin);
   1.588 +      st += 2;
   1.589 +      /* Figure F.23: Decoding the magnitude category of v */
   1.590 +      if ((m = arith_decode(cinfo, st)) != 0) {
   1.591 +	if (arith_decode(cinfo, st)) {
   1.592 +	  m <<= 1;
   1.593 +	  st = entropy->ac_stats[tbl] +
   1.594 +	       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
   1.595 +	  while (arith_decode(cinfo, st)) {
   1.596 +	    if ((m <<= 1) == 0x8000) {
   1.597 +	      WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
   1.598 +	      entropy->ct = -1;			/* magnitude overflow */
   1.599 +	      return TRUE;
   1.600 +	    }
   1.601 +	    st += 1;
   1.602 +	  }
   1.603 +	}
   1.604 +      }
   1.605 +      v = m;
   1.606 +      /* Figure F.24: Decoding the magnitude bit pattern of v */
   1.607 +      st += 14;
   1.608 +      while (m >>= 1)
   1.609 +	if (arith_decode(cinfo, st)) v |= m;
   1.610 +      v += 1; if (sign) v = -v;
   1.611 +      (*block)[jpeg_natural_order[k]] = (JCOEF) v;
   1.612 +    }
   1.613 +  }
   1.614 +
   1.615 +  return TRUE;
   1.616 +}
   1.617 +
   1.618 +
   1.619 +/*
   1.620 + * Initialize for an arithmetic-compressed scan.
   1.621 + */
   1.622 +
   1.623 +METHODDEF(void)
   1.624 +start_pass (j_decompress_ptr cinfo)
   1.625 +{
   1.626 +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
   1.627 +  int ci, tbl;
   1.628 +  jpeg_component_info * compptr;
   1.629 +
   1.630 +  if (cinfo->progressive_mode) {
   1.631 +    /* Validate progressive scan parameters */
   1.632 +    if (cinfo->Ss == 0) {
   1.633 +      if (cinfo->Se != 0)
   1.634 +	goto bad;
   1.635 +    } else {
   1.636 +      /* need not check Ss/Se < 0 since they came from unsigned bytes */
   1.637 +      if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
   1.638 +	goto bad;
   1.639 +      /* AC scans may have only one component */
   1.640 +      if (cinfo->comps_in_scan != 1)
   1.641 +	goto bad;
   1.642 +    }
   1.643 +    if (cinfo->Ah != 0) {
   1.644 +      /* Successive approximation refinement scan: must have Al = Ah-1. */
   1.645 +      if (cinfo->Ah-1 != cinfo->Al)
   1.646 +	goto bad;
   1.647 +    }
   1.648 +    if (cinfo->Al > 13) {	/* need not check for < 0 */
   1.649 +      bad:
   1.650 +      ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
   1.651 +	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
   1.652 +    }
   1.653 +    /* Update progression status, and verify that scan order is legal.
   1.654 +     * Note that inter-scan inconsistencies are treated as warnings
   1.655 +     * not fatal errors ... not clear if this is right way to behave.
   1.656 +     */
   1.657 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.658 +      int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
   1.659 +      int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
   1.660 +      if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
   1.661 +	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
   1.662 +      for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
   1.663 +	int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
   1.664 +	if (cinfo->Ah != expected)
   1.665 +	  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
   1.666 +	coef_bit_ptr[coefi] = cinfo->Al;
   1.667 +      }
   1.668 +    }
   1.669 +    /* Select MCU decoding routine */
   1.670 +    if (cinfo->Ah == 0) {
   1.671 +      if (cinfo->Ss == 0)
   1.672 +	entropy->pub.decode_mcu = decode_mcu_DC_first;
   1.673 +      else
   1.674 +	entropy->pub.decode_mcu = decode_mcu_AC_first;
   1.675 +    } else {
   1.676 +      if (cinfo->Ss == 0)
   1.677 +	entropy->pub.decode_mcu = decode_mcu_DC_refine;
   1.678 +      else
   1.679 +	entropy->pub.decode_mcu = decode_mcu_AC_refine;
   1.680 +    }
   1.681 +  } else {
   1.682 +    /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
   1.683 +     * This ought to be an error condition, but we make it a warning.
   1.684 +     */
   1.685 +    if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
   1.686 +	(cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
   1.687 +      WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
   1.688 +    /* Select MCU decoding routine */
   1.689 +    entropy->pub.decode_mcu = decode_mcu;
   1.690 +  }
   1.691 +
   1.692 +  /* Allocate & initialize requested statistics areas */
   1.693 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.694 +    compptr = cinfo->cur_comp_info[ci];
   1.695 +    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
   1.696 +      tbl = compptr->dc_tbl_no;
   1.697 +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
   1.698 +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
   1.699 +      if (entropy->dc_stats[tbl] == NULL)
   1.700 +	entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
   1.701 +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
   1.702 +      MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
   1.703 +      /* Initialize DC predictions to 0 */
   1.704 +      entropy->last_dc_val[ci] = 0;
   1.705 +      entropy->dc_context[ci] = 0;
   1.706 +    }
   1.707 +    if (! cinfo->progressive_mode || cinfo->Ss) {
   1.708 +      tbl = compptr->ac_tbl_no;
   1.709 +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
   1.710 +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
   1.711 +      if (entropy->ac_stats[tbl] == NULL)
   1.712 +	entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
   1.713 +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
   1.714 +      MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
   1.715 +    }
   1.716 +  }
   1.717 +
   1.718 +  /* Initialize arithmetic decoding variables */
   1.719 +  entropy->c = 0;
   1.720 +  entropy->a = 0;
   1.721 +  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */
   1.722 +
   1.723 +  /* Initialize restart counter */
   1.724 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.725 +}
   1.726 +
   1.727 +
   1.728 +/*
   1.729 + * Module initialization routine for arithmetic entropy decoding.
   1.730 + */
   1.731 +
   1.732 +GLOBAL(void)
   1.733 +jinit_arith_decoder (j_decompress_ptr cinfo)
   1.734 +{
   1.735 +  arith_entropy_ptr entropy;
   1.736 +  int i;
   1.737 +
   1.738 +  entropy = (arith_entropy_ptr)
   1.739 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.740 +				SIZEOF(arith_entropy_decoder));
   1.741 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
   1.742 +  entropy->pub.start_pass = start_pass;
   1.743 +
   1.744 +  /* Mark tables unallocated */
   1.745 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
   1.746 +    entropy->dc_stats[i] = NULL;
   1.747 +    entropy->ac_stats[i] = NULL;
   1.748 +  }
   1.749 +
   1.750 +  /* Initialize index for fixed probability estimation */
   1.751 +  entropy->fixed_bin[0] = 113;
   1.752 +
   1.753 +  if (cinfo->progressive_mode) {
   1.754 +    /* Create progression status table */
   1.755 +    int *coef_bit_ptr, ci;
   1.756 +    cinfo->coef_bits = (int (*)[DCTSIZE2])
   1.757 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.758 +				  cinfo->num_components*DCTSIZE2*SIZEOF(int));
   1.759 +    coef_bit_ptr = & cinfo->coef_bits[0][0];
   1.760 +    for (ci = 0; ci < cinfo->num_components; ci++) 
   1.761 +      for (i = 0; i < DCTSIZE2; i++)
   1.762 +	*coef_bit_ptr++ = -1;
   1.763 +  }
   1.764 +}

mercurial