|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jdhuff.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, 2016, 2018-2019, D. R. Commander.
|
|
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 decoding routines.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Much of the complexity here has to do with supporting input suspension.
|
|
shun-iwasawa |
82a8f5 |
* If the data source module demands suspension, we want to be able to back
|
|
shun-iwasawa |
82a8f5 |
* up to the start of the current MCU. To do this, we copy state variables
|
|
shun-iwasawa |
82a8f5 |
* into local working storage, and update them back to the permanent
|
|
shun-iwasawa |
82a8f5 |
* storage 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 "jdhuff.h" /* Declarations shared with jdphuff.c */
|
|
shun-iwasawa |
82a8f5 |
#include "jpegcomp.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jstdhuff.c"
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Expanded entropy decoder object for Huffman decoding.
|
|
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 |
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).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_decoder pub; /* public fields */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* These fields are loaded into local variables at start of each MCU.
|
|
shun-iwasawa |
82a8f5 |
* In case of suspension, we exit WITHOUT updating them.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
bitread_perm_state bitstate; /* Bit buffer at start of MCU */
|
|
shun-iwasawa |
82a8f5 |
savable_state saved; /* Other 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 |
|
|
shun-iwasawa |
82a8f5 |
/* Pointers to derived tables (these workspaces have image lifespan) */
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Precalculated info set up by start_pass for use in decode_mcu: */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Pointers to derived tables to be used for each block within an MCU */
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
|
|
shun-iwasawa |
82a8f5 |
/* Whether we care about the DC and AC coefficient values for each block */
|
|
shun-iwasawa |
82a8f5 |
boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
|
|
shun-iwasawa |
82a8f5 |
boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
|
|
shun-iwasawa |
82a8f5 |
} huff_entropy_decoder;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef huff_entropy_decoder *huff_entropy_ptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize for a Huffman-compressed scan.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
start_pass_huff_decoder(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int ci, blkn, dctbl, actbl;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl **pdtbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
|
|
shun-iwasawa |
82a8f5 |
* This ought to be an error condition, but we make it a warning because
|
|
shun-iwasawa |
82a8f5 |
* there are some baseline files out there with all zeroes in these bytes.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2 - 1 ||
|
|
shun-iwasawa |
82a8f5 |
cinfo->Ah != 0 || cinfo->Al != 0)
|
|
shun-iwasawa |
82a8f5 |
WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
|
|
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 |
/* 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 |
pdtbl = (d_derived_tbl **)(entropy->dc_derived_tbls) + dctbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, pdtbl);
|
|
shun-iwasawa |
82a8f5 |
pdtbl = (d_derived_tbl **)(entropy->ac_derived_tbls) + actbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, pdtbl);
|
|
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 |
/* Precalculate decoding info for each block in an MCU of this scan */
|
|
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 |
/* Precalculate which table to use for each block */
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
|
|
shun-iwasawa |
82a8f5 |
/* Decide whether we really care about the coefficient values */
|
|
shun-iwasawa |
82a8f5 |
if (compptr->component_needed) {
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_needed[blkn] = TRUE;
|
|
shun-iwasawa |
82a8f5 |
/* we don't need the ACs if producing a 1/8th-size image */
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1);
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize bitread state variables */
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.bits_left = 0;
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.insufficient_data = FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize restart counter */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go = cinfo->restart_interval;
|
|
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 jdphuff.c.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC, int tblno,
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl **pdtbl)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
JHUFF_TBL *htbl;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *dtbl;
|
|
shun-iwasawa |
82a8f5 |
int p, i, l, si, numsymbols;
|
|
shun-iwasawa |
82a8f5 |
int lookbits, ctr;
|
|
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 = (d_derived_tbl *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(d_derived_tbl));
|
|
shun-iwasawa |
82a8f5 |
dtbl = *pdtbl;
|
|
shun-iwasawa |
82a8f5 |
dtbl->pub = htbl; /* fill in back link */
|
|
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 |
numsymbols = 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 F.15: generate decoding tables for bit-sequential decoding */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
p = 0;
|
|
shun-iwasawa |
82a8f5 |
for (l = 1; l <= 16; l++) {
|
|
shun-iwasawa |
82a8f5 |
if (htbl->bits[l]) {
|
|
shun-iwasawa |
82a8f5 |
/* valoffset[l] = huffval[] index of 1st symbol of code length l,
|
|
shun-iwasawa |
82a8f5 |
* minus the minimum code of length l
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
dtbl->valoffset[l] = (JLONG)p - (JLONG)huffcode[p];
|
|
shun-iwasawa |
82a8f5 |
p += htbl->bits[l];
|
|
shun-iwasawa |
82a8f5 |
dtbl->maxcode[l] = huffcode[p - 1]; /* maximum code of length l */
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
dtbl->valoffset[17] = 0;
|
|
shun-iwasawa |
82a8f5 |
dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Compute lookahead tables to speed up decoding.
|
|
shun-iwasawa |
82a8f5 |
* First we set all the table entries to 0, indicating "too long";
|
|
shun-iwasawa |
82a8f5 |
* then we iterate through the Huffman codes that are short enough and
|
|
shun-iwasawa |
82a8f5 |
* fill in all the entries that correspond to bit sequences starting
|
|
shun-iwasawa |
82a8f5 |
* with that code.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++)
|
|
shun-iwasawa |
82a8f5 |
dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
p = 0;
|
|
shun-iwasawa |
82a8f5 |
for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
|
|
shun-iwasawa |
82a8f5 |
for (i = 1; i <= (int)htbl->bits[l]; i++, p++) {
|
|
shun-iwasawa |
82a8f5 |
/* l = current code's length, p = its index in huffcode[] & huffval[]. */
|
|
shun-iwasawa |
82a8f5 |
/* Generate left-justified code followed by all possible bit sequences */
|
|
shun-iwasawa |
82a8f5 |
lookbits = huffcode[p] << (HUFF_LOOKAHEAD - l);
|
|
shun-iwasawa |
82a8f5 |
for (ctr = 1 << (HUFF_LOOKAHEAD - l); ctr > 0; ctr--) {
|
|
shun-iwasawa |
82a8f5 |
dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p];
|
|
shun-iwasawa |
82a8f5 |
lookbits++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Validate symbols as being reasonable.
|
|
shun-iwasawa |
82a8f5 |
* For AC tables, we make no check, but accept all byte values 0..255.
|
|
shun-iwasawa |
82a8f5 |
* For DC tables, we require the symbols to be in range 0..15.
|
|
shun-iwasawa |
82a8f5 |
* (Tighter bounds could be applied depending on the data depth and mode,
|
|
shun-iwasawa |
82a8f5 |
* but this is sufficient to ensure safe decoding.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (isDC) {
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < numsymbols; i++) {
|
|
shun-iwasawa |
82a8f5 |
int sym = htbl->huffval[i];
|
|
shun-iwasawa |
82a8f5 |
if (sym < 0 || sym > 15)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Out-of-line code for bit fetching (shared with jdphuff.c).
|
|
shun-iwasawa |
82a8f5 |
* See jdhuff.h for info about usage.
|
|
shun-iwasawa |
82a8f5 |
* Note: current values of get_buffer and bits_left are passed as parameters,
|
|
shun-iwasawa |
82a8f5 |
* but are returned in the corresponding fields of the state struct.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
|
|
shun-iwasawa |
82a8f5 |
* of get_buffer to be used. (On machines with wider words, an even larger
|
|
shun-iwasawa |
82a8f5 |
* buffer could be used.) However, on some machines 32-bit shifts are
|
|
shun-iwasawa |
82a8f5 |
* quite slow and take time proportional to the number of places shifted.
|
|
shun-iwasawa |
82a8f5 |
* (This is true with most PC compilers, for instance.) In this case it may
|
|
shun-iwasawa |
82a8f5 |
* be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
|
|
shun-iwasawa |
82a8f5 |
* average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef SLOW_SHIFT_32
|
|
shun-iwasawa |
82a8f5 |
#define MIN_GET_BITS 15 /* minimum allowable value */
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
#define MIN_GET_BITS (BIT_BUF_SIZE - 7)
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
jpeg_fill_bit_buffer(bitread_working_state *state,
|
|
shun-iwasawa |
82a8f5 |
register bit_buf_type get_buffer, register int bits_left,
|
|
shun-iwasawa |
82a8f5 |
int nbits)
|
|
shun-iwasawa |
82a8f5 |
/* Load up the bit buffer to a depth of at least nbits */
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* Copy heavily used state fields into locals (hopefully registers) */
|
|
shun-iwasawa |
82a8f5 |
register const JOCTET *next_input_byte = state->next_input_byte;
|
|
shun-iwasawa |
82a8f5 |
register size_t bytes_in_buffer = state->bytes_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
j_decompress_ptr cinfo = state->cinfo;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
|
|
shun-iwasawa |
82a8f5 |
/* (It is assumed that no request will be for more than that many bits.) */
|
|
shun-iwasawa |
82a8f5 |
/* We fail to do so only if we hit a marker or are forced to suspend. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
|
|
shun-iwasawa |
82a8f5 |
while (bits_left < MIN_GET_BITS) {
|
|
shun-iwasawa |
82a8f5 |
register int c;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Attempt to read a byte */
|
|
shun-iwasawa |
82a8f5 |
if (bytes_in_buffer == 0) {
|
|
shun-iwasawa |
82a8f5 |
if (!(*cinfo->src->fill_input_buffer) (cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
next_input_byte = cinfo->src->next_input_byte;
|
|
shun-iwasawa |
82a8f5 |
bytes_in_buffer = cinfo->src->bytes_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
bytes_in_buffer--;
|
|
shun-iwasawa |
82a8f5 |
c = GETJOCTET(*next_input_byte++);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If it's 0xFF, check and discard stuffed zero byte */
|
|
shun-iwasawa |
82a8f5 |
if (c == 0xFF) {
|
|
shun-iwasawa |
82a8f5 |
/* Loop here to discard any padding FF's on terminating marker,
|
|
shun-iwasawa |
82a8f5 |
* so that we can save a valid unread_marker value. NOTE: we will
|
|
shun-iwasawa |
82a8f5 |
* accept multiple FF's followed by a 0 as meaning a single FF data
|
|
shun-iwasawa |
82a8f5 |
* byte. This data pattern is not valid according to the standard.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
do {
|
|
shun-iwasawa |
82a8f5 |
if (bytes_in_buffer == 0) {
|
|
shun-iwasawa |
82a8f5 |
if (!(*cinfo->src->fill_input_buffer) (cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
next_input_byte = cinfo->src->next_input_byte;
|
|
shun-iwasawa |
82a8f5 |
bytes_in_buffer = cinfo->src->bytes_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
bytes_in_buffer--;
|
|
shun-iwasawa |
82a8f5 |
c = GETJOCTET(*next_input_byte++);
|
|
shun-iwasawa |
82a8f5 |
} while (c == 0xFF);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (c == 0) {
|
|
shun-iwasawa |
82a8f5 |
/* Found FF/00, which represents an FF data byte */
|
|
shun-iwasawa |
82a8f5 |
c = 0xFF;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* Oops, it's actually a marker indicating end of compressed data.
|
|
shun-iwasawa |
82a8f5 |
* Save the marker code for later use.
|
|
shun-iwasawa |
82a8f5 |
* Fine point: it might appear that we should save the marker into
|
|
shun-iwasawa |
82a8f5 |
* bitread working state, not straight into permanent state. But
|
|
shun-iwasawa |
82a8f5 |
* once we have hit a marker, we cannot need to suspend within the
|
|
shun-iwasawa |
82a8f5 |
* current MCU, because we will read no more bytes from the data
|
|
shun-iwasawa |
82a8f5 |
* source. So it is OK to update permanent state right away.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
cinfo->unread_marker = c;
|
|
shun-iwasawa |
82a8f5 |
/* See if we need to insert some fake zero bits. */
|
|
shun-iwasawa |
82a8f5 |
goto no_more_bytes;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* OK, load c into get_buffer */
|
|
shun-iwasawa |
82a8f5 |
get_buffer = (get_buffer << 8) | c;
|
|
shun-iwasawa |
82a8f5 |
bits_left += 8;
|
|
shun-iwasawa |
82a8f5 |
} /* end while */
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
no_more_bytes:
|
|
shun-iwasawa |
82a8f5 |
/* We get here if we've read the marker that terminates the compressed
|
|
shun-iwasawa |
82a8f5 |
* data segment. There should be enough bits in the buffer register
|
|
shun-iwasawa |
82a8f5 |
* to satisfy the request; if so, no problem.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (nbits > bits_left) {
|
|
shun-iwasawa |
82a8f5 |
/* Uh-oh. Report corrupted data to user and stuff zeroes into
|
|
shun-iwasawa |
82a8f5 |
* the data stream, so that we can produce some kind of image.
|
|
shun-iwasawa |
82a8f5 |
* We use a nonvolatile flag to ensure that only one warning message
|
|
shun-iwasawa |
82a8f5 |
* appears per data segment.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (!cinfo->entropy->insufficient_data) {
|
|
shun-iwasawa |
82a8f5 |
WARNMS(cinfo, JWRN_HIT_MARKER);
|
|
shun-iwasawa |
82a8f5 |
cinfo->entropy->insufficient_data = TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* Fill the buffer with zero bits */
|
|
shun-iwasawa |
82a8f5 |
get_buffer <<= MIN_GET_BITS - bits_left;
|
|
shun-iwasawa |
82a8f5 |
bits_left = MIN_GET_BITS;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Unload the local registers */
|
|
shun-iwasawa |
82a8f5 |
state->next_input_byte = next_input_byte;
|
|
shun-iwasawa |
82a8f5 |
state->bytes_in_buffer = bytes_in_buffer;
|
|
shun-iwasawa |
82a8f5 |
state->get_buffer = get_buffer;
|
|
shun-iwasawa |
82a8f5 |
state->bits_left = bits_left;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Macro version of the above, which performs much better but does not
|
|
shun-iwasawa |
82a8f5 |
handle markers. We have to hand off any blocks with markers to the
|
|
shun-iwasawa |
82a8f5 |
slower routines. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define GET_BYTE { \
|
|
shun-iwasawa |
82a8f5 |
register int c0, c1; \
|
|
shun-iwasawa |
82a8f5 |
c0 = GETJOCTET(*buffer++); \
|
|
shun-iwasawa |
82a8f5 |
c1 = GETJOCTET(*buffer); \
|
|
shun-iwasawa |
82a8f5 |
/* Pre-execute most common case */ \
|
|
shun-iwasawa |
82a8f5 |
get_buffer = (get_buffer << 8) | c0; \
|
|
shun-iwasawa |
82a8f5 |
bits_left += 8; \
|
|
shun-iwasawa |
82a8f5 |
if (c0 == 0xFF) { \
|
|
shun-iwasawa |
82a8f5 |
/* Pre-execute case of FF/00, which represents an FF data byte */ \
|
|
shun-iwasawa |
82a8f5 |
buffer++; \
|
|
shun-iwasawa |
82a8f5 |
if (c1 != 0) { \
|
|
shun-iwasawa |
82a8f5 |
/* Oops, it's actually a marker indicating end of compressed data. */ \
|
|
shun-iwasawa |
82a8f5 |
cinfo->unread_marker = c1; \
|
|
shun-iwasawa |
82a8f5 |
/* Back out pre-execution and fill the buffer with zero bits */ \
|
|
shun-iwasawa |
82a8f5 |
buffer -= 2; \
|
|
shun-iwasawa |
82a8f5 |
get_buffer &= ~0xFF; \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
} \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if SIZEOF_SIZE_T == 8 || defined(_WIN64)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Pre-fetch 48 bytes, because the holding register is 64-bit */
|
|
shun-iwasawa |
82a8f5 |
#define FILL_BIT_BUFFER_FAST \
|
|
shun-iwasawa |
82a8f5 |
if (bits_left <= 16) { \
|
|
shun-iwasawa |
82a8f5 |
GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Pre-fetch 16 bytes, because the holding register is 32-bit */
|
|
shun-iwasawa |
82a8f5 |
#define FILL_BIT_BUFFER_FAST \
|
|
shun-iwasawa |
82a8f5 |
if (bits_left <= 16) { \
|
|
shun-iwasawa |
82a8f5 |
GET_BYTE GET_BYTE \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Out-of-line code for Huffman code decoding.
|
|
shun-iwasawa |
82a8f5 |
* See jdhuff.h for info about usage.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(int)
|
|
shun-iwasawa |
82a8f5 |
jpeg_huff_decode(bitread_working_state *state,
|
|
shun-iwasawa |
82a8f5 |
register bit_buf_type get_buffer, register int bits_left,
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *htbl, int min_bits)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register int l = min_bits;
|
|
shun-iwasawa |
82a8f5 |
register JLONG code;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* HUFF_DECODE has determined that the code is at least min_bits */
|
|
shun-iwasawa |
82a8f5 |
/* bits long, so fetch that many bits in one swoop. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(*state, l, return -1);
|
|
shun-iwasawa |
82a8f5 |
code = GET_BITS(l);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Collect the rest of the Huffman code one bit at a time. */
|
|
shun-iwasawa |
82a8f5 |
/* This is per Figure F.16. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
while (code > htbl->maxcode[l]) {
|
|
shun-iwasawa |
82a8f5 |
code <<= 1;
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(*state, 1, return -1);
|
|
shun-iwasawa |
82a8f5 |
code |= GET_BITS(1);
|
|
shun-iwasawa |
82a8f5 |
l++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Unload the local registers */
|
|
shun-iwasawa |
82a8f5 |
state->get_buffer = get_buffer;
|
|
shun-iwasawa |
82a8f5 |
state->bits_left = bits_left;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* With garbage input we may reach the sentinel value l = 17. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (l > 16) {
|
|
shun-iwasawa |
82a8f5 |
WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
|
|
shun-iwasawa |
82a8f5 |
return 0; /* fake a zero as the safest result */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return htbl->pub->huffval[(int)(code + htbl->valoffset[l])];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Figure F.12: extend sign bit.
|
|
shun-iwasawa |
82a8f5 |
* On some machines, a shift and add will be faster than a table lookup.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define AVOID_TABLES
|
|
shun-iwasawa |
82a8f5 |
#ifdef AVOID_TABLES
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define NEG_1 ((unsigned int)-1)
|
|
shun-iwasawa |
82a8f5 |
#define HUFF_EXTEND(x, s) \
|
|
shun-iwasawa |
82a8f5 |
((x) + ((((x) - (1 << ((s) - 1))) >> 31) & (((NEG_1) << (s)) + 1)))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define HUFF_EXTEND(x, s) \
|
|
shun-iwasawa |
82a8f5 |
((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static const int extend_test[16] = { /* entry n is 2**(n-1) */
|
|
shun-iwasawa |
82a8f5 |
0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
|
|
shun-iwasawa |
82a8f5 |
0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */
|
|
shun-iwasawa |
82a8f5 |
0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif /* AVOID_TABLES */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Check for a restart marker & resynchronize decoder.
|
|
shun-iwasawa |
82a8f5 |
* Returns FALSE if must suspend.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
process_restart(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int ci;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Throw away any unused bits remaining in bit buffer; */
|
|
shun-iwasawa |
82a8f5 |
/* include any full bytes in next_marker's count of discarded bytes */
|
|
shun-iwasawa |
82a8f5 |
cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.bits_left = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Advance past the RSTn marker */
|
|
shun-iwasawa |
82a8f5 |
if (!(*cinfo->marker->read_restart_marker) (cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
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 |
|
|
shun-iwasawa |
82a8f5 |
/* Reset restart counter */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go = cinfo->restart_interval;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Reset out-of-data flag, unless read_restart_marker left us smack up
|
|
shun-iwasawa |
82a8f5 |
* against a marker. In that case we will end up treating the next data
|
|
shun-iwasawa |
82a8f5 |
* segment as empty, and we can avoid producing bogus output pixels by
|
|
shun-iwasawa |
82a8f5 |
* leaving the flag set.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->unread_marker == 0)
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.insufficient_data = FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_slow(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
int blkn;
|
|
shun-iwasawa |
82a8f5 |
savable_state state;
|
|
shun-iwasawa |
82a8f5 |
/* Outer loop handles each block in the MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(state, entropy->saved);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block = MCU_data ? MCU_data[blkn] : NULL;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *dctbl = entropy->dc_cur_tbls[blkn];
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *actbl = entropy->ac_cur_tbls[blkn];
|
|
shun-iwasawa |
82a8f5 |
register int s, k, r;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Decode a single block's worth of coefficients */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Section F.2.2.1: decode the DC coefficient difference */
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (entropy->dc_needed[blkn]) {
|
|
shun-iwasawa |
82a8f5 |
/* Convert DC difference to actual value, update last_dc_val */
|
|
shun-iwasawa |
82a8f5 |
int ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
/* This is really just
|
|
shun-iwasawa |
82a8f5 |
* s += state.last_dc_val[ci];
|
|
shun-iwasawa |
82a8f5 |
* It is written this way in order to shut up UBSan.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
s = (int)((unsigned int)s + (unsigned int)state.last_dc_val[ci]);
|
|
shun-iwasawa |
82a8f5 |
state.last_dc_val[ci] = s;
|
|
shun-iwasawa |
82a8f5 |
if (block) {
|
|
shun-iwasawa |
82a8f5 |
/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
|
|
shun-iwasawa |
82a8f5 |
(*block)[0] = (JCOEF)s;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (entropy->ac_needed[blkn] && block) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Section F.2.2.2: decode the AC coefficients */
|
|
shun-iwasawa |
82a8f5 |
/* Since zeroes are skipped, output area must be cleared beforehand */
|
|
shun-iwasawa |
82a8f5 |
for (k = 1; k < DCTSIZE2; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
k += r;
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
/* Output coefficient in natural (dezigzagged) order.
|
|
shun-iwasawa |
82a8f5 |
* Note: the extra entries in jpeg_natural_order[] will save us
|
|
shun-iwasawa |
82a8f5 |
* if k >= DCTSIZE2, which could happen if the data is corrupted.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
(*block)[jpeg_natural_order[k]] = (JCOEF)s;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r != 15)
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
k += 15;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Section F.2.2.2: decode the AC coefficients */
|
|
shun-iwasawa |
82a8f5 |
/* In this path we just discard the values */
|
|
shun-iwasawa |
82a8f5 |
for (k = 1; k < DCTSIZE2; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
k += r;
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
DROP_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r != 15)
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
k += 15;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(entropy->saved, state);
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_fast(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
JOCTET *buffer;
|
|
shun-iwasawa |
82a8f5 |
int blkn;
|
|
shun-iwasawa |
82a8f5 |
savable_state state;
|
|
shun-iwasawa |
82a8f5 |
/* Outer loop handles each block in the MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
buffer = (JOCTET *)br_state.next_input_byte;
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(state, entropy->saved);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block = MCU_data ? MCU_data[blkn] : NULL;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *dctbl = entropy->dc_cur_tbls[blkn];
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *actbl = entropy->ac_cur_tbls[blkn];
|
|
shun-iwasawa |
82a8f5 |
register int s, k, r, l;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE_FAST(s, l, dctbl);
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
FILL_BIT_BUFFER_FAST
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (entropy->dc_needed[blkn]) {
|
|
shun-iwasawa |
82a8f5 |
int ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
s = (int)((unsigned int)s + (unsigned int)state.last_dc_val[ci]);
|
|
shun-iwasawa |
82a8f5 |
state.last_dc_val[ci] = s;
|
|
shun-iwasawa |
82a8f5 |
if (block)
|
|
shun-iwasawa |
82a8f5 |
(*block)[0] = (JCOEF)s;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (entropy->ac_needed[blkn] && block) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (k = 1; k < DCTSIZE2; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE_FAST(s, l, actbl);
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
k += r;
|
|
shun-iwasawa |
82a8f5 |
FILL_BIT_BUFFER_FAST
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
(*block)[jpeg_natural_order[k]] = (JCOEF)s;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r != 15) break;
|
|
shun-iwasawa |
82a8f5 |
k += 15;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (k = 1; k < DCTSIZE2; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE_FAST(s, l, actbl);
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
k += r;
|
|
shun-iwasawa |
82a8f5 |
FILL_BIT_BUFFER_FAST
|
|
shun-iwasawa |
82a8f5 |
DROP_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r != 15) break;
|
|
shun-iwasawa |
82a8f5 |
k += 15;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->unread_marker != 0) {
|
|
shun-iwasawa |
82a8f5 |
cinfo->unread_marker = 0;
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte);
|
|
shun-iwasawa |
82a8f5 |
br_state.next_input_byte = buffer;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(entropy->saved, state);
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Decode and return one MCU's worth of Huffman-compressed coefficients.
|
|
shun-iwasawa |
82a8f5 |
* The coefficients are reordered from zigzag order into natural array order,
|
|
shun-iwasawa |
82a8f5 |
* but are not dequantized.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The i'th block of the MCU is stored into the block pointed to by
|
|
shun-iwasawa |
82a8f5 |
* MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
|
|
shun-iwasawa |
82a8f5 |
* (Wholesale zeroing is usually a little faster than retail...)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Returns FALSE if data source requested suspension. In that case no
|
|
shun-iwasawa |
82a8f5 |
* changes have been made to permanent state. (Exception: some output
|
|
shun-iwasawa |
82a8f5 |
* coefficients may already have been assigned. This is harmless for
|
|
shun-iwasawa |
82a8f5 |
* this module, since we'll just re-assign them on the next call.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define BUFSIZE (DCTSIZE2 * 8)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int usefast = 1;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process restart marker if needed; may have to suspend */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0)
|
|
shun-iwasawa |
82a8f5 |
if (!process_restart(cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
usefast = 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU ||
|
|
shun-iwasawa |
82a8f5 |
cinfo->unread_marker != 0)
|
|
shun-iwasawa |
82a8f5 |
usefast = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If we've run out of data, just leave the MCU set to zeroes.
|
|
shun-iwasawa |
82a8f5 |
* This way, we return uniform gray for the remainder of the segment.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (!entropy->pub.insufficient_data) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (usefast) {
|
|
shun-iwasawa |
82a8f5 |
if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
use_slow:
|
|
shun-iwasawa |
82a8f5 |
if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Account for restart interval (no-op if not using restarts) */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Module initialization routine for Huffman entropy decoding.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jinit_huff_decoder(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
huff_entropy_ptr entropy;
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Motion JPEG frames typically do not include the Huffman tables if they
|
|
shun-iwasawa |
82a8f5 |
are the default tables. Thus, if the tables are not set by the time
|
|
shun-iwasawa |
82a8f5 |
the Huffman decoder is initialized (usually within the body of
|
|
shun-iwasawa |
82a8f5 |
jpeg_start_decompress()), we set them to default values. */
|
|
shun-iwasawa |
82a8f5 |
std_huff_tables((j_common_ptr)cinfo);
|
|
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_decoder));
|
|
shun-iwasawa |
82a8f5 |
cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.start_pass = start_pass_huff_decoder;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.decode_mcu = decode_mcu;
|
|
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 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|