shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jcphuff.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1995-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2011, 2015, 2018, D. R. Commander.
shun-iwasawa 82a8f5
 * Copyright (C) 2016, 2018, Matthieu Darbois.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains Huffman entropy encoding routines for progressive JPEG.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We do not support output suspension in this module, since the library
shun-iwasawa 82a8f5
 * currently does not allow multiple-scan files to be written with output
shun-iwasawa 82a8f5
 * suspension.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jsimd.h"
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
#include <limits.h></limits.h>
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef HAVE_INTRIN_H
shun-iwasawa 82a8f5
#include <intrin.h></intrin.h>
shun-iwasawa 82a8f5
#ifdef _MSC_VER
shun-iwasawa 82a8f5
#ifdef HAVE_BITSCANFORWARD64
shun-iwasawa 82a8f5
#pragma intrinsic(_BitScanForward64)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#ifdef HAVE_BITSCANFORWARD
shun-iwasawa 82a8f5
#pragma intrinsic(_BitScanForward)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef C_PROGRESSIVE_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
shun-iwasawa 82a8f5
 * used for bit counting rather than the lookup table.  This will reduce the
shun-iwasawa 82a8f5
 * memory footprint by 64k, which is important for some mobile applications
shun-iwasawa 82a8f5
 * that create many isolated instances of libjpeg-turbo (web browsers, for
shun-iwasawa 82a8f5
 * instance.)  This may improve performance on some mobile platforms as well.
shun-iwasawa 82a8f5
 * This feature is enabled by default only on Arm processors, because some x86
shun-iwasawa 82a8f5
 * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
shun-iwasawa 82a8f5
 * shown to have a significant performance impact even on the x86 chips that
shun-iwasawa 82a8f5
 * have a fast implementation of it.  When building for Armv6, you can
shun-iwasawa 82a8f5
 * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
shun-iwasawa 82a8f5
 * flags (this defines __thumb__).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* NOTE: Both GCC and Clang define __GNUC__ */
shun-iwasawa 82a8f5
#if defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))
shun-iwasawa 82a8f5
#if !defined(__thumb__) || defined(__thumb2__)
shun-iwasawa 82a8f5
#define USE_CLZ_INTRINSIC
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef USE_CLZ_INTRINSIC
shun-iwasawa 82a8f5
#define JPEG_NBITS_NONZERO(x)  (32 - __builtin_clz(x))
shun-iwasawa 82a8f5
#define JPEG_NBITS(x)          (x ? JPEG_NBITS_NONZERO(x) : 0)
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#include "jpeg_nbits_table.h"
shun-iwasawa 82a8f5
#define JPEG_NBITS(x)          (jpeg_nbits_table[x])
shun-iwasawa 82a8f5
#define JPEG_NBITS_NONZERO(x)  JPEG_NBITS(x)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Expanded entropy encoder object for progressive Huffman encoding. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct jpeg_entropy_encoder pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Pointer to routine to prepare data for encode_mcu_AC_first() */
shun-iwasawa 82a8f5
  void (*AC_first_prepare) (const JCOEF *block,
shun-iwasawa 82a8f5
                            const int *jpeg_natural_order_start, int Sl,
shun-iwasawa 82a8f5
                            int Al, JCOEF *values, size_t *zerobits);
shun-iwasawa 82a8f5
  /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
shun-iwasawa 82a8f5
  int (*AC_refine_prepare) (const JCOEF *block,
shun-iwasawa 82a8f5
                            const int *jpeg_natural_order_start, int Sl,
shun-iwasawa 82a8f5
                            int Al, JCOEF *absvalues, size_t *bits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Mode flag: TRUE for optimization, FALSE for actual data output */
shun-iwasawa 82a8f5
  boolean gather_statistics;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Bit-level coding status.
shun-iwasawa 82a8f5
   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  JOCTET *next_output_byte;     /* => next byte to write in buffer */
shun-iwasawa 82a8f5
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
shun-iwasawa 82a8f5
  size_t put_buffer;            /* current bit-accumulation buffer */
shun-iwasawa 82a8f5
  int put_bits;                 /* # of bits now in it */
shun-iwasawa 82a8f5
  j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Coding status for DC components */
shun-iwasawa 82a8f5
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Coding status for AC components */
shun-iwasawa 82a8f5
  int ac_tbl_no;                /* the table number of the single component */
shun-iwasawa 82a8f5
  unsigned int EOBRUN;          /* run length of EOBs */
shun-iwasawa 82a8f5
  unsigned int BE;              /* # of buffered correction bits before MCU */
shun-iwasawa 82a8f5
  char *bit_buffer;             /* buffer for correction bits (1 per char) */
shun-iwasawa 82a8f5
  /* packing correction bits tightly would save some space but cost time... */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
shun-iwasawa 82a8f5
  int next_restart_num;         /* next restart number to write (0-7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Pointers to derived tables (these workspaces have image lifespan).
shun-iwasawa 82a8f5
   * Since any one scan codes only DC or only AC, we only need one set
shun-iwasawa 82a8f5
   * of tables, not one for DC and one for AC.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Statistics tables for optimization; again, one set is enough */
shun-iwasawa 82a8f5
  long *count_ptrs[NUM_HUFF_TBLS];
shun-iwasawa 82a8f5
} phuff_entropy_encoder;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef phuff_entropy_encoder *phuff_entropy_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
shun-iwasawa 82a8f5
 * buffer can hold.  Larger sizes may slightly improve compression, but
