|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jchuff.c
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file was part of the Independent JPEG Group's software:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1991-1997, Thomas G. Lane.
|
|
shun-iwasawa |
82a8f5 |
* libjpeg-turbo Modifications:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2009-2011, 2014-2016, 2018-2019, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2015, 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.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Much of the complexity here has to do with supporting output suspension.
|
|
shun-iwasawa |
82a8f5 |
* If the data destination module demands suspension, we want to be able to
|
|
shun-iwasawa |
82a8f5 |
* back up to the start of the current MCU. To do this, we copy state
|
|
shun-iwasawa |
82a8f5 |
* variables into local working storage, and update them back to the
|
|
shun-iwasawa |
82a8f5 |
* permanent JPEG objects only upon successful completion of an MCU.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* NOTE: All referenced figures are from
|
|
shun-iwasawa |
82a8f5 |
* Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
|
|
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 |
/*
|
|
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 Huffman encoding.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The savable_state subrecord contains fields that change within an MCU,
|
|
shun-iwasawa |
82a8f5 |
* but must not be updated permanently until we complete the MCU.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
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 |
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
|
shun-iwasawa |
82a8f5 |
} savable_state;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* This macro is to work around compilers with missing or broken
|
|
shun-iwasawa |
82a8f5 |
* structure assignment. You'll need to fix this code if you have
|
|
shun-iwasawa |
82a8f5 |
* such a compiler and you change MAX_COMPS_IN_SCAN.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifndef NO_STRUCT_ASSIGN
|
|
shun-iwasawa |
82a8f5 |
#define ASSIGN_STATE(dest, src) ((dest) = (src))
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
#if MAX_COMPS_IN_SCAN == 4
|
|
shun-iwasawa |
82a8f5 |
#define ASSIGN_STATE(dest, src) \
|
|
shun-iwasawa |
82a8f5 |
((dest).put_buffer = (src).put_buffer, \
|
|
shun-iwasawa |
82a8f5 |
(dest).put_bits = (src).put_bits, \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[0] = (src).last_dc_val[0], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[1] = (src).last_dc_val[1], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[2] = (src).last_dc_val[2], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[3] = (src).last_dc_val[3])
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
shun-iwasawa |
82a8f5 |
struct jpeg_entropy_encoder pub; /* public fields */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
savable_state saved; /* Bit buffer & DC state at start of MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* These fields are NOT loaded into local working state. */
|
|
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 |
c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
|
|
shun-iwasawa |
82a8f5 |
long *dc_count_ptrs[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
long *ac_count_ptrs[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
int simd;
|
|
shun-iwasawa |
82a8f5 |
} huff_entropy_encoder;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef huff_entropy_encoder *huff_entropy_ptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Working state while writing an MCU.
|
|
shun-iwasawa |
82a8f5 |
* This struct contains all the fields that are needed by subroutines.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
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 |
savable_state cur; /* Current bit buffer & DC state */
|
|
shun-iwasawa |
82a8f5 |
j_compress_ptr cinfo; /* dump_buffer needs access to this */
|
|
shun-iwasawa |
82a8f5 |
} working_state;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Forward declarations */
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo);
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo);
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize for a Huffman-compressed scan.
|
|
shun-iwasawa |
82a8f5 |
* If gather_statistics is TRUE, we do not output anything during the scan,
|
|
shun-iwasawa |
82a8f5 |
* just count the Huffman symbols used and generate Huffman code tables.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int ci, dctbl, actbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (gather_statistics) {
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.encode_mcu = encode_mcu_gather;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.finish_pass = finish_pass_gather;
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.encode_mcu = encode_mcu_huff;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.finish_pass = finish_pass_huff;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
entropy->simd = jsimd_can_huff_encode_one_block();
|
|
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 |
dctbl = compptr->dc_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
actbl = compptr->ac_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
if (gather_statistics) {
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
/* Check for invalid table indexes */
|
|
shun-iwasawa |
82a8f5 |
/* (make_c_derived_tbl does this in the other path) */
|
|
shun-iwasawa |
82a8f5 |
if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
|
|
shun-iwasawa |
82a8f5 |
if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
|
|
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->dc_count_ptrs[dctbl] == NULL)
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_count_ptrs[dctbl] = (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->dc_count_ptrs[dctbl], 257 * sizeof(long));
|
|
shun-iwasawa |
82a8f5 |
if (entropy->ac_count_ptrs[actbl] == NULL)
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_count_ptrs[actbl] = (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->ac_count_ptrs[actbl], 257 * sizeof(long));
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* Compute derived values for Huffman tables */
|
|
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, TRUE, dctbl,
|
|
shun-iwasawa |
82a8f5 |
&entropy->dc_derived_tbls[dctbl]);
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
|
|
shun-iwasawa |
82a8f5 |
&entropy->ac_derived_tbls[actbl]);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* Initialize DC predictions to 0 */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.last_dc_val[ci] = 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize bit buffer to empty */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.put_buffer = 0;
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.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 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Compute the derived values for a Huffman table.
|
|
shun-iwasawa |
82a8f5 |
* This routine also performs some validation checks on the table.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Note this is also used by jcphuff.c.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno,
|
|
shun-iwasawa |
82a8f5 |
c_derived_tbl **pdtbl)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
JHUFF_TBL *htbl;
|
|
shun-iwasawa |
82a8f5 |
c_derived_tbl *dtbl;
|
|
shun-iwasawa |
82a8f5 |
int p, i, l, lastp, si, maxsymbol;
|
|
shun-iwasawa |
82a8f5 |
char huffsize[257];
|
|
shun-iwasawa |
82a8f5 |
unsigned int huffcode[257];
|
|
shun-iwasawa |
82a8f5 |
unsigned int code;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Note that huffsize[] and huffcode[] are filled in code-length order,
|
|
shun-iwasawa |
82a8f5 |
* paralleling the order of the symbols themselves in htbl->huffval[].
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Find the input Huffman table */
|
|
shun-iwasawa |
82a8f5 |
if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
|
shun-iwasawa |
82a8f5 |
htbl =
|
|
shun-iwasawa |
82a8f5 |
isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
|
|
shun-iwasawa |
82a8f5 |
if (htbl == NULL)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Allocate a workspace if we haven't already done so. */
|
|
shun-iwasawa |
82a8f5 |
if (*pdtbl == NULL)
|
|
shun-iwasawa |
82a8f5 |
*pdtbl = (c_derived_tbl *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(c_derived_tbl));
|
|
shun-iwasawa |
82a8f5 |
dtbl = *pdtbl;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Figure C.1: make table of Huffman code length for each symbol */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
p = 0;
|
|
shun-iwasawa |
82a8f5 |
for (l = 1; l <= 16; l++) {
|
|
shun-iwasawa |
82a8f5 |
i = (int)htbl->bits[l];
|
|
shun-iwasawa |
82a8f5 |
if (i < 0 || p + i > 256) /* protect against table overrun */
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
shun-iwasawa |
82a8f5 |
while (i--)
|
|
shun-iwasawa |
82a8f5 |
huffsize[p++] = (char)l;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
huffsize[p] = 0;
|
|
shun-iwasawa |
82a8f5 |
lastp = p;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Figure C.2: generate the codes themselves */
|
|
shun-iwasawa |
82a8f5 |
/* We also validate that the counts represent a legal Huffman code tree. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
code = 0;
|
|
shun-iwasawa |
82a8f5 |
si = huffsize[0];
|
|
shun-iwasawa |
82a8f5 |
p = 0;
|
|
shun-iwasawa |
82a8f5 |
while (huffsize[p]) {
|
|
shun-iwasawa |
82a8f5 |
while (((int)huffsize[p]) == si) {
|
|
shun-iwasawa |
82a8f5 |
huffcode[p++] = code;
|
|
shun-iwasawa |
82a8f5 |
code++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* code is now 1 more than the last code used for codelength si; but
|
|
shun-iwasawa |
82a8f5 |
* it must still fit in si bits, since no code is allowed to be all ones.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (((JLONG)code) >= (((JLONG)1) << si))
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
shun-iwasawa |
82a8f5 |
code <<= 1;
|
|
shun-iwasawa |
82a8f5 |
si++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Figure C.3: generate encoding tables */
|
|
shun-iwasawa |
82a8f5 |
/* These are code and size indexed by symbol value */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Set all codeless symbols to have code length 0;
|
|
shun-iwasawa |
82a8f5 |
* this lets us detect duplicate VAL entries here, and later
|
|
shun-iwasawa |
82a8f5 |
* allows emit_bits to detect any attempt to emit such symbols.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
MEMZERO(dtbl->ehufsi, sizeof(dtbl->ehufsi));
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* This is also a convenient place to check for out-of-range
|
|
shun-iwasawa |
82a8f5 |
* and duplicated VAL entries. We allow 0..255 for AC symbols
|
|
shun-iwasawa |
82a8f5 |
* but only 0..15 for DC. (We could constrain them further
|
|
shun-iwasawa |
82a8f5 |
* based on data depth and mode, but this seems enough.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
maxsymbol = isDC ? 15 : 255;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (p = 0; p < lastp; p++) {
|
|
shun-iwasawa |
82a8f5 |
i = htbl->huffval[p];
|
|
shun-iwasawa |
82a8f5 |
if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
shun-iwasawa |
82a8f5 |
dtbl->ehufco[i] = huffcode[p];
|
|
shun-iwasawa |
82a8f5 |
dtbl->ehufsi[i] = huffsize[p];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Outputting bytes to the file */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Emit a byte, taking 'action' if must suspend. */
|
|
shun-iwasawa |
82a8f5 |
#define emit_byte(state, val, action) { \
|
|
shun-iwasawa |
82a8f5 |
*(state)->next_output_byte++ = (JOCTET)(val); \
|
|
shun-iwasawa |
82a8f5 |
if (--(state)->free_in_buffer == 0) \
|
|
shun-iwasawa |
82a8f5 |
if (!dump_buffer(state)) \
|
|
shun-iwasawa |
82a8f5 |
{ action; } \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
dump_buffer(working_state *state)
|
|
shun-iwasawa |
82a8f5 |
/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
struct jpeg_destination_mgr *dest = state->cinfo->dest;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!(*dest->empty_output_buffer) (state->cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
/* After a successful buffer dump, must reset buffer pointers */
|
|
shun-iwasawa |
82a8f5 |
state->next_output_byte = dest->next_output_byte;
|
|
shun-iwasawa |
82a8f5 |
state->free_in_buffer = dest->free_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Outputting bits to the file */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* These macros perform the same task as the emit_bits() function in the
|
|
shun-iwasawa |
82a8f5 |
* original libjpeg code. In addition to reducing overhead by explicitly
|
|
shun-iwasawa |
82a8f5 |
* inlining the code, additional performance is achieved by taking into
|
|
shun-iwasawa |
82a8f5 |
* account the size of the bit buffer and waiting until it is almost full
|
|
shun-iwasawa |
82a8f5 |
* before emptying it. This mostly benefits 64-bit platforms, since 6
|
|
shun-iwasawa |
82a8f5 |
* bytes can be stored in a 64-bit bit buffer before it has to be emptied.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define EMIT_BYTE() { \
|
|
shun-iwasawa |
82a8f5 |
JOCTET c; \
|
|
shun-iwasawa |
82a8f5 |
put_bits -= 8; \
|
|
shun-iwasawa |
82a8f5 |
c = (JOCTET)GETJOCTET(put_buffer >> put_bits); \
|
|
shun-iwasawa |
82a8f5 |
*buffer++ = c; \
|
|
shun-iwasawa |
82a8f5 |
if (c == 0xFF) /* need to stuff a zero byte? */ \
|
|
shun-iwasawa |
82a8f5 |
*buffer++ = 0; \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define PUT_BITS(code, size) { \
|
|
shun-iwasawa |
82a8f5 |
put_bits += size; \
|
|
shun-iwasawa |
82a8f5 |
put_buffer = (put_buffer << size) | code; \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if SIZEOF_SIZE_T != 8 && !defined(_WIN64)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define CHECKBUF15() { \
|
|
shun-iwasawa |
82a8f5 |
if (put_bits > 15) { \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define CHECKBUF31() { \
|
|
shun-iwasawa |
82a8f5 |
if (put_bits > 31) { \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define CHECKBUF47() { \
|
|
shun-iwasawa |
82a8f5 |
if (put_bits > 47) { \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
EMIT_BYTE() \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if !defined(_WIN32) && !defined(SIZEOF_SIZE_T)
|
|
shun-iwasawa |
82a8f5 |
#error Cannot determine word size
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if SIZEOF_SIZE_T == 8 || defined(_WIN64)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define EMIT_BITS(code, size) { \
|
|
shun-iwasawa |
82a8f5 |
CHECKBUF47() \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(code, size) \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define EMIT_CODE(code, size) { \
|
|
shun-iwasawa |
82a8f5 |
temp2 &= (((JLONG)1) << nbits) - 1; \
|
|
shun-iwasawa |
82a8f5 |
CHECKBUF31() \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(code, size) \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(temp2, nbits) \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define EMIT_BITS(code, size) { \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(code, size) \
|
|
shun-iwasawa |
82a8f5 |
CHECKBUF15() \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define EMIT_CODE(code, size) { \
|
|
shun-iwasawa |
82a8f5 |
temp2 &= (((JLONG)1) << nbits) - 1; \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(code, size) \
|
|
shun-iwasawa |
82a8f5 |
CHECKBUF15() \
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(temp2, nbits) \
|
|
shun-iwasawa |
82a8f5 |
CHECKBUF15() \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Although it is exceedingly rare, it is possible for a Huffman-encoded
|
|
shun-iwasawa |
82a8f5 |
* coefficient block to be larger than the 128-byte unencoded block. For each
|
|
shun-iwasawa |
82a8f5 |
* of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can
|
|
shun-iwasawa |
82a8f5 |
* theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per
|
|
shun-iwasawa |
82a8f5 |
* encoded block.) If, for instance, one artificially sets the AC
|
|
shun-iwasawa |
82a8f5 |
* coefficients to alternating values of 32767 and -32768 (using the JPEG
|
|
shun-iwasawa |
82a8f5 |
* scanning order-- 1, 8, 16, etc.), then this will produce an encoded block
|
|
shun-iwasawa |
82a8f5 |
* larger than 200 bytes.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
#define BUFSIZE (DCTSIZE2 * 8)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define LOAD_BUFFER() { \
|
|
shun-iwasawa |
82a8f5 |
if (state->free_in_buffer < BUFSIZE) { \
|
|
shun-iwasawa |
82a8f5 |
localbuf = 1; \
|
|
shun-iwasawa |
82a8f5 |
buffer = _buffer; \
|
|
shun-iwasawa |
82a8f5 |
} else \
|
|
shun-iwasawa |
82a8f5 |
buffer = state->next_output_byte; \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define STORE_BUFFER() { \
|
|
shun-iwasawa |
82a8f5 |
if (localbuf) { \
|
|
shun-iwasawa |
82a8f5 |
bytes = buffer - _buffer; \
|
|
shun-iwasawa |
82a8f5 |
buffer = _buffer; \
|
|
shun-iwasawa |
82a8f5 |
while (bytes > 0) { \
|
|
shun-iwasawa |
82a8f5 |
bytestocopy = MIN(bytes, state->free_in_buffer); \
|
|
shun-iwasawa |
82a8f5 |
MEMCOPY(state->next_output_byte, buffer, bytestocopy); \
|
|
shun-iwasawa |
82a8f5 |
state->next_output_byte += bytestocopy; \
|
|
shun-iwasawa |
82a8f5 |
buffer += bytestocopy; \
|
|
shun-iwasawa |
82a8f5 |
state->free_in_buffer -= bytestocopy; \
|
|
shun-iwasawa |
82a8f5 |
if (state->free_in_buffer == 0) \
|
|
shun-iwasawa |
82a8f5 |
if (!dump_buffer(state)) return FALSE; \
|
|
shun-iwasawa |
82a8f5 |
bytes -= bytestocopy; \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
} else { \
|
|
shun-iwasawa |
82a8f5 |
state->free_in_buffer -= (buffer - state->next_output_byte); \
|
|
shun-iwasawa |
82a8f5 |
state->next_output_byte = buffer; \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
flush_bits(working_state *state)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
JOCTET _buffer[BUFSIZE], *buffer;
|
|
shun-iwasawa |
82a8f5 |
size_t put_buffer; int put_bits;
|
|
shun-iwasawa |
82a8f5 |
size_t bytes, bytestocopy; int localbuf = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
put_buffer = state->cur.put_buffer;
|
|
shun-iwasawa |
82a8f5 |
put_bits = state->cur.put_bits;
|
|
shun-iwasawa |
82a8f5 |
LOAD_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* fill any partial byte with ones */
|
|
shun-iwasawa |
82a8f5 |
PUT_BITS(0x7F, 7)
|
|
shun-iwasawa |
82a8f5 |
while (put_bits >= 8) EMIT_BYTE()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
|
|
shun-iwasawa |
82a8f5 |
state->cur.put_bits = 0;
|
|
shun-iwasawa |
82a8f5 |
STORE_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode a single block's worth of coefficients */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val,
|
|
shun-iwasawa |
82a8f5 |
c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
JOCTET _buffer[BUFSIZE], *buffer;
|
|
shun-iwasawa |
82a8f5 |
size_t bytes, bytestocopy; int localbuf = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOAD_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val,
|
|
shun-iwasawa |
82a8f5 |
dctbl, actbl);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
STORE_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val,
|
|
shun-iwasawa |
82a8f5 |
c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int temp, temp2, temp3;
|
|
shun-iwasawa |
82a8f5 |
int nbits;
|
|
shun-iwasawa |
82a8f5 |
int r, code, size;
|
|
shun-iwasawa |
82a8f5 |
JOCTET _buffer[BUFSIZE], *buffer;
|
|
shun-iwasawa |
82a8f5 |
size_t put_buffer; int put_bits;
|
|
shun-iwasawa |
82a8f5 |
int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0];
|
|
shun-iwasawa |
82a8f5 |
size_t bytes, bytestocopy; int localbuf = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
put_buffer = state->cur.put_buffer;
|
|
shun-iwasawa |
82a8f5 |
put_bits = state->cur.put_bits;
|
|
shun-iwasawa |
82a8f5 |
LOAD_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode the DC coefficient difference per section F.1.2.1 */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
temp = temp2 = block[0] - last_dc_val;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* This is a well-known technique for obtaining the absolute value without a
|
|
shun-iwasawa |
82a8f5 |
* branch. It is derived from an assembly language technique presented in
|
|
shun-iwasawa |
82a8f5 |
* "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by
|
|
shun-iwasawa |
82a8f5 |
* 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;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* For a negative input, want temp2 = bitwise complement of abs(input) */
|
|
shun-iwasawa |
82a8f5 |
/* This code assumes we are on a two's complement machine */
|
|
shun-iwasawa |
82a8f5 |
temp2 += 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 |
|
|
shun-iwasawa |
82a8f5 |
/* Emit the Huffman-coded symbol for the number of bits */
|
|
shun-iwasawa |
82a8f5 |
code = dctbl->ehufco[nbits];
|
|
shun-iwasawa |
82a8f5 |
size = dctbl->ehufsi[nbits];
|
|
shun-iwasawa |
82a8f5 |
EMIT_BITS(code, size)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Mask off any extra bits in code */
|
|
shun-iwasawa |
82a8f5 |
temp2 &= (((JLONG)1) << nbits) - 1;
|
|
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(temp2, nbits)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode the AC coefficients per section F.1.2.2 */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
r = 0; /* r = run length of zeros */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Manually unroll the k loop to eliminate the counter variable. This
|
|
shun-iwasawa |
82a8f5 |
* improves performance greatly on systems with a limited number of
|
|
shun-iwasawa |
82a8f5 |
* registers (such as x86.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
#define kloop(jpeg_natural_order_of_k) { \
|
|
shun-iwasawa |
82a8f5 |
if ((temp = block[jpeg_natural_order_of_k]) == 0) { \
|
|
shun-iwasawa |
82a8f5 |
r++; \
|
|
shun-iwasawa |
82a8f5 |
} else { \
|
|
shun-iwasawa |
82a8f5 |
temp2 = temp; \
|
|
shun-iwasawa |
82a8f5 |
/* Branch-less absolute value, bitwise complement, etc., same as above */ \
|
|
shun-iwasawa |
82a8f5 |
temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); \
|
|
shun-iwasawa |
82a8f5 |
temp ^= temp3; \
|
|
shun-iwasawa |
82a8f5 |
temp -= temp3; \
|
|
shun-iwasawa |
82a8f5 |
temp2 += temp3; \
|
|
shun-iwasawa |
82a8f5 |
nbits = JPEG_NBITS_NONZERO(temp); \
|
|
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_BITS(code_0xf0, size_0xf0) \
|
|
shun-iwasawa |
82a8f5 |
r -= 16; \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
/* Emit Huffman symbol for run length / number of bits */ \
|
|
shun-iwasawa |
82a8f5 |
temp3 = (r << 4) + nbits; \
|
|
shun-iwasawa |
82a8f5 |
code = actbl->ehufco[temp3]; \
|
|
shun-iwasawa |
82a8f5 |
size = actbl->ehufsi[temp3]; \
|
|
shun-iwasawa |
82a8f5 |
EMIT_CODE(code, size) \
|
|
shun-iwasawa |
82a8f5 |
r = 0; \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* One iteration for each value in jpeg_natural_order[] */
|
|
shun-iwasawa |
82a8f5 |
kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3);
|
|
shun-iwasawa |
82a8f5 |
kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18);
|
|
shun-iwasawa |
82a8f5 |
kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26);
|
|
shun-iwasawa |
82a8f5 |
kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27);
|
|
shun-iwasawa |
82a8f5 |
kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21);
|
|
shun-iwasawa |
82a8f5 |
kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57);
|
|
shun-iwasawa |
82a8f5 |
kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15);
|
|
shun-iwasawa |
82a8f5 |
kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58);
|
|
shun-iwasawa |
82a8f5 |
kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39);
|
|
shun-iwasawa |
82a8f5 |
kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47);
|
|
shun-iwasawa |
82a8f5 |
kloop(55); kloop(62); kloop(63);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If the last coef(s) were zero, emit an end-of-block code */
|
|
shun-iwasawa |
82a8f5 |
if (r > 0) {
|
|
shun-iwasawa |
82a8f5 |
code = actbl->ehufco[0];
|
|
shun-iwasawa |
82a8f5 |
size = actbl->ehufsi[0];
|
|
shun-iwasawa |
82a8f5 |
EMIT_BITS(code, size)
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
state->cur.put_buffer = put_buffer;
|
|
shun-iwasawa |
82a8f5 |
state->cur.put_bits = put_bits;
|
|
shun-iwasawa |
82a8f5 |
STORE_BUFFER()
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
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(boolean)
|
|
shun-iwasawa |
82a8f5 |
emit_restart(working_state *state, int restart_num)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int ci;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!flush_bits(state))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
emit_byte(state, 0xFF, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Re-initialize DC predictions to 0 */
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
|
|
shun-iwasawa |
82a8f5 |
state->cur.last_dc_val[ci] = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* The restart counter is not updated until we successfully write the MCU. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Encode and output one MCU's worth of Huffman-compressed coefficients.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
working_state state;
|
|
shun-iwasawa |
82a8f5 |
int blkn, ci;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
state.next_output_byte = cinfo->dest->next_output_byte;
|
|
shun-iwasawa |
82a8f5 |
state.free_in_buffer = cinfo->dest->free_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(state.cur, entropy->saved);
|
|
shun-iwasawa |
82a8f5 |
state.cinfo = cinfo;
|
|
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 |
if (!emit_restart(&state, entropy->next_restart_num))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode the MCU data blocks */
|
|
shun-iwasawa |
82a8f5 |
if (entropy->simd) {
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
compptr = cinfo->cur_comp_info[ci];
|
|
shun-iwasawa |
82a8f5 |
if (!encode_one_block_simd(&state,
|
|
shun-iwasawa |
82a8f5 |
MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
/* Update last_dc_val */
|
|
shun-iwasawa |
82a8f5 |
state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
compptr = cinfo->cur_comp_info[ci];
|
|
shun-iwasawa |
82a8f5 |
if (!encode_one_block(&state,
|
|
shun-iwasawa |
82a8f5 |
MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
/* Update last_dc_val */
|
|
shun-iwasawa |
82a8f5 |
state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
cinfo->dest->next_output_byte = state.next_output_byte;
|
|
shun-iwasawa |
82a8f5 |
cinfo->dest->free_in_buffer = state.free_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(entropy->saved, state.cur);
|
|
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 scan.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
finish_pass_huff(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
working_state state;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state ... flush_bits needs it */
|
|
shun-iwasawa |
82a8f5 |
state.next_output_byte = cinfo->dest->next_output_byte;
|
|
shun-iwasawa |
82a8f5 |
state.free_in_buffer = cinfo->dest->free_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(state.cur, entropy->saved);
|
|
shun-iwasawa |
82a8f5 |
state.cinfo = cinfo;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Flush out the last data */
|
|
shun-iwasawa |
82a8f5 |
if (!flush_bits(&state))
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_CANT_SUSPEND);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Update state */
|
|
shun-iwasawa |
82a8f5 |
cinfo->dest->next_output_byte = state.next_output_byte;
|
|
shun-iwasawa |
82a8f5 |
cinfo->dest->free_in_buffer = state.free_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(entropy->saved, state.cur);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Huffman coding optimization.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* We first scan the supplied data and count the number of uses of each symbol
|
|
shun-iwasawa |
82a8f5 |
* that is to be Huffman-coded. (This process MUST agree with the code above.)
|
|
shun-iwasawa |
82a8f5 |
* Then we build a Huffman coding tree for the observed counts.
|
|
shun-iwasawa |
82a8f5 |
* Symbols which are not needed at all for the particular image are not
|
|
shun-iwasawa |
82a8f5 |
* assigned any code, which saves space in the DHT marker as well as in
|
|
shun-iwasawa |
82a8f5 |
* the compressed data.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process a single block's worth of coefficients */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(void)
|
|
shun-iwasawa |
82a8f5 |
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
|
|
shun-iwasawa |
82a8f5 |
long dc_counts[], long ac_counts[])
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register int temp;
|
|
shun-iwasawa |
82a8f5 |
register int nbits;
|
|
shun-iwasawa |
82a8f5 |
register int k, r;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode the DC coefficient difference per section F.1.2.1 */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
temp = block[0] - last_dc_val;
|
|
shun-iwasawa |
82a8f5 |
if (temp < 0)
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Find the number of bits needed for the magnitude of the coefficient */
|
|
shun-iwasawa |
82a8f5 |
nbits = 0;
|
|
shun-iwasawa |
82a8f5 |
while (temp) {
|
|
shun-iwasawa |
82a8f5 |
nbits++;
|
|
shun-iwasawa |
82a8f5 |
temp >>= 1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
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 the Huffman symbol for the number of bits */
|
|
shun-iwasawa |
82a8f5 |
dc_counts[nbits]++;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encode the AC coefficients per section F.1.2.2 */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
r = 0; /* r = run length of zeros */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (k = 1; k < DCTSIZE2; k++) {
|
|
shun-iwasawa |
82a8f5 |
if ((temp = block[jpeg_natural_order[k]]) == 0) {
|
|
shun-iwasawa |
82a8f5 |
r++;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* if run length > 15, must emit special run-length-16 codes (0xF0) */
|
|
shun-iwasawa |
82a8f5 |
while (r > 15) {
|
|
shun-iwasawa |
82a8f5 |
ac_counts[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 |
if (temp < 0)
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Find the number of bits needed for the magnitude of the coefficient */
|
|
shun-iwasawa |
82a8f5 |
nbits = 1; /* there must be at least one 1 bit */
|
|
shun-iwasawa |
82a8f5 |
while ((temp >>= 1))
|
|
shun-iwasawa |
82a8f5 |
nbits++;
|
|
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 Huffman symbol for run length / number of bits */
|
|
shun-iwasawa |
82a8f5 |
ac_counts[(r << 4) + nbits]++;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
r = 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If the last coef(s) were zero, emit an end-of-block code */
|
|
shun-iwasawa |
82a8f5 |
if (r > 0)
|
|
shun-iwasawa |
82a8f5 |
ac_counts[0]++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Trial-encode one MCU's worth of Huffman-compressed coefficients.
|
|
shun-iwasawa |
82a8f5 |
* No data is actually output, so no suspension return is possible.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int blkn, ci;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Take care of restart intervals if needed */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0) {
|
|
shun-iwasawa |
82a8f5 |
/* Re-initialize DC predictions to 0 */
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.last_dc_val[ci] = 0;
|
|
shun-iwasawa |
82a8f5 |
/* Update restart state */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go = cinfo->restart_interval;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
compptr = cinfo->cur_comp_info[ci];
|
|
shun-iwasawa |
82a8f5 |
htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_count_ptrs[compptr->dc_tbl_no],
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_count_ptrs[compptr->ac_tbl_no]);
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
|
|
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 |
* Generate the best Huffman code table for the given counts, fill htbl.
|
|
shun-iwasawa |
82a8f5 |
* Note this is also used by jcphuff.c.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The JPEG standard requires that no symbol be assigned a codeword of all
|
|
shun-iwasawa |
82a8f5 |
* one bits (so that padding bits added at the end of a compressed segment
|
|
shun-iwasawa |
82a8f5 |
* can't look like a valid code). Because of the canonical ordering of
|
|
shun-iwasawa |
82a8f5 |
* codewords, this just means that there must be an unused slot in the
|
|
shun-iwasawa |
82a8f5 |
* longest codeword length category. Annex K (Clause K.2) of
|
|
shun-iwasawa |
82a8f5 |
* Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot
|
|
shun-iwasawa |
82a8f5 |
* by pretending that symbol 256 is a valid symbol with count 1. In theory
|
|
shun-iwasawa |
82a8f5 |
* that's not optimal; giving it count zero but including it in the symbol set
|
|
shun-iwasawa |
82a8f5 |
* anyway should give a better Huffman code. But the theoretically better code
|
|
shun-iwasawa |
82a8f5 |
* actually seems to come out worse in practice, because it produces more
|
|
shun-iwasawa |
82a8f5 |
* all-ones bytes (which incur stuffed zero bytes in the final file). In any
|
|
shun-iwasawa |
82a8f5 |
* case the difference is tiny.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The JPEG standard requires Huffman codes to be no more than 16 bits long.
|
|
shun-iwasawa |
82a8f5 |
* If some symbols have a very small but nonzero probability, the Huffman tree
|
|
shun-iwasawa |
82a8f5 |
* must be adjusted to meet the code length restriction. We currently use
|
|
shun-iwasawa |
82a8f5 |
* the adjustment method suggested in JPEG section K.2. This method is *not*
|
|
shun-iwasawa |
82a8f5 |
* optimal; it may not choose the best possible limited-length code. But
|
|
shun-iwasawa |
82a8f5 |
* typically only very-low-frequency symbols will be given less-than-optimal
|
|
shun-iwasawa |
82a8f5 |
* lengths, so the code is almost optimal. Experimental comparisons against
|
|
shun-iwasawa |
82a8f5 |
* an optimal limited-length-code algorithm indicate that the difference is
|
|
shun-iwasawa |
82a8f5 |
* microscopic --- usually less than a hundredth of a percent of total size.
|
|
shun-iwasawa |
82a8f5 |
* So the extra complexity of an optimal algorithm doesn't seem worthwhile.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
#define MAX_CLEN 32 /* assumed maximum initial code length */
|
|
shun-iwasawa |
82a8f5 |
UINT8 bits[MAX_CLEN + 1]; /* bits[k] = # of symbols with code length k */
|
|
shun-iwasawa |
82a8f5 |
int codesize[257]; /* codesize[k] = code length of symbol k */
|
|
shun-iwasawa |
82a8f5 |
int others[257]; /* next symbol in current branch of tree */
|
|
shun-iwasawa |
82a8f5 |
int c1, c2;
|
|
shun-iwasawa |
82a8f5 |
int p, i, j;
|
|
shun-iwasawa |
82a8f5 |
long v;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* This algorithm is explained in section K.2 of the JPEG standard */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
MEMZERO(bits, sizeof(bits));
|
|
shun-iwasawa |
82a8f5 |
MEMZERO(codesize, sizeof(codesize));
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < 257; i++)
|
|
shun-iwasawa |
82a8f5 |
others[i] = -1; /* init links to empty */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
freq[256] = 1; /* make sure 256 has a nonzero count */
|
|
shun-iwasawa |
82a8f5 |
/* Including the pseudo-symbol 256 in the Huffman procedure guarantees
|
|
shun-iwasawa |
82a8f5 |
* that no real symbol is given code-value of all ones, because 256
|
|
shun-iwasawa |
82a8f5 |
* will be placed last in the largest codeword category.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Huffman's basic algorithm to assign optimal code lengths to symbols */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (;;) {
|
|
shun-iwasawa |
82a8f5 |
/* Find the smallest nonzero frequency, set c1 = its symbol */
|
|
shun-iwasawa |
82a8f5 |
/* In case of ties, take the larger symbol number */
|
|
shun-iwasawa |
82a8f5 |
c1 = -1;
|
|
shun-iwasawa |
82a8f5 |
v = 1000000000L;
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i <= 256; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (freq[i] && freq[i] <= v) {
|
|
shun-iwasawa |
82a8f5 |
v = freq[i];
|
|
shun-iwasawa |
82a8f5 |
c1 = i;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Find the next smallest nonzero frequency, set c2 = its symbol */
|
|
shun-iwasawa |
82a8f5 |
/* In case of ties, take the larger symbol number */
|
|
shun-iwasawa |
82a8f5 |
c2 = -1;
|
|
shun-iwasawa |
82a8f5 |
v = 1000000000L;
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i <= 256; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (freq[i] && freq[i] <= v && i != c1) {
|
|
shun-iwasawa |
82a8f5 |
v = freq[i];
|
|
shun-iwasawa |
82a8f5 |
c2 = i;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Done if we've merged everything into one frequency */
|
|
shun-iwasawa |
82a8f5 |
if (c2 < 0)
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Else merge the two counts/trees */
|
|
shun-iwasawa |
82a8f5 |
freq[c1] += freq[c2];
|
|
shun-iwasawa |
82a8f5 |
freq[c2] = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Increment the codesize of everything in c1's tree branch */
|
|
shun-iwasawa |
82a8f5 |
codesize[c1]++;
|
|
shun-iwasawa |
82a8f5 |
while (others[c1] >= 0) {
|
|
shun-iwasawa |
82a8f5 |
c1 = others[c1];
|
|
shun-iwasawa |
82a8f5 |
codesize[c1]++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
others[c1] = c2; /* chain c2 onto c1's tree branch */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Increment the codesize of everything in c2's tree branch */
|
|
shun-iwasawa |
82a8f5 |
codesize[c2]++;
|
|
shun-iwasawa |
82a8f5 |
while (others[c2] >= 0) {
|
|
shun-iwasawa |
82a8f5 |
c2 = others[c2];
|
|
shun-iwasawa |
82a8f5 |
codesize[c2]++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Now count the number of symbols of each code length */
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i <= 256; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (codesize[i]) {
|
|
shun-iwasawa |
82a8f5 |
/* The JPEG standard seems to think that this can't happen, */
|
|
shun-iwasawa |
82a8f5 |
/* but I'm paranoid... */
|
|
shun-iwasawa |
82a8f5 |
if (codesize[i] > MAX_CLEN)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
bits[codesize[i]]++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
|
|
shun-iwasawa |
82a8f5 |
* Huffman procedure assigned any such lengths, we must adjust the coding.
|
|
shun-iwasawa |
82a8f5 |
* Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next
|
|
shun-iwasawa |
82a8f5 |
* bit works: Since symbols are paired for the longest Huffman code, the
|
|
shun-iwasawa |
82a8f5 |
* symbols are removed from this length category two at a time. The prefix
|
|
shun-iwasawa |
82a8f5 |
* for the pair (which is one bit shorter) is allocated to one of the pair;
|
|
shun-iwasawa |
82a8f5 |
* then, skipping the BITS entry for that prefix length, a code word from the
|
|
shun-iwasawa |
82a8f5 |
* next shortest nonzero BITS entry is converted into a prefix for two code
|
|
shun-iwasawa |
82a8f5 |
* words one bit longer.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = MAX_CLEN; i > 16; i--) {
|
|
shun-iwasawa |
82a8f5 |
while (bits[i] > 0) {
|
|
shun-iwasawa |
82a8f5 |
j = i - 2; /* find length of new prefix to be used */
|
|
shun-iwasawa |
82a8f5 |
while (bits[j] == 0)
|
|
shun-iwasawa |
82a8f5 |
j--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
bits[i] -= 2; /* remove two symbols */
|
|
shun-iwasawa |
82a8f5 |
bits[i - 1]++; /* one goes in this length */
|
|
shun-iwasawa |
82a8f5 |
bits[j + 1] += 2; /* two new symbols in this length */
|
|
shun-iwasawa |
82a8f5 |
bits[j]--; /* symbol of this length is now a prefix */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Remove the count for the pseudo-symbol 256 from the largest codelength */
|
|
shun-iwasawa |
82a8f5 |
while (bits[i] == 0) /* find largest codelength still in use */
|
|
shun-iwasawa |
82a8f5 |
i--;
|
|
shun-iwasawa |
82a8f5 |
bits[i]--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Return final symbol counts (only for lengths 0..16) */
|
|
shun-iwasawa |
82a8f5 |
MEMCOPY(htbl->bits, bits, sizeof(htbl->bits));
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Return a list of the symbols sorted by code length */
|
|
shun-iwasawa |
82a8f5 |
/* It's not real clear to me why we don't need to consider the codelength
|
|
shun-iwasawa |
82a8f5 |
* changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think
|
|
shun-iwasawa |
82a8f5 |
* this works.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
p = 0;
|
|
shun-iwasawa |
82a8f5 |
for (i = 1; i <= MAX_CLEN; i++) {
|
|
shun-iwasawa |
82a8f5 |
for (j = 0; j <= 255; j++) {
|
|
shun-iwasawa |
82a8f5 |
if (codesize[j] == i) {
|
|
shun-iwasawa |
82a8f5 |
htbl->huffval[p] = (UINT8)j;
|
|
shun-iwasawa |
82a8f5 |
p++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Set sent_table FALSE so updated table will be written to JPEG file. */
|
|
shun-iwasawa |
82a8f5 |
htbl->sent_table = FALSE;
|
|
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(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int ci, dctbl, actbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
JHUFF_TBL **htblptr;
|
|
shun-iwasawa |
82a8f5 |
boolean did_dc[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
boolean did_ac[NUM_HUFF_TBLS];
|
|
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_dc, sizeof(did_dc));
|
|
shun-iwasawa |
82a8f5 |
MEMZERO(did_ac, sizeof(did_ac));
|
|
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 |
dctbl = compptr->dc_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
actbl = compptr->ac_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
if (!did_dc[dctbl]) {
|
|
shun-iwasawa |
82a8f5 |
htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
|
|
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->dc_count_ptrs[dctbl]);
|
|
shun-iwasawa |
82a8f5 |
did_dc[dctbl] = TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (!did_ac[actbl]) {
|
|
shun-iwasawa |
82a8f5 |
htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
|
|
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->ac_count_ptrs[actbl]);
|
|
shun-iwasawa |
82a8f5 |
did_ac[actbl] = TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif /* ENTROPY_OPT_SUPPORTED */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Module initialization routine for Huffman entropy encoding.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jinit_huff_encoder(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy;
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
entropy = (huff_entropy_ptr)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(huff_entropy_encoder));
|
|
shun-iwasawa |
82a8f5 |
cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.start_pass = start_pass_huff;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Mark tables unallocated */
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
|
|
shun-iwasawa |
82a8f5 |
#ifdef ENTROPY_OPT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|