kusano 7d535a
/*
kusano 7d535a
 * jdarith.c
kusano 7d535a
 *
kusano 7d535a
 * Developed 1997-2012 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains portable arithmetic entropy decoding routines for JPEG
kusano 7d535a
 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
kusano 7d535a
 *
kusano 7d535a
 * Both sequential and progressive modes are supported in this single module.
kusano 7d535a
 *
kusano 7d535a
 * Suspension is not currently supported in this module.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Expanded entropy decoder object for arithmetic decoding. */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_entropy_decoder pub; /* public fields */
kusano 7d535a
kusano 7d535a
  INT32 c;       /* C register, base of coding interval + input bit buffer */
kusano 7d535a
  INT32 a;               /* A register, normalized size of coding interval */
kusano 7d535a
  int ct;     /* bit shift counter, # of bits left in bit buffer part of C */
kusano 7d535a
                                                         /* init: ct = -16 */
kusano 7d535a
                                                         /* run: ct = 0..7 */
kusano 7d535a
                                                         /* error: ct = -1 */
kusano 7d535a
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
kusano 7d535a
  int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
kusano 7d535a
kusano 7d535a
  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
kusano 7d535a
kusano 7d535a
  /* Pointers to statistics areas (these workspaces have image lifespan) */
kusano 7d535a
  unsigned char * dc_stats[NUM_ARITH_TBLS];
kusano 7d535a
  unsigned char * ac_stats[NUM_ARITH_TBLS];
kusano 7d535a
kusano 7d535a
  /* Statistics bin for coding with fixed probability 0.5 */
kusano 7d535a
  unsigned char fixed_bin[4];