shun-iwasawa 82a8f5
 * 1000 is already well into the realm of overkill.
shun-iwasawa 82a8f5
 * The minimum safe size is 64 bits.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
shun-iwasawa 82a8f5
 * We assume that int right shift is unsigned if JLONG right shift is,
shun-iwasawa 82a8f5
 * which should be safe.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef RIGHT_SHIFT_IS_UNSIGNED
shun-iwasawa 82a8f5
#define ISHIFT_TEMPS    int ishift_temp;
shun-iwasawa 82a8f5
#define IRIGHT_SHIFT(x, shft) \
shun-iwasawa 82a8f5
  ((ishift_temp = (x)) < 0 ? \
shun-iwasawa 82a8f5
   (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
shun-iwasawa 82a8f5
   (ishift_temp >> (shft)))
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#define ISHIFT_TEMPS
shun-iwasawa 82a8f5
#define IRIGHT_SHIFT(x, shft)   ((x) >> (shft))
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PAD(v, p)  ((v + (p) - 1) & (~((p) - 1)))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Forward declarations */
shun-iwasawa 82a8f5
METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
shun-iwasawa 82a8f5
                                       JBLOCKROW *MCU_data);
shun-iwasawa 82a8f5
METHODDEF(void) encode_mcu_AC_first_prepare
shun-iwasawa 82a8f5
  (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
shun-iwasawa 82a8f5
   JCOEF *values, size_t *zerobits);
shun-iwasawa 82a8f5
METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
shun-iwasawa 82a8f5
                                       JBLOCKROW *MCU_data);
shun-iwasawa 82a8f5
METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
shun-iwasawa 82a8f5
                                        JBLOCKROW *MCU_data);
shun-iwasawa 82a8f5
METHODDEF(int) encode_mcu_AC_refine_prepare
shun-iwasawa 82a8f5
  (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
shun-iwasawa 82a8f5
   JCOEF *absvalues, size_t *bits);
shun-iwasawa 82a8f5
METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
shun-iwasawa 82a8f5
                                        JBLOCKROW *MCU_data);
shun-iwasawa 82a8f5
METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
shun-iwasawa 82a8f5
METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Count bit loop zeroes */
shun-iwasawa 82a8f5
INLINE
shun-iwasawa 82a8f5
METHODDEF(int)
shun-iwasawa 82a8f5
count_zeroes(size_t *x)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int result;
shun-iwasawa 82a8f5
#if defined(HAVE_BUILTIN_CTZL)
shun-iwasawa 82a8f5
  result = __builtin_ctzl(*x);
shun-iwasawa 82a8f5
  *x >>= result;
shun-iwasawa 82a8f5
#elif defined(HAVE_BITSCANFORWARD64)
shun-iwasawa 82a8f5
  _BitScanForward64(&result, *x);
shun-iwasawa 82a8f5
  *x >>= result;
shun-iwasawa 82a8f5
#elif defined(HAVE_BITSCANFORWARD)
shun-iwasawa 82a8f5
  _BitScanForward(&result, *x);
shun-iwasawa 82a8f5
  *x >>= result;
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  result = 0;
shun-iwasawa 82a8f5
  while ((*x & 1) == 0) {
shun-iwasawa 82a8f5
    ++result;
shun-iwasawa 82a8f5
    *x >>= 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  return result;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize for a Huffman-compressed scan using progressive JPEG.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  boolean is_DC_band;
shun-iwasawa 82a8f5
  int ci, tbl;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->cinfo = cinfo;
shun-iwasawa 82a8f5
  entropy->gather_statistics = gather_statistics;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  is_DC_band = (cinfo->Ss == 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* We assume jcmaster.c already validated the scan parameters. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Select execution routines */
shun-iwasawa 82a8f5
  if (cinfo->Ah == 0) {
shun-iwasawa 82a8f5
    if (is_DC_band)
shun-iwasawa 82a8f5
      entropy->pub.encode_mcu = encode_mcu_DC_first;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      entropy->pub.encode_mcu = encode_mcu_AC_first;
shun-iwasawa 82a8f5
    if (jsimd_can_encode_mcu_AC_first_prepare())
shun-iwasawa 82a8f5
      entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (is_DC_band)
shun-iwasawa 82a8f5
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
shun-iwasawa 82a8f5
    else {
shun-iwasawa 82a8f5
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
shun-iwasawa 82a8f5
      if (jsimd_can_encode_mcu_AC_refine_prepare())
shun-iwasawa 82a8f5
        entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare;
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;
shun-iwasawa 82a8f5
      /* AC refinement needs a correction bit buffer */
shun-iwasawa 82a8f5
      if (entropy->bit_buffer == NULL)
shun-iwasawa 82a8f5
        entropy->bit_buffer = (char *)
shun-iwasawa 82a8f5
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                      MAX_CORR_BITS * sizeof(char));
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (gather_statistics)
shun-iwasawa 82a8f5
    entropy->pub.finish_pass = finish_pass_gather_phuff;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    entropy->pub.finish_pass = finish_pass_phuff;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
shun-iwasawa 82a8f5
   * for AC coefficients.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shun-iwasawa 82a8f5
    compptr = cinfo->cur_comp_info[ci];
shun-iwasawa 82a8f5
    /* Initialize DC predictions to 0 */
shun-iwasawa 82a8f5
    entropy->last_dc_val[ci] = 0;
shun-iwasawa 82a8f5
    /* Get table index */
shun-iwasawa 82a8f5
    if (is_DC_band) {
shun-iwasawa 82a8f5
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
shun-iwasawa 82a8f5
        continue;
shun-iwasawa 82a8f5
      tbl = compptr->dc_tbl_no;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (gather_statistics) {
shun-iwasawa 82a8f5
      /* Check for invalid table index */
shun-iwasawa 82a8f5
      /* (make_c_derived_tbl does this in the other path) */
shun-iwasawa 82a8f5
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
shun-iwasawa 82a8f5
      /* Allocate and zero the statistics tables */
shun-iwasawa 82a8f5
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
shun-iwasawa 82a8f5
      if (entropy->count_ptrs[tbl] == NULL)
shun-iwasawa 82a8f5
        entropy->count_ptrs[tbl] = (long *)
shun-iwasawa 82a8f5
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                      257 * sizeof(long));
shun-iwasawa 82a8f5
      MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long));
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* Compute derived values for Huffman table */
shun-iwasawa 82a8f5
      /* We may do this more than once for a table, but it's not expensive */
shun-iwasawa 82a8f5
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
shun-iwasawa 82a8f5
                              &entropy->derived_tbls[tbl]);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Initialize AC stuff */
shun-iwasawa 82a8f5
  entropy->EOBRUN = 0;
shun-iwasawa 82a8f5
  entropy->BE = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Initialize bit buffer to empty */
shun-iwasawa 82a8f5
  entropy->put_buffer = 0;
shun-iwasawa 82a8f5
  entropy->put_bits = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Initialize restart stuff */
shun-iwasawa 82a8f5
  entropy->restarts_to_go = cinfo->restart_interval;
shun-iwasawa 82a8f5
  entropy->next_restart_num = 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Outputting bytes to the file.
shun-iwasawa 82a8f5
 * NB: these must be called only when actually outputting,
shun-iwasawa 82a8f5
 * that is, entropy->gather_statistics == FALSE.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Emit a byte */
shun-iwasawa 82a8f5
#define emit_byte(entropy, val) { \
shun-iwasawa 82a8f5
  *(entropy)->next_output_byte++ = (JOCTET)(val); \
shun-iwasawa 82a8f5
  if (--(entropy)->free_in_buffer == 0) \
shun-iwasawa 82a8f5
    dump_buffer(entropy); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
dump_buffer(phuff_entropy_ptr entropy)
shun-iwasawa 82a8f5
/* Empty the output buffer; we do not support suspension in this module. */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!(*dest->empty_output_buffer) (entropy->cinfo))
shun-iwasawa 82a8f5
    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
shun-iwasawa 82a8f5
  /* After a successful buffer dump, must reset buffer pointers */
shun-iwasawa 82a8f5
  entropy->next_output_byte = dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = dest->free_in_buffer;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Outputting bits to the file */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Only the right 24 bits of put_buffer are used; the valid bits are
shun-iwasawa 82a8f5
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
shun-iwasawa 82a8f5
 * in one call, and we never retain more than 7 bits in put_buffer
shun-iwasawa 82a8f5
 * between calls, so 24 bits are sufficient.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size)
shun-iwasawa 82a8f5
/* Emit some bits, unless we are in gather mode */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* This routine is heavily used, so it's worth coding tightly. */
shun-iwasawa 82a8f5
  register size_t put_buffer = (size_t)code;
shun-iwasawa 82a8f5
  register int put_bits = entropy->put_bits;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* if size is 0, caller used an invalid Huffman table entry */
shun-iwasawa 82a8f5
  if (size == 0)
shun-iwasawa 82a8f5
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (entropy->gather_statistics)
shun-iwasawa 82a8f5
    return;                     /* do nothing if we're only getting stats */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  put_bits += size;             /* new number of bits in buffer */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  put_buffer <<= 24 - put_bits; /* align incoming bits */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (put_bits >= 8) {
shun-iwasawa 82a8f5
    int c = (int)((put_buffer >> 16) & 0xFF);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    emit_byte(entropy, c);
shun-iwasawa 82a8f5
    if (c == 0xFF) {            /* need to stuff a zero byte? */
shun-iwasawa 82a8f5
      emit_byte(entropy, 0);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    put_buffer <<= 8;
shun-iwasawa 82a8f5
    put_bits -= 8;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->put_buffer = put_buffer; /* update variables */
shun-iwasawa 82a8f5
  entropy->put_bits = put_bits;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
flush_bits(phuff_entropy_ptr entropy)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
shun-iwasawa 82a8f5
  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
shun-iwasawa 82a8f5
  entropy->put_bits = 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Emit (or just count) a Huffman symbol.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (entropy->gather_statistics)
shun-iwasawa 82a8f5
    entropy->count_ptrs[tbl_no][symbol]++;
shun-iwasawa 82a8f5
  else {
shun-iwasawa 82a8f5
    c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
shun-iwasawa 82a8f5
    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Emit bits from a correction bit buffer.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart,
shun-iwasawa 82a8f5
                   unsigned int nbits)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (entropy->gather_statistics)
shun-iwasawa 82a8f5
    return;                     /* no real work */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (nbits > 0) {
shun-iwasawa 82a8f5
    emit_bits(entropy, (unsigned int)(*bufstart), 1);
shun-iwasawa 82a8f5
    bufstart++;
shun-iwasawa 82a8f5
    nbits--;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Emit any pending EOBRUN symbol.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
emit_eobrun(phuff_entropy_ptr entropy)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register int temp, nbits;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
shun-iwasawa 82a8f5
    temp = entropy->EOBRUN;
shun-iwasawa 82a8f5
    nbits = JPEG_NBITS_NONZERO(temp) - 1;
shun-iwasawa 82a8f5
    /* safety check: shouldn't happen given limited correction-bit buffer */
shun-iwasawa 82a8f5
    if (nbits > 14)
shun-iwasawa 82a8f5
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
shun-iwasawa 82a8f5
    if (nbits)
shun-iwasawa 82a8f5
      emit_bits(entropy, entropy->EOBRUN, nbits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    entropy->EOBRUN = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Emit any buffered correction bits */
shun-iwasawa 82a8f5
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
shun-iwasawa 82a8f5
    entropy->BE = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Emit a restart marker & resynchronize predictions.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
emit_restart(phuff_entropy_ptr entropy, int restart_num)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ci;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  emit_eobrun(entropy);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!entropy->gather_statistics) {
shun-iwasawa 82a8f5
    flush_bits(entropy);
shun-iwasawa 82a8f5
    emit_byte(entropy, 0xFF);
shun-iwasawa 82a8f5
    emit_byte(entropy, JPEG_RST0 + restart_num);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (entropy->cinfo->Ss == 0) {
shun-iwasawa 82a8f5
    /* Re-initialize DC predictions to 0 */
shun-iwasawa 82a8f5
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
shun-iwasawa 82a8f5
      entropy->last_dc_val[ci] = 0;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* Re-initialize all AC-related fields to 0 */
shun-iwasawa 82a8f5
    entropy->EOBRUN = 0;
shun-iwasawa 82a8f5
    entropy->BE = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * MCU encoding for DC initial scan (either spectral selection,
shun-iwasawa 82a8f5
 * or first pass of successive approximation).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(boolean)
shun-iwasawa 82a8f5
encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  register int temp, temp2, temp3;
shun-iwasawa 82a8f5
  register int nbits;
shun-iwasawa 82a8f5
  int blkn, ci;
shun-iwasawa 82a8f5
  int Al = cinfo->Al;
shun-iwasawa 82a8f5
  JBLOCKROW block;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
  ISHIFT_TEMPS
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->next_output_byte = cinfo->dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit restart marker if needed */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval)
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0)
shun-iwasawa 82a8f5
      emit_restart(entropy, entropy->next_restart_num);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Encode the MCU data blocks */
shun-iwasawa 82a8f5
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shun-iwasawa 82a8f5
    block = MCU_data[blkn];
shun-iwasawa 82a8f5
    ci = cinfo->MCU_membership[blkn];
shun-iwasawa 82a8f5
    compptr = cinfo->cur_comp_info[ci];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Compute the DC value after the required point transform by Al.
shun-iwasawa 82a8f5
     * This is simply an arithmetic right shift.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* DC differences are figured on the point-transformed values. */
shun-iwasawa 82a8f5
    temp = temp2 - entropy->last_dc_val[ci];
shun-iwasawa 82a8f5
    entropy->last_dc_val[ci] = temp2;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Encode the DC coefficient difference per section G.1.2.1 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* This is a well-known technique for obtaining the absolute value without
shun-iwasawa 82a8f5
     * a branch.  It is derived from an assembly language technique presented
shun-iwasawa 82a8f5
     * in "How to Optimize for the Pentium Processors", Copyright (c) 1996,
shun-iwasawa 82a8f5
     * 1997 by Agner Fog.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
shun-iwasawa 82a8f5
    temp ^= temp3;
shun-iwasawa 82a8f5
    temp -= temp3;              /* temp is abs value of input */
shun-iwasawa 82a8f5
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
shun-iwasawa 82a8f5
    temp2 = temp ^ temp3;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Find the number of bits needed for the magnitude of the coefficient */
shun-iwasawa 82a8f5
    nbits = JPEG_NBITS(temp);
shun-iwasawa 82a8f5
    /* Check for out-of-range coefficient values.
shun-iwasawa 82a8f5
     * Since we're encoding a difference, the range limit is twice as much.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    if (nbits > MAX_COEF_BITS + 1)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Count/emit the Huffman-coded symbol for the number of bits */
shun-iwasawa 82a8f5
    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Emit that number of bits of the value, if positive, */
shun-iwasawa 82a8f5
    /* or the complement of its magnitude, if negative. */
shun-iwasawa 82a8f5
    if (nbits)                  /* emit_bits rejects calls with size 0 */
shun-iwasawa 82a8f5
      emit_bits(entropy, (unsigned int)temp2, nbits);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->dest->next_output_byte = entropy->next_output_byte;
shun-iwasawa 82a8f5
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Update restart-interval state too */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval) {
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0) {
shun-iwasawa 82a8f5
      entropy->restarts_to_go = cinfo->restart_interval;
shun-iwasawa 82a8f5
      entropy->next_restart_num++;
shun-iwasawa 82a8f5
      entropy->next_restart_num &= 7;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    entropy->restarts_to_go--;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Data preparation for encode_mcu_AC_first().
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \
shun-iwasawa 82a8f5
  for (k = 0; k < Sl; k++) { \
shun-iwasawa 82a8f5
    temp = block[jpeg_natural_order_start[k]]; \
shun-iwasawa 82a8f5
    if (temp == 0) \
shun-iwasawa 82a8f5
      continue; \
shun-iwasawa 82a8f5
    /* We must apply the point transform by Al.  For AC coefficients this \
shun-iwasawa 82a8f5
     * is an integer division with rounding towards 0.  To do this portably \
shun-iwasawa 82a8f5
     * in C, we shift after obtaining the absolute value; so the code is \
shun-iwasawa 82a8f5
     * interwoven with finding the abs value (temp) and output bits (temp2). \
shun-iwasawa 82a8f5
     */ \
shun-iwasawa 82a8f5
    temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
shun-iwasawa 82a8f5
    temp ^= temp2; \
shun-iwasawa 82a8f5
    temp -= temp2;              /* temp is abs value of input */ \
shun-iwasawa 82a8f5
    temp >>= Al;                /* apply the point transform */ \
shun-iwasawa 82a8f5
    /* Watch out for case that nonzero coef is zero after point transform */ \
shun-iwasawa 82a8f5
    if (temp == 0) \
shun-iwasawa 82a8f5
      continue; \
shun-iwasawa 82a8f5
    /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
shun-iwasawa 82a8f5
    temp2 ^= temp; \
shun-iwasawa 82a8f5
    values[k] = temp; \
shun-iwasawa 82a8f5
    values[k + DCTSIZE2] = temp2; \
shun-iwasawa 82a8f5
    zerobits |= ((size_t)1U) << k; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
encode_mcu_AC_first_prepare(const JCOEF *block,
shun-iwasawa 82a8f5
                            const int *jpeg_natural_order_start, int Sl,
shun-iwasawa 82a8f5
                            int Al, JCOEF *values, size_t *bits)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register int k, temp, temp2;
shun-iwasawa 82a8f5
  size_t zerobits = 0U;
shun-iwasawa 82a8f5
  int Sl0 = Sl;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  if (Sl0 > 32)
shun-iwasawa 82a8f5
    Sl0 = 32;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  COMPUTE_ABSVALUES_AC_FIRST(Sl0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  bits[0] = zerobits;
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  zerobits = 0U;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (Sl > 32) {
shun-iwasawa 82a8f5
    Sl -= 32;
shun-iwasawa 82a8f5
    jpeg_natural_order_start += 32;
shun-iwasawa 82a8f5
    values += 32;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    COMPUTE_ABSVALUES_AC_FIRST(Sl);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  bits[1] = zerobits;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * MCU encoding for AC initial scan (either spectral selection,
shun-iwasawa 82a8f5
 * or first pass of successive approximation).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define ENCODE_COEFS_AC_FIRST(label) { \
shun-iwasawa 82a8f5
  while (zerobits) { \
shun-iwasawa 82a8f5
    r = count_zeroes(&zerobits); \
shun-iwasawa 82a8f5
    cvalue += r; \
shun-iwasawa 82a8f5
label \
shun-iwasawa 82a8f5
    temp  = cvalue[0]; \
shun-iwasawa 82a8f5
    temp2 = cvalue[DCTSIZE2]; \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
shun-iwasawa 82a8f5
    while (r > 15) { \
shun-iwasawa 82a8f5
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
shun-iwasawa 82a8f5
      r -= 16; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Find the number of bits needed for the magnitude of the coefficient */ \
shun-iwasawa 82a8f5
    nbits = JPEG_NBITS_NONZERO(temp);  /* there must be at least one 1 bit */ \
shun-iwasawa 82a8f5
    /* Check for out-of-range coefficient values */ \
shun-iwasawa 82a8f5
    if (nbits > MAX_COEF_BITS) \
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Count/emit Huffman symbol for run length / number of bits */ \
shun-iwasawa 82a8f5
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Emit that number of bits of the value, if positive, */ \
shun-iwasawa 82a8f5
    /* or the complement of its magnitude, if negative. */ \
shun-iwasawa 82a8f5
    emit_bits(entropy, (unsigned int)temp2, nbits); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    cvalue++; \
shun-iwasawa 82a8f5
    zerobits >>= 1; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(boolean)
shun-iwasawa 82a8f5
encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  register int temp, temp2;
shun-iwasawa 82a8f5
  register int nbits, r;
shun-iwasawa 82a8f5
  int Sl = cinfo->Se - cinfo->Ss + 1;
shun-iwasawa 82a8f5
  int Al = cinfo->Al;
shun-iwasawa 82a8f5
  JCOEF values_unaligned[2 * DCTSIZE2 + 15];
shun-iwasawa 82a8f5
  JCOEF *values;
shun-iwasawa 82a8f5
  const JCOEF *cvalue;
shun-iwasawa 82a8f5
  size_t zerobits;
shun-iwasawa 82a8f5
  size_t bits[8 / SIZEOF_SIZE_T];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->next_output_byte = cinfo->dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit restart marker if needed */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval)
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0)
shun-iwasawa 82a8f5
      emit_restart(entropy, entropy->next_restart_num);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef WITH_SIMD
shun-iwasawa 82a8f5
  cvalue = values = (JCOEF *)PAD((size_t)values_unaligned, 16);
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  /* Not using SIMD, so alignment is not needed */
shun-iwasawa 82a8f5
  cvalue = values = values_unaligned;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Prepare data */
shun-iwasawa 82a8f5
  entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
shun-iwasawa 82a8f5
                            Sl, Al, values, bits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  zerobits = bits[0];
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  zerobits |= bits[1];
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit any pending EOBRUN */
shun-iwasawa 82a8f5
  if (zerobits && (entropy->EOBRUN > 0))
shun-iwasawa 82a8f5
    emit_eobrun(entropy);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  zerobits = bits[0];
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ENCODE_COEFS_AC_FIRST((void)0;);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  zerobits = bits[1];
shun-iwasawa 82a8f5
  if (zerobits) {
shun-iwasawa 82a8f5
    int diff = ((values + DCTSIZE2 / 2) - cvalue);
shun-iwasawa 82a8f5
    r = count_zeroes(&zerobits);
shun-iwasawa 82a8f5
    r += diff;
shun-iwasawa 82a8f5
    cvalue += r;
shun-iwasawa 82a8f5
    goto first_iter_ac_first;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
shun-iwasawa 82a8f5
    entropy->EOBRUN++;          /* count an EOB */
shun-iwasawa 82a8f5
    if (entropy->EOBRUN == 0x7FFF)
shun-iwasawa 82a8f5
      emit_eobrun(entropy);     /* force it out to avoid overflow */
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->dest->next_output_byte = entropy->next_output_byte;
shun-iwasawa 82a8f5
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Update restart-interval state too */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval) {
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0) {
shun-iwasawa 82a8f5
      entropy->restarts_to_go = cinfo->restart_interval;
shun-iwasawa 82a8f5
      entropy->next_restart_num++;
shun-iwasawa 82a8f5
      entropy->next_restart_num &= 7;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    entropy->restarts_to_go--;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * MCU encoding for DC successive approximation refinement scan.
shun-iwasawa 82a8f5
 * Note: we assume such scans can be multi-component, although the spec
shun-iwasawa 82a8f5
 * is not very clear on the point.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(boolean)
shun-iwasawa 82a8f5
encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  register int temp;
shun-iwasawa 82a8f5
  int blkn;
shun-iwasawa 82a8f5
  int Al = cinfo->Al;
shun-iwasawa 82a8f5
  JBLOCKROW block;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->next_output_byte = cinfo->dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit restart marker if needed */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval)
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0)
shun-iwasawa 82a8f5
      emit_restart(entropy, entropy->next_restart_num);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Encode the MCU data blocks */
shun-iwasawa 82a8f5
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shun-iwasawa 82a8f5
    block = MCU_data[blkn];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* We simply emit the Al'th bit of the DC coefficient value. */
shun-iwasawa 82a8f5
    temp = (*block)[0];
shun-iwasawa 82a8f5
    emit_bits(entropy, (unsigned int)(temp >> Al), 1);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->dest->next_output_byte = entropy->next_output_byte;
shun-iwasawa 82a8f5
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Update restart-interval state too */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval) {
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0) {
shun-iwasawa 82a8f5
      entropy->restarts_to_go = cinfo->restart_interval;
shun-iwasawa 82a8f5
      entropy->next_restart_num++;
shun-iwasawa 82a8f5
      entropy->next_restart_num &= 7;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    entropy->restarts_to_go--;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Data preparation for encode_mcu_AC_refine().
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \
shun-iwasawa 82a8f5
  /* It is convenient to make a pre-pass to determine the transformed \
shun-iwasawa 82a8f5
   * coefficients' absolute values and the EOB position. \
shun-iwasawa 82a8f5
   */ \
shun-iwasawa 82a8f5
  for (k = 0; k < Sl; k++) { \
shun-iwasawa 82a8f5
    temp = block[jpeg_natural_order_start[k]]; \
shun-iwasawa 82a8f5
    /* We must apply the point transform by Al.  For AC coefficients this \
shun-iwasawa 82a8f5
     * is an integer division with rounding towards 0.  To do this portably \
shun-iwasawa 82a8f5
     * in C, we shift after obtaining the absolute value. \
shun-iwasawa 82a8f5
     */ \
shun-iwasawa 82a8f5
    temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
shun-iwasawa 82a8f5
    temp ^= temp2; \
shun-iwasawa 82a8f5
    temp -= temp2;              /* temp is abs value of input */ \
shun-iwasawa 82a8f5
    temp >>= Al;                /* apply the point transform */ \
shun-iwasawa 82a8f5
    if (temp != 0) { \
shun-iwasawa 82a8f5
      zerobits |= ((size_t)1U) << k; \
shun-iwasawa 82a8f5
      signbits |= ((size_t)(temp2 + 1)) << k; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
    absvalues[k] = (JCOEF)temp; /* save abs value for main pass */ \
shun-iwasawa 82a8f5
    if (temp == 1) \
shun-iwasawa 82a8f5
      EOB = k + koffset;        /* EOB = index of last newly-nonzero coef */ \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(int)
shun-iwasawa 82a8f5
encode_mcu_AC_refine_prepare(const JCOEF *block,
shun-iwasawa 82a8f5
                             const int *jpeg_natural_order_start, int Sl,
shun-iwasawa 82a8f5
                             int Al, JCOEF *absvalues, size_t *bits)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register int k, temp, temp2;
shun-iwasawa 82a8f5
  int EOB = 0;
shun-iwasawa 82a8f5
  size_t zerobits = 0U, signbits = 0U;
shun-iwasawa 82a8f5
  int Sl0 = Sl;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  if (Sl0 > 32)
shun-iwasawa 82a8f5
    Sl0 = 32;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  bits[0] = zerobits;
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 8
shun-iwasawa 82a8f5
  bits[1] = signbits;
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  bits[2] = signbits;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  zerobits = 0U;
shun-iwasawa 82a8f5
  signbits = 0U;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (Sl > 32) {
shun-iwasawa 82a8f5
    Sl -= 32;
shun-iwasawa 82a8f5
    jpeg_natural_order_start += 32;
shun-iwasawa 82a8f5
    absvalues += 32;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  bits[1] = zerobits;
shun-iwasawa 82a8f5
  bits[3] = signbits;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return EOB;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * MCU encoding for AC successive approximation refinement scan.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define ENCODE_COEFS_AC_REFINE(label) { \
shun-iwasawa 82a8f5
  while (zerobits) { \
shun-iwasawa 82a8f5
    int idx = count_zeroes(&zerobits); \
shun-iwasawa 82a8f5
    r += idx; \
shun-iwasawa 82a8f5
    cabsvalue += idx; \
shun-iwasawa 82a8f5
    signbits >>= idx; \
shun-iwasawa 82a8f5
label \
shun-iwasawa 82a8f5
    /* Emit any required ZRLs, but not if they can be folded into EOB */ \
shun-iwasawa 82a8f5
    while (r > 15 && (cabsvalue <= EOBPTR)) { \
shun-iwasawa 82a8f5
      /* emit any pending EOBRUN and the BE correction bits */ \
shun-iwasawa 82a8f5
      emit_eobrun(entropy); \
shun-iwasawa 82a8f5
      /* Emit ZRL */ \
shun-iwasawa 82a8f5
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
shun-iwasawa 82a8f5
      r -= 16; \
shun-iwasawa 82a8f5
      /* Emit buffered correction bits that must be associated with ZRL */ \
shun-iwasawa 82a8f5
      emit_buffered_bits(entropy, BR_buffer, BR); \
shun-iwasawa 82a8f5
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
shun-iwasawa 82a8f5
      BR = 0; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    temp = *cabsvalue++; \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* If the coef was previously nonzero, it only needs a correction bit. \
shun-iwasawa 82a8f5
     * NOTE: a straight translation of the spec's figure G.7 would suggest \
shun-iwasawa 82a8f5
     * that we also need to test r > 15.  But if r > 15, we can only get here \
shun-iwasawa 82a8f5
     * if k > EOB, which implies that this coefficient is not 1. \
shun-iwasawa 82a8f5
     */ \
shun-iwasawa 82a8f5
    if (temp > 1) { \
shun-iwasawa 82a8f5
      /* The correction bit is the next bit of the absolute value. */ \
shun-iwasawa 82a8f5
      BR_buffer[BR++] = (char)(temp & 1); \
shun-iwasawa 82a8f5
      signbits >>= 1; \
shun-iwasawa 82a8f5
      zerobits >>= 1; \
shun-iwasawa 82a8f5
      continue; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Emit any pending EOBRUN and the BE correction bits */ \
shun-iwasawa 82a8f5
    emit_eobrun(entropy); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Count/emit Huffman symbol for run length / number of bits */ \
shun-iwasawa 82a8f5
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Emit output bit for newly-nonzero coef */ \
shun-iwasawa 82a8f5
    temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
shun-iwasawa 82a8f5
    emit_bits(entropy, (unsigned int)temp, 1); \
shun-iwasawa 82a8f5
    \
shun-iwasawa 82a8f5
    /* Emit buffered correction bits that must be associated with this code */ \
shun-iwasawa 82a8f5
    emit_buffered_bits(entropy, BR_buffer, BR); \
shun-iwasawa 82a8f5
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
shun-iwasawa 82a8f5
    BR = 0; \
shun-iwasawa 82a8f5
    r = 0;                      /* reset zero run length */ \
shun-iwasawa 82a8f5
    signbits >>= 1; \
shun-iwasawa 82a8f5
    zerobits >>= 1; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(boolean)
shun-iwasawa 82a8f5
encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  register int temp, r;
shun-iwasawa 82a8f5
  char *BR_buffer;
shun-iwasawa 82a8f5
  unsigned int BR;
shun-iwasawa 82a8f5
  int Sl = cinfo->Se - cinfo->Ss + 1;
shun-iwasawa 82a8f5
  int Al = cinfo->Al;
shun-iwasawa 82a8f5
  JCOEF absvalues_unaligned[DCTSIZE2 + 15];
shun-iwasawa 82a8f5
  JCOEF *absvalues;
shun-iwasawa 82a8f5
  const JCOEF *cabsvalue, *EOBPTR;
shun-iwasawa 82a8f5
  size_t zerobits, signbits;
shun-iwasawa 82a8f5
  size_t bits[16 / SIZEOF_SIZE_T];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->next_output_byte = cinfo->dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit restart marker if needed */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval)
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0)
shun-iwasawa 82a8f5
      emit_restart(entropy, entropy->next_restart_num);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef WITH_SIMD
shun-iwasawa 82a8f5
  cabsvalue = absvalues = (JCOEF *)PAD((size_t)absvalues_unaligned, 16);
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  /* Not using SIMD, so alignment is not needed */
shun-iwasawa 82a8f5
  cabsvalue = absvalues = absvalues_unaligned;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Prepare data */
shun-iwasawa 82a8f5
  EOBPTR = absvalues +
shun-iwasawa 82a8f5
    entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
shun-iwasawa 82a8f5
                               Sl, Al, absvalues, bits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  r = 0;                        /* r = run length of zeros */
shun-iwasawa 82a8f5
  BR = 0;                       /* BR = count of buffered bits added now */
shun-iwasawa 82a8f5
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  zerobits = bits[0];
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 8
shun-iwasawa 82a8f5
  signbits = bits[1];
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  signbits = bits[2];
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  ENCODE_COEFS_AC_REFINE((void)0;);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 4
shun-iwasawa 82a8f5
  zerobits = bits[1];
shun-iwasawa 82a8f5
  signbits = bits[3];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (zerobits) {
shun-iwasawa 82a8f5
    int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);
shun-iwasawa 82a8f5
    int idx = count_zeroes(&zerobits);
shun-iwasawa 82a8f5
    signbits >>= idx;
shun-iwasawa 82a8f5
    idx += diff;
shun-iwasawa 82a8f5
    r += idx;
shun-iwasawa 82a8f5
    cabsvalue += idx;
shun-iwasawa 82a8f5
    goto first_iter_ac_refine;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  r |= (int)((absvalues + Sl) - cabsvalue);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
shun-iwasawa 82a8f5
    entropy->EOBRUN++;          /* count an EOB */
shun-iwasawa 82a8f5
    entropy->BE += BR;          /* concat my correction bits to older ones */
shun-iwasawa 82a8f5
    /* We force out the EOB if we risk either:
shun-iwasawa 82a8f5
     * 1. overflow of the EOB counter;
shun-iwasawa 82a8f5
     * 2. overflow of the correction bit buffer during the next MCU.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    if (entropy->EOBRUN == 0x7FFF ||
shun-iwasawa 82a8f5
        entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
shun-iwasawa 82a8f5
      emit_eobrun(entropy);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->dest->next_output_byte = entropy->next_output_byte;
shun-iwasawa 82a8f5
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Update restart-interval state too */
shun-iwasawa 82a8f5
  if (cinfo->restart_interval) {
shun-iwasawa 82a8f5
    if (entropy->restarts_to_go == 0) {
shun-iwasawa 82a8f5
      entropy->restarts_to_go = cinfo->restart_interval;
shun-iwasawa 82a8f5
      entropy->next_restart_num++;
shun-iwasawa 82a8f5
      entropy->next_restart_num &= 7;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    entropy->restarts_to_go--;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up at the end of a Huffman-compressed progressive scan.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_pass_phuff(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy->next_output_byte = cinfo->dest->next_output_byte;
shun-iwasawa 82a8f5
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Flush out any buffered data */
shun-iwasawa 82a8f5
  emit_eobrun(entropy);
shun-iwasawa 82a8f5
  flush_bits(entropy);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->dest->next_output_byte = entropy->next_output_byte;
shun-iwasawa 82a8f5
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up a statistics-gathering pass and create the new Huffman tables.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_pass_gather_phuff(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
shun-iwasawa 82a8f5
  boolean is_DC_band;
shun-iwasawa 82a8f5
  int ci, tbl;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
  JHUFF_TBL **htblptr;
shun-iwasawa 82a8f5
  boolean did[NUM_HUFF_TBLS];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Flush out buffered data (all we care about is counting the EOB symbol) */
shun-iwasawa 82a8f5
  emit_eobrun(entropy);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  is_DC_band = (cinfo->Ss == 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* It's important not to apply jpeg_gen_optimal_table more than once
shun-iwasawa 82a8f5
   * per table, because it clobbers the input frequency counts!
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  MEMZERO(did, sizeof(did));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shun-iwasawa 82a8f5
    compptr = cinfo->cur_comp_info[ci];
shun-iwasawa 82a8f5
    if (is_DC_band) {
shun-iwasawa 82a8f5
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
shun-iwasawa 82a8f5
        continue;
shun-iwasawa 82a8f5
      tbl = compptr->dc_tbl_no;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      tbl = compptr->ac_tbl_no;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (!did[tbl]) {
shun-iwasawa 82a8f5
      if (is_DC_band)
shun-iwasawa 82a8f5
        htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];
shun-iwasawa 82a8f5
      if (*htblptr == NULL)
shun-iwasawa 82a8f5
        *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
shun-iwasawa 82a8f5
      did[tbl] = TRUE;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Module initialization routine for progressive Huffman entropy encoding.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jinit_phuff_encoder(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  phuff_entropy_ptr entropy;
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  entropy = (phuff_entropy_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(phuff_entropy_encoder));
shun-iwasawa 82a8f5
  cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
shun-iwasawa 82a8f5
  entropy->pub.start_pass = start_pass_phuff;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Mark tables unallocated */
shun-iwasawa 82a8f5
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
shun-iwasawa 82a8f5
    entropy->derived_tbls[i] = NULL;
shun-iwasawa 82a8f5
    entropy->count_ptrs[i] = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* C_PROGRESSIVE_SUPPORTED */