kusano 7d535a
/*
kusano 7d535a
 * jcarith.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 encoding 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 encoder object for arithmetic encoding. */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_entropy_encoder pub; /* public fields */
kusano 7d535a
kusano 7d535a
  INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */
kusano 7d535a
  INT32 a;               /* A register, normalized size of coding interval */
kusano 7d535a
  INT32 sc;        /* counter for stacked 0xFF values which might overflow */
kusano 7d535a
  INT32 zc;          /* counter for pending 0x00 output values which might *
kusano 7d535a
                          * be discarded at the end ("Pacman" termination) */
kusano 7d535a
  int ct;  /* bit shift counter, determines when next byte will be written */
kusano 7d535a
  int buffer;                /* buffer for most recent output byte != 0xFF */
kusano 7d535a
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
  int next_restart_num;		/* next restart number to write (0-7) */
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_encoder;
kusano 7d535a
kusano 7d535a
typedef arith_entropy_encoder * 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
/* NOTE: Uncomment the following #define if you want to use the
kusano 7d535a
 * given formula for calculating the AC conditioning parameter Kx
kusano 7d535a
 * for spectral selection progressive coding in section G.1.3.2
kusano 7d535a
 * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4).
kusano 7d535a
 * Although the spec and P&M authors claim that this "has proven
kusano 7d535a
 * to give good results for 8 bit precision samples", I'm not
kusano 7d535a
 * convinced yet that this is really beneficial.
kusano 7d535a
 * Early tests gave only very marginal compression enhancements
kusano 7d535a
 * (a few - around 5 or so - bytes even for very large files),
kusano 7d535a
 * which would turn out rather negative if we'd suppress the
kusano 7d535a
 * DAC (Define Arithmetic Conditioning) marker segments for
kusano 7d535a
 * the default parameters in the future.
kusano 7d535a
 * Note that currently the marker writing module emits 12-byte
kusano 7d535a
 * DAC segments for a full-component scan in a color image.
kusano 7d535a
 * This is not worth worrying about IMHO. However, since the
kusano 7d535a
 * spec defines the default values to be used if the tables
kusano 7d535a
 * are omitted (unlike Huffman tables, which are required
kusano 7d535a
 * anyway), one might optimize this behaviour in the future,
kusano 7d535a
 * and then it would be disadvantageous to use custom tables if
kusano 7d535a
 * they don't provide sufficient gain to exceed the DAC size.
kusano 7d535a
 *
kusano 7d535a
 * On the other hand, I'd consider it as a reasonable result
kusano 7d535a
 * that the conditioning has no significant influence on the
kusano 7d535a
 * compression performance. This means that the basic
kusano 7d535a
 * statistical model is already rather stable.
kusano 7d535a
 *
kusano 7d535a
 * Thus, at the moment, we use the default conditioning values
kusano 7d535a
 * anyway, and do not use the custom formula.
kusano 7d535a
 *
kusano 7d535a
#define CALCULATE_SPECTRAL_CONDITIONING
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
kusano 7d535a
 * We assume that int right shift is unsigned if INT32 right shift is,
kusano 7d535a
 * which should be safe.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifdef RIGHT_SHIFT_IS_UNSIGNED
kusano 7d535a
#define ISHIFT_TEMPS	int ishift_temp;
kusano 7d535a
#define IRIGHT_SHIFT(x,shft)  \
kusano 7d535a
	((ishift_temp = (x)) < 0 ? \
kusano 7d535a
	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
kusano 7d535a
	 (ishift_temp >> (shft)))
kusano 7d535a
#else
kusano 7d535a
#define ISHIFT_TEMPS
kusano 7d535a
#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_byte (int val, j_compress_ptr cinfo)
kusano 7d535a
/* Write next output byte; we do not support suspension in this module. */
kusano 7d535a
{
kusano 7d535a
  struct jpeg_destination_mgr * dest = cinfo->dest;
kusano 7d535a
kusano 7d535a
  *dest->next_output_byte++ = (JOCTET) val;
kusano 7d535a
  if (--dest->free_in_buffer == 0)
kusano 7d535a
    if (! (*dest->empty_output_buffer) (cinfo))
kusano 7d535a
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of an arithmetic-compressed scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_pass (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
kusano 7d535a
  INT32 temp;
kusano 7d535a
kusano 7d535a
  /* Section D.1.8: Termination of encoding */
kusano 7d535a
kusano 7d535a
  /* Find the e->c in the coding interval with the largest
kusano 7d535a
   * number of trailing zero bits */
kusano 7d535a
  if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c)
kusano 7d535a
    e->c = temp + 0x8000L;
kusano 7d535a
  else
kusano 7d535a
    e->c = temp;
kusano 7d535a
  /* Send remaining bytes to output */
kusano 7d535a
  e->c <<= e->ct;
kusano 7d535a
  if (e->c & 0xF8000000L) {
kusano 7d535a
    /* One final overflow has to be handled */
kusano 7d535a
    if (e->buffer >= 0) {
kusano 7d535a
      if (e->zc)
kusano 7d535a
	do emit_byte(0x00, cinfo);
kusano 7d535a
	while (--e->zc);
kusano 7d535a
      emit_byte(e->buffer + 1, cinfo);
kusano 7d535a
      if (e->buffer + 1 == 0xFF)
kusano 7d535a
	emit_byte(0x00, cinfo);
kusano 7d535a
    }
kusano 7d535a
    e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
kusano 7d535a
    e->sc = 0;
kusano 7d535a
  } else {
kusano 7d535a
    if (e->buffer == 0)
kusano 7d535a
      ++e->zc;
kusano 7d535a
    else if (e->buffer >= 0) {
kusano 7d535a
      if (e->zc)
kusano 7d535a
	do emit_byte(0x00, cinfo);
kusano 7d535a
	while (--e->zc);
kusano 7d535a
      emit_byte(e->buffer, cinfo);
kusano 7d535a
    }
kusano 7d535a
    if (e->sc) {
kusano 7d535a
      if (e->zc)
kusano 7d535a
	do emit_byte(0x00, cinfo);
kusano 7d535a
	while (--e->zc);
kusano 7d535a
      do {
kusano 7d535a
	emit_byte(0xFF, cinfo);
kusano 7d535a
	emit_byte(0x00, cinfo);
kusano 7d535a
      } while (--e->sc);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  /* Output final bytes only if they are not 0x00 */
kusano 7d535a
  if (e->c & 0x7FFF800L) {
kusano 7d535a
    if (e->zc)  /* output final pending zero bytes */
kusano 7d535a
      do emit_byte(0x00, cinfo);
kusano 7d535a
      while (--e->zc);
kusano 7d535a
    emit_byte((e->c >> 19) & 0xFF, cinfo);
kusano 7d535a
    if (((e->c >> 19) & 0xFF) == 0xFF)
kusano 7d535a
      emit_byte(0x00, cinfo);
kusano 7d535a
    if (e->c & 0x7F800L) {
kusano 7d535a
      emit_byte((e->c >> 11) & 0xFF, cinfo);
kusano 7d535a
      if (((e->c >> 11) & 0xFF) == 0xFF)
kusano 7d535a
	emit_byte(0x00, cinfo);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The core arithmetic encoding 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
 * Parameter 'val' to be encoded may be 0 or 1 (binary decision).
kusano 7d535a
 *
kusano 7d535a
 * Note: I've added full "Pacman" termination support to the
kusano 7d535a
 * byte output routines, which is equivalent to the optional
kusano 7d535a
 * Discard_final_zeros procedure (Figure D.15) in the spec.
kusano 7d535a
 * Thus, we always produce the shortest possible output
kusano 7d535a
 * stream compliant to the spec (no trailing zero bytes,
kusano 7d535a
 * except for FF stuffing).
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(void)
kusano 7d535a
arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) 
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;
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
  /* Encode & estimation procedures per sections D.1.4 & D.1.5 */
kusano 7d535a
  e->a -= qe;
kusano 7d535a
  if (val != (sv >> 7)) {
kusano 7d535a
    /* Encode the less probable symbol */
kusano 7d535a
    if (e->a >= qe) {
kusano 7d535a
      /* If the interval size (qe) for the less probable symbol (LPS)
kusano 7d535a
       * is larger than the interval size for the MPS, then exchange
kusano 7d535a
       * the two symbols for coding efficiency, otherwise code the LPS
kusano 7d535a
       * as usual: */
kusano 7d535a
      e->c += e->a;
kusano 7d535a
      e->a = qe;
kusano 7d535a
    }
kusano 7d535a
    *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
kusano 7d535a
  } else {
kusano 7d535a
    /* Encode the more probable symbol */
kusano 7d535a
    if (e->a >= 0x8000L)
kusano 7d535a
      return;  /* A >= 0x8000 -> ready, no renormalization required */
kusano 7d535a
    if (e->a < qe) {
kusano 7d535a
      /* If the interval size (qe) for the less probable symbol (LPS)
kusano 7d535a
       * is larger than the interval size for the MPS, then exchange
kusano 7d535a
       * the two symbols for coding efficiency: */
kusano 7d535a
      e->c += e->a;
kusano 7d535a
      e->a = qe;
kusano 7d535a
    }
kusano 7d535a
    *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Renormalization & data output per section D.1.6 */
kusano 7d535a
  do {
kusano 7d535a
    e->a <<= 1;
kusano 7d535a
    e->c <<= 1;
kusano 7d535a
    if (--e->ct == 0) {
kusano 7d535a
      /* Another byte is ready for output */
kusano 7d535a
      temp = e->c >> 19;
kusano 7d535a
      if (temp > 0xFF) {
kusano 7d535a
	/* Handle overflow over all stacked 0xFF bytes */
kusano 7d535a
	if (e->buffer >= 0) {
kusano 7d535a
	  if (e->zc)
kusano 7d535a
	    do emit_byte(0x00, cinfo);
kusano 7d535a
	    while (--e->zc);
kusano 7d535a
	  emit_byte(e->buffer + 1, cinfo);
kusano 7d535a
	  if (e->buffer + 1 == 0xFF)
kusano 7d535a
	    emit_byte(0x00, cinfo);
kusano 7d535a
	}
kusano 7d535a
	e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
kusano 7d535a
	e->sc = 0;
kusano 7d535a
	/* Note: The 3 spacer bits in the C register guarantee
kusano 7d535a
	 * that the new buffer byte can't be 0xFF here
kusano 7d535a
	 * (see page 160 in the P&M JPEG book). */
kusano 7d535a
	e->buffer = temp & 0xFF;  /* new output byte, might overflow later */
kusano 7d535a
      } else if (temp == 0xFF) {
kusano 7d535a
	++e->sc;  /* stack 0xFF byte (which might overflow later) */
kusano 7d535a
      } else {
kusano 7d535a
	/* Output all stacked 0xFF bytes, they will not overflow any more */
kusano 7d535a
	if (e->buffer == 0)
kusano 7d535a
	  ++e->zc;
kusano 7d535a
	else if (e->buffer >= 0) {
kusano 7d535a
	  if (e->zc)
kusano 7d535a
	    do emit_byte(0x00, cinfo);
kusano 7d535a
	    while (--e->zc);
kusano 7d535a
	  emit_byte(e->buffer, cinfo);
kusano 7d535a
	}
kusano 7d535a
	if (e->sc) {
kusano 7d535a
	  if (e->zc)
kusano 7d535a
	    do emit_byte(0x00, cinfo);
kusano 7d535a
	    while (--e->zc);
kusano 7d535a
	  do {
kusano 7d535a
	    emit_byte(0xFF, cinfo);
kusano 7d535a
	    emit_byte(0x00, cinfo);
kusano 7d535a
	  } while (--e->sc);
kusano 7d535a
	}
kusano 7d535a
	e->buffer = temp & 0xFF;  /* new output byte (can still overflow) */
kusano 7d535a
      }
kusano 7d535a
      e->c &= 0x7FFFFL;
kusano 7d535a
      e->ct += 8;
kusano 7d535a
    }
kusano 7d535a
  } while (e->a < 0x8000L);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Emit a restart marker & resynchronize predictions.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_restart (j_compress_ptr cinfo, int restart_num)
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
  finish_pass(cinfo);
kusano 7d535a
kusano 7d535a
  emit_byte(0xFF, cinfo);
kusano 7d535a
  emit_byte(JPEG_RST0 + restart_num, cinfo);
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
    /* DC needs no table for refinement scan */
kusano 7d535a
    if (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
    /* AC needs no table when not present */
kusano 7d535a
    if (cinfo->Se) {
kusano 7d535a
      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Reset arithmetic encoding variables */
kusano 7d535a
  entropy->c = 0;
kusano 7d535a
  entropy->a = 0x10000L;
kusano 7d535a
  entropy->sc = 0;
kusano 7d535a
  entropy->zc = 0;
kusano 7d535a
  entropy->ct = 11;
kusano 7d535a
  entropy->buffer = -1;  /* empty */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU encoding 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
encode_mcu_DC_first (j_compress_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;
kusano 7d535a
  int v, v2, m;
kusano 7d535a
  ISHIFT_TEMPS
kusano 7d535a
kusano 7d535a
  /* Emit restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0) {
kusano 7d535a
      emit_restart(cinfo, entropy->next_restart_num);
kusano 7d535a
      entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
      entropy->next_restart_num++;
kusano 7d535a
      entropy->next_restart_num &= 7;
kusano 7d535a
    }
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Encode the MCU data blocks */
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
    /* Compute the DC value after the required point transform by Al.
kusano 7d535a
     * This is simply an arithmetic right shift.
kusano 7d535a
     */
kusano 7d535a
    m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al);
kusano 7d535a
kusano 7d535a
    /* Sections F.1.4.1 & F.1.4.4.1: Encoding 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.4: Encode_DC_DIFF */
kusano 7d535a
    if ((v = m - entropy->last_dc_val[ci]) == 0) {
kusano 7d535a
      arith_encode(cinfo, st, 0);
kusano 7d535a
      entropy->dc_context[ci] = 0;	/* zero diff category */
kusano 7d535a
    } else {
kusano 7d535a
      entropy->last_dc_val[ci] = m;
kusano 7d535a
      arith_encode(cinfo, st, 1);
kusano 7d535a
      /* Figure F.6: Encoding nonzero value v */
kusano 7d535a
      /* Figure F.7: Encoding the sign of v */
kusano 7d535a
      if (v > 0) {
kusano 7d535a
	arith_encode(cinfo, st + 1, 0);	/* Table F.4: SS = S0 + 1 */
kusano 7d535a
	st += 2;			/* Table F.4: SP = S0 + 2 */
kusano 7d535a
	entropy->dc_context[ci] = 4;	/* small positive diff category */
kusano 7d535a
      } else {
kusano 7d535a
	v = -v;
kusano 7d535a
	arith_encode(cinfo, st + 1, 1);	/* Table F.4: SS = S0 + 1 */
kusano 7d535a
	st += 3;			/* Table F.4: SN = S0 + 3 */
kusano 7d535a
	entropy->dc_context[ci] = 8;	/* small negative diff category */
kusano 7d535a
      }
kusano 7d535a
      /* Figure F.8: Encoding the magnitude category of v */
kusano 7d535a
      m = 0;
kusano 7d535a
      if (v -= 1) {
kusano 7d535a
	arith_encode(cinfo, st, 1);
kusano 7d535a
	m = 1;
kusano 7d535a
	v2 = v;
kusano 7d535a
	st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
kusano 7d535a
	while (v2 >>= 1) {
kusano 7d535a
	  arith_encode(cinfo, st, 1);
kusano 7d535a
	  m <<= 1;
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st, 0);
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] += 8;	/* large diff category */
kusano 7d535a
      /* Figure F.9: Encoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	arith_encode(cinfo, st, (m & v) ? 1 : 0);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU encoding 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
encode_mcu_AC_first (j_compress_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, k, ke;
kusano 7d535a
  int v, v2, m;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Emit restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0) {
kusano 7d535a
      emit_restart(cinfo, entropy->next_restart_num);
kusano 7d535a
      entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
      entropy->next_restart_num++;
kusano 7d535a
      entropy->next_restart_num &= 7;
kusano 7d535a
    }
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* Encode the MCU data block */
kusano 7d535a
  block = MCU_data[0];
kusano 7d535a
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
kusano 7d535a
kusano 7d535a
  /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
kusano 7d535a
kusano 7d535a
  /* Establish EOB (end-of-block) index */
kusano 7d535a
  ke = cinfo->Se;
kusano 7d535a
  do {
kusano 7d535a
    /* We must apply the point transform by Al.  For AC coefficients this
kusano 7d535a
     * is an integer division with rounding towards 0.  To do this portably
kusano 7d535a
     * in C, we shift after obtaining the absolute value.
kusano 7d535a
     */
kusano 7d535a
    if ((v = (*block)[natural_order[ke]]) >= 0) {
kusano 7d535a
      if (v >>= cinfo->Al) break;
kusano 7d535a
    } else {
kusano 7d535a
      v = -v;
kusano 7d535a
      if (v >>= cinfo->Al) break;
kusano 7d535a
    }
kusano 7d535a
  } while (--ke);
kusano 7d535a
kusano 7d535a
  /* Figure F.5: Encode_AC_Coefficients */
kusano 7d535a
  for (k = cinfo->Ss - 1; k < ke;) {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    arith_encode(cinfo, st, 0);		/* EOB decision */
kusano 7d535a
    for (;;) {
kusano 7d535a
      if ((v = (*block)[natural_order[++k]]) >= 0) {
kusano 7d535a
	if (v >>= cinfo->Al) {
kusano 7d535a
	  arith_encode(cinfo, st + 1, 1);
kusano 7d535a
	  arith_encode(cinfo, entropy->fixed_bin, 0);
kusano 7d535a
	  break;
kusano 7d535a
	}
kusano 7d535a
      } else {
kusano 7d535a
	v = -v;
kusano 7d535a
	if (v >>= cinfo->Al) {
kusano 7d535a
	  arith_encode(cinfo, st + 1, 1);
kusano 7d535a
	  arith_encode(cinfo, entropy->fixed_bin, 1);
kusano 7d535a
	  break;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st + 1, 0);
kusano 7d535a
      st += 3;
kusano 7d535a
    }
kusano 7d535a
    st += 2;
kusano 7d535a
    /* Figure F.8: Encoding the magnitude category of v */
kusano 7d535a
    m = 0;
kusano 7d535a
    if (v -= 1) {
kusano 7d535a
      arith_encode(cinfo, st, 1);
kusano 7d535a
      m = 1;
kusano 7d535a
      v2 = v;
kusano 7d535a
      if (v2 >>= 1) {
kusano 7d535a
	arith_encode(cinfo, st, 1);
kusano 7d535a
	m <<= 1;
kusano 7d535a
	st = entropy->ac_stats[tbl] +
kusano 7d535a
	     (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
kusano 7d535a
	while (v2 >>= 1) {
kusano 7d535a
	  arith_encode(cinfo, st, 1);
kusano 7d535a
	  m <<= 1;
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    arith_encode(cinfo, st, 0);
kusano 7d535a
    /* Figure F.9: Encoding the magnitude bit pattern of v */
kusano 7d535a
    st += 14;
kusano 7d535a
    while (m >>= 1)
kusano 7d535a
      arith_encode(cinfo, st, (m & v) ? 1 : 0);
kusano 7d535a
  }
kusano 7d535a
  /* Encode EOB decision only if k < cinfo->Se */
kusano 7d535a
  if (k < cinfo->Se) {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    arith_encode(cinfo, st, 1);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU encoding for DC successive approximation refinement scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
encode_mcu_DC_refine (j_compress_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 Al, blkn;
kusano 7d535a
kusano 7d535a
  /* Emit restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0) {
kusano 7d535a
      emit_restart(cinfo, entropy->next_restart_num);
kusano 7d535a
      entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
      entropy->next_restart_num++;
kusano 7d535a
      entropy->next_restart_num &= 7;
kusano 7d535a
    }
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  st = entropy->fixed_bin;	/* use fixed probability estimation */
kusano 7d535a
  Al = cinfo->Al;
kusano 7d535a
kusano 7d535a
  /* Encode the MCU data blocks */
kusano 7d535a
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
kusano 7d535a
    /* We simply emit the Al'th bit of the DC coefficient value. */
kusano 7d535a
    arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * MCU encoding for AC successive approximation refinement scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
encode_mcu_AC_refine (j_compress_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, k, ke, kex;
kusano 7d535a
  int v;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Emit restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0) {
kusano 7d535a
      emit_restart(cinfo, entropy->next_restart_num);
kusano 7d535a
      entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
      entropy->next_restart_num++;
kusano 7d535a
      entropy->next_restart_num &= 7;
kusano 7d535a
    }
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* Encode the MCU data block */
kusano 7d535a
  block = MCU_data[0];
kusano 7d535a
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
kusano 7d535a
kusano 7d535a
  /* Section G.1.3.3: Encoding of AC coefficients */
kusano 7d535a
kusano 7d535a
  /* Establish EOB (end-of-block) index */
kusano 7d535a
  ke = cinfo->Se;
kusano 7d535a
  do {
kusano 7d535a
    /* We must apply the point transform by Al.  For AC coefficients this
kusano 7d535a
     * is an integer division with rounding towards 0.  To do this portably
kusano 7d535a
     * in C, we shift after obtaining the absolute value.
kusano 7d535a
     */
kusano 7d535a
    if ((v = (*block)[natural_order[ke]]) >= 0) {
kusano 7d535a
      if (v >>= cinfo->Al) break;
kusano 7d535a
    } else {
kusano 7d535a
      v = -v;
kusano 7d535a
      if (v >>= cinfo->Al) break;
kusano 7d535a
    }
kusano 7d535a
  } while (--ke);
kusano 7d535a
kusano 7d535a
  /* Establish EOBx (previous stage end-of-block) index */
kusano 7d535a
  for (kex = ke; kex > 0; kex--)
kusano 7d535a
    if ((v = (*block)[natural_order[kex]]) >= 0) {
kusano 7d535a
      if (v >>= cinfo->Ah) break;
kusano 7d535a
    } else {
kusano 7d535a
      v = -v;
kusano 7d535a
      if (v >>= cinfo->Ah) break;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
  /* Figure G.10: Encode_AC_Coefficients_SA */
kusano 7d535a
  for (k = cinfo->Ss - 1; k < ke;) {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    if (k >= kex)
kusano 7d535a
      arith_encode(cinfo, st, 0);	/* EOB decision */
kusano 7d535a
    for (;;) {
kusano 7d535a
      if ((v = (*block)[natural_order[++k]]) >= 0) {
kusano 7d535a
	if (v >>= cinfo->Al) {
kusano 7d535a
	  if (v >> 1)			/* previously nonzero coef */
kusano 7d535a
	    arith_encode(cinfo, st + 2, (v & 1));
kusano 7d535a
	  else {			/* newly nonzero coef */
kusano 7d535a
	    arith_encode(cinfo, st + 1, 1);
kusano 7d535a
	    arith_encode(cinfo, entropy->fixed_bin, 0);
kusano 7d535a
	  }
kusano 7d535a
	  break;
kusano 7d535a
	}
kusano 7d535a
      } else {
kusano 7d535a
	v = -v;
kusano 7d535a
	if (v >>= cinfo->Al) {
kusano 7d535a
	  if (v >> 1)			/* previously nonzero coef */
kusano 7d535a
	    arith_encode(cinfo, st + 2, (v & 1));
kusano 7d535a
	  else {			/* newly nonzero coef */
kusano 7d535a
	    arith_encode(cinfo, st + 1, 1);
kusano 7d535a
	    arith_encode(cinfo, entropy->fixed_bin, 1);
kusano 7d535a
	  }
kusano 7d535a
	  break;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st + 1, 0);
kusano 7d535a
      st += 3;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  /* Encode EOB decision only if k < cinfo->Se */
kusano 7d535a
  if (k < cinfo->Se) {
kusano 7d535a
    st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
    arith_encode(cinfo, st, 1);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Encode and output one MCU's worth of arithmetic-compressed coefficients.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
encode_mcu (j_compress_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, k, ke;
kusano 7d535a
  int v, v2, m;
kusano 7d535a
  const int * natural_order;
kusano 7d535a
kusano 7d535a
  /* Emit restart marker if needed */
kusano 7d535a
  if (cinfo->restart_interval) {
kusano 7d535a
    if (entropy->restarts_to_go == 0) {
kusano 7d535a
      emit_restart(cinfo, entropy->next_restart_num);
kusano 7d535a
      entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
      entropy->next_restart_num++;
kusano 7d535a
      entropy->next_restart_num &= 7;
kusano 7d535a
    }
kusano 7d535a
    entropy->restarts_to_go--;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  natural_order = cinfo->natural_order;
kusano 7d535a
kusano 7d535a
  /* Encode the MCU data blocks */
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.1.4.1 & F.1.4.4.1: Encoding 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.4: Encode_DC_DIFF */
kusano 7d535a
    if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) {
kusano 7d535a
      arith_encode(cinfo, st, 0);
kusano 7d535a
      entropy->dc_context[ci] = 0;	/* zero diff category */
kusano 7d535a
    } else {
kusano 7d535a
      entropy->last_dc_val[ci] = (*block)[0];
kusano 7d535a
      arith_encode(cinfo, st, 1);
kusano 7d535a
      /* Figure F.6: Encoding nonzero value v */
kusano 7d535a
      /* Figure F.7: Encoding the sign of v */
kusano 7d535a
      if (v > 0) {
kusano 7d535a
	arith_encode(cinfo, st + 1, 0);	/* Table F.4: SS = S0 + 1 */
kusano 7d535a
	st += 2;			/* Table F.4: SP = S0 + 2 */
kusano 7d535a
	entropy->dc_context[ci] = 4;	/* small positive diff category */
kusano 7d535a
      } else {
kusano 7d535a
	v = -v;
kusano 7d535a
	arith_encode(cinfo, st + 1, 1);	/* Table F.4: SS = S0 + 1 */
kusano 7d535a
	st += 3;			/* Table F.4: SN = S0 + 3 */
kusano 7d535a
	entropy->dc_context[ci] = 8;	/* small negative diff category */
kusano 7d535a
      }
kusano 7d535a
      /* Figure F.8: Encoding the magnitude category of v */
kusano 7d535a
      m = 0;
kusano 7d535a
      if (v -= 1) {
kusano 7d535a
	arith_encode(cinfo, st, 1);
kusano 7d535a
	m = 1;
kusano 7d535a
	v2 = v;
kusano 7d535a
	st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
kusano 7d535a
	while (v2 >>= 1) {
kusano 7d535a
	  arith_encode(cinfo, st, 1);
kusano 7d535a
	  m <<= 1;
kusano 7d535a
	  st += 1;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st, 0);
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] += 8;	/* large diff category */
kusano 7d535a
      /* Figure F.9: Encoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	arith_encode(cinfo, st, (m & v) ? 1 : 0);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
kusano 7d535a
kusano 7d535a
    if ((ke = cinfo->lim_Se) == 0) continue;
kusano 7d535a
    tbl = compptr->ac_tbl_no;
kusano 7d535a
kusano 7d535a
    /* Establish EOB (end-of-block) index */
kusano 7d535a
    do {
kusano 7d535a
      if ((*block)[natural_order[ke]]) break;
kusano 7d535a
    } while (--ke);
kusano 7d535a
kusano 7d535a
    /* Figure F.5: Encode_AC_Coefficients */
kusano 7d535a
    for (k = 0; k < ke;) {
kusano 7d535a
      st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
      arith_encode(cinfo, st, 0);	/* EOB decision */
kusano 7d535a
      while ((v = (*block)[natural_order[++k]]) == 0) {
kusano 7d535a
	arith_encode(cinfo, st + 1, 0);
kusano 7d535a
	st += 3;
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st + 1, 1);
kusano 7d535a
      /* Figure F.6: Encoding nonzero value v */
kusano 7d535a
      /* Figure F.7: Encoding the sign of v */
kusano 7d535a
      if (v > 0) {
kusano 7d535a
	arith_encode(cinfo, entropy->fixed_bin, 0);
kusano 7d535a
      } else {
kusano 7d535a
	v = -v;
kusano 7d535a
	arith_encode(cinfo, entropy->fixed_bin, 1);
kusano 7d535a
      }
kusano 7d535a
      st += 2;
kusano 7d535a
      /* Figure F.8: Encoding the magnitude category of v */
kusano 7d535a
      m = 0;
kusano 7d535a
      if (v -= 1) {
kusano 7d535a
	arith_encode(cinfo, st, 1);
kusano 7d535a
	m = 1;
kusano 7d535a
	v2 = v;
kusano 7d535a
	if (v2 >>= 1) {
kusano 7d535a
	  arith_encode(cinfo, st, 1);
kusano 7d535a
	  m <<= 1;
kusano 7d535a
	  st = entropy->ac_stats[tbl] +
kusano 7d535a
	       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
kusano 7d535a
	  while (v2 >>= 1) {
kusano 7d535a
	    arith_encode(cinfo, st, 1);
kusano 7d535a
	    m <<= 1;
kusano 7d535a
	    st += 1;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      arith_encode(cinfo, st, 0);
kusano 7d535a
      /* Figure F.9: Encoding the magnitude bit pattern of v */
kusano 7d535a
      st += 14;
kusano 7d535a
      while (m >>= 1)
kusano 7d535a
	arith_encode(cinfo, st, (m & v) ? 1 : 0);
kusano 7d535a
    }
kusano 7d535a
    /* Encode EOB decision only if k < cinfo->lim_Se */
kusano 7d535a
    if (k < cinfo->lim_Se) {
kusano 7d535a
      st = entropy->ac_stats[tbl] + 3 * k;
kusano 7d535a
      arith_encode(cinfo, st, 1);
kusano 7d535a
    }
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_compress_ptr cinfo, boolean gather_statistics)
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 (gather_statistics)
kusano 7d535a
    /* Make sure to avoid that in the master control logic!
kusano 7d535a
     * We are fully adaptive here and need no extra
kusano 7d535a
     * statistics gathering pass!
kusano 7d535a
     */
kusano 7d535a
    ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
kusano 7d535a
  /* We assume jcmaster.c already validated the progressive scan parameters. */
kusano 7d535a
kusano 7d535a
  /* Select execution routines */
kusano 7d535a
  if (cinfo->progressive_mode) {
kusano 7d535a
    if (cinfo->Ah == 0) {
kusano 7d535a
      if (cinfo->Ss == 0)
kusano 7d535a
	entropy->pub.encode_mcu = encode_mcu_DC_first;
kusano 7d535a
      else
kusano 7d535a
	entropy->pub.encode_mcu = encode_mcu_AC_first;
kusano 7d535a
    } else {
kusano 7d535a
      if (cinfo->Ss == 0)
kusano 7d535a
	entropy->pub.encode_mcu = encode_mcu_DC_refine;
kusano 7d535a
      else
kusano 7d535a
	entropy->pub.encode_mcu = encode_mcu_AC_refine;
kusano 7d535a
    }
kusano 7d535a
  } else
kusano 7d535a
    entropy->pub.encode_mcu = encode_mcu;
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
    /* DC needs no table for refinement scan */
kusano 7d535a
    if (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
    /* AC needs no table when not present */
kusano 7d535a
    if (cinfo->Se) {
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
#ifdef CALCULATE_SPECTRAL_CONDITIONING
kusano 7d535a
      if (cinfo->progressive_mode)
kusano 7d535a
	/* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
kusano 7d535a
	cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4);
kusano 7d535a
#endif
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Initialize arithmetic encoding variables */
kusano 7d535a
  entropy->c = 0;
kusano 7d535a
  entropy->a = 0x10000L;
kusano 7d535a
  entropy->sc = 0;
kusano 7d535a
  entropy->zc = 0;
kusano 7d535a
  entropy->ct = 11;
kusano 7d535a
  entropy->buffer = -1;  /* empty */
kusano 7d535a
kusano 7d535a
  /* Initialize restart stuff */
kusano 7d535a
  entropy->restarts_to_go = cinfo->restart_interval;
kusano 7d535a
  entropy->next_restart_num = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for arithmetic entropy encoding.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_arith_encoder (j_compress_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_encoder));
kusano 7d535a
  cinfo->entropy = &entropy->pub;
kusano 7d535a
  entropy->pub.start_pass = start_pass;
kusano 7d535a
  entropy->pub.finish_pass = finish_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
}