kusano 7d535a
} arith_entropy_decoder;
kusano 7d535a
kusano 7d535a
typedef arith_entropy_decoder * arith_entropy_ptr;
kusano 7d535a
kusano 7d535a
/* The following two definitions specify the allocation chunk size
kusano 7d535a
 * for the statistics area.
kusano 7d535a
 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
kusano 7d535a
 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
kusano 7d535a
 *
kusano 7d535a
 * We use a compact representation with 1 byte per statistics bin,
kusano 7d535a
 * thus the numbers directly represent byte sizes.
kusano 7d535a
 * This 1 byte per statistics bin contains the meaning of the MPS
kusano 7d535a
 * (more probable symbol) in the highest bit (mask 0x80), and the
kusano 7d535a
 * index into the probability estimation state machine table
kusano 7d535a
 * in the lower bits (mask 0x7F).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define DC_STAT_BINS 64
kusano 7d535a
#define AC_STAT_BINS 256
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
get_byte (j_decompress_ptr cinfo)
kusano 7d535a
/* Read next input byte; we do not support suspension in this module. */
kusano 7d535a
{
kusano 7d535a
  struct jpeg_source_mgr * src = cinfo->src;
kusano 7d535a
kusano 7d535a
  if (src->bytes_in_buffer == 0)
kusano 7d535a
    if (! (*src->fill_input_buffer) (cinfo))
kusano 7d535a
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
kusano 7d535a
  src->bytes_in_buffer--;
kusano 7d535a
  return GETJOCTET(*src->next_input_byte++);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The core arithmetic decoding routine (common in JPEG and JBIG).
kusano 7d535a
 * This needs to go as fast as possible.
kusano 7d535a
 * Machine-dependent optimization facilities
kusano 7d535a
 * are not utilized in this portable implementation.
kusano 7d535a
 * However, this code should be fairly efficient and
kusano 7d535a
 * may be a good base for further optimizations anyway.
kusano 7d535a
 *
kusano 7d535a
 * Return value is 0 or 1 (binary decision).
kusano 7d535a
 *
kusano 7d535a
 * Note: I've changed the handling of the code base & bit
kusano 7d535a
 * buffer register C compared to other implementations
kusano 7d535a
 * based on the standards layout & procedures.
kusano 7d535a
 * While it also contains both the actual base of the
kusano 7d535a
 * coding interval (16 bits) and the next-bits buffer,
kusano 7d535a
 * the cut-point between these two parts is floating
kusano 7d535a
 * (instead of fixed) with the bit shift counter CT.
kusano 7d535a
 * Thus, we also need only one (variable instead of
kusano 7d535a
 * fixed size) shift for the LPS/MPS decision, and
kusano 7d535a
 * we can get away with any renormalization update
kusano 7d535a
 * of C (except for new data insertion, of course).
kusano 7d535a
 *
kusano 7d535a
 * I've also introduced a new scheme for accessing
kusano 7d535a
 * the probability estimation state machine table,
kusano 7d535a
 * derived from Markus Kuhn's JBIG implementation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
arith_decode (j_decompress_ptr cinfo, unsigned char *st)
kusano 7d535a
{
kusano 7d535a
  register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  register unsigned char nl, nm;
kusano 7d535a
  register INT32 qe, temp;
kusano 7d535a
  register int sv, data;
kusano 7d535a
kusano 7d535a
  /* Renormalization & data input per section D.2.6 */
kusano 7d535a
  while (e->a < 0x8000L) {
kusano 7d535a
    if (--e->ct < 0) {
kusano 7d535a
      /* Need to fetch next data byte */
kusano 7d535a
      if (cinfo->unread_marker)
kusano 7d535a
	data = 0;		/* stuff zero data */
kusano 7d535a
      else {
kusano 7d535a
	data = get_byte(cinfo);	/* read next input byte */
kusano 7d535a
	if (data == 0xFF) {	/* zero stuff or marker code */
kusano 7d535a
	  do data = get_byte(cinfo);
kusano 7d535a
	  while (data == 0xFF);	/* swallow extra 0xFF bytes */
kusano 7d535a
	  if (data == 0)
kusano 7d535a
	    data = 0xFF;	/* discard stuffed zero byte */
kusano 7d535a
	  else {
kusano 7d535a
	    /* Note: Different from the Huffman decoder, hitting
kusano 7d535a
	     * a marker while processing the compressed data
kusano 7d535a
	     * segment is legal in arithmetic coding.
kusano 7d535a
	     * The convention is to supply zero data
kusano 7d535a
	     * then until decoding is complete.
kusano 7d535a
	     */
kusano 7d535a
	    cinfo->unread_marker = data;
kusano 7d535a
	    data = 0;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      e->c = (e->c << 8) | data; /* insert data into C register */
kusano 7d535a
      if ((e->ct += 8) < 0)	 /* update bit shift counter */
kusano 7d535a
	/* Need more initial bytes */
kusano 7d535a
	if (++e->ct == 0)
kusano 7d535a
	  /* Got 2 initial bytes -> re-init A and exit loop */
kusano 7d535a
	  e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
kusano 7d535a
    }
kusano 7d535a
    e->a <<= 1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Fetch values from our compact representation of Table D.3(D.2):
kusano 7d535a
   * Qe values and probability estimation state machine
kusano 7d535a
   */
kusano 7d535a
  sv = *st;
kusano 7d535a
  qe = jpeg_aritab[sv & 0x7F];	/* => Qe_Value */
kusano 7d535a
  nl = qe & 0xFF; qe >>= 8;	/* Next_Index_LPS + Switch_MPS */
kusano 7d535a
  nm = qe & 0xFF; qe >>= 8;	/* Next_Index_MPS */
kusano 7d535a
kusano 7d535a
  /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
kusano 7d535a
  temp = e->a - qe;
kusano 7d535a
  e->a = temp;
kusano 7d535a
  temp <<= e->ct;
kusano 7d535a
  if (e->c >= temp) {
kusano 7d535a
    e->c -= temp;
kusano 7d535a
    /* Conditional LPS (less probable symbol) exchange */
kusano 7d535a
    if (e->a < qe) {
kusano 7d535a
      e->a = qe;
kusano 7d535a
      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
kusano 7d535a
    } else {
kusano 7d535a
      e->a = qe;
kusano 7d535a
      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
kusano 7d535a
      sv ^= 0x80;		/* Exchange LPS/MPS */
kusano 7d535a
    }
kusano 7d535a
  } else if (e->a < 0x8000L) {
kusano 7d535a
    /* Conditional MPS (more probable symbol) exchange */
kusano 7d535a
    if (e->a < qe) {
kusano 7d535a
      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
kusano 7d535a
      sv ^= 0x80;		/* Exchange LPS/MPS */
kusano 7d535a
    } else {
kusano 7d535a
      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return sv >> 7;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Check for a restart marker & resynchronize decoder.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
process_restart (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
kusano 7d535a
  /* Advance past the RSTn marker */
kusano 7d535a
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
kusano 7d535a
    ERREXIT(cinfo, JERR_CANT_SUSPEND);
kusano 7d535a
kusano 7d535a
  /* Re-initialize statistics areas */
kusano 7d535a
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
kusano 7d535a
      MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
kusano 7d535a
      /* Reset DC predictions to 0 */
kusano 7d535a
      entropy->last_dc_val[ci] = 0;
kusano 7d535a
      entropy->dc_context[ci] = 0;
kusano 7d535a
    }
kusano 7d535a
    if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
kusano 7d535a
	(cinfo->progressive_mode && cinfo->Ss)) {
kusano 7d535a
      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Reset arithmetic decoding variables */
kusano 7d535a
  entropy->c = 0;
kusano 7d535a
  entropy->a = 0;
kusano 7d535a
  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */
kusano 7d535a
kusano 7d535a
  /* Reset restart counter */
kusano 7d535a
  entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Arithmetic MCU decoding.
kusano 7d535a
 * Each of these routines decodes and returns one MCU's worth of
kusano 7d535a
 * arithmetic-compressed coefficients.
kusano 7d535a
 * The coefficients are reordered from zigzag order into natural array order,
kusano 7d535a
 * but are not dequantized.
kusano 7d535a
 *
kusano 7d535a
 * The i'th block of the MCU is stored into the block pointed to by
kusano 7d535a
 * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU decoding for DC initial scan (either spectral selection,
kusano 7d535a
 * or first pass of successive approximation).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  JBLOCKROW block;
kusano 7d535a
  unsigned char *st;
kusano 7d535a
  int blkn, ci, tbl, sign;
kusano 7d535a
  int v, m;
kusano 7d535a
kusano 7d535a
  /* Process restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0)
kusano 7d535a
      process_restart(cinfo);
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
kusano 7d535a
kusano 7d535a
  /* Outer loop handles each block in the MCU */
kusano 7d535a
kusano 7d535a
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
kusano 7d535a
    block = MCU_data[blkn];
kusano 7d535a
    ci = cinfo->MCU_membership[blkn];
kusano 7d535a
    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
kusano 7d535a
kusano 7d535a
    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
kusano 7d535a
kusano 7d535a
    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
kusano 7d535a
    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
kusano 7d535a
kusano 7d535a
    /* Figure F.19: Decode_DC_DIFF */
kusano 7d535a
    if (arith_decode(cinfo, st) == 0)
kusano 7d535a
      entropy->dc_context[ci] = 0;
kusano 7d535a
    else {
kusano 7d535a
      /* Figure F.21: Decoding nonzero value v */
kusano 7d535a
      /* Figure F.22: Decoding the sign of v */
kusano 7d535a
      sign = arith_decode(cinfo, st + 1);
kusano 7d535a
      st += 2; st += sign;
kusano 7d535a
      /* Figure F.23: Decoding the magnitude category of v */
kusano 7d535a
      if ((m = arith_decode(cinfo, st)) != 0) {
kusano 7d535a
	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */
kusano 7d535a
	while (arith_decode(cinfo, st)) {
kusano 7d535a
	  if ((m <<= 1) == 0x8000) {
kusano 7d535a
	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	    entropy->ct = -1;			/* magnitude overflow */
kusano 7d535a
	    return TRUE;
kusano 7d535a
	  }
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
kusano 7d535a
      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
kusano 7d535a
	entropy->dc_context[ci] = 0;		   /* zero diff category */
kusano 7d535a
      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
kusano 7d535a
	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
kusano 7d535a
      else
kusano 7d535a
	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
kusano 7d535a
      v = m;
kusano 7d535a
      /* Figure F.24: Decoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	if (arith_decode(cinfo, st)) v |= m;
kusano 7d535a
      v += 1; if (sign) v = -v;
kusano 7d535a
      entropy->last_dc_val[ci] += v;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
kusano 7d535a
    (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU decoding for AC initial scan (either spectral selection,
kusano 7d535a
 * or first pass of successive approximation).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  JBLOCKROW block;
kusano 7d535a
  unsigned char *st;
kusano 7d535a
  int tbl, sign, k;
kusano 7d535a
  int v, m;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Process restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0)
kusano 7d535a
      process_restart(cinfo);
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* There is always only one block per MCU */
kusano 7d535a
  block = MCU_data[0];
kusano 7d535a
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
kusano 7d535a
kusano 7d535a
  /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
kusano 7d535a
kusano 7d535a
  /* Figure F.20: Decode_AC_coefficients */
kusano 7d535a
  k = cinfo->Ss - 1;
kusano 7d535a
  do {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    if (arith_decode(cinfo, st)) break;		/* EOB flag */
kusano 7d535a
    for (;;) {
kusano 7d535a
      k++;
kusano 7d535a
      if (arith_decode(cinfo, st + 1)) break;
kusano 7d535a
      st += 3;
kusano 7d535a
      if (k >= cinfo->Se) {
kusano 7d535a
	WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	entropy->ct = -1;			/* spectral overflow */
kusano 7d535a
	return TRUE;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Figure F.21: Decoding nonzero value v */
kusano 7d535a
    /* Figure F.22: Decoding the sign of v */
kusano 7d535a
    sign = arith_decode(cinfo, entropy->fixed_bin);
kusano 7d535a
    st += 2;
kusano 7d535a
    /* Figure F.23: Decoding the magnitude category of v */
kusano 7d535a
    if ((m = arith_decode(cinfo, st)) != 0) {
kusano 7d535a
      if (arith_decode(cinfo, st)) {
kusano 7d535a
	m <<= 1;
kusano 7d535a
	st = entropy->ac_stats[tbl] +
kusano 7d535a
	     (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
kusano 7d535a
	while (arith_decode(cinfo, st)) {
kusano 7d535a
	  if ((m <<= 1) == 0x8000) {
kusano 7d535a
	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	    entropy->ct = -1;			/* magnitude overflow */
kusano 7d535a
	    return TRUE;
kusano 7d535a
	  }
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    v = m;
kusano 7d535a
    /* Figure F.24: Decoding the magnitude bit pattern of v */
kusano 7d535a
    st += 14;
kusano 7d535a
    while (m >>= 1)
kusano 7d535a
      if (arith_decode(cinfo, st)) v |= m;
kusano 7d535a
    v += 1; if (sign) v = -v;
kusano 7d535a
    /* Scale and output coefficient in natural (dezigzagged) order */
kusano 7d535a
    (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
kusano 7d535a
  } while (k < cinfo->Se);
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU decoding for DC successive approximation refinement scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  unsigned char *st;
kusano 7d535a
  int p1, blkn;
kusano 7d535a
kusano 7d535a
  /* Process restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0)
kusano 7d535a
      process_restart(cinfo);
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  st = entropy->fixed_bin;	/* use fixed probability estimation */
kusano 7d535a
  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */
kusano 7d535a
kusano 7d535a
  /* Outer loop handles each block in the MCU */
kusano 7d535a
kusano 7d535a
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
kusano 7d535a
    /* Encoded data is simply the next bit of the two's-complement DC value */
kusano 7d535a
    if (arith_decode(cinfo, st))
kusano 7d535a
      MCU_data[blkn][0][0] |= p1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU decoding for AC successive approximation refinement scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  JBLOCKROW block;
kusano 7d535a
  JCOEFPTR thiscoef;
kusano 7d535a
  unsigned char *st;
kusano 7d535a
  int tbl, k, kex;
kusano 7d535a
  int p1, m1;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Process restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0)
kusano 7d535a
      process_restart(cinfo);
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* There is always only one block per MCU */
kusano 7d535a
  block = MCU_data[0];
kusano 7d535a
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
kusano 7d535a
kusano 7d535a
  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */
kusano 7d535a
  m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
kusano 7d535a
kusano 7d535a
  /* Establish EOBx (previous stage end-of-block) index */
kusano 7d535a
  kex = cinfo->Se;
kusano 7d535a
  do {
kusano 7d535a
    if ((*block)[natural_order[kex]]) break;
kusano 7d535a
  } while (--kex);
kusano 7d535a
kusano 7d535a
  k = cinfo->Ss - 1;
kusano 7d535a
  do {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    if (k >= kex)
kusano 7d535a
      if (arith_decode(cinfo, st)) break;	/* EOB flag */
kusano 7d535a
    for (;;) {
kusano 7d535a
      thiscoef = *block + natural_order[++k];
kusano 7d535a
      if (*thiscoef) {				/* previously nonzero coef */
kusano 7d535a
	if (arith_decode(cinfo, st + 2)) {
kusano 7d535a
	  if (*thiscoef < 0)
kusano 7d535a
	    *thiscoef += m1;
kusano 7d535a
	  else
kusano 7d535a
	    *thiscoef += p1;
kusano 7d535a
	}
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
      if (arith_decode(cinfo, st + 1)) {	/* newly nonzero coef */
kusano 7d535a
	if (arith_decode(cinfo, entropy->fixed_bin))
kusano 7d535a
	  *thiscoef = m1;
kusano 7d535a
	else
kusano 7d535a
	  *thiscoef = p1;
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
      st += 3;
kusano 7d535a
      if (k >= cinfo->Se) {
kusano 7d535a
	WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	entropy->ct = -1;			/* spectral overflow */
kusano 7d535a
	return TRUE;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  } while (k < cinfo->Se);
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Decode one MCU's worth of arithmetic-compressed coefficients.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
  JBLOCKROW block;
kusano 7d535a
  unsigned char *st;
kusano 7d535a
  int blkn, ci, tbl, sign, k;
kusano 7d535a
  int v, m;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Process restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0)
kusano 7d535a
      process_restart(cinfo);
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (entropy->ct == -1) return TRUE;	/* if error do nothing */
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* Outer loop handles each block in the MCU */
kusano 7d535a
kusano 7d535a
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
kusano 7d535a
    block = MCU_data[blkn];
kusano 7d535a
    ci = cinfo->MCU_membership[blkn];
kusano 7d535a
    compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
kusano 7d535a
    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
kusano 7d535a
kusano 7d535a
    tbl = compptr->dc_tbl_no;
kusano 7d535a
kusano 7d535a
    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
kusano 7d535a
    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
kusano 7d535a
kusano 7d535a
    /* Figure F.19: Decode_DC_DIFF */
kusano 7d535a
    if (arith_decode(cinfo, st) == 0)
kusano 7d535a
      entropy->dc_context[ci] = 0;
kusano 7d535a
    else {
kusano 7d535a
      /* Figure F.21: Decoding nonzero value v */
kusano 7d535a
      /* Figure F.22: Decoding the sign of v */
kusano 7d535a
      sign = arith_decode(cinfo, st + 1);
kusano 7d535a
      st += 2; st += sign;
kusano 7d535a
      /* Figure F.23: Decoding the magnitude category of v */
kusano 7d535a
      if ((m = arith_decode(cinfo, st)) != 0) {
kusano 7d535a
	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */
kusano 7d535a
	while (arith_decode(cinfo, st)) {
kusano 7d535a
	  if ((m <<= 1) == 0x8000) {
kusano 7d535a
	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	    entropy->ct = -1;			/* magnitude overflow */
kusano 7d535a
	    return TRUE;
kusano 7d535a
	  }
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
kusano 7d535a
      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
kusano 7d535a
	entropy->dc_context[ci] = 0;		   /* zero diff category */
kusano 7d535a
      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
kusano 7d535a
	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
kusano 7d535a
      else
kusano 7d535a
	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
kusano 7d535a
      v = m;
kusano 7d535a
      /* Figure F.24: Decoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	if (arith_decode(cinfo, st)) v |= m;
kusano 7d535a
      v += 1; if (sign) v = -v;
kusano 7d535a
      entropy->last_dc_val[ci] += v;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
kusano 7d535a
kusano 7d535a
    /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
kusano 7d535a
kusano 7d535a
    if (cinfo->lim_Se == 0) continue;
kusano 7d535a
    tbl = compptr->ac_tbl_no;
kusano 7d535a
    k = 0;
kusano 7d535a
kusano 7d535a
    /* Figure F.20: Decode_AC_coefficients */
kusano 7d535a
    do {
kusano 7d535a
      st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
      if (arith_decode(cinfo, st)) break;	/* EOB flag */
kusano 7d535a
      for (;;) {
kusano 7d535a
	k++;
kusano 7d535a
	if (arith_decode(cinfo, st + 1)) break;
kusano 7d535a
	st += 3;
kusano 7d535a
	if (k >= cinfo->lim_Se) {
kusano 7d535a
	  WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	  entropy->ct = -1;			/* spectral overflow */
kusano 7d535a
	  return TRUE;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      /* Figure F.21: Decoding nonzero value v */
kusano 7d535a
      /* Figure F.22: Decoding the sign of v */
kusano 7d535a
      sign = arith_decode(cinfo, entropy->fixed_bin);
kusano 7d535a
      st += 2;
kusano 7d535a
      /* Figure F.23: Decoding the magnitude category of v */
kusano 7d535a
      if ((m = arith_decode(cinfo, st)) != 0) {
kusano 7d535a
	if (arith_decode(cinfo, st)) {
kusano 7d535a
	  m <<= 1;
kusano 7d535a
	  st = entropy->ac_stats[tbl] +
kusano 7d535a
	       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
kusano 7d535a
	  while (arith_decode(cinfo, st)) {
kusano 7d535a
	    if ((m <<= 1) == 0x8000) {
kusano 7d535a
	      WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
kusano 7d535a
	      entropy->ct = -1;			/* magnitude overflow */
kusano 7d535a
	      return TRUE;
kusano 7d535a
	    }
kusano 7d535a
	    st += 1;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      v = m;
kusano 7d535a
      /* Figure F.24: Decoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	if (arith_decode(cinfo, st)) v |= m;
kusano 7d535a
      v += 1; if (sign) v = -v;
kusano 7d535a
      (*block)[natural_order[k]] = (JCOEF) v;
kusano 7d535a
    } while (k < cinfo->lim_Se);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for an arithmetic-compressed scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  int ci, tbl;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
kusano 7d535a
  if (cinfo->progressive_mode) {
kusano 7d535a
    /* Validate progressive scan parameters */
kusano 7d535a
    if (cinfo->Ss == 0) {
kusano 7d535a
      if (cinfo->Se != 0)
kusano 7d535a
	goto bad;
kusano 7d535a
    } else {
kusano 7d535a
      /* need not check Ss/Se < 0 since they came from unsigned bytes */
kusano 7d535a
      if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
kusano 7d535a
	goto bad;
kusano 7d535a
      /* AC scans may have only one component */
kusano 7d535a
      if (cinfo->comps_in_scan != 1)
kusano 7d535a
	goto bad;
kusano 7d535a
    }
kusano 7d535a
    if (cinfo->Ah != 0) {
kusano 7d535a
      /* Successive approximation refinement scan: must have Al = Ah-1. */
kusano 7d535a
      if (cinfo->Ah-1 != cinfo->Al)
kusano 7d535a
	goto bad;
kusano 7d535a
    }
kusano 7d535a
    if (cinfo->Al > 13) {	/* need not check for < 0 */
kusano 7d535a
      bad:
kusano 7d535a
      ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
kusano 7d535a
	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
kusano 7d535a
    }
kusano 7d535a
    /* Update progression status, and verify that scan order is legal.
kusano 7d535a
     * Note that inter-scan inconsistencies are treated as warnings
kusano 7d535a
     * not fatal errors ... not clear if this is right way to behave.
kusano 7d535a
     */
kusano 7d535a
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
      int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
kusano 7d535a
      int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
kusano 7d535a
      if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
kusano 7d535a
	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
kusano 7d535a
      for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
kusano 7d535a
	int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
kusano 7d535a
	if (cinfo->Ah != expected)
kusano 7d535a
	  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
kusano 7d535a
	coef_bit_ptr[coefi] = cinfo->Al;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Select MCU decoding routine */
kusano 7d535a
    if (cinfo->Ah == 0) {
kusano 7d535a
      if (cinfo->Ss == 0)
kusano 7d535a
	entropy->pub.decode_mcu = decode_mcu_DC_first;
kusano 7d535a
      else
kusano 7d535a
	entropy->pub.decode_mcu = decode_mcu_AC_first;
kusano 7d535a
    } else {
kusano 7d535a
      if (cinfo->Ss == 0)
kusano 7d535a
	entropy->pub.decode_mcu = decode_mcu_DC_refine;
kusano 7d535a
      else
kusano 7d535a
	entropy->pub.decode_mcu = decode_mcu_AC_refine;
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
kusano 7d535a
     * This ought to be an error condition, but we make it a warning.
kusano 7d535a
     */
kusano 7d535a
    if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
kusano 7d535a
	(cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
kusano 7d535a
      WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
kusano 7d535a
    /* Select MCU decoding routine */
kusano 7d535a
    entropy->pub.decode_mcu = decode_mcu;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Allocate & initialize requested statistics areas */
kusano 7d535a
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
kusano 7d535a
      tbl = compptr->dc_tbl_no;
kusano 7d535a
      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
kusano 7d535a
	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
kusano 7d535a
      if (entropy->dc_stats[tbl] == NULL)
kusano 7d535a
	entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
kusano 7d535a
	  ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
kusano 7d535a
      MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
kusano 7d535a
      /* Initialize DC predictions to 0 */
kusano 7d535a
      entropy->last_dc_val[ci] = 0;
kusano 7d535a
      entropy->dc_context[ci] = 0;
kusano 7d535a
    }
kusano 7d535a
    if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
kusano 7d535a
	(cinfo->progressive_mode && cinfo->Ss)) {
kusano 7d535a
      tbl = compptr->ac_tbl_no;
kusano 7d535a
      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
kusano 7d535a
	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
kusano 7d535a
      if (entropy->ac_stats[tbl] == NULL)
kusano 7d535a
	entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
kusano 7d535a
	  ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
kusano 7d535a
      MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Initialize arithmetic decoding variables */
kusano 7d535a
  entropy->c = 0;
kusano 7d535a
  entropy->a = 0;
kusano 7d535a
  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */
kusano 7d535a
kusano 7d535a
  /* Initialize restart counter */
kusano 7d535a
  entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for arithmetic entropy decoding.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_arith_decoder (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr entropy;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  entropy = (arith_entropy_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(arith_entropy_decoder));
kusano 7d535a
  cinfo->entropy = &entropy->pub;
kusano 7d535a
  entropy->pub.start_pass = start_pass;
kusano 7d535a
kusano 7d535a
  /* Mark tables unallocated */
kusano 7d535a
  for (i = 0; i < NUM_ARITH_TBLS; i++) {
kusano 7d535a
    entropy->dc_stats[i] = NULL;
kusano 7d535a
    entropy->ac_stats[i] = NULL;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Initialize index for fixed probability estimation */
kusano 7d535a
  entropy->fixed_bin[0] = 113;
kusano 7d535a
kusano 7d535a
  if (cinfo->progressive_mode) {
kusano 7d535a
    /* Create progression status table */
kusano 7d535a
    int *coef_bit_ptr, ci;
kusano 7d535a
    cinfo->coef_bits = (int (*)[DCTSIZE2])
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  cinfo->num_components*DCTSIZE2*SIZEOF(int));
kusano 7d535a
    coef_bit_ptr = & cinfo->coef_bits[0][0];
kusano 7d535a
    for (ci = 0; ci < cinfo->num_components; ci++) 
kusano 7d535a
      for (i = 0; i < DCTSIZE2; i++)
kusano 7d535a
	*coef_bit_ptr++ = -1;
kusano 7d535a
  }
kusano 7d535a
